| 
									
										
										
										
											2013-03-26 23:54:41 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 20:46:40 +08:00
										 |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2013-03-26 23:54:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-16 03:30:21 +08:00
										 |  |  | const asyncLib = require("neo-async"); | 
					
						
							| 
									
										
										
										
											2018-08-23 01:23:48 +08:00
										 |  |  | const validateOptions = require("schema-utils"); | 
					
						
							| 
									
										
										
										
											2018-03-22 19:05:58 +08:00
										 |  |  | const { ConcatSource, RawSource } = require("webpack-sources"); | 
					
						
							| 
									
										
										
										
											2017-04-06 20:46:40 +08:00
										 |  |  | const ModuleFilenameHelpers = require("./ModuleFilenameHelpers"); | 
					
						
							| 
									
										
										
										
											2018-11-27 07:06:02 +08:00
										 |  |  | const ProgressPlugin = require("./ProgressPlugin"); | 
					
						
							| 
									
										
										
										
											2017-04-06 20:46:40 +08:00
										 |  |  | const SourceMapDevToolModuleOptionsPlugin = require("./SourceMapDevToolModuleOptionsPlugin"); | 
					
						
							| 
									
										
										
										
											2019-11-12 20:55:13 +08:00
										 |  |  | const getLazyHashedEtag = require("./cache/getLazyHashedEtag"); | 
					
						
							| 
									
										
										
										
											2017-11-23 17:59:29 +08:00
										 |  |  | const createHash = require("./util/createHash"); | 
					
						
							| 
									
										
										
										
											2019-06-11 19:09:42 +08:00
										 |  |  | const { relative, dirname } = require("./util/fs"); | 
					
						
							| 
									
										
										
										
											2019-11-11 22:25:03 +08:00
										 |  |  | const { absolutify } = require("./util/identifier"); | 
					
						
							| 
									
										
										
										
											2017-04-06 20:46:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-11 20:05:55 +08:00
										 |  |  | const schema = require("../schemas/plugins/SourceMapDevToolPlugin.json"); | 
					
						
							| 
									
										
										
										
											2017-10-28 05:23:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-17 22:02:33 +08:00
										 |  |  | /** @typedef {import("source-map").RawSourceMap} SourceMap */ | 
					
						
							| 
									
										
										
										
											2019-06-14 16:45:56 +08:00
										 |  |  | /** @typedef {import("webpack-sources").Source} Source */ | 
					
						
							| 
									
										
										
										
											2018-09-20 16:13:55 +08:00
										 |  |  | /** @typedef {import("../declarations/plugins/SourceMapDevToolPlugin").SourceMapDevToolPluginOptions} SourceMapDevToolPluginOptions */ | 
					
						
							| 
									
										
										
										
											2019-11-12 20:55:13 +08:00
										 |  |  | /** @typedef {import("./Cache").Etag} Etag */ | 
					
						
							| 
									
										
										
										
											2018-08-23 01:23:48 +08:00
										 |  |  | /** @typedef {import("./Chunk")} Chunk */ | 
					
						
							| 
									
										
										
										
											2019-06-14 16:45:56 +08:00
										 |  |  | /** @typedef {import("./Compilation")} Compilation */ | 
					
						
							| 
									
										
										
										
											2018-08-23 01:23:48 +08:00
										 |  |  | /** @typedef {import("./Compiler")} Compiler */ | 
					
						
							| 
									
										
										
										
											2019-06-14 16:45:56 +08:00
										 |  |  | /** @typedef {import("./Module")} Module */ | 
					
						
							| 
									
										
										
										
											2019-10-31 03:47:17 +08:00
										 |  |  | /** @typedef {import("./util/Hash")} Hash */ | 
					
						
							| 
									
										
										
										
											2018-08-23 01:23:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-14 16:45:56 +08:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2019-06-27 00:40:56 +08:00
										 |  |  |  * @typedef {object} SourceMapTask | 
					
						
							| 
									
										
										
										
											2019-06-14 16:45:56 +08:00
										 |  |  |  * @property {Source} asset | 
					
						
							| 
									
										
										
										
											2019-07-17 22:02:33 +08:00
										 |  |  |  * @property {(string | Module)[]} modules | 
					
						
							| 
									
										
										
										
											2019-06-14 16:45:56 +08:00
										 |  |  |  * @property {string} source | 
					
						
							| 
									
										
										
										
											2019-06-27 00:40:56 +08:00
										 |  |  |  * @property {string} file | 
					
						
							|  |  |  |  * @property {SourceMap} sourceMap | 
					
						
							| 
									
										
										
										
											2019-10-31 03:47:17 +08:00
										 |  |  |  * @property {string} cacheIdent cache identifier | 
					
						
							| 
									
										
										
										
											2019-11-12 20:55:13 +08:00
										 |  |  |  * @property {Etag} cacheETag cache ETag | 
					
						
							| 
									
										
										
										
											2019-06-14 16:45:56 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2019-06-27 00:40:56 +08:00
										 |  |  |  * Creating {@link SourceMapTask} for given file | 
					
						
							| 
									
										
										
										
											2019-07-09 04:31:11 +08:00
										 |  |  |  * @param {string} file current compiled file | 
					
						
							| 
									
										
										
										
											2019-07-25 04:41:14 +08:00
										 |  |  |  * @param {Source} asset the asset | 
					
						
							| 
									
										
										
										
											2019-07-09 04:31:11 +08:00
										 |  |  |  * @param {SourceMapDevToolPluginOptions} options source map options | 
					
						
							|  |  |  |  * @param {Compilation} compilation compilation instance | 
					
						
							| 
									
										
										
										
											2019-10-31 03:47:17 +08:00
										 |  |  |  * @param {string} cacheIdent cache identifier | 
					
						
							| 
									
										
										
										
											2019-11-12 20:55:13 +08:00
										 |  |  |  * @param {Etag} cacheETag cache ETag | 
					
						
							| 
									
										
										
										
											2019-07-09 04:31:11 +08:00
										 |  |  |  * @returns {SourceMapTask | undefined} created task instance or `undefined` | 
					
						
							| 
									
										
										
										
											2019-06-14 16:45:56 +08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-10-31 03:47:17 +08:00
										 |  |  | const getTaskForFile = ( | 
					
						
							|  |  |  | 	file, | 
					
						
							|  |  |  | 	asset, | 
					
						
							|  |  |  | 	options, | 
					
						
							|  |  |  | 	compilation, | 
					
						
							|  |  |  | 	cacheIdent, | 
					
						
							|  |  |  | 	cacheETag | 
					
						
							|  |  |  | ) => { | 
					
						
							| 
									
										
										
										
											2019-06-14 16:45:56 +08:00
										 |  |  | 	let source; | 
					
						
							| 
									
										
										
										
											2019-07-17 22:02:33 +08:00
										 |  |  | 	/** @type {SourceMap} */ | 
					
						
							| 
									
										
										
										
											2019-06-14 16:45:56 +08:00
										 |  |  | 	let sourceMap; | 
					
						
							| 
									
										
										
										
											2019-06-27 00:40:56 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * Check if asset can build source map | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 	if (asset.sourceAndMap) { | 
					
						
							| 
									
										
										
										
											2017-07-17 13:15:42 +08:00
										 |  |  | 		const sourceAndMap = asset.sourceAndMap(options); | 
					
						
							| 
									
										
										
										
											2019-07-17 22:02:33 +08:00
										 |  |  | 		sourceMap = /** @type {SourceMap} */ (sourceAndMap.map); | 
					
						
							| 
									
										
										
										
											2017-07-17 13:15:42 +08:00
										 |  |  | 		source = sourceAndMap.source; | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2019-07-17 22:02:33 +08:00
										 |  |  | 		sourceMap = /** @type {SourceMap} */ (asset.map(options)); | 
					
						
							| 
									
										
										
										
											2017-07-17 13:15:42 +08:00
										 |  |  | 		source = asset.source(); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-06-14 16:45:56 +08:00
										 |  |  | 	if (!sourceMap || typeof source !== "string") return; | 
					
						
							| 
									
										
										
										
											2019-11-11 22:25:03 +08:00
										 |  |  | 	const context = compilation.options.context; | 
					
						
							|  |  |  | 	const root = compilation.compiler.root; | 
					
						
							| 
									
										
										
										
											2019-06-14 16:45:56 +08:00
										 |  |  | 	const modules = sourceMap.sources.map(source => { | 
					
						
							| 
									
										
										
										
											2019-11-11 22:25:03 +08:00
										 |  |  | 		if (!source.startsWith("webpack://")) return source; | 
					
						
							|  |  |  | 		source = absolutify(context, source.slice(10), root); | 
					
						
							| 
									
										
										
										
											2019-06-14 16:45:56 +08:00
										 |  |  | 		const module = compilation.findModule(source); | 
					
						
							|  |  |  | 		return module || source; | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return { | 
					
						
							|  |  |  | 		file, | 
					
						
							|  |  |  | 		asset, | 
					
						
							|  |  |  | 		source, | 
					
						
							|  |  |  | 		sourceMap, | 
					
						
							| 
									
										
										
										
											2019-10-31 03:47:17 +08:00
										 |  |  | 		modules, | 
					
						
							|  |  |  | 		cacheIdent, | 
					
						
							|  |  |  | 		cacheETag | 
					
						
							| 
									
										
										
										
											2019-06-14 16:45:56 +08:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2017-07-17 13:15:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 20:46:40 +08:00
										 |  |  | class SourceMapDevToolPlugin { | 
					
						
							| 
									
										
										
										
											2018-09-20 16:13:55 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2019-07-09 04:31:11 +08:00
										 |  |  | 	 * @param {SourceMapDevToolPluginOptions} [options] options object | 
					
						
							| 
									
										
										
										
											2019-06-27 00:40:56 +08:00
										 |  |  | 	 * @throws {Error} throws error, if got more than 1 arguments | 
					
						
							| 
									
										
										
										
											2018-09-20 16:13:55 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2018-07-06 17:19:30 +08:00
										 |  |  | 	constructor(options = {}) { | 
					
						
							| 
									
										
										
										
											2020-01-14 20:14:37 +08:00
										 |  |  | 		validateOptions(schema, options, { | 
					
						
							| 
									
										
										
										
											2019-08-07 21:55:03 +08:00
										 |  |  | 			name: "SourceMap DevTool Plugin", | 
					
						
							|  |  |  | 			baseDataPath: "options" | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-10-28 05:23:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-25 22:07:42 +08:00
										 |  |  | 		/** @type {string | false} */ | 
					
						
							| 
									
										
										
										
											2017-04-06 20:46:40 +08:00
										 |  |  | 		this.sourceMapFilename = options.filename; | 
					
						
							| 
									
										
										
										
											2018-09-20 16:13:55 +08:00
										 |  |  | 		/** @type {string | false} */ | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		this.sourceMappingURLComment = | 
					
						
							|  |  |  | 			options.append === false | 
					
						
							|  |  |  | 				? false | 
					
						
							| 
									
										
										
										
											2019-10-18 21:10:03 +08:00
										 |  |  | 				: options.append || "\n//# source" + "MappingURL=[url]"; | 
					
						
							| 
									
										
										
										
											2019-06-27 00:40:56 +08:00
										 |  |  | 		/** @type {string | Function} */ | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		this.moduleFilenameTemplate = | 
					
						
							|  |  |  | 			options.moduleFilenameTemplate || "webpack://[namespace]/[resourcePath]"; | 
					
						
							| 
									
										
										
										
											2019-06-27 00:40:56 +08:00
										 |  |  | 		/** @type {string | Function} */ | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		this.fallbackModuleFilenameTemplate = | 
					
						
							|  |  |  | 			options.fallbackModuleFilenameTemplate || | 
					
						
							|  |  |  | 			"webpack://[namespace]/[resourcePath]?[hash]"; | 
					
						
							| 
									
										
										
										
											2019-06-27 00:40:56 +08:00
										 |  |  | 		/** @type {string} */ | 
					
						
							| 
									
										
										
										
											2017-10-20 04:23:28 +08:00
										 |  |  | 		this.namespace = options.namespace || ""; | 
					
						
							| 
									
										
										
										
											2019-06-27 00:40:56 +08:00
										 |  |  | 		/** @type {SourceMapDevToolPluginOptions} */ | 
					
						
							| 
									
										
										
										
											2017-04-06 20:46:40 +08:00
										 |  |  | 		this.options = options; | 
					
						
							| 
									
										
										
										
											2015-02-19 08:11:29 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-04-06 20:46:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 01:23:48 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2019-06-27 00:40:56 +08:00
										 |  |  | 	 * Apply compiler | 
					
						
							| 
									
										
										
										
											2019-07-09 04:31:11 +08:00
										 |  |  | 	 * @param {Compiler} compiler compiler instance | 
					
						
							| 
									
										
										
										
											2018-08-23 01:23:48 +08:00
										 |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-04-06 20:46:40 +08:00
										 |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2019-06-11 19:09:42 +08:00
										 |  |  | 		const outputFs = compiler.outputFileSystem; | 
					
						
							| 
									
										
										
										
											2017-04-06 20:46:40 +08:00
										 |  |  | 		const sourceMapFilename = this.sourceMapFilename; | 
					
						
							|  |  |  | 		const sourceMappingURLComment = this.sourceMappingURLComment; | 
					
						
							|  |  |  | 		const moduleFilenameTemplate = this.moduleFilenameTemplate; | 
					
						
							| 
									
										
										
										
											2017-10-20 04:23:28 +08:00
										 |  |  | 		const namespace = this.namespace; | 
					
						
							| 
									
										
										
										
											2017-04-06 20:46:40 +08:00
										 |  |  | 		const fallbackModuleFilenameTemplate = this.fallbackModuleFilenameTemplate; | 
					
						
							| 
									
										
										
										
											2017-12-01 17:44:22 +08:00
										 |  |  | 		const requestShortener = compiler.requestShortener; | 
					
						
							| 
									
										
										
										
											2017-04-06 20:46:40 +08:00
										 |  |  | 		const options = this.options; | 
					
						
							| 
									
										
										
										
											2018-08-24 02:10:46 +08:00
										 |  |  | 		options.test = options.test || /\.(m?js|css)($|\?)/i; | 
					
						
							| 
									
										
										
										
											2017-07-17 13:15:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		const matchObject = ModuleFilenameHelpers.matchObject.bind( | 
					
						
							|  |  |  | 			undefined, | 
					
						
							|  |  |  | 			options | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2017-07-17 13:15:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		compiler.hooks.compilation.tap("SourceMapDevToolPlugin", compilation => { | 
					
						
							| 
									
										
										
										
											2017-04-06 20:46:40 +08:00
										 |  |  | 			new SourceMapDevToolModuleOptionsPlugin(options).apply(compilation); | 
					
						
							| 
									
										
										
										
											2017-07-17 13:15:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-12 20:24:58 +08:00
										 |  |  | 			compilation.hooks.finishAssets.tapAsync( | 
					
						
							| 
									
										
										
										
											2018-11-27 07:06:02 +08:00
										 |  |  | 				"SourceMapDevToolPlugin", | 
					
						
							| 
									
										
										
										
											2019-10-31 03:47:17 +08:00
										 |  |  | 				(assets, callback) => { | 
					
						
							| 
									
										
										
										
											2018-08-28 17:50:33 +08:00
										 |  |  | 					const chunkGraph = compilation.chunkGraph; | 
					
						
							| 
									
										
										
										
											2019-06-27 00:40:56 +08:00
										 |  |  | 					/** @type {Map<string | Module, string>} */ | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					const moduleToSourceNameMapping = new Map(); | 
					
						
							| 
									
										
										
										
											2019-06-27 00:40:56 +08:00
										 |  |  | 					/** | 
					
						
							|  |  |  | 					 * @type {Function} | 
					
						
							|  |  |  | 					 * @returns {void} | 
					
						
							|  |  |  | 					 */ | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					const reportProgress = | 
					
						
							| 
									
										
										
										
											2018-11-27 07:06:02 +08:00
										 |  |  | 						ProgressPlugin.getReporter(compilation.compiler) || (() => {}); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-09 18:18:06 +08:00
										 |  |  | 					/** @type {Map<string, Chunk>} */ | 
					
						
							|  |  |  | 					const fileToChunk = new Map(); | 
					
						
							|  |  |  | 					for (const chunk of compilation.chunks) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						for (const file of chunk.files) { | 
					
						
							| 
									
										
										
										
											2019-09-09 18:18:06 +08:00
										 |  |  | 							fileToChunk.set(file, chunk); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						for (const file of chunk.auxiliaryFiles) { | 
					
						
							|  |  |  | 							fileToChunk.set(file, chunk); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					/** @type {string[]} */ | 
					
						
							|  |  |  | 					const files = []; | 
					
						
							|  |  |  | 					for (const file of Object.keys(assets)) { | 
					
						
							|  |  |  | 						if (matchObject(file)) { | 
					
						
							|  |  |  | 							files.push(file); | 
					
						
							| 
									
										
										
										
											2017-07-07 18:51:55 +08:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2018-01-22 20:52:43 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2017-07-17 13:15:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					reportProgress(0.0); | 
					
						
							| 
									
										
										
										
											2019-07-17 22:02:33 +08:00
										 |  |  | 					/** @type {SourceMapTask[]} */ | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					const tasks = []; | 
					
						
							| 
									
										
										
										
											2019-10-16 03:30:21 +08:00
										 |  |  | 					let fileIndex = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					asyncLib.each( | 
					
						
							|  |  |  | 						files, | 
					
						
							| 
									
										
										
										
											2019-10-31 03:47:17 +08:00
										 |  |  | 						(file, callback) => { | 
					
						
							| 
									
										
										
										
											2019-10-16 03:30:21 +08:00
										 |  |  | 							const asset = compilation.getAsset(file).source; | 
					
						
							|  |  |  | 							const cacheIdent = `${compilation.compilerPath}/SourceMapDevToolPlugin/${file}`; | 
					
						
							| 
									
										
										
										
											2019-11-12 20:55:13 +08:00
										 |  |  | 							const cacheETag = getLazyHashedEtag(asset); | 
					
						
							| 
									
										
										
										
											2018-01-04 22:41:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 03:47:17 +08:00
										 |  |  | 							compilation.cache.get(cacheIdent, cacheETag, (err, assets) => { | 
					
						
							|  |  |  | 								if (err) { | 
					
						
							|  |  |  | 									return callback(err); | 
					
						
							|  |  |  | 								} | 
					
						
							|  |  |  | 								/** | 
					
						
							|  |  |  | 								 * If presented in cache, reassigns assets. Cache assets already have source maps. | 
					
						
							|  |  |  | 								 */ | 
					
						
							|  |  |  | 								if (assets) { | 
					
						
							|  |  |  | 									for (const cachedFile in assets) { | 
					
						
							|  |  |  | 										if (cachedFile === file) { | 
					
						
							|  |  |  | 											compilation.updateAsset(cachedFile, assets[cachedFile]); | 
					
						
							|  |  |  | 										} else { | 
					
						
							|  |  |  | 											compilation.emitAsset(cachedFile, assets[cachedFile], { | 
					
						
							|  |  |  | 												development: true | 
					
						
							|  |  |  | 											}); | 
					
						
							|  |  |  | 										} | 
					
						
							|  |  |  | 										/** | 
					
						
							|  |  |  | 										 * Add file to chunk, if not presented there | 
					
						
							|  |  |  | 										 */ | 
					
						
							|  |  |  | 										if (cachedFile !== file) { | 
					
						
							|  |  |  | 											const chunk = fileToChunk.get(file); | 
					
						
							|  |  |  | 											if (chunk !== undefined) | 
					
						
							|  |  |  | 												chunk.auxiliaryFiles.add(cachedFile); | 
					
						
							|  |  |  | 										} | 
					
						
							| 
									
										
										
										
											2019-10-16 03:30:21 +08:00
										 |  |  | 									} | 
					
						
							| 
									
										
										
										
											2018-01-04 22:41:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-16 03:30:21 +08:00
										 |  |  | 									reportProgress( | 
					
						
							| 
									
										
										
										
											2019-11-12 21:35:19 +08:00
										 |  |  | 										(0.5 * ++fileIndex) / files.length, | 
					
						
							| 
									
										
										
										
											2019-10-16 03:30:21 +08:00
										 |  |  | 										file, | 
					
						
							| 
									
										
										
										
											2019-11-12 21:35:19 +08:00
										 |  |  | 										"restored cached SourceMap" | 
					
						
							| 
									
										
										
										
											2019-10-16 03:30:21 +08:00
										 |  |  | 									); | 
					
						
							| 
									
										
										
										
											2018-01-04 22:41:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 03:47:17 +08:00
										 |  |  | 									return callback(); | 
					
						
							|  |  |  | 								} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-12 21:35:19 +08:00
										 |  |  | 								reportProgress( | 
					
						
							|  |  |  | 									(0.5 * fileIndex) / files.length, | 
					
						
							|  |  |  | 									file, | 
					
						
							|  |  |  | 									"generate SourceMap" | 
					
						
							|  |  |  | 								); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 03:47:17 +08:00
										 |  |  | 								/** @type {SourceMapTask | undefined} */ | 
					
						
							|  |  |  | 								const task = getTaskForFile( | 
					
						
							|  |  |  | 									file, | 
					
						
							|  |  |  | 									asset, | 
					
						
							|  |  |  | 									options, | 
					
						
							|  |  |  | 									compilation, | 
					
						
							|  |  |  | 									cacheIdent, | 
					
						
							|  |  |  | 									cacheETag | 
					
						
							|  |  |  | 								); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 								if (task) { | 
					
						
							|  |  |  | 									const modules = task.modules; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 03:47:17 +08:00
										 |  |  | 									for (let idx = 0; idx < modules.length; idx++) { | 
					
						
							|  |  |  | 										const module = modules[idx]; | 
					
						
							|  |  |  | 										if (!moduleToSourceNameMapping.get(module)) { | 
					
						
							|  |  |  | 											moduleToSourceNameMapping.set( | 
					
						
							|  |  |  | 												module, | 
					
						
							|  |  |  | 												ModuleFilenameHelpers.createFilename( | 
					
						
							| 
									
										
										
										
											2019-10-16 03:30:21 +08:00
										 |  |  | 													module, | 
					
						
							| 
									
										
										
										
											2019-10-31 03:47:17 +08:00
										 |  |  | 													{ | 
					
						
							|  |  |  | 														moduleFilenameTemplate: moduleFilenameTemplate, | 
					
						
							|  |  |  | 														namespace: namespace | 
					
						
							|  |  |  | 													}, | 
					
						
							|  |  |  | 													{ | 
					
						
							|  |  |  | 														requestShortener, | 
					
						
							|  |  |  | 														chunkGraph | 
					
						
							|  |  |  | 													} | 
					
						
							|  |  |  | 												) | 
					
						
							|  |  |  | 											); | 
					
						
							| 
									
										
										
										
											2019-10-16 03:30:21 +08:00
										 |  |  | 										} | 
					
						
							|  |  |  | 									} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 03:47:17 +08:00
										 |  |  | 									tasks.push(task); | 
					
						
							| 
									
										
										
										
											2019-10-16 03:30:21 +08:00
										 |  |  | 								} | 
					
						
							| 
									
										
										
										
											2019-10-31 03:47:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 								reportProgress( | 
					
						
							| 
									
										
										
										
											2019-11-12 21:35:19 +08:00
										 |  |  | 									(0.5 * ++fileIndex) / files.length, | 
					
						
							| 
									
										
										
										
											2019-10-31 03:47:17 +08:00
										 |  |  | 									file, | 
					
						
							| 
									
										
										
										
											2019-11-12 21:35:19 +08:00
										 |  |  | 									"generated SourceMap" | 
					
						
							| 
									
										
										
										
											2019-10-31 03:47:17 +08:00
										 |  |  | 								); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 								callback(); | 
					
						
							|  |  |  | 							}); | 
					
						
							| 
									
										
										
										
											2019-10-16 03:30:21 +08:00
										 |  |  | 						}, | 
					
						
							| 
									
										
										
										
											2019-10-31 03:47:17 +08:00
										 |  |  | 						err => { | 
					
						
							|  |  |  | 							if (err) { | 
					
						
							|  |  |  | 								return callback(err); | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-16 03:30:21 +08:00
										 |  |  | 							reportProgress(0.5, "resolve sources"); | 
					
						
							|  |  |  | 							/** @type {Set<string>} */ | 
					
						
							|  |  |  | 							const usedNamesSet = new Set(moduleToSourceNameMapping.values()); | 
					
						
							|  |  |  | 							/** @type {Set<string>} */ | 
					
						
							|  |  |  | 							const conflictDetectionSet = new Set(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-27 00:40:56 +08:00
										 |  |  | 							/** | 
					
						
							| 
									
										
										
										
											2019-10-16 03:30:21 +08:00
										 |  |  | 							 * all modules in defined order (longest identifier first) | 
					
						
							|  |  |  | 							 * @type {Array<string | Module>} | 
					
						
							| 
									
										
										
										
											2019-06-27 00:40:56 +08:00
										 |  |  | 							 */ | 
					
						
							| 
									
										
										
										
											2019-10-16 03:30:21 +08:00
										 |  |  | 							const allModules = Array.from( | 
					
						
							|  |  |  | 								moduleToSourceNameMapping.keys() | 
					
						
							|  |  |  | 							).sort((a, b) => { | 
					
						
							|  |  |  | 								const ai = typeof a === "string" ? a : a.identifier(); | 
					
						
							|  |  |  | 								const bi = typeof b === "string" ? b : b.identifier(); | 
					
						
							|  |  |  | 								return ai.length - bi.length; | 
					
						
							| 
									
										
										
										
											2019-09-11 17:13:46 +08:00
										 |  |  | 							}); | 
					
						
							| 
									
										
										
										
											2019-10-16 03:30:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 							// find modules with conflicting source names
 | 
					
						
							|  |  |  | 							for (let idx = 0; idx < allModules.length; idx++) { | 
					
						
							|  |  |  | 								const module = allModules[idx]; | 
					
						
							|  |  |  | 								let sourceName = moduleToSourceNameMapping.get(module); | 
					
						
							|  |  |  | 								let hasName = conflictDetectionSet.has(sourceName); | 
					
						
							|  |  |  | 								if (!hasName) { | 
					
						
							|  |  |  | 									conflictDetectionSet.add(sourceName); | 
					
						
							|  |  |  | 									continue; | 
					
						
							|  |  |  | 								} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 								// try the fallback name first
 | 
					
						
							|  |  |  | 								sourceName = ModuleFilenameHelpers.createFilename( | 
					
						
							|  |  |  | 									module, | 
					
						
							|  |  |  | 									{ | 
					
						
							|  |  |  | 										moduleFilenameTemplate: fallbackModuleFilenameTemplate, | 
					
						
							|  |  |  | 										namespace: namespace | 
					
						
							|  |  |  | 									}, | 
					
						
							|  |  |  | 									{ | 
					
						
							|  |  |  | 										requestShortener, | 
					
						
							|  |  |  | 										chunkGraph | 
					
						
							|  |  |  | 									} | 
					
						
							| 
									
										
										
										
											2018-09-20 16:13:55 +08:00
										 |  |  | 								); | 
					
						
							| 
									
										
										
										
											2019-10-16 03:30:21 +08:00
										 |  |  | 								hasName = usedNamesSet.has(sourceName); | 
					
						
							|  |  |  | 								if (!hasName) { | 
					
						
							|  |  |  | 									moduleToSourceNameMapping.set(module, sourceName); | 
					
						
							|  |  |  | 									usedNamesSet.add(sourceName); | 
					
						
							|  |  |  | 									continue; | 
					
						
							|  |  |  | 								} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-13 00:51:26 +08:00
										 |  |  | 								// otherwise just append stars until we have a valid name
 | 
					
						
							| 
									
										
										
										
											2019-10-16 03:30:21 +08:00
										 |  |  | 								while (hasName) { | 
					
						
							|  |  |  | 									sourceName += "*"; | 
					
						
							|  |  |  | 									hasName = usedNamesSet.has(sourceName); | 
					
						
							|  |  |  | 								} | 
					
						
							|  |  |  | 								moduleToSourceNameMapping.set(module, sourceName); | 
					
						
							|  |  |  | 								usedNamesSet.add(sourceName); | 
					
						
							| 
									
										
										
										
											2018-09-20 16:13:55 +08:00
										 |  |  | 							} | 
					
						
							| 
									
										
										
										
											2019-10-16 03:30:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 							let taskIndex = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 							asyncLib.each( | 
					
						
							|  |  |  | 								tasks, | 
					
						
							| 
									
										
										
										
											2019-10-31 03:47:17 +08:00
										 |  |  | 								(task, callback) => { | 
					
						
							| 
									
										
										
										
											2019-10-16 03:30:21 +08:00
										 |  |  | 									const assets = Object.create(null); | 
					
						
							|  |  |  | 									const file = task.file; | 
					
						
							|  |  |  | 									const chunk = fileToChunk.get(file); | 
					
						
							|  |  |  | 									const sourceMap = task.sourceMap; | 
					
						
							|  |  |  | 									const source = task.source; | 
					
						
							|  |  |  | 									const modules = task.modules; | 
					
						
							| 
									
										
										
										
											2019-11-12 21:35:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 									reportProgress( | 
					
						
							|  |  |  | 										0.5 + (0.5 * taskIndex) / tasks.length, | 
					
						
							|  |  |  | 										file, | 
					
						
							|  |  |  | 										"attach SourceMap" | 
					
						
							|  |  |  | 									); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-16 03:30:21 +08:00
										 |  |  | 									const moduleFilenames = modules.map(m => | 
					
						
							|  |  |  | 										moduleToSourceNameMapping.get(m) | 
					
						
							|  |  |  | 									); | 
					
						
							|  |  |  | 									sourceMap.sources = moduleFilenames; | 
					
						
							|  |  |  | 									if (options.noSources) { | 
					
						
							|  |  |  | 										sourceMap.sourcesContent = undefined; | 
					
						
							|  |  |  | 									} | 
					
						
							|  |  |  | 									sourceMap.sourceRoot = options.sourceRoot || ""; | 
					
						
							|  |  |  | 									sourceMap.file = file; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 03:47:17 +08:00
										 |  |  | 									/** @type {string | false} */ | 
					
						
							|  |  |  | 									let currentSourceMappingURLComment = sourceMappingURLComment; | 
					
						
							|  |  |  | 									if ( | 
					
						
							|  |  |  | 										currentSourceMappingURLComment !== false && | 
					
						
							|  |  |  | 										/\.css($|\?)/i.test(file) | 
					
						
							|  |  |  | 									) { | 
					
						
							|  |  |  | 										currentSourceMappingURLComment = currentSourceMappingURLComment.replace( | 
					
						
							|  |  |  | 											/^\n\/\/(.*)$/, | 
					
						
							|  |  |  | 											"\n/*$1*/" | 
					
						
							|  |  |  | 										); | 
					
						
							|  |  |  | 									} | 
					
						
							|  |  |  | 									const sourceMapString = JSON.stringify(sourceMap); | 
					
						
							|  |  |  | 									if (sourceMapFilename) { | 
					
						
							|  |  |  | 										let filename = file; | 
					
						
							|  |  |  | 										const pathParams = { | 
					
						
							|  |  |  | 											chunk, | 
					
						
							|  |  |  | 											filename: options.fileContext | 
					
						
							|  |  |  | 												? relative( | 
					
						
							|  |  |  | 														outputFs, | 
					
						
							|  |  |  | 														`/${options.fileContext}`, | 
					
						
							|  |  |  | 														`/${filename}` | 
					
						
							|  |  |  | 												  ) | 
					
						
							|  |  |  | 												: filename, | 
					
						
							|  |  |  | 											contentHash: /** @type {string} */ (createHash("md4") | 
					
						
							|  |  |  | 												.update(sourceMapString) | 
					
						
							|  |  |  | 												.digest("hex")) | 
					
						
							|  |  |  | 										}; | 
					
						
							|  |  |  | 										let sourceMapFile = compilation.getPath( | 
					
						
							|  |  |  | 											sourceMapFilename, | 
					
						
							|  |  |  | 											pathParams | 
					
						
							|  |  |  | 										); | 
					
						
							|  |  |  | 										const sourceMapUrl = options.publicPath | 
					
						
							|  |  |  | 											? options.publicPath + sourceMapFile | 
					
						
							|  |  |  | 											: relative( | 
					
						
							|  |  |  | 													outputFs, | 
					
						
							|  |  |  | 													dirname(outputFs, `/${file}`), | 
					
						
							|  |  |  | 													`/${sourceMapFile}` | 
					
						
							|  |  |  | 											  ); | 
					
						
							|  |  |  | 										/** | 
					
						
							|  |  |  | 										 * Add source map url to compilation asset, if {@link currentSourceMappingURLComment} presented | 
					
						
							|  |  |  | 										 */ | 
					
						
							|  |  |  | 										if (currentSourceMappingURLComment !== false) { | 
					
						
							|  |  |  | 											const asset = new ConcatSource( | 
					
						
							|  |  |  | 												new RawSource(source), | 
					
						
							|  |  |  | 												compilation.getPath( | 
					
						
							|  |  |  | 													currentSourceMappingURLComment, | 
					
						
							|  |  |  | 													Object.assign({ url: sourceMapUrl }, pathParams) | 
					
						
							|  |  |  | 												) | 
					
						
							|  |  |  | 											); | 
					
						
							|  |  |  | 											assets[file] = asset; | 
					
						
							|  |  |  | 											compilation.updateAsset(file, asset); | 
					
						
							|  |  |  | 										} | 
					
						
							|  |  |  | 										/** | 
					
						
							|  |  |  | 										 * Add source map file to compilation assets and chunk files | 
					
						
							|  |  |  | 										 */ | 
					
						
							|  |  |  | 										const asset = new RawSource(sourceMapString); | 
					
						
							|  |  |  | 										assets[sourceMapFile] = asset; | 
					
						
							|  |  |  | 										compilation.emitAsset(sourceMapFile, asset, { | 
					
						
							|  |  |  | 											development: true | 
					
						
							|  |  |  | 										}); | 
					
						
							|  |  |  | 										if (chunk !== undefined) | 
					
						
							|  |  |  | 											chunk.auxiliaryFiles.add(sourceMapFile); | 
					
						
							|  |  |  | 									} else { | 
					
						
							|  |  |  | 										if (currentSourceMappingURLComment === false) { | 
					
						
							|  |  |  | 											throw new Error( | 
					
						
							|  |  |  | 												"SourceMapDevToolPlugin: append can't be false when no filename is provided" | 
					
						
							|  |  |  | 											); | 
					
						
							|  |  |  | 										} | 
					
						
							|  |  |  | 										/** | 
					
						
							|  |  |  | 										 * Add source map as data url to asset | 
					
						
							|  |  |  | 										 */ | 
					
						
							|  |  |  | 										const asset = new ConcatSource( | 
					
						
							|  |  |  | 											new RawSource(source), | 
					
						
							|  |  |  | 											currentSourceMappingURLComment | 
					
						
							|  |  |  | 												.replace(/\[map\]/g, () => sourceMapString) | 
					
						
							|  |  |  | 												.replace( | 
					
						
							|  |  |  | 													/\[url\]/g, | 
					
						
							|  |  |  | 													() => | 
					
						
							|  |  |  | 														`data:application/json;charset=utf-8;base64,${Buffer.from( | 
					
						
							|  |  |  | 															sourceMapString, | 
					
						
							|  |  |  | 															"utf-8" | 
					
						
							|  |  |  | 														).toString("base64")}`
 | 
					
						
							|  |  |  | 												) | 
					
						
							|  |  |  | 										); | 
					
						
							|  |  |  | 										assets[file] = asset; | 
					
						
							|  |  |  | 										compilation.updateAsset(file, asset); | 
					
						
							|  |  |  | 									} | 
					
						
							| 
									
										
										
										
											2019-10-16 03:30:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 									compilation.cache.store( | 
					
						
							| 
									
										
										
										
											2019-10-31 03:47:17 +08:00
										 |  |  | 										task.cacheIdent, | 
					
						
							|  |  |  | 										task.cacheETag, | 
					
						
							| 
									
										
										
										
											2019-10-16 03:30:21 +08:00
										 |  |  | 										assets, | 
					
						
							| 
									
										
										
										
											2019-10-31 03:47:17 +08:00
										 |  |  | 										err => { | 
					
						
							|  |  |  | 											reportProgress( | 
					
						
							| 
									
										
										
										
											2019-11-12 21:35:19 +08:00
										 |  |  | 												0.5 + (0.5 * ++taskIndex) / tasks.length, | 
					
						
							| 
									
										
										
										
											2019-10-31 03:47:17 +08:00
										 |  |  | 												task.file, | 
					
						
							| 
									
										
										
										
											2019-11-12 21:35:19 +08:00
										 |  |  | 												"attached SourceMap" | 
					
						
							| 
									
										
										
										
											2019-10-31 03:47:17 +08:00
										 |  |  | 											); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 											if (err) { | 
					
						
							|  |  |  | 												return callback(err); | 
					
						
							| 
									
										
										
										
											2019-10-16 03:30:21 +08:00
										 |  |  | 											} | 
					
						
							| 
									
										
										
										
											2019-10-31 03:47:17 +08:00
										 |  |  | 											callback(); | 
					
						
							| 
									
										
										
										
											2019-10-16 03:30:21 +08:00
										 |  |  | 										} | 
					
						
							|  |  |  | 									); | 
					
						
							|  |  |  | 								}, | 
					
						
							| 
									
										
										
										
											2019-10-31 03:47:17 +08:00
										 |  |  | 								err => { | 
					
						
							|  |  |  | 									reportProgress(1.0); | 
					
						
							|  |  |  | 									callback(err); | 
					
						
							|  |  |  | 								} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 							); | 
					
						
							| 
									
										
										
										
											2017-04-06 20:46:40 +08:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2019-10-16 03:30:21 +08:00
										 |  |  | 					); | 
					
						
							| 
									
										
										
										
											2018-11-27 07:06:02 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			); | 
					
						
							| 
									
										
										
										
											2013-03-26 23:54:41 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-04-06 20:46:40 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-06-17 22:35:31 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-04-06 20:46:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | module.exports = SourceMapDevToolPlugin; |