| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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"); | 
					
						
							| 
									
										
										
										
											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"); | 
					
						
							| 
									
										
										
										
											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 */ | 
					
						
							| 
									
										
										
										
											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 */ | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  |  * @property {Chunk} chunk | 
					
						
							| 
									
										
										
										
											2019-06-14 16:45:56 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-17 22:02:33 +08:00
										 |  |  | /** @type {WeakMap<Source, { file: string, assets: Record<string, Source>}>} */ | 
					
						
							| 
									
										
										
										
											2018-05-19 20:02:31 +08:00
										 |  |  | const assetsCache = new WeakMap(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 {Chunk} chunk related chunk | 
					
						
							|  |  |  |  * @param {SourceMapDevToolPluginOptions} options source map options | 
					
						
							|  |  |  |  * @param {Compilation} compilation compilation instance | 
					
						
							|  |  |  |  * @returns {SourceMapTask | undefined} created task instance or `undefined` | 
					
						
							| 
									
										
										
										
											2019-06-14 16:45:56 +08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-07-25 04:41:14 +08:00
										 |  |  | const getTaskForFile = (file, asset, chunk, options, compilation) => { | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  | 	const modules = sourceMap.sources.map(source => { | 
					
						
							|  |  |  | 		const module = compilation.findModule(source); | 
					
						
							|  |  |  | 		return module || source; | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return { | 
					
						
							|  |  |  | 		chunk, | 
					
						
							|  |  |  | 		file, | 
					
						
							|  |  |  | 		asset, | 
					
						
							|  |  |  | 		source, | 
					
						
							|  |  |  | 		sourceMap, | 
					
						
							|  |  |  | 		modules | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											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 = {}) { | 
					
						
							| 
									
										
										
										
											2019-08-07 21:55:03 +08:00
										 |  |  | 		validateOptions(schema, options || {}, { | 
					
						
							|  |  |  | 			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 | 
					
						
							|  |  |  | 				: options.append || "\n//# sourceMappingURL=[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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			compilation.hooks.afterOptimizeChunkAssets.tap( | 
					
						
							| 
									
										
										
										
											2018-11-27 07:06:02 +08:00
										 |  |  | 				"SourceMapDevToolPlugin", | 
					
						
							|  |  |  | 				chunks => { | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							|  |  |  | 					const files = []; | 
					
						
							|  |  |  | 					for (const chunk of chunks) { | 
					
						
							|  |  |  | 						for (const file of chunk.files) { | 
					
						
							|  |  |  | 							if (matchObject(file)) { | 
					
						
							|  |  |  | 								files.push({ | 
					
						
							|  |  |  | 									file, | 
					
						
							|  |  |  | 									chunk | 
					
						
							|  |  |  | 								}); | 
					
						
							|  |  |  | 							} | 
					
						
							| 
									
										
										
										
											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 = []; | 
					
						
							|  |  |  | 					files.forEach(({ file, chunk }, idx) => { | 
					
						
							| 
									
										
										
										
											2019-07-25 04:41:14 +08:00
										 |  |  | 						const asset = compilation.assets[file]; | 
					
						
							|  |  |  | 						const cache = assetsCache.get(asset); | 
					
						
							|  |  |  | 						/** | 
					
						
							|  |  |  | 						 * If presented in cache, reassigns assets. Cache assets already have source maps. | 
					
						
							|  |  |  | 						 */ | 
					
						
							|  |  |  | 						if (cache && cache.file === file) { | 
					
						
							|  |  |  | 							for (const cachedFile in cache.assets) { | 
					
						
							|  |  |  | 								compilation.assets[cachedFile] = cache.assets[cachedFile]; | 
					
						
							|  |  |  | 								/** | 
					
						
							|  |  |  | 								 * Add file to chunk, if not presented there | 
					
						
							|  |  |  | 								 */ | 
					
						
							|  |  |  | 								if (cachedFile !== file) chunk.files.add(cachedFile); | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 							return; | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						reportProgress( | 
					
						
							| 
									
										
										
										
											2018-05-29 22:14:16 +08:00
										 |  |  | 							(0.5 * idx) / files.length, | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 							file, | 
					
						
							|  |  |  | 							"generate SourceMap" | 
					
						
							|  |  |  | 						); | 
					
						
							| 
									
										
										
										
											2019-06-27 00:40:56 +08:00
										 |  |  | 						/** @type {SourceMapTask | undefined} */ | 
					
						
							| 
									
										
										
										
											2019-07-25 04:41:14 +08:00
										 |  |  | 						const task = getTaskForFile( | 
					
						
							|  |  |  | 							file, | 
					
						
							|  |  |  | 							asset, | 
					
						
							|  |  |  | 							chunk, | 
					
						
							|  |  |  | 							options, | 
					
						
							|  |  |  | 							compilation | 
					
						
							|  |  |  | 						); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 						if (task) { | 
					
						
							| 
									
										
										
										
											2019-06-14 16:45:56 +08:00
										 |  |  | 							const modules = task.modules; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 							for (let idx = 0; idx < modules.length; idx++) { | 
					
						
							|  |  |  | 								const module = modules[idx]; | 
					
						
							|  |  |  | 								if (!moduleToSourceNameMapping.get(module)) { | 
					
						
							|  |  |  | 									moduleToSourceNameMapping.set( | 
					
						
							|  |  |  | 										module, | 
					
						
							|  |  |  | 										ModuleFilenameHelpers.createFilename( | 
					
						
							|  |  |  | 											module, | 
					
						
							|  |  |  | 											{ | 
					
						
							|  |  |  | 												moduleFilenameTemplate: moduleFilenameTemplate, | 
					
						
							|  |  |  | 												namespace: namespace | 
					
						
							|  |  |  | 											}, | 
					
						
							| 
									
										
										
										
											2018-08-28 17:50:33 +08:00
										 |  |  | 											{ | 
					
						
							|  |  |  | 												requestShortener, | 
					
						
							|  |  |  | 												chunkGraph | 
					
						
							|  |  |  | 											} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 										) | 
					
						
							|  |  |  | 									); | 
					
						
							|  |  |  | 								} | 
					
						
							| 
									
										
										
										
											2018-01-04 22:41:26 +08:00
										 |  |  | 							} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 							tasks.push(task); | 
					
						
							| 
									
										
										
										
											2018-01-04 22:41:26 +08:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2018-01-04 22:41:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					reportProgress(0.5, "resolve sources"); | 
					
						
							| 
									
										
										
										
											2019-06-27 00:40:56 +08:00
										 |  |  | 					/** @type {Set<string>} */ | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					const usedNamesSet = new Set(moduleToSourceNameMapping.values()); | 
					
						
							| 
									
										
										
										
											2019-06-27 00:40:56 +08:00
										 |  |  | 					/** @type {Set<string>} */ | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					const conflictDetectionSet = new Set(); | 
					
						
							| 
									
										
										
										
											2018-01-04 22:41:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-27 00:40:56 +08:00
										 |  |  | 					/** | 
					
						
							|  |  |  | 					 * all modules in defined order (longest identifier first) | 
					
						
							|  |  |  | 					 * @type {Array<string | Module>} | 
					
						
							|  |  |  | 					 */ | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +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; | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					); | 
					
						
							| 
									
										
										
										
											2018-01-04 22:41:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +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 | 
					
						
							|  |  |  | 							}, | 
					
						
							| 
									
										
										
										
											2018-08-28 17:50:33 +08:00
										 |  |  | 							{ | 
					
						
							|  |  |  | 								requestShortener, | 
					
						
							|  |  |  | 								chunkGraph | 
					
						
							|  |  |  | 							} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						); | 
					
						
							|  |  |  | 						hasName = usedNamesSet.has(sourceName); | 
					
						
							|  |  |  | 						if (!hasName) { | 
					
						
							|  |  |  | 							moduleToSourceNameMapping.set(module, sourceName); | 
					
						
							|  |  |  | 							usedNamesSet.add(sourceName); | 
					
						
							|  |  |  | 							continue; | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2017-07-07 18:51:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						// elsewise just append stars until we have a valid name
 | 
					
						
							|  |  |  | 						while (hasName) { | 
					
						
							|  |  |  | 							sourceName += "*"; | 
					
						
							|  |  |  | 							hasName = usedNamesSet.has(sourceName); | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2017-07-07 18:51:55 +08:00
										 |  |  | 						moduleToSourceNameMapping.set(module, sourceName); | 
					
						
							|  |  |  | 						usedNamesSet.add(sourceName); | 
					
						
							| 
									
										
										
										
											2017-07-08 18:25:52 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					tasks.forEach((task, index) => { | 
					
						
							|  |  |  | 						reportProgress( | 
					
						
							| 
									
										
										
										
											2018-05-29 22:14:16 +08:00
										 |  |  | 							0.5 + (0.5 * index) / tasks.length, | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 							task.file, | 
					
						
							|  |  |  | 							"attach SourceMap" | 
					
						
							|  |  |  | 						); | 
					
						
							| 
									
										
										
										
											2018-05-19 20:02:31 +08:00
										 |  |  | 						const assets = Object.create(null); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						const chunk = task.chunk; | 
					
						
							|  |  |  | 						const file = task.file; | 
					
						
							|  |  |  | 						const asset = task.asset; | 
					
						
							|  |  |  | 						const sourceMap = task.sourceMap; | 
					
						
							|  |  |  | 						const source = task.source; | 
					
						
							|  |  |  | 						const modules = task.modules; | 
					
						
							|  |  |  | 						const moduleFilenames = modules.map(m => | 
					
						
							|  |  |  | 							moduleToSourceNameMapping.get(m) | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 						sourceMap.sources = moduleFilenames; | 
					
						
							|  |  |  | 						if (options.noSources) { | 
					
						
							|  |  |  | 							sourceMap.sourcesContent = undefined; | 
					
						
							| 
									
										
										
										
											2017-04-06 20:46:40 +08:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						sourceMap.sourceRoot = options.sourceRoot || ""; | 
					
						
							|  |  |  | 						sourceMap.file = file; | 
					
						
							| 
									
										
										
										
											2018-05-19 20:02:31 +08:00
										 |  |  | 						assetsCache.set(asset, { file, assets }); | 
					
						
							| 
									
										
										
										
											2018-09-20 16:13:55 +08:00
										 |  |  | 						/** @type {string | false} */ | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						let currentSourceMappingURLComment = sourceMappingURLComment; | 
					
						
							|  |  |  | 						if ( | 
					
						
							|  |  |  | 							currentSourceMappingURLComment !== false && | 
					
						
							|  |  |  | 							/\.css($|\?)/i.test(file) | 
					
						
							|  |  |  | 						) { | 
					
						
							|  |  |  | 							currentSourceMappingURLComment = currentSourceMappingURLComment.replace( | 
					
						
							|  |  |  | 								/^\n\/\/(.*)$/, | 
					
						
							|  |  |  | 								"\n/*$1*/" | 
					
						
							|  |  |  | 							); | 
					
						
							| 
									
										
										
										
											2017-06-03 00:05:25 +08:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						const sourceMapString = JSON.stringify(sourceMap); | 
					
						
							|  |  |  | 						if (sourceMapFilename) { | 
					
						
							|  |  |  | 							let filename = file; | 
					
						
							| 
									
										
										
										
											2019-05-16 21:20:12 +08:00
										 |  |  | 							const pathParams = { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 								chunk, | 
					
						
							|  |  |  | 								filename: options.fileContext | 
					
						
							| 
									
										
										
										
											2019-06-11 19:09:42 +08:00
										 |  |  | 									? relative( | 
					
						
							|  |  |  | 											outputFs, | 
					
						
							|  |  |  | 											`/${options.fileContext}`, | 
					
						
							|  |  |  | 											`/${filename}` | 
					
						
							|  |  |  | 									  ) | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 									: filename, | 
					
						
							| 
									
										
										
										
											2019-07-17 22:02:33 +08:00
										 |  |  | 								contentHash: /** @type {string} */ (createHash("md4") | 
					
						
							| 
									
										
										
										
											2018-03-23 02:52:11 +08:00
										 |  |  | 									.update(sourceMapString) | 
					
						
							| 
									
										
										
										
											2019-07-17 22:02:33 +08:00
										 |  |  | 									.digest("hex")) | 
					
						
							| 
									
										
										
										
											2019-05-16 21:20:12 +08:00
										 |  |  | 							}; | 
					
						
							|  |  |  | 							let sourceMapFile = compilation.getPath( | 
					
						
							|  |  |  | 								sourceMapFilename, | 
					
						
							|  |  |  | 								pathParams | 
					
						
							|  |  |  | 							); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 							const sourceMapUrl = options.publicPath | 
					
						
							| 
									
										
										
										
											2019-06-11 19:09:42 +08:00
										 |  |  | 								? options.publicPath + sourceMapFile | 
					
						
							|  |  |  | 								: relative( | 
					
						
							|  |  |  | 										outputFs, | 
					
						
							|  |  |  | 										dirname(outputFs, `/${file}`), | 
					
						
							|  |  |  | 										`/${sourceMapFile}` | 
					
						
							|  |  |  | 								  ); | 
					
						
							| 
									
										
										
										
											2019-06-27 00:40:56 +08:00
										 |  |  | 							/** | 
					
						
							|  |  |  | 							 * Add source map url to compilation asset, if {@link currentSourceMappingURLComment} presented | 
					
						
							|  |  |  | 							 */ | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 							if (currentSourceMappingURLComment !== false) { | 
					
						
							| 
									
										
										
										
											2018-05-19 20:02:31 +08:00
										 |  |  | 								assets[file] = compilation.assets[file] = new ConcatSource( | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 									new RawSource(source), | 
					
						
							| 
									
										
										
										
											2019-05-16 21:20:12 +08:00
										 |  |  | 									compilation.getPath( | 
					
						
							|  |  |  | 										currentSourceMappingURLComment, | 
					
						
							|  |  |  | 										Object.assign({ url: sourceMapUrl }, pathParams) | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 									) | 
					
						
							|  |  |  | 								); | 
					
						
							|  |  |  | 							} | 
					
						
							| 
									
										
										
										
											2019-06-27 00:40:56 +08:00
										 |  |  | 							/** | 
					
						
							|  |  |  | 							 * Add source map file to compilation assets and chunk files | 
					
						
							|  |  |  | 							 */ | 
					
						
							| 
									
										
										
										
											2018-05-19 20:02:31 +08:00
										 |  |  | 							assets[sourceMapFile] = compilation.assets[ | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 								sourceMapFile | 
					
						
							|  |  |  | 							] = new RawSource(sourceMapString); | 
					
						
							| 
									
										
										
										
											2019-06-14 16:45:56 +08:00
										 |  |  | 							chunk.files.add(sourceMapFile); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						} else { | 
					
						
							| 
									
										
										
										
											2018-09-20 16:13:55 +08:00
										 |  |  | 							if (currentSourceMappingURLComment === false) { | 
					
						
							|  |  |  | 								throw new Error( | 
					
						
							|  |  |  | 									"SourceMapDevToolPlugin: append can't be false when no filename is provided" | 
					
						
							|  |  |  | 								); | 
					
						
							|  |  |  | 							} | 
					
						
							| 
									
										
										
										
											2019-06-27 00:40:56 +08:00
										 |  |  | 							/** | 
					
						
							|  |  |  | 							 * Add source map as data url to asset | 
					
						
							|  |  |  | 							 */ | 
					
						
							| 
									
										
										
										
											2018-05-19 20:02:31 +08:00
										 |  |  | 							assets[file] = compilation.assets[file] = new ConcatSource( | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 								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")}`
 | 
					
						
							|  |  |  | 									) | 
					
						
							|  |  |  | 							); | 
					
						
							| 
									
										
										
										
											2017-04-06 20:46:40 +08:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					}); | 
					
						
							|  |  |  | 					reportProgress(1.0); | 
					
						
							| 
									
										
										
										
											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; |