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

feat(SourceMapDevToolPlugin): support append option as a function #17252

Merged
merged 3 commits into from May 24, 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
8 changes: 7 additions & 1 deletion declarations/plugins/SourceMapDevToolPlugin.d.ts
Expand Up @@ -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).
*/
Expand Down
4 changes: 3 additions & 1 deletion lib/EvalSourceMapDevToolPlugin.js
Expand Up @@ -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]";
Expand Down
11 changes: 9 additions & 2 deletions lib/SourceMapDevToolPlugin.js
Expand Up @@ -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 */
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion schemas/plugins/SourceMapDevToolPlugin.check.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions schemas/plugins/SourceMapDevToolPlugin.json
Expand Up @@ -47,6 +47,10 @@
{
"type": "string",
"minLength": 1
},
{
"instanceof": "Function",
"tsType": "((pathData: import(\"../../lib/Compilation\").PathData, assetInfo?: import(\"../../lib/Compilation\").AssetInfo) => string)"
}
]
},
Expand Down
@@ -0,0 +1,6 @@
it("should have [file] replaced with chunk filename in append", function() {
const fs = require("fs"),
path = require("path");
const source = fs.readFileSync(path.join(__dirname, "some-test.js"), "utf-8");
expect(source).toMatch("//# sourceMappingURL=http://localhost:50505/some-test.js.map");
});
@@ -0,0 +1,5 @@
const testObject = {
a: 1
};

module.exports = testObject;
@@ -0,0 +1,26 @@
const webpack = require("../../../../");
const TerserPlugin = require("terser-webpack-plugin");

/** @type {import("../../../../types").Configuration} */
module.exports = {
node: {
__dirname: false,
__filename: false
},
entry: {
bundle0: ["./index.js"],
"some-test": ["./test.js"]
},
output: {
filename: "[name].js"
},
optimization: {
minimizer: [new TerserPlugin()]
},
plugins: [
new webpack.SourceMapDevToolPlugin({
filename: "sourcemaps/[file].map",
append: data => `\n//# sourceMappingURL=http://localhost:50505/[file].map`
})
]
};
11 changes: 9 additions & 2 deletions types.d.ts
Expand Up @@ -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;
Expand All @@ -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).
Expand Down