Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[INTERNAL] Minifier: Fix handling of input source maps of bundles #944

Merged
merged 2 commits into from
Oct 10, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 25 additions & 4 deletions lib/processors/minifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,19 +201,40 @@ export default async function({
});

if (sourceMapContent) {
// Provide source map to terser as "input source map"
sourceMapOptions.content = sourceMapContent;
const sourceMapJson = JSON.parse(sourceMapContent);

if (sourceMapJson.sections) {
// TODO 4.0
// Module "@jridgewell/trace-mapping" (used by Terser) can't handle index map sections lacking
// a "names" array. Since this is a common occurrence for UI5 Tooling bundles, we search for
// such cases here and fix them until https://github.com/jridgewell/trace-mapping/pull/29 is
// resolved and Terser upgraded the dependency

// Also use the source map for the debug variant of the resource
// Create a dedicated clone before modifying the source map as to not alter the debug source map
const clonedSourceMapJson = JSON.parse(sourceMapContent);
clonedSourceMapJson.sections.forEach(({map}) => {
if (!map.names) {
// Add missing names array
map.names = [];
}
});
// Use modified source map as input source map
sourceMapOptions.content = JSON.stringify(clonedSourceMapJson);
} else {
// Provide source map to terser as "input source map"
sourceMapOptions.content = sourceMapContent;
}

// Use the original source map for the debug variant of the resource
// First update the file reference within the source map
const sourceMapJson = JSON.parse(sourceMapContent);
sourceMapJson.file = dbgFilename;

// Then create a new resource
dbgSourceMapResource = new Resource({
string: JSON.stringify(sourceMapJson),
path: dbgPath + ".map"
});
// And reference the resource in the debug resource
dbgResource.setString(code + `//# sourceMappingURL=${dbgFilename}.map\n`);
}
} else {
Expand Down
4 changes: 3 additions & 1 deletion lib/processors/minifierWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ export default async function execMinification({
// Note: err.filename contains the debug-name
throw new Error(
`Minification failed with error: ${err.message} in file ${filename} ` +
`(line ${err.line}, col ${err.col}, pos ${err.pos})`);
`(line ${err.line}, col ${err.col}, pos ${err.pos})`, {
cause: err
});
}
}

Expand Down