feat(SourceMapDevToolPlugin): support `append` option as a function

This commit is contained in:
Nitin Kumar 2023-05-24 11:09:36 +05:30
parent 27e95ff3b5
commit e7ae10a4dc
6 changed files with 33 additions and 7 deletions

View File

@ -17,7 +17,13 @@ export interface SourceMapDevToolPluginOptions {
/**
* Appends the given value to the original asset. Usually the #sourceMappingURL comment. [url] is replaced with a URL to the source map file. false disables the appending.
*/
append?: (false | null) | string;
append?:
| (false | null)
| string
| ((
pathData: import("../../lib/Compilation").PathData,
assetInfo?: import("../../lib/Compilation").AssetInfo
) => string);
/**
* Indicates whether column mappings should be used (defaults to true).
*/

View File

@ -48,7 +48,9 @@ class EvalSourceMapDevToolPlugin {
options = inputOptions;
}
this.sourceMapComment =
options.append || "//# sourceURL=[module]\n//# sourceMappingURL=[url]";
options.append && typeof options.append !== "function"
? options.append
: "//# sourceURL=[module]\n//# sourceMappingURL=[url]";
this.moduleFilenameTemplate =
options.moduleFilenameTemplate ||
"webpack://[namespace]/[resource-path]?[hash]";

View File

@ -23,6 +23,7 @@ const { makePathsAbsolute } = require("./util/identifier");
/** @typedef {import("./CacheFacade").ItemCacheFacade} ItemCacheFacade */
/** @typedef {import("./Chunk")} Chunk */
/** @typedef {import("./Compilation").AssetInfo} AssetInfo */
/** @typedef {import("./Compilation").PathData} PathData */
/** @typedef {import("./Compiler")} Compiler */
/** @typedef {import("./Module")} Module */
/** @typedef {import("./NormalModule").SourceMap} SourceMap */
@ -139,7 +140,7 @@ class SourceMapDevToolPlugin {
/** @type {string | false} */
this.sourceMapFilename = options.filename;
/** @type {string | false} */
/** @type {string | false | (function(PathData, AssetInfo=): string)}} */
this.sourceMappingURLComment =
options.append === false
? false
@ -447,13 +448,14 @@ class SourceMapDevToolPlugin {
);
}
/** @type {string | false} */
/** @type {string | false | (function(PathData, AssetInfo=): string)} */
let currentSourceMappingURLComment = sourceMappingURLComment;
let cssExtensionDetected =
CSS_EXTENSION_DETECT_REGEXP.test(file);
resetRegexpState(CSS_EXTENSION_DETECT_REGEXP);
if (
currentSourceMappingURLComment !== false &&
typeof currentSourceMappingURLComment !== "function" &&
cssExtensionDetected
) {
currentSourceMappingURLComment =
@ -534,6 +536,11 @@ class SourceMapDevToolPlugin {
"SourceMapDevToolPlugin: append can't be false when no filename is provided"
);
}
if (typeof currentSourceMappingURLComment === "function") {
throw new Error(
"SourceMapDevToolPlugin: append can't be a function when no filename is provided"
);
}
/**
* Add source map as data url to asset
*/

File diff suppressed because one or more lines are too long

View File

@ -47,6 +47,10 @@
{
"type": "string",
"minLength": 1
},
{
"instanceof": "Function",
"tsType": "((pathData: import(\"../../lib/Compilation\").PathData, assetInfo?: import(\"../../lib/Compilation\").AssetInfo) => string)"
}
]
},

11
types.d.ts vendored
View File

@ -11648,7 +11648,10 @@ declare interface SourceMap {
declare class SourceMapDevToolPlugin {
constructor(options?: SourceMapDevToolPluginOptions);
sourceMapFilename: string | false;
sourceMappingURLComment: string | false;
sourceMappingURLComment:
| string
| false
| ((arg0: PathData, arg1?: AssetInfo) => string);
moduleFilenameTemplate: string | Function;
fallbackModuleFilenameTemplate: string | Function;
namespace: string;
@ -11663,7 +11666,11 @@ declare interface SourceMapDevToolPluginOptions {
/**
* Appends the given value to the original asset. Usually the #sourceMappingURL comment. [url] is replaced with a URL to the source map file. false disables the appending.
*/
append?: null | string | false;
append?:
| null
| string
| false
| ((pathData: PathData, assetInfo?: AssetInfo) => string);
/**
* Indicates whether column mappings should be used (defaults to true).