SourceMapDevToolPlugin.js 19 KB

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