Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions packages/vite/src/node/__tests__/build.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ import {
ChunkMetadataMap,
build,
createBuilder,
injectEnvironmentToHooks,
onRollupLog,
resolveBuildOutputs,
resolveLibFilename,
resolveRolldownOptions,
} from '../build'
import type { Logger } from '../logger'
import { createLogger } from '../logger'
import type { Plugin } from '../plugin'
import { getHookHandler } from '../plugins'

const dirname = import.meta.dirname

Expand Down Expand Up @@ -1578,6 +1581,34 @@ async function buildProjectWithRenderBuiltUrl(
})) as RolldownOutput
}

describe('injectEnvironmentToHooks', () => {
test('binds environment to hotUpdate hook', async () => {
const config = await resolveConfig(
{ configFile: false, logLevel: 'error' },
'serve',
)
const environment = new BuildEnvironment('client', config)
const chunkMetadataMap = new ChunkMetadataMap()
let name: string | undefined
const plugin: Plugin = {
name: 'test-hot-update-env',
hotUpdate() {
name = this.environment.name
},
}

const wrapped = injectEnvironmentToHooks(
environment,
chunkMetadataMap,
plugin,
)
const context = { meta: {} } as import('rolldown').PluginContext

await getHookHandler(wrapped.hotUpdate!).call(context, {} as any)
expect(name).toBe('client')
})
})

/**
* for each chunks in output1, if there's a chunk in output2 with the same fileName,
* ensure that the chunk code is the same. if not, the chunk hash should have changed.
Expand Down
9 changes: 9 additions & 0 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1339,6 +1339,15 @@ export function injectEnvironmentToHooks(
}
}

if (plugin.hotUpdate) {
clone.hotUpdate = wrapEnvironmentHook(
environment,
chunkMetadataMap,
plugin,
'hotUpdate',
)
}

return clone
}

Expand Down
Loading