Skip to content

Commit edfaa0f

Browse files
authored
fix: omit empty ignoreList property from source maps (#20319)
1 parent 9a3b428 commit edfaa0f

7 files changed

Lines changed: 93 additions & 30 deletions

File tree

.changeset/sourcemap-ignorelist.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Optimize source map generation: only include `ignoreList` property when it has content, avoiding empty arrays in source maps.

lib/EvalSourceMapDevToolPlugin.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -165,20 +165,23 @@ class EvalSourceMapDevToolPlugin {
165165
}
166166
);
167167
sourceMap.sources = moduleFilenames;
168-
sourceMap.ignoreList = options.ignoreList
169-
? sourceMap.sources.reduce(
170-
/** @type {(acc: number[], sourceName: string, idx: number) => number[]} */ (
171-
(acc, sourceName, idx) => {
172-
const rule = /** @type {Rules} */ (options.ignoreList);
173-
if (ModuleFilenameHelpers.matchPart(sourceName, rule)) {
174-
acc.push(idx);
175-
}
176-
return acc;
168+
if (options.ignoreList) {
169+
const ignoreList = sourceMap.sources.reduce(
170+
/** @type {(acc: number[], sourceName: string, idx: number) => number[]} */ (
171+
(acc, sourceName, idx) => {
172+
const rule = /** @type {Rules} */ (options.ignoreList);
173+
if (ModuleFilenameHelpers.matchPart(sourceName, rule)) {
174+
acc.push(idx);
177175
}
178-
),
179-
[]
180-
)
181-
: [];
176+
return acc;
177+
}
178+
),
179+
[]
180+
);
181+
if (ignoreList.length > 0) {
182+
sourceMap.ignoreList = ignoreList;
183+
}
184+
}
182185

183186
if (options.noSources) {
184187
sourceMap.sourcesContent = undefined;

lib/SourceMapDevToolPlugin.js

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -435,24 +435,27 @@ class SourceMapDevToolPlugin {
435435
moduleToSourceNameMapping.get(m)
436436
);
437437
sourceMap.sources = /** @type {string[]} */ (moduleFilenames);
438-
sourceMap.ignoreList = options.ignoreList
439-
? sourceMap.sources.reduce(
440-
/** @type {(acc: number[], sourceName: string, idx: number) => number[]} */ (
441-
(acc, sourceName, idx) => {
442-
const rule = /** @type {Rules} */ (
443-
options.ignoreList
444-
);
445-
if (
446-
ModuleFilenameHelpers.matchPart(sourceName, rule)
447-
) {
448-
acc.push(idx);
449-
}
450-
return acc;
438+
if (options.ignoreList) {
439+
const ignoreList = sourceMap.sources.reduce(
440+
/** @type {(acc: number[], sourceName: string, idx: number) => number[]} */ (
441+
(acc, sourceName, idx) => {
442+
const rule = /** @type {Rules} */ (
443+
options.ignoreList
444+
);
445+
if (
446+
ModuleFilenameHelpers.matchPart(sourceName, rule)
447+
) {
448+
acc.push(idx);
451449
}
452-
),
453-
[]
454-
)
455-
: [];
450+
return acc;
451+
}
452+
),
453+
[]
454+
);
455+
if (ignoreList.length > 0) {
456+
sourceMap.ignoreList = ignoreList;
457+
}
458+
}
456459

457460
if (options.noSources) {
458461
sourceMap.sourcesContent = undefined;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
it("should not include empty ignoreList in source map", () => {
2+
const fs = require("fs");
3+
const path = require("path");
4+
const sourceMapPath = path.join(__dirname, "bundle0.js.map");
5+
const sourceMapContent = fs.readFileSync(sourceMapPath, "utf-8");
6+
7+
expect(sourceMapContent).not.toMatch(/"ignoreList"\s*:\s*\[\s*\]/);
8+
9+
// Verify default source map properties are present
10+
const sourceMap = JSON.parse(sourceMapContent);
11+
expect(sourceMap).toHaveProperty("version", 3);
12+
expect(sourceMap).toHaveProperty("sources");
13+
expect(Array.isArray(sourceMap.sources)).toBe(true);
14+
expect(sourceMap.sources.length).toBeGreaterThan(0);
15+
});
16+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../").Configuration} */
4+
module.exports = {
5+
mode: "development",
6+
devtool: "source-map",
7+
entry: "./index.js"
8+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
it("should not include empty ignoreList in eval source map", () => {
2+
const fs = require("fs");
3+
const source = fs.readFileSync(__filename, "utf-8");
4+
5+
const match = /sourceMappingURL\s*=\s*data:application\/json;charset=utf-8;base64,([A-Za-z0-9+\/=]+)/.exec(
6+
source
7+
);
8+
expect(match).not.toBeNull();
9+
const mapString = Buffer.from(match[1], "base64").toString("utf-8");
10+
11+
expect(mapString).not.toMatch(/"ignoreList"\s*:\s*\[\s*\]/);
12+
13+
// Verify default source map properties are present
14+
const sourceMap = JSON.parse(mapString);
15+
expect(sourceMap).toHaveProperty("version", 3);
16+
expect(sourceMap).toHaveProperty("sources");
17+
expect(Array.isArray(sourceMap.sources)).toBe(true);
18+
expect(sourceMap.sources.length).toBeGreaterThan(0);
19+
});
20+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../").Configuration} */
4+
module.exports = {
5+
mode: "development",
6+
devtool: "eval-source-map",
7+
entry: "./index.js"
8+
};

0 commit comments

Comments
 (0)