webpack/lib/SourceMapDevToolPlugin.js

607 lines
18 KiB
JavaScript
Raw Normal View History

2013-03-26 23:54:41 +08:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
2018-07-30 23:08:51 +08:00
"use strict";
2013-03-26 23:54:41 +08:00
const asyncLib = require("neo-async");
const { ConcatSource, RawSource } = require("webpack-sources");
const Compilation = require("./Compilation");
const ModuleFilenameHelpers = require("./ModuleFilenameHelpers");
2018-11-27 07:06:02 +08:00
const ProgressPlugin = require("./ProgressPlugin");
const SourceMapDevToolModuleOptionsPlugin = require("./SourceMapDevToolModuleOptionsPlugin");
const createSchemaValidation = require("./util/create-schema-validation");
const createHash = require("./util/createHash");
2025-07-03 17:06:45 +08:00
const { dirname, relative } = require("./util/fs");
2024-11-21 21:03:36 +08:00
const generateDebugId = require("./util/generateDebugId");
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("./CacheFacade").ItemCacheFacade} ItemCacheFacade */
/** @typedef {import("./Chunk")} Chunk */
2023-06-17 01:13:03 +08:00
/** @typedef {import("./Compilation").Asset} Asset */
/** @typedef {import("./Compilation").AssetInfo} AssetInfo */
/** @typedef {import("./Compiler")} Compiler */
/** @typedef {import("./Module")} Module */
/** @typedef {import("./NormalModule").RawSourceMap} RawSourceMap */
2024-08-08 02:59:26 +08:00
/** @typedef {import("./TemplatedPathPlugin").TemplatePath} TemplatePath */
2024-03-12 00:33:52 +08:00
/** @typedef {import("./util/fs").OutputFileSystem} OutputFileSystem */
const validate = createSchemaValidation(
require("../schemas/plugins/SourceMapDevToolPlugin.check"),
() => require("../schemas/plugins/SourceMapDevToolPlugin.json"),
{
name: "SourceMap DevTool Plugin",
baseDataPath: "options"
}
);
/**
* @typedef {object} SourceMapTask
* @property {Source} asset
* @property {AssetInfo} assetInfo
2019-07-17 22:02:33 +08:00
* @property {(string | Module)[]} modules
* @property {string} source
* @property {string} file
* @property {RawSourceMap} sourceMap
* @property {ItemCacheFacade} cacheItem cache item
*/
const METACHARACTERS_REGEXP = /[-[\]\\/{}()*+?.^$|]/g;
const CONTENT_HASH_DETECT_REGEXP = /\[contenthash(:\w+)?\]/;
const CSS_AND_JS_MODULE_EXTENSIONS_REGEXP = /\.((c|m)?js|css)($|\?)/i;
const CSS_EXTENSION_DETECT_REGEXP = /\.css($|\?)/i;
const MAP_URL_COMMENT_REGEXP = /\[map\]/g;
const URL_COMMENT_REGEXP = /\[url\]/g;
const URL_FORMATTING_REGEXP = /^\n\/\/(.*)$/;
2022-04-27 04:46:26 +08:00
/**
* Reset's .lastIndex of stateful Regular Expressions
* For when `test` or `exec` is called on them
* @param {RegExp} regexp Stateful Regular Expression to be reset
* @returns {void}
*/
const resetRegexpState = (regexp) => {
2022-04-27 04:46:26 +08:00
regexp.lastIndex = -1;
};
/**
* Escapes regular expression metacharacters
* @param {string} str String to quote
* @returns {string} Escaped string
*/
const quoteMeta = (str) => str.replace(METACHARACTERS_REGEXP, "\\$&");
/**
* Creating {@link SourceMapTask} for given file
2019-07-09 04:31:11 +08:00
* @param {string} file current compiled file
* @param {Source} asset the asset
* @param {AssetInfo} assetInfo the asset info
* @param {MapOptions} options source map options
2019-07-09 04:31:11 +08:00
* @param {Compilation} compilation compilation instance
* @param {ItemCacheFacade} cacheItem cache item
2019-07-09 04:31:11 +08:00
* @returns {SourceMapTask | undefined} created task instance or `undefined`
*/
const getTaskForFile = (
file,
asset,
assetInfo,
options,
compilation,
cacheItem
) => {
let source;
/** @type {RawSourceMap} */
let sourceMap;
/**
* Check if asset can build source map
*/
2018-02-25 09:00:20 +08:00
if (asset.sourceAndMap) {
const sourceAndMap = asset.sourceAndMap(options);
sourceMap = /** @type {RawSourceMap} */ (sourceAndMap.map);
source = sourceAndMap.source;
} else {
sourceMap = /** @type {RawSourceMap} */ (asset.map(options));
source = asset.source();
}
if (!sourceMap || typeof source !== "string") return;
2025-08-20 18:50:12 +08:00
const context = compilation.options.context;
const root = compilation.compiler.root;
const cachedAbsolutify = makePathsAbsolute.bindContextCache(context, root);
const modules = sourceMap.sources.map((source) => {
if (!source.startsWith("webpack://")) return source;
2020-04-17 16:54:27 +08:00
source = cachedAbsolutify(source.slice(10));
const module = compilation.findModule(source);
return module || source;
});
return {
file,
asset,
source,
assetInfo,
sourceMap,
2019-10-31 03:47:17 +08:00
modules,
cacheItem
};
2017-11-08 18:32:05 +08:00
};
2025-04-23 20:03:37 +08:00
const PLUGIN_NAME = "SourceMapDevToolPlugin";
class SourceMapDevToolPlugin {
/**
2025-04-16 22:04:11 +08:00
* @param {SourceMapDevToolPluginOptions=} options options object
* @throws {Error} throws error, if got more than 1 arguments
*/
2018-07-06 17:19:30 +08:00
constructor(options = {}) {
validate(options);
2017-10-28 05:23:38 +08:00
2024-08-06 11:08:48 +08:00
this.sourceMapFilename = /** @type {string | false} */ (options.filename);
2024-08-08 02:59:26 +08:00
/** @type {false | TemplatePath}} */
2018-02-25 09:00:20 +08:00
this.sourceMappingURLComment =
options.append === false
? false
2024-07-31 09:37:24 +08:00
: // eslint-disable-next-line no-useless-concat
options.append || "\n//# source" + "MappingURL=[url]";
2018-02-25 09:00:20 +08:00
this.moduleFilenameTemplate =
options.moduleFilenameTemplate || "webpack://[namespace]/[resourcePath]";
this.fallbackModuleFilenameTemplate =
options.fallbackModuleFilenameTemplate ||
"webpack://[namespace]/[resourcePath]?[hash]";
this.namespace = options.namespace || "";
this.options = options;
2015-02-19 08:11:29 +08:00
}
/**
2020-04-23 16:48:36 +08:00
* Apply the plugin
2019-07-09 04:31:11 +08:00
* @param {Compiler} compiler compiler instance
* @returns {void}
*/
apply(compiler) {
2025-08-20 18:50:12 +08:00
const outputFs =
/** @type {OutputFileSystem} */
(compiler.outputFileSystem);
const sourceMapFilename = this.sourceMapFilename;
const sourceMappingURLComment = this.sourceMappingURLComment;
const moduleFilenameTemplate = this.moduleFilenameTemplate;
const namespace = this.namespace;
const fallbackModuleFilenameTemplate = this.fallbackModuleFilenameTemplate;
const requestShortener = compiler.requestShortener;
const options = this.options;
options.test = options.test || CSS_AND_JS_MODULE_EXTENSIONS_REGEXP;
2018-02-25 09:00:20 +08:00
const matchObject = ModuleFilenameHelpers.matchObject.bind(
undefined,
options
);
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
new SourceMapDevToolModuleOptionsPlugin(options).apply(compilation);
compilation.hooks.processAssets.tapAsync(
{
2025-04-23 20:03:37 +08:00
name: PLUGIN_NAME,
stage: Compilation.PROCESS_ASSETS_STAGE_DEV_TOOLING,
additionalAssets: true
},
2019-10-31 03:47:17 +08:00
(assets, callback) => {
const chunkGraph = compilation.chunkGraph;
2025-04-23 20:03:37 +08:00
const cache = compilation.getCache(PLUGIN_NAME);
/** @type {Map<string | Module, string>} */
2018-02-25 09:00:20 +08:00
const moduleToSourceNameMapping = new Map();
const reportProgress =
2018-11-27 07:06:02 +08:00
ProgressPlugin.getReporter(compilation.compiler) || (() => {});
2018-02-25 09:00:20 +08:00
/** @type {Map<string, Chunk>} */
const fileToChunk = new Map();
for (const chunk of compilation.chunks) {
2018-02-25 09:00:20 +08:00
for (const file of chunk.files) {
fileToChunk.set(file, chunk);
}
for (const file of chunk.auxiliaryFiles) {
fileToChunk.set(file, chunk);
}
}
/** @type {string[]} */
const files = [];
for (const file of Object.keys(assets)) {
if (matchObject(file)) {
files.push(file);
2017-07-07 18:51:55 +08:00
}
2018-01-22 20:52:43 +08:00
}
2024-07-31 16:17:46 +08:00
reportProgress(0);
2019-07-17 22:02:33 +08:00
/** @type {SourceMapTask[]} */
2018-02-25 09:00:20 +08:00
const tasks = [];
let fileIndex = 0;
asyncLib.each(
files,
2019-10-31 03:47:17 +08:00
(file, callback) => {
2023-06-17 01:13:03 +08:00
const asset =
/** @type {Readonly<Asset>} */
(compilation.getAsset(file));
2020-08-01 04:03:14 +08:00
if (asset.info.related && asset.info.related.sourceMap) {
fileIndex++;
return callback();
}
const chunk = fileToChunk.get(file);
const sourceMapNamespace = compilation.getPath(this.namespace, {
2024-10-10 23:37:04 +08:00
chunk
});
const cacheItem = cache.getItemCache(
file,
cache.mergeEtags(
cache.getLazyHashedEtag(asset.source),
sourceMapNamespace
)
);
2018-01-04 22:41:26 +08:00
2020-08-01 04:03:14 +08:00
cacheItem.get((err, cacheEntry) => {
2019-10-31 03:47:17 +08:00
if (err) {
return callback(err);
}
/**
* If presented in cache, reassigns assets. Cache assets already have source maps.
*/
2020-08-01 04:03:14 +08:00
if (cacheEntry) {
const { assets, assetsInfo } = cacheEntry;
for (const cachedFile of Object.keys(assets)) {
2019-10-31 03:47:17 +08:00
if (cachedFile === file) {
2020-08-01 04:03:14 +08:00
compilation.updateAsset(
cachedFile,
assets[cachedFile],
assetsInfo[cachedFile]
);
2019-10-31 03:47:17 +08:00
} else {
2020-08-01 04:03:14 +08:00
compilation.emitAsset(
cachedFile,
assets[cachedFile],
assetsInfo[cachedFile]
);
2019-10-31 03:47:17 +08:00
}
/**
* Add file to chunk, if not presented there
*/
if (cachedFile !== file && chunk !== undefined) {
2024-10-10 23:37:04 +08:00
chunk.auxiliaryFiles.add(cachedFile);
}
}
2018-01-04 22:41:26 +08:00
reportProgress(
2019-11-12 21:35:19 +08:00
(0.5 * ++fileIndex) / files.length,
file,
2019-11-12 21:35:19 +08:00
"restored cached SourceMap"
);
2018-01-04 22:41:26 +08:00
2019-10-31 03:47:17 +08:00
return callback();
}
2019-11-12 21:35:19 +08:00
reportProgress(
(0.5 * fileIndex) / files.length,
file,
"generate SourceMap"
);
2019-10-31 03:47:17 +08:00
/** @type {SourceMapTask | undefined} */
const task = getTaskForFile(
file,
2020-08-01 04:03:14 +08:00
asset.source,
asset.info,
{
module: options.module,
columns: options.columns
},
2019-10-31 03:47:17 +08:00
compilation,
cacheItem
2019-10-31 03:47:17 +08:00
);
if (task) {
const modules = task.modules;
2018-02-25 09:00:20 +08:00
2019-10-31 03:47:17 +08:00
for (let idx = 0; idx < modules.length; idx++) {
const module = modules[idx];
if (
typeof module === "string" &&
/^(data|https?):/.test(module)
) {
moduleToSourceNameMapping.set(module, module);
continue;
}
2019-10-31 03:47:17 +08:00
if (!moduleToSourceNameMapping.get(module)) {
moduleToSourceNameMapping.set(
module,
ModuleFilenameHelpers.createFilename(
module,
2019-10-31 03:47:17 +08:00
{
2024-07-31 04:09:42 +08:00
moduleFilenameTemplate,
namespace: sourceMapNamespace
2019-10-31 03:47:17 +08:00
},
{
requestShortener,
chunkGraph,
hashFunction: compilation.outputOptions.hashFunction
2019-10-31 03:47:17 +08:00
}
)
);
}
}
2019-10-31 03:47:17 +08:00
tasks.push(task);
}
2019-10-31 03:47:17 +08:00
reportProgress(
2019-11-12 21:35:19 +08:00
(0.5 * ++fileIndex) / files.length,
2019-10-31 03:47:17 +08:00
file,
2019-11-12 21:35:19 +08:00
"generated SourceMap"
2019-10-31 03:47:17 +08:00
);
callback();
});
},
(err) => {
2019-10-31 03:47:17 +08:00
if (err) {
return callback(err);
}
reportProgress(0.5, "resolve sources");
/** @type {Set<string>} */
const usedNamesSet = new Set(moduleToSourceNameMapping.values());
/** @type {Set<string>} */
const conflictDetectionSet = new Set();
/**
* all modules in defined order (longest identifier first)
2025-08-28 18:34:30 +08:00
* @type {(string | Module)[]}
*/
2025-07-03 17:06:45 +08:00
const allModules = [...moduleToSourceNameMapping.keys()].sort(
(a, b) => {
const ai = typeof a === "string" ? a : a.identifier();
const bi = typeof b === "string" ? b : b.identifier();
return ai.length - bi.length;
}
);
// find modules with conflicting source names
for (let idx = 0; idx < allModules.length; idx++) {
const module = allModules[idx];
2023-06-17 01:13:03 +08:00
let sourceName =
/** @type {string} */
(moduleToSourceNameMapping.get(module));
let hasName = conflictDetectionSet.has(sourceName);
if (!hasName) {
conflictDetectionSet.add(sourceName);
continue;
}
// try the fallback name first
sourceName = ModuleFilenameHelpers.createFilename(
module,
{
moduleFilenameTemplate: fallbackModuleFilenameTemplate,
2024-07-31 04:09:42 +08:00
namespace
},
{
requestShortener,
chunkGraph,
hashFunction: compilation.outputOptions.hashFunction
}
);
hasName = usedNamesSet.has(sourceName);
if (!hasName) {
moduleToSourceNameMapping.set(module, sourceName);
usedNamesSet.add(sourceName);
continue;
}
2020-03-13 00:51:26 +08:00
// otherwise just append stars until we have a valid name
while (hasName) {
sourceName += "*";
hasName = usedNamesSet.has(sourceName);
}
moduleToSourceNameMapping.set(module, sourceName);
usedNamesSet.add(sourceName);
}
let taskIndex = 0;
asyncLib.each(
tasks,
2019-10-31 03:47:17 +08:00
(task, callback) => {
const assets = Object.create(null);
2020-08-01 04:03:14 +08:00
const assetsInfo = Object.create(null);
const file = task.file;
const chunk = fileToChunk.get(file);
const sourceMap = task.sourceMap;
const source = task.source;
const modules = task.modules;
2019-11-12 21:35:19 +08:00
reportProgress(
0.5 + (0.5 * taskIndex) / tasks.length,
file,
"attach SourceMap"
);
const moduleFilenames = modules.map((m) =>
moduleToSourceNameMapping.get(m)
);
2024-08-09 01:03:17 +08:00
sourceMap.sources = /** @type {string[]} */ (moduleFilenames);
if (options.noSources) {
sourceMap.sourcesContent = undefined;
}
sourceMap.sourceRoot = options.sourceRoot || "";
sourceMap.file = file;
const usesContentHash =
sourceMapFilename &&
CONTENT_HASH_DETECT_REGEXP.test(sourceMapFilename);
2022-04-27 04:46:26 +08:00
resetRegexpState(CONTENT_HASH_DETECT_REGEXP);
// If SourceMap and asset uses contenthash, avoid a circular dependency by hiding hash in `file`
if (usesContentHash && task.assetInfo.contenthash) {
const contenthash = task.assetInfo.contenthash;
2024-08-02 02:36:27 +08:00
const pattern = Array.isArray(contenthash)
? contenthash.map(quoteMeta).join("|")
: quoteMeta(contenthash);
sourceMap.file = sourceMap.file.replace(
new RegExp(pattern, "g"),
(m) => "x".repeat(m.length)
);
}
2024-08-08 02:59:26 +08:00
/** @type {false | TemplatePath} */
2019-10-31 03:47:17 +08:00
let currentSourceMappingURLComment = sourceMappingURLComment;
2024-07-31 04:09:42 +08:00
const cssExtensionDetected =
2022-04-27 04:46:26 +08:00
CSS_EXTENSION_DETECT_REGEXP.test(file);
resetRegexpState(CSS_EXTENSION_DETECT_REGEXP);
2019-10-31 03:47:17 +08:00
if (
currentSourceMappingURLComment !== false &&
typeof currentSourceMappingURLComment !== "function" &&
2022-04-27 04:46:26 +08:00
cssExtensionDetected
2019-10-31 03:47:17 +08:00
) {
2021-05-11 15:31:46 +08:00
currentSourceMappingURLComment =
currentSourceMappingURLComment.replace(
URL_FORMATTING_REGEXP,
2021-05-11 15:31:46 +08:00
"\n/*$1*/"
);
2019-10-31 03:47:17 +08:00
}
if (options.debugIds) {
2024-11-21 21:03:36 +08:00
const debugId = generateDebugId(source, sourceMap.file);
sourceMap.debugId = debugId;
currentSourceMappingURLComment = `\n//# debugId=${debugId}${currentSourceMappingURLComment}`;
}
2019-10-31 03:47:17 +08:00
const sourceMapString = JSON.stringify(sourceMap);
if (sourceMapFilename) {
2024-07-31 04:09:42 +08:00
const filename = file;
2025-10-07 22:40:59 +08:00
const sourceMapContentHash = usesContentHash
? createHash(compilation.outputOptions.hashFunction)
.update(sourceMapString)
.digest("hex")
: undefined;
2019-10-31 03:47:17 +08:00
const pathParams = {
chunk,
filename: options.fileContext
? relative(
outputFs,
`/${options.fileContext}`,
`/${filename}`
2024-07-31 05:43:19 +08:00
)
2019-10-31 03:47:17 +08:00
: filename,
contentHash: sourceMapContentHash
2019-10-31 03:47:17 +08:00
};
2021-05-11 15:31:46 +08:00
const { path: sourceMapFile, info: sourceMapInfo } =
compilation.getPathWithInfo(
sourceMapFilename,
pathParams
);
2019-10-31 03:47:17 +08:00
const sourceMapUrl = options.publicPath
? options.publicPath + sourceMapFile
: relative(
outputFs,
dirname(outputFs, `/${file}`),
`/${sourceMapFile}`
2024-07-31 05:43:19 +08:00
);
2020-08-01 04:03:14 +08:00
/** @type {Source} */
let asset = new RawSource(source);
2019-10-31 03:47:17 +08:00
if (currentSourceMappingURLComment !== false) {
2020-08-01 04:03:14 +08:00
// Add source map url to compilation asset, if currentSourceMappingURLComment is set
asset = new ConcatSource(
asset,
2024-07-31 09:37:24 +08:00
compilation.getPath(currentSourceMappingURLComment, {
url: sourceMapUrl,
...pathParams
})
2019-10-31 03:47:17 +08:00
);
}
2020-08-01 04:03:14 +08:00
const assetInfo = {
related: { sourceMap: sourceMapFile }
};
assets[file] = asset;
assetsInfo[file] = assetInfo;
compilation.updateAsset(file, asset, assetInfo);
// Add source map file to compilation assets and chunk files
const sourceMapAsset = new RawSource(sourceMapString);
const sourceMapAssetInfo = {
...sourceMapInfo,
2019-10-31 03:47:17 +08:00
development: true
2020-08-01 04:03:14 +08:00
};
assets[sourceMapFile] = sourceMapAsset;
assetsInfo[sourceMapFile] = sourceMapAssetInfo;
compilation.emitAsset(
sourceMapFile,
sourceMapAsset,
sourceMapAssetInfo
);
if (chunk !== undefined) {
2019-10-31 03:47:17 +08:00
chunk.auxiliaryFiles.add(sourceMapFile);
}
2019-10-31 03:47:17 +08:00
} else {
if (currentSourceMappingURLComment === false) {
throw new Error(
2025-04-23 20:03:37 +08:00
`${PLUGIN_NAME}: append can't be false when no filename is provided`
2019-10-31 03:47:17 +08:00
);
}
if (typeof currentSourceMappingURLComment === "function") {
throw new Error(
2025-04-23 20:03:37 +08:00
`${PLUGIN_NAME}: append can't be a function when no filename is provided`
);
}
2019-10-31 03:47:17 +08:00
/**
* Add source map as data url to asset
*/
const asset = new ConcatSource(
new RawSource(source),
currentSourceMappingURLComment
.replace(MAP_URL_COMMENT_REGEXP, () => sourceMapString)
2019-10-31 03:47:17 +08:00
.replace(
URL_COMMENT_REGEXP,
2019-10-31 03:47:17 +08:00
() =>
`data:application/json;charset=utf-8;base64,${Buffer.from(
sourceMapString,
"utf8"
2019-10-31 03:47:17 +08:00
).toString("base64")}`
)
);
assets[file] = asset;
2020-08-01 04:03:14 +08:00
assetsInfo[file] = undefined;
2019-10-31 03:47:17 +08:00
compilation.updateAsset(file, asset);
}
task.cacheItem.store({ assets, assetsInfo }, (err) => {
reportProgress(
0.5 + (0.5 * ++taskIndex) / tasks.length,
task.file,
"attached SourceMap"
);
2019-10-31 03:47:17 +08:00
if (err) {
return callback(err);
}
callback();
});
},
(err) => {
2024-07-31 16:17:46 +08:00
reportProgress(1);
2019-10-31 03:47:17 +08:00
callback(err);
}
2018-02-25 09:00:20 +08:00
);
}
);
2018-11-27 07:06:02 +08:00
}
2018-02-25 09:00:20 +08:00
);
2013-03-26 23:54:41 +08:00
});
}
}
module.exports = SourceMapDevToolPlugin;