SourceMapDevToolPlugin.js 17 KB

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