SourceMapDevToolPlugin.js 18 KB

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