diff --git a/src/compiler/output-targets/dist-lazy/generate-lazy-module.ts b/src/compiler/output-targets/dist-lazy/generate-lazy-module.ts index f9460cfea48..fd869ca4868 100644 --- a/src/compiler/output-targets/dist-lazy/generate-lazy-module.ts +++ b/src/compiler/output-targets/dist-lazy/generate-lazy-module.ts @@ -240,11 +240,13 @@ const writeLazyChunk = async ( destinations.map((dst) => { const filePath = join(dst, rollupResult.fileName); let fileCode = code; + const writes: Promise[] = []; if (sourceMap) { fileCode = code + getSourceMappingUrlForEndOfFile(rollupResult.fileName); - compilerCtx.fs.writeFile(filePath + '.map', JSON.stringify(sourceMap), { outputTargetType }); + writes.push(compilerCtx.fs.writeFile(filePath + '.map', JSON.stringify(sourceMap), { outputTargetType })); } - compilerCtx.fs.writeFile(filePath, fileCode, { outputTargetType }); + writes.push(compilerCtx.fs.writeFile(filePath, fileCode, { outputTargetType })); + return Promise.all(writes); }), ); }; @@ -281,11 +283,13 @@ const writeLazyEntry = async ( destinations.map((dst) => { const filePath = join(dst, rollupResult.fileName); let fileCode = code; + const writes: Promise[] = []; if (sourceMap) { fileCode = code + getSourceMappingUrlForEndOfFile(rollupResult.fileName); - compilerCtx.fs.writeFile(filePath + '.map', JSON.stringify(sourceMap), { outputTargetType }); + writes.push(compilerCtx.fs.writeFile(filePath + '.map', JSON.stringify(sourceMap), { outputTargetType })); } - return compilerCtx.fs.writeFile(filePath, fileCode, { outputTargetType }); + writes.push(compilerCtx.fs.writeFile(filePath, fileCode, { outputTargetType })); + return Promise.all(writes); }), ); }; diff --git a/src/compiler/output-targets/dist-lazy/lazy-bundleid-plugin.ts b/src/compiler/output-targets/dist-lazy/lazy-bundleid-plugin.ts index 39a3bfaeaca..ddcc731270e 100644 --- a/src/compiler/output-targets/dist-lazy/lazy-bundleid-plugin.ts +++ b/src/compiler/output-targets/dist-lazy/lazy-bundleid-plugin.ts @@ -37,6 +37,20 @@ export const lazyBundleIdPlugin = ( async generateBundle(_, bundle) { const files = Object.entries(bundle as any); const map = new Map(); + const filesToDelete: string[] = []; + + // For browser builds, loader entries are skipped by writeLazyEntry + // So we need to delete their sourcemaps to avoid orphaned files + if (suffix && config.sourceMap) { + for (const [key, file] of files) { + if (file.type === 'chunk' && file.isEntry && file.name === 'loader') { + const mapFileName = key + '.map'; + if (bundle[mapFileName]) { + filesToDelete.push(mapFileName); + } + } + } + } for (const [_key, file] of files) { if (!file.isEntry) continue; @@ -44,7 +58,16 @@ export const lazyBundleIdPlugin = ( const entryModule = buildCtx.entryModules.find((em) => em.entryKey === file.name); if (!entryModule) continue; - map.set(file.fileName, (await getBundleId(file.name, file.code, suffix)) + '.entry.js'); + const newFileName = (await getBundleId(file.name, file.code, suffix)) + '.entry.js'; + map.set(file.fileName, newFileName); + + // If we're renaming the file, mark the old sourcemap for deletion + if (file.fileName !== newFileName && config.sourceMap) { + const oldMapFileName = file.fileName + '.map'; + if (bundle[oldMapFileName]) { + filesToDelete.push(oldMapFileName); + } + } } if (!map.size) return; @@ -52,8 +75,11 @@ export const lazyBundleIdPlugin = ( for (const [_key, file] of files) { if (!file.isEntry) continue; - file.facadeModuleId = map.get(file.fileName) || file.facadeModuleId; - file.fileName = map.get(file.fileName) || file.fileName; + const newFileName = map.get(file.fileName); + if (!newFileName) continue; + + file.facadeModuleId = newFileName; + file.fileName = newFileName; const magicString = new MagicString(file.code); @@ -70,6 +96,11 @@ export const lazyBundleIdPlugin = ( file.map = magicString.generateMap(); } } + + // Delete orphaned sourcemap files + for (const fileName of filesToDelete) { + delete bundle[fileName]; + } }, }; }; diff --git a/src/compiler/output-targets/dist-lazy/write-lazy-entry-module.ts b/src/compiler/output-targets/dist-lazy/write-lazy-entry-module.ts index d6a46f92b06..46864eca74c 100644 --- a/src/compiler/output-targets/dist-lazy/write-lazy-entry-module.ts +++ b/src/compiler/output-targets/dist-lazy/write-lazy-entry-module.ts @@ -21,10 +21,13 @@ export const writeLazyModule = async ( await Promise.all( destinations.map((dst) => { - compilerCtx.fs.writeFile(join(dst, fileName), code, { outputTargetType }); + const jsPath = join(dst, fileName); + const mapPath = jsPath + '.map'; + const writes: Promise[] = [compilerCtx.fs.writeFile(jsPath, code, { outputTargetType })]; if (!!sourceMap) { - compilerCtx.fs.writeFile(join(dst, fileName) + '.map', JSON.stringify(sourceMap), { outputTargetType }); + writes.push(compilerCtx.fs.writeFile(mapPath, JSON.stringify(sourceMap), { outputTargetType })); } + return Promise.all(writes); }), );