SourceMapDevToolPlugin.js 18 KB

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