Compare commits

...

6 Commits

Author SHA1 Message Date
hai-x 433bcc39ef
Merge 4abdfa94df into 11144e4eec 2025-10-02 23:12:50 +08:00
fregante 11144e4eec
fix: support web workers loading for jsonp format 2025-10-02 17:00:39 +03:00
Hai 4abdfa94df fix 2025-09-30 23:39:56 +08:00
Hai 4587dbb336 fix 2025-09-30 02:42:01 +08:00
Hai c056283928 fix 2025-09-30 02:26:28 +08:00
Hai 5604ce9b89 feat: support ignoreList 2025-09-30 02:26:28 +08:00
15 changed files with 168 additions and 7 deletions

View File

@ -5,11 +5,11 @@
*/
/**
* Include source maps for modules based on their extension (defaults to .js and .css).
* One or multiple conditions used to match resource.
*/
export type Rules = Rule[] | Rule;
/**
* Include source maps for modules based on their extension (defaults to .js and .css).
* Condition used to match resource (string, RegExp or Function).
*/
export type Rule = RegExp | string | ((str: string) => boolean);
@ -47,6 +47,10 @@ export interface SourceMapDevToolPluginOptions {
* Defines the output filename of the SourceMap (will be inlined if no value is provided).
*/
filename?: (false | null) | string;
/**
* Decide whether to ignore source files that match the specified value in the SourceMap.
*/
ignoreList?: Rules;
/**
* Include source maps for module paths that match the given value.
*/

View File

@ -18,6 +18,7 @@ const { makePathsAbsolute } = require("./util/identifier");
/** @typedef {import("webpack-sources").RawSourceMap} RawSourceMap */
/** @typedef {import("webpack-sources").Source} Source */
/** @typedef {import("../declarations/plugins/SourceMapDevToolPlugin").SourceMapDevToolPluginOptions} SourceMapDevToolPluginOptions */
/** @typedef {import("../declarations/plugins/SourceMapDevToolPlugin").Rules} Rules */
/** @typedef {import("./ChunkGraph").ModuleId} ModuleId */
/** @typedef {import("./Compiler")} Compiler */
@ -163,6 +164,21 @@ class EvalSourceMapDevToolPlugin {
}
);
sourceMap.sources = moduleFilenames;
sourceMap.ignoreList = options.ignoreList
? sourceMap.sources.reduce(
/** @type {(acc: number[], sourceName: string, idx: number) => number[]} */ (
(acc, sourceName, idx) => {
const rule = /** @type {Rules} */ (options.ignoreList);
if (ModuleFilenameHelpers.matchPart(sourceName, rule)) {
acc.push(idx);
}
return acc;
}
),
[]
)
: [];
if (options.noSources) {
sourceMap.sourcesContent = undefined;
}

View File

@ -20,6 +20,7 @@ const { makePathsAbsolute } = require("./util/identifier");
/** @typedef {import("webpack-sources").MapOptions} MapOptions */
/** @typedef {import("webpack-sources").Source} Source */
/** @typedef {import("../declarations/plugins/SourceMapDevToolPlugin").SourceMapDevToolPluginOptions} SourceMapDevToolPluginOptions */
/** @typedef {import("../declarations/plugins/SourceMapDevToolPlugin").Rules} Rules */
/** @typedef {import("./CacheFacade").ItemCacheFacade} ItemCacheFacade */
/** @typedef {import("./Chunk")} Chunk */
/** @typedef {import("./Compilation").Asset} Asset */
@ -433,6 +434,25 @@ class SourceMapDevToolPlugin {
moduleToSourceNameMapping.get(m)
);
sourceMap.sources = /** @type {string[]} */ (moduleFilenames);
sourceMap.ignoreList = options.ignoreList
? sourceMap.sources.reduce(
/** @type {(acc: number[], sourceName: string, idx: number) => number[]} */ (
(acc, sourceName, idx) => {
const rule = /** @type {Rules} */ (
options.ignoreList
);
if (
ModuleFilenameHelpers.matchPart(sourceName, rule)
) {
acc.push(idx);
}
return acc;
}
),
[]
)
: [];
if (options.noSources) {
sourceMap.sourcesContent = undefined;
}

View File

@ -69,7 +69,7 @@ class JsonpChunkLoadingRuntimeModule extends RuntimeModule {
if (options && options.baseUri) {
return `${RuntimeGlobals.baseURI} = ${JSON.stringify(options.baseUri)};`;
}
return `${RuntimeGlobals.baseURI} = (document && document.baseURI) || self.location.href;`;
return `${RuntimeGlobals.baseURI} = (typeof document !== 'undefined' && document.baseURI) || self.location.href;`;
}
/**

File diff suppressed because one or more lines are too long

View File

@ -1,7 +1,7 @@
{
"definitions": {
"rule": {
"description": "Include source maps for modules based on their extension (defaults to .js and .css).",
"description": "Condition used to match resource (string, RegExp or Function).",
"anyOf": [
{
"instanceof": "RegExp",
@ -18,7 +18,7 @@
]
},
"rules": {
"description": "Include source maps for modules based on their extension (defaults to .js and .css).",
"description": "One or multiple conditions used to match resource.",
"anyOf": [
{
"type": "array",
@ -106,6 +106,14 @@
}
]
},
"ignoreList": {
"description": "Decide whether to ignore source files that match the specified value in the SourceMap.",
"oneOf": [
{
"$ref": "#/definitions/rules"
}
]
},
"include": {
"description": "Include source maps for module paths that match the given value.",
"oneOf": [
@ -149,7 +157,12 @@
"type": "string"
},
"test": {
"$ref": "#/definitions/rules"
"description": "Include source maps for modules based on their extension (defaults to .js and .css).",
"oneOf": [
{
"$ref": "#/definitions/rules"
}
]
}
}
}

View File

@ -0,0 +1 @@
export default "ignored";

View File

@ -0,0 +1,34 @@
import used from "./used";
import ignored from "./ignored";
import fs from "fs";
const getSourceMap = () => {
const source = fs.readFileSync(__filename, "utf-8");
const match =
/\/\/# sourceMappingURL\s*=\s*data:application\/json;charset=utf-8;base64,(.*)\\n\/\/#/.exec(
source
);
const mapString = Buffer.from(match[1], "base64").toString("utf-8");
return JSON.parse(mapString);
};
const map = getSourceMap();
it("marks matching modules in ignoreList", () => {
const sources = map.sources;
const ignoredIndex = sources.findIndex((source) =>
/ignored\.js/.test(source)
);
expect(ignored).toBe("ignored");
expect(ignoredIndex).not.toBe(-1);
expect(Array.isArray(map.ignoreList)).toBe(true);
expect(map.ignoreList).toContain(ignoredIndex);
});
it("keeps other modules outside ignoreList", () => {
const sources = map.sources;
const usedIndex = sources.findIndex((source) => /used\.js/.test(source));
expect(used).toBe("used");
expect(usedIndex).not.toBe(-1);
expect(map.ignoreList).not.toContain(usedIndex);
});

View File

@ -0,0 +1 @@
export default "used";

View File

@ -0,0 +1,17 @@
"use strict";
const webpack = require("../../../../");
/** @type {import("../../../../").Configuration} */
module.exports = {
devtool: false,
plugins: [
new webpack.EvalSourceMapDevToolPlugin({
ignoreList: [/ignored\.js/]
})
],
optimization: {
// Ensure the correct `sourceMappingURL` is detected
concatenateModules: true
}
};

View File

@ -0,0 +1 @@
export default "ignored";

View File

@ -0,0 +1,33 @@
import used from "./used";
import ignored from "./ignored";
import fs from "fs";
import path from "path";
const getSourceMap = () => {
const content = fs.readFileSync(
path.join(__dirname, "bundle0.js.map"),
"utf-8"
);
return JSON.parse(content);
};
const map = getSourceMap();
it("marks matching modules in ignoreList", () => {
const sources = map.sources;
const ignoredIndex = sources.findIndex((source) =>
/ignored\.js/.test(source)
);
expect(ignored).toBe("ignored");
expect(ignoredIndex).not.toBe(-1);
expect(Array.isArray(map.ignoreList)).toBe(true);
expect(map.ignoreList).toContain(ignoredIndex);
});
it("keeps other modules outside ignoreList", () => {
const sources = map.sources;
const usedIndex = sources.findIndex((source) => /used\.js/.test(source));
expect(used).toBe("used");
expect(usedIndex).not.toBe(-1);
expect(map.ignoreList).not.toContain(usedIndex);
});

View File

@ -0,0 +1 @@
export default "used";

View File

@ -0,0 +1,15 @@
"use strict";
const webpack = require("../../../../");
/** @type {import("../../../../").Configuration} */
module.exports = {
mode: "development",
devtool: false,
plugins: [
new webpack.SourceMapDevToolPlugin({
filename: "[file].map",
ignoreList: [/ignored\.js/]
})
]
};

5
types.d.ts vendored
View File

@ -16513,6 +16513,11 @@ declare interface SourceMapDevToolPluginOptions {
*/
filename?: null | string | false;
/**
* Decide whether to ignore source files that match the specified value in the SourceMap.
*/
ignoreList?: string | RegExp | Rule[] | ((str: string) => boolean);
/**
* Include source maps for module paths that match the given value.
*/