Skip to content
Merged
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
12 changes: 8 additions & 4 deletions src/compiler/output-targets/dist-lazy/generate-lazy-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,13 @@ const writeLazyChunk = async (
destinations.map((dst) => {
const filePath = join(dst, rollupResult.fileName);
let fileCode = code;
const writes: Promise<any>[] = [];
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);
}),
);
};
Expand Down Expand Up @@ -281,11 +283,13 @@ const writeLazyEntry = async (
destinations.map((dst) => {
const filePath = join(dst, rollupResult.fileName);
let fileCode = code;
const writes: Promise<any>[] = [];
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);
}),
);
};
Expand Down
37 changes: 34 additions & 3 deletions src/compiler/output-targets/dist-lazy/lazy-bundleid-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,49 @@ export const lazyBundleIdPlugin = (
async generateBundle(_, bundle) {
const files = Object.entries<OutputChunk>(bundle as any);
const map = new Map<string, string>();
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;

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;

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);

Expand All @@ -70,6 +96,11 @@ export const lazyBundleIdPlugin = (
file.map = magicString.generateMap();
}
}

// Delete orphaned sourcemap files
for (const fileName of filesToDelete) {
delete bundle[fileName];
}
},
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>[] = [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);
}),
);

Expand Down
Loading