SourceMapDevToolPlugin.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const asyncLib = require("neo-async");
  7. const { ConcatSource, RawSource } = require("webpack-sources");
  8. const Compilation = require("./Compilation");
  9. const ModuleFilenameHelpers = require("./ModuleFilenameHelpers");
  10. const ProgressPlugin = require("./ProgressPlugin");
  11. const SourceMapDevToolModuleOptionsPlugin = require("./SourceMapDevToolModuleOptionsPlugin");
  12. const createSchemaValidation = require("./util/create-schema-validation");
  13. const createHash = require("./util/createHash");
  14. const { dirname, relative } = require("./util/fs");
  15. const generateDebugId = require("./util/generateDebugId");
  16. const { makePathsAbsolute } = require("./util/identifier");
  17. /** @typedef {import("webpack-sources").MapOptions} MapOptions */
  18. /** @typedef {import("webpack-sources").Source} Source */
  19. /** @typedef {import("../declarations/WebpackOptions").DevtoolNamespace} DevtoolNamespace */
  20. /** @typedef {import("../declarations/WebpackOptions").DevtoolModuleFilenameTemplate} DevtoolModuleFilenameTemplate */
  21. /** @typedef {import("../declarations/WebpackOptions").DevtoolFallbackModuleFilenameTemplate} DevtoolFallbackModuleFilenameTemplate */
  22. /** @typedef {import("../declarations/plugins/SourceMapDevToolPlugin").SourceMapDevToolPluginOptions} SourceMapDevToolPluginOptions */
  23. /** @typedef {import("../declarations/plugins/SourceMapDevToolPlugin").Rules} Rules */
  24. /** @typedef {import("./CacheFacade").ItemCacheFacade} ItemCacheFacade */
  25. /** @typedef {import("./Chunk")} Chunk */
  26. /** @typedef {import("./Compilation").Asset} Asset */
  27. /** @typedef {import("./Compilation").AssetInfo} AssetInfo */
  28. /** @typedef {import("./Compiler")} Compiler */
  29. /** @typedef {import("./Module")} Module */
  30. /** @typedef {import("./NormalModule").RawSourceMap} RawSourceMap */
  31. /** @typedef {import("./TemplatedPathPlugin").TemplatePath} TemplatePath */
  32. /** @typedef {import("./util/fs").OutputFileSystem} OutputFileSystem */
  33. const validate = createSchemaValidation(
  34. require("../schemas/plugins/SourceMapDevToolPlugin.check"),
  35. () => require("../schemas/plugins/SourceMapDevToolPlugin.json"),
  36. {
  37. name: "SourceMap DevTool Plugin",
  38. baseDataPath: "options"
  39. }
  40. );
  41. /**
  42. * @typedef {object} SourceMapTask
  43. * @property {Source} asset
  44. * @property {AssetInfo} assetInfo
  45. * @property {(string | Module)[]} modules
  46. * @property {string} source
  47. * @property {string} file
  48. * @property {RawSourceMap} sourceMap
  49. * @property {ItemCacheFacade} cacheItem cache item
  50. */
  51. const METACHARACTERS_REGEXP = /[-[\]\\/{}()*+?.^$|]/g;
  52. const CONTENT_HASH_DETECT_REGEXP = /\[contenthash(?::\w+)?\]/;
  53. const CSS_AND_JS_MODULE_EXTENSIONS_REGEXP = /\.((c|m)?js|css)($|\?)/i;
  54. const CSS_EXTENSION_DETECT_REGEXP = /\.css(?:$|\?)/i;
  55. const MAP_URL_COMMENT_REGEXP = /\[map\]/g;
  56. const URL_COMMENT_REGEXP = /\[url\]/g;
  57. const URL_FORMATTING_REGEXP = /^\n\/\/(.*)$/;
  58. /**
  59. * Reset's .lastIndex of stateful Regular Expressions
  60. * For when `test` or `exec` is called on them
  61. * @param {RegExp} regexp Stateful Regular Expression to be reset
  62. * @returns {void}
  63. */
  64. const resetRegexpState = (regexp) => {
  65. regexp.lastIndex = -1;
  66. };
  67. /**
  68. * Escapes regular expression metacharacters
  69. * @param {string} str String to quote
  70. * @returns {string} Escaped string
  71. */
  72. const quoteMeta = (str) => str.replace(METACHARACTERS_REGEXP, "\\$&");
  73. /**
  74. * Creating {@link SourceMapTask} for given file
  75. * @param {string} file current compiled file
  76. * @param {Source} asset the asset
  77. * @param {AssetInfo} assetInfo the asset info
  78. * @param {MapOptions} options source map options
  79. * @param {Compilation} compilation compilation instance
  80. * @param {ItemCacheFacade} cacheItem cache item
  81. * @returns {SourceMapTask | undefined} created task instance or `undefined`
  82. */
  83. const getTaskForFile = (
  84. file,
  85. asset,
  86. assetInfo,
  87. options,
  88. compilation,
  89. cacheItem
  90. ) => {
  91. /** @type {string | Buffer} */
  92. let source;
  93. /** @type {null | RawSourceMap} */
  94. let sourceMap;
  95. /**
  96. * Check if asset can build source map
  97. */
  98. if (asset.sourceAndMap) {
  99. const sourceAndMap = asset.sourceAndMap(options);
  100. sourceMap = sourceAndMap.map;
  101. source = sourceAndMap.source;
  102. } else {
  103. sourceMap = asset.map(options);
  104. source = asset.source();
  105. }
  106. if (!sourceMap || typeof source !== "string") return;
  107. const context = compilation.options.context;
  108. const root = compilation.compiler.root;
  109. const cachedAbsolutify = makePathsAbsolute.bindContextCache(context, root);
  110. const modules = sourceMap.sources.map((source) => {
  111. if (!source.startsWith("webpack://")) return source;
  112. source = cachedAbsolutify(source.slice(10));
  113. const module = compilation.findModule(source);
  114. return module || source;
  115. });
  116. return {
  117. file,
  118. asset,
  119. source,
  120. assetInfo,
  121. sourceMap,
  122. modules,
  123. cacheItem
  124. };
  125. };
  126. const PLUGIN_NAME = "SourceMapDevToolPlugin";
  127. class SourceMapDevToolPlugin {
  128. /**
  129. * @param {SourceMapDevToolPluginOptions=} options options object
  130. * @throws {Error} throws error, if got more than 1 arguments
  131. */
  132. constructor(options = {}) {
  133. validate(options);
  134. /** @type {undefined | null | false | string} */
  135. this.sourceMapFilename = options.filename;
  136. /** @type {false | TemplatePath} */
  137. this.sourceMappingURLComment =
  138. options.append === false
  139. ? false
  140. : // eslint-disable-next-line no-useless-concat
  141. options.append || "\n//# source" + "MappingURL=[url]";
  142. /** @type {DevtoolModuleFilenameTemplate} */
  143. this.moduleFilenameTemplate =
  144. options.moduleFilenameTemplate || "webpack://[namespace]/[resourcePath]";
  145. /** @type {DevtoolFallbackModuleFilenameTemplate} */
  146. this.fallbackModuleFilenameTemplate =
  147. options.fallbackModuleFilenameTemplate ||
  148. "webpack://[namespace]/[resourcePath]?[hash]";
  149. /** @type {DevtoolNamespace} */
  150. this.namespace = options.namespace || "";
  151. /** @type {SourceMapDevToolPluginOptions} */
  152. this.options = options;
  153. }
  154. /**
  155. * Apply the plugin
  156. * @param {Compiler} compiler compiler instance
  157. * @returns {void}
  158. */
  159. apply(compiler) {
  160. const outputFs =
  161. /** @type {OutputFileSystem} */
  162. (compiler.outputFileSystem);
  163. const sourceMapFilename = this.sourceMapFilename;
  164. const sourceMappingURLComment = this.sourceMappingURLComment;
  165. const moduleFilenameTemplate = this.moduleFilenameTemplate;
  166. const namespace = this.namespace;
  167. const fallbackModuleFilenameTemplate = this.fallbackModuleFilenameTemplate;
  168. const requestShortener = compiler.requestShortener;
  169. const options = this.options;
  170. options.test = options.test || CSS_AND_JS_MODULE_EXTENSIONS_REGEXP;
  171. /** @type {(filename: string) => boolean} */
  172. const matchObject = ModuleFilenameHelpers.matchObject.bind(
  173. undefined,
  174. options
  175. );
  176. compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
  177. new SourceMapDevToolModuleOptionsPlugin(options).apply(compilation);
  178. compilation.hooks.processAssets.tapAsync(
  179. {
  180. name: PLUGIN_NAME,
  181. stage: Compilation.PROCESS_ASSETS_STAGE_DEV_TOOLING,
  182. additionalAssets: true
  183. },
  184. (assets, callback) => {
  185. const chunkGraph = compilation.chunkGraph;
  186. const cache = compilation.getCache(PLUGIN_NAME);
  187. /** @type {Map<string | Module, string>} */
  188. const moduleToSourceNameMapping = new Map();
  189. const reportProgress =
  190. ProgressPlugin.getReporter(compilation.compiler) || (() => {});
  191. /** @type {Map<string, Chunk>} */
  192. const fileToChunk = new Map();
  193. for (const chunk of compilation.chunks) {
  194. for (const file of chunk.files) {
  195. fileToChunk.set(file, chunk);
  196. }
  197. for (const file of chunk.auxiliaryFiles) {
  198. fileToChunk.set(file, chunk);
  199. }
  200. }
  201. /** @type {string[]} */
  202. const files = [];
  203. for (const file of Object.keys(assets)) {
  204. if (matchObject(file)) {
  205. files.push(file);
  206. }
  207. }
  208. reportProgress(0);
  209. /** @type {SourceMapTask[]} */
  210. const tasks = [];
  211. let fileIndex = 0;
  212. asyncLib.each(
  213. files,
  214. (file, callback) => {
  215. const asset =
  216. /** @type {Readonly<Asset>} */
  217. (compilation.getAsset(file));
  218. if (asset.info.related && asset.info.related.sourceMap) {
  219. fileIndex++;
  220. return callback();
  221. }
  222. const chunk = fileToChunk.get(file);
  223. const sourceMapNamespace = compilation.getPath(this.namespace, {
  224. chunk
  225. });
  226. const cacheItem = cache.getItemCache(
  227. file,
  228. cache.mergeEtags(
  229. cache.getLazyHashedEtag(asset.source),
  230. sourceMapNamespace
  231. )
  232. );
  233. cacheItem.get((err, cacheEntry) => {
  234. if (err) {
  235. return callback(err);
  236. }
  237. /**
  238. * If presented in cache, reassigns assets. Cache assets already have source maps.
  239. */
  240. if (cacheEntry) {
  241. const { assets, assetsInfo } = cacheEntry;
  242. for (const cachedFile of Object.keys(assets)) {
  243. if (cachedFile === file) {
  244. compilation.updateAsset(
  245. cachedFile,
  246. assets[cachedFile],
  247. assetsInfo[cachedFile]
  248. );
  249. } else {
  250. compilation.emitAsset(
  251. cachedFile,
  252. assets[cachedFile],
  253. assetsInfo[cachedFile]
  254. );
  255. }
  256. /**
  257. * Add file to chunk, if not presented there
  258. */
  259. if (cachedFile !== file && chunk !== undefined) {
  260. chunk.auxiliaryFiles.add(cachedFile);
  261. }
  262. }
  263. reportProgress(
  264. (0.5 * ++fileIndex) / files.length,
  265. file,
  266. "restored cached SourceMap"
  267. );
  268. return callback();
  269. }
  270. reportProgress(
  271. (0.5 * fileIndex) / files.length,
  272. file,
  273. "generate SourceMap"
  274. );
  275. /** @type {SourceMapTask | undefined} */
  276. const task = getTaskForFile(
  277. file,
  278. asset.source,
  279. asset.info,
  280. {
  281. module: options.module,
  282. columns: options.columns
  283. },
  284. compilation,
  285. cacheItem
  286. );
  287. if (task) {
  288. const modules = task.modules;
  289. for (let idx = 0; idx < modules.length; idx++) {
  290. const module = modules[idx];
  291. if (
  292. typeof module === "string" &&
  293. /^(?:data|https?):/.test(module)
  294. ) {
  295. moduleToSourceNameMapping.set(module, module);
  296. continue;
  297. }
  298. if (!moduleToSourceNameMapping.get(module)) {
  299. moduleToSourceNameMapping.set(
  300. module,
  301. ModuleFilenameHelpers.createFilename(
  302. module,
  303. {
  304. moduleFilenameTemplate,
  305. namespace: sourceMapNamespace
  306. },
  307. {
  308. requestShortener,
  309. chunkGraph,
  310. hashFunction: compilation.outputOptions.hashFunction
  311. }
  312. )
  313. );
  314. }
  315. }
  316. tasks.push(task);
  317. }
  318. reportProgress(
  319. (0.5 * ++fileIndex) / files.length,
  320. file,
  321. "generated SourceMap"
  322. );
  323. callback();
  324. });
  325. },
  326. (err) => {
  327. if (err) {
  328. return callback(err);
  329. }
  330. reportProgress(0.5, "resolve sources");
  331. /** @type {Set<string>} */
  332. const usedNamesSet = new Set(moduleToSourceNameMapping.values());
  333. /** @type {Set<string>} */
  334. const conflictDetectionSet = new Set();
  335. /**
  336. * all modules in defined order (longest identifier first)
  337. * @type {(string | Module)[]}
  338. */
  339. const allModules = [...moduleToSourceNameMapping.keys()].sort(
  340. (a, b) => {
  341. const ai = typeof a === "string" ? a : a.identifier();
  342. const bi = typeof b === "string" ? b : b.identifier();
  343. return ai.length - bi.length;
  344. }
  345. );
  346. // find modules with conflicting source names
  347. for (let idx = 0; idx < allModules.length; idx++) {
  348. const module = allModules[idx];
  349. let sourceName =
  350. /** @type {string} */
  351. (moduleToSourceNameMapping.get(module));
  352. let hasName = conflictDetectionSet.has(sourceName);
  353. if (!hasName) {
  354. conflictDetectionSet.add(sourceName);
  355. continue;
  356. }
  357. // try the fallback name first
  358. sourceName = ModuleFilenameHelpers.createFilename(
  359. module,
  360. {
  361. moduleFilenameTemplate: fallbackModuleFilenameTemplate,
  362. namespace
  363. },
  364. {
  365. requestShortener,
  366. chunkGraph,
  367. hashFunction: compilation.outputOptions.hashFunction
  368. }
  369. );
  370. hasName = usedNamesSet.has(sourceName);
  371. if (!hasName) {
  372. moduleToSourceNameMapping.set(module, sourceName);
  373. usedNamesSet.add(sourceName);
  374. continue;
  375. }
  376. // otherwise just append stars until we have a valid name
  377. while (hasName) {
  378. sourceName += "*";
  379. hasName = usedNamesSet.has(sourceName);
  380. }
  381. moduleToSourceNameMapping.set(module, sourceName);
  382. usedNamesSet.add(sourceName);
  383. }
  384. let taskIndex = 0;
  385. asyncLib.each(
  386. tasks,
  387. (task, callback) => {
  388. /** @type {Record<string, Source>} */
  389. const assets = Object.create(null);
  390. /** @type {Record<string, AssetInfo | undefined>} */
  391. const assetsInfo = Object.create(null);
  392. const file = task.file;
  393. const chunk = fileToChunk.get(file);
  394. const sourceMap = task.sourceMap;
  395. const source = task.source;
  396. const modules = task.modules;
  397. reportProgress(
  398. 0.5 + (0.5 * taskIndex) / tasks.length,
  399. file,
  400. "attach SourceMap"
  401. );
  402. const moduleFilenames = modules.map((m) =>
  403. moduleToSourceNameMapping.get(m)
  404. );
  405. sourceMap.sources = /** @type {string[]} */ (moduleFilenames);
  406. if (options.ignoreList) {
  407. const ignoreList = sourceMap.sources.reduce(
  408. /** @type {(acc: number[], sourceName: string, idx: number) => number[]} */ (
  409. (acc, sourceName, idx) => {
  410. const rule = /** @type {Rules} */ (
  411. options.ignoreList
  412. );
  413. if (
  414. ModuleFilenameHelpers.matchPart(sourceName, rule)
  415. ) {
  416. acc.push(idx);
  417. }
  418. return acc;
  419. }
  420. ),
  421. []
  422. );
  423. if (ignoreList.length > 0) {
  424. sourceMap.ignoreList = ignoreList;
  425. }
  426. }
  427. if (options.noSources) {
  428. sourceMap.sourcesContent = undefined;
  429. }
  430. sourceMap.sourceRoot = options.sourceRoot || "";
  431. sourceMap.file = file;
  432. const usesContentHash =
  433. sourceMapFilename &&
  434. CONTENT_HASH_DETECT_REGEXP.test(sourceMapFilename);
  435. resetRegexpState(CONTENT_HASH_DETECT_REGEXP);
  436. // If SourceMap and asset uses contenthash, avoid a circular dependency by hiding hash in `file`
  437. if (usesContentHash && task.assetInfo.contenthash) {
  438. const contenthash = task.assetInfo.contenthash;
  439. const pattern = Array.isArray(contenthash)
  440. ? contenthash.map(quoteMeta).join("|")
  441. : quoteMeta(contenthash);
  442. sourceMap.file = sourceMap.file.replace(
  443. new RegExp(pattern, "g"),
  444. (m) => "x".repeat(m.length)
  445. );
  446. }
  447. /** @type {false | TemplatePath} */
  448. let currentSourceMappingURLComment = sourceMappingURLComment;
  449. const cssExtensionDetected =
  450. CSS_EXTENSION_DETECT_REGEXP.test(file);
  451. resetRegexpState(CSS_EXTENSION_DETECT_REGEXP);
  452. if (
  453. currentSourceMappingURLComment !== false &&
  454. typeof currentSourceMappingURLComment !== "function" &&
  455. cssExtensionDetected
  456. ) {
  457. currentSourceMappingURLComment =
  458. currentSourceMappingURLComment.replace(
  459. URL_FORMATTING_REGEXP,
  460. "\n/*$1*/"
  461. );
  462. }
  463. if (options.debugIds) {
  464. const debugId = generateDebugId(source, sourceMap.file);
  465. sourceMap.debugId = debugId;
  466. const debugIdComment = `\n//# debugId=${debugId}`;
  467. currentSourceMappingURLComment =
  468. currentSourceMappingURLComment
  469. ? `${debugIdComment}${currentSourceMappingURLComment}`
  470. : debugIdComment;
  471. }
  472. const sourceMapString = JSON.stringify(sourceMap);
  473. if (sourceMapFilename) {
  474. const filename = file;
  475. const sourceMapContentHash = usesContentHash
  476. ? createHash(compilation.outputOptions.hashFunction)
  477. .update(sourceMapString)
  478. .digest("hex")
  479. : undefined;
  480. const pathParams = {
  481. chunk,
  482. filename: options.fileContext
  483. ? relative(
  484. outputFs,
  485. `/${options.fileContext}`,
  486. `/${filename}`
  487. )
  488. : filename,
  489. contentHash: sourceMapContentHash
  490. };
  491. const { path: sourceMapFile, info: sourceMapInfo } =
  492. compilation.getPathWithInfo(
  493. sourceMapFilename,
  494. pathParams
  495. );
  496. const sourceMapUrl = options.publicPath
  497. ? options.publicPath + sourceMapFile
  498. : relative(
  499. outputFs,
  500. dirname(outputFs, `/${file}`),
  501. `/${sourceMapFile}`
  502. );
  503. /** @type {Source} */
  504. let asset = new RawSource(source);
  505. if (currentSourceMappingURLComment !== false) {
  506. // Add source map url to compilation asset, if currentSourceMappingURLComment is set
  507. asset = new ConcatSource(
  508. asset,
  509. compilation.getPath(currentSourceMappingURLComment, {
  510. url: sourceMapUrl,
  511. ...pathParams
  512. })
  513. );
  514. }
  515. const assetInfo = {
  516. related: { sourceMap: sourceMapFile }
  517. };
  518. assets[file] = asset;
  519. assetsInfo[file] = assetInfo;
  520. compilation.updateAsset(file, asset, assetInfo);
  521. // Add source map file to compilation assets and chunk files
  522. const sourceMapAsset = new RawSource(sourceMapString);
  523. const sourceMapAssetInfo = {
  524. ...sourceMapInfo,
  525. development: true
  526. };
  527. assets[sourceMapFile] = sourceMapAsset;
  528. assetsInfo[sourceMapFile] = sourceMapAssetInfo;
  529. compilation.emitAsset(
  530. sourceMapFile,
  531. sourceMapAsset,
  532. sourceMapAssetInfo
  533. );
  534. if (chunk !== undefined) {
  535. chunk.auxiliaryFiles.add(sourceMapFile);
  536. }
  537. } else {
  538. if (currentSourceMappingURLComment === false) {
  539. throw new Error(
  540. `${PLUGIN_NAME}: append can't be false when no filename is provided`
  541. );
  542. }
  543. if (typeof currentSourceMappingURLComment === "function") {
  544. throw new Error(
  545. `${PLUGIN_NAME}: append can't be a function when no filename is provided`
  546. );
  547. }
  548. /**
  549. * Add source map as data url to asset
  550. */
  551. const asset = new ConcatSource(
  552. new RawSource(source),
  553. currentSourceMappingURLComment
  554. .replace(MAP_URL_COMMENT_REGEXP, () => sourceMapString)
  555. .replace(
  556. URL_COMMENT_REGEXP,
  557. () =>
  558. `data:application/json;charset=utf-8;base64,${Buffer.from(
  559. sourceMapString,
  560. "utf8"
  561. ).toString("base64")}`
  562. )
  563. );
  564. assets[file] = asset;
  565. assetsInfo[file] = undefined;
  566. compilation.updateAsset(file, asset);
  567. }
  568. task.cacheItem.store({ assets, assetsInfo }, (err) => {
  569. reportProgress(
  570. 0.5 + (0.5 * ++taskIndex) / tasks.length,
  571. task.file,
  572. "attached SourceMap"
  573. );
  574. if (err) {
  575. return callback(err);
  576. }
  577. callback();
  578. });
  579. },
  580. (err) => {
  581. reportProgress(1);
  582. callback(err);
  583. }
  584. );
  585. }
  586. );
  587. }
  588. );
  589. });
  590. }
  591. }
  592. module.exports = SourceMapDevToolPlugin;