Skip to content

Commit

Permalink
fix: update manifest on hmr
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Dec 9, 2024
1 parent 2416882 commit 6c00b66
Showing 1 changed file with 39 additions and 22 deletions.
61 changes: 39 additions & 22 deletions packages/vite/src/node/server/environments/rolldown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,20 +294,22 @@ class RolldownEnvironment extends DevEnvironment {
)
}

async buildHmr(
file: string,
): Promise<rolldown.RolldownOutputChunk | undefined> {
async buildHmr(file: string): Promise<{
manifest: ChunkManifest
chunk?: rolldown.RolldownOutputChunk
}> {
logger.info(`hmr '${file}'`, { timestamp: true })
console.time(`[rolldown:${this.name}:rebuild]`)
await this.buildInner()
console.timeEnd(`[rolldown:${this.name}:rebuild]`)
const manifest = getChunkManifest(this.result.output)
const chunk = this.result.output.find(
(v) => v.type === 'chunk' && v.name === 'hmr-update',
)
if (chunk) {
assert(chunk.type === 'chunk')
return chunk
}
return { manifest, chunk }
}

async handleUpdate(ctx: HmrContext): Promise<void> {
Expand All @@ -327,17 +329,20 @@ class RolldownEnvironment extends DevEnvironment {
this.rolldownDevOptions.hmr ||
this.rolldownDevOptions.ssrModuleRunner
) {
const chunk = await this.buildHmr(ctx.file)
if (!chunk) {
return
}
const result = await this.buildHmr(ctx.file)
if (this.name === 'client') {
ctx.server.ws.send('rolldown:hmr', chunk.fileName)
ctx.server.ws.send('rolldown:hmr', {
manifest: result.manifest,
fileName: result.chunk?.fileName,
})
} else {
this.getRunner().evaluate(
chunk.code,
path.join(this.outDir, chunk.fileName),
)
// TODO: manifest
if (result.chunk) {
this.getRunner().evaluate(
result.chunk.code,
path.join(this.outDir, result.chunk.fileName),
)
}
}
} else {
await this.build()
Expand Down Expand Up @@ -418,6 +423,7 @@ function patchRuntimePlugin(environment: RolldownEnvironment): rolldown.Plugin {
name: 'vite:rolldown-patch-runtime',
renderChunk(code, chunk) {
// TODO: this magic string is heavy
// (append only so no need to generate sourcemap?)

if (chunk.name === 'hmr-update') {
const output = new MagicString(code)
Expand Down Expand Up @@ -472,14 +478,8 @@ self.__rolldown_runtime.require(${JSON.stringify(stableId)});
}
},
generateBundle(_options, bundle) {
// inject chunk manifest to entries
// TODO: update manifest on rebuild via __rolldown_hot
const manifest: Record<string, string> = {}
for (const chunk of Object.values(bundle)) {
if (chunk.name) {
manifest[chunk.name] = chunk.fileName
}
}
// inject chunk manifest
const manifest = getChunkManifest(Object.values(bundle))
for (const chunk of Object.values(bundle)) {
if (chunk.type === 'chunk' && chunk.isEntry) {
chunk.code += `
Expand All @@ -492,6 +492,20 @@ self.__rolldown_runtime.manifest = ${JSON.stringify(manifest, null, 2)};
}
}

type ChunkManifest = Record<string, string>

function getChunkManifest(
outputs: (rolldown.RolldownOutputChunk | rolldown.RolldownOutputAsset)[],
): ChunkManifest {
const manifest: Record<string, string> = {}
for (const chunk of outputs) {
if (chunk.name) {
manifest[chunk.name] = chunk.fileName
}
}
return manifest
}

function moveInlineSourcemapToEnd(code: string) {
const sourcemap = code.match(/^\/\/# sourceMappingURL=.*/m)?.[0]
if (sourcemap) {
Expand Down Expand Up @@ -555,7 +569,10 @@ function getRolldownClientCode() {
code += `
const hot = createHotContext("/__rolldown");
hot.on("rolldown:hmr", (data) => {
import("/" + data + "?t=" + Date.now());
self.__rolldown_runtime.manifest = data.manifest;
if (data.fileName) {
import("/" + data.fileName + "?t=" + Date.now());
}
});
self.__rolldown_hot = hot;
self.__rolldown_updateStyle = updateStyle;
Expand Down

0 comments on commit 6c00b66

Please sign in to comment.