Skip to content

Commit

Permalink
wip: inject chunk info to entry
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Dec 9, 2024
1 parent 8bad3d2 commit 78c00e0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
9 changes: 5 additions & 4 deletions packages/vite/misc/rolldown-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,14 @@ self.__rolldown_runtime = {
}
}
},
/**
* @type {Record<string, string>}
*/
manifest: {},
/**
* @param {string} chunkName
*/
async ensureChunk(chunkName) {
// TODO: chunk manifest? (need to inject from generateBundle?)
const chunkPath = chunkName
const data = await import(chunkPath)
Object.assign(this.moduleFactoryMap, data.default)
await import('/' + this.manifest[chunkName])
},
}
36 changes: 36 additions & 0 deletions packages/vite/src/node/server/environments/rolldown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,17 @@ self.__rolldown_runtime.patch(__rolldown_modules);
}
}

if (chunk.isDynamicEntry) {
const output = new MagicString(code)
output.append(`
Object.assign(self.__rolldown_runtime.moduleFactoryMap, __rolldown_modules);
`)
return {
code: output.toString(),
map: output.generateMap({ hires: 'boundary' }),
}
}

if (chunk.isEntry) {
const output = new MagicString(code)
assert(chunk.facadeModuleId)
Expand Down Expand Up @@ -460,7 +471,32 @@ self.__rolldown_runtime.require(${JSON.stringify(stableId)});
}
}
},
generateBundle(_options, bundle) {
// inject chunk manifest to entries
const manifest: Record<string, string> = {}
for (const chunk of Object.values(bundle)) {
if (chunk.name) {
manifest[chunk.name] = chunk.fileName
}
}
for (const chunk of Object.values(bundle)) {
if (chunk.type === 'chunk' && chunk.isEntry) {
chunk.code += `
self.__rolldown_runtime.manifest = ${JSON.stringify(manifest, null, 2)};
`
chunk.code = moveInlineSourcemapToEnd(chunk.code)
}
}
},
}
}

function moveInlineSourcemapToEnd(code: string) {
const sourcemap = code.match(/^\/\/# sourceMappingURL=.*/m)?.[0]
if (sourcemap) {
code = code.replace(sourcemap, '') + '\n' + sourcemap
}
return code
}

// patch vite:css transform for hmr
Expand Down

0 comments on commit 78c00e0

Please sign in to comment.