You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

608 lines
18 KiB

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