提交学习笔记专用
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.

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