Skip to content

Commit a4394b4

Browse files
fix(javascript): leave the enclose wrapper's unnamed source out of the minifier's source map (#22654)
* fix(javascript): leave the enclose wrapper's unnamed source out of the minifier's source map terser adds a null entry to sources for the wrapper enclose parses from no file, which no mapping references and which Rollup rejects (terser#1333). * fix(javascript): keep the comments of an empty statement the SyntaxTree printer drops Same change as #22649, which main needs for the syntax-printer suite to pass.
1 parent 2f34cea commit a4394b4

3 files changed

Lines changed: 23 additions & 4 deletions

File tree

‎.changeset/025-javascript-minifier-corrections.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"webpack": patch
33
---
44

5-
Fix JavaScript minifier crashes, altered behavior, lost comments and unparsable output, including parameter aliases and ESTree trees.
5+
Fix JavaScript minifier crashes, altered behavior, lost comments, unparsable output and null source map sources, including parameter aliases and ESTree trees.

‎lib/javascript/syntax-printer.js‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7376,9 +7376,10 @@ const findOriginalPosition = (map, line, column) => {
73767376
* does, mapping back through the input's own map, but setting a source's
73777377
* content once rather than with every mapping into it.
73787378
* @param {SourceMapOptions} options what to build the map from
7379+
* @param {boolean} corrected whether a node without a source, as the `enclose` wrapper, adds no entry to `sources`
73797380
* @returns {Generator<EXPECTED_ANY, SourceMapBuilder, EXPECTED_ANY>} the builder, once the input's map is read
73807381
*/
7381-
function* createSourceMap(options) {
7382+
function* createSourceMap(options, corrected) {
73827383
const { encodeDecodedMappings } = require("../util/createMappings");
73837384

73847385
/** @type {Map<string, number>} */
@@ -7555,7 +7556,7 @@ function* createSourceMap(options) {
75557556
originalColumn,
75567557
name
75577558
);
7558-
if (!withContent.has(source)) {
7559+
if (!withContent.has(source) && (source || !corrected)) {
75597560
withContent.add(source);
75607561
builtContent[sourceIndexOf(source)] = sourcesContent[source];
75617562
}
@@ -7930,7 +7931,11 @@ const installMinify = (modules) => {
79307931
* @param {SourceMapOptions} options what to build the map from
79317932
* @returns {Generator<EXPECTED_ANY, SourceMapBuilder, EXPECTED_ANY>} the builder
79327933
*/
7933-
const SourceMap = (options) => createSourceMap(options);
7934+
const SourceMap = (options) =>
7935+
createSourceMap(
7936+
options,
7937+
modules.corrections !== undefined && modules.corrections.enabled
7938+
);
79347939
const { base54 } = modules.scope;
79357940

79367941
/**

‎test/unitCases/syntax-printer.unittest.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6025,6 +6025,20 @@ describe("syntax-printer", () => {
60256025
}
60266026
});
60276027

6028+
it("should leave the enclose wrapper's unnamed source out of the map", async () => {
6029+
const { minify } = await load();
6030+
const files = { "in.js": "console.log(b);" };
6031+
const options = () => ({ enclose: "a:b", compress: false, sourceMap: {} });
6032+
const map = JSON.parse(/** @type {string} */ ((await minify(files, options())).map));
6033+
expect(map.sources).toStrictEqual(["in.js"]);
6034+
// cspell:disable-next-line -- VLQ-encoded mappings
6035+
expect(map.mappings).toBe("aAAAA,QAAQC,IAAIC,E");
6036+
6037+
const reference = await terserReference().minify(files, options());
6038+
expect((await uncorrected(() => minify(files, options()))).map).toBe(reference.map);
6039+
expect(JSON.parse(/** @type {string} */ (reference.map)).sources).toStrictEqual([null, "in.js"]);
6040+
});
6041+
60286042
// cspell:disable -- VLQ-encoded source-map mappings strings below
60296043
it("should map through an input map as terser reads it, index maps and decoded mappings included", async () => {
60306044
const { minify } = await load();

0 commit comments

Comments
 (0)