| 
									
										
										
										
											2014-02-13 18:40:16 +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-01-13 11:32:24 +08:00
										 |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2014-02-13 18:40:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-16 23:19:39 +08:00
										 |  |  | const { ConcatSource, RawSource } = require("webpack-sources"); | 
					
						
							| 
									
										
										
										
											2019-10-02 17:19:44 +08:00
										 |  |  | const ModuleFilenameHelpers = require("./ModuleFilenameHelpers"); | 
					
						
							|  |  |  | const NormalModule = require("./NormalModule"); | 
					
						
							| 
									
										
										
										
											2017-01-13 11:32:24 +08:00
										 |  |  | const SourceMapDevToolModuleOptionsPlugin = require("./SourceMapDevToolModuleOptionsPlugin"); | 
					
						
							| 
									
										
										
										
											2019-10-11 21:46:57 +08:00
										 |  |  | const JavascriptModulesPlugin = require("./javascript/JavascriptModulesPlugin"); | 
					
						
							| 
									
										
										
										
											2019-11-11 22:25:03 +08:00
										 |  |  | const ConcatenatedModule = require("./optimize/ConcatenatedModule"); | 
					
						
							|  |  |  | const { absolutify } = require("./util/identifier"); | 
					
						
							| 
									
										
										
										
											2017-01-13 11:32:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-02 17:19:44 +08:00
										 |  |  | /** @typedef {import("webpack-sources").Source} Source */ | 
					
						
							| 
									
										
										
										
											2020-08-03 02:09:36 +08:00
										 |  |  | /** @typedef {import("../declarations/WebpackOptions").DevTool} DevToolOptions */ | 
					
						
							|  |  |  | /** @typedef {import("../declarations/plugins/SourceMapDevToolPlugin").SourceMapDevToolPluginOptions} SourceMapDevToolPluginOptions */ | 
					
						
							| 
									
										
										
										
											2018-07-06 17:19:30 +08:00
										 |  |  | /** @typedef {import("./Compiler")} Compiler */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-02 17:19:44 +08:00
										 |  |  | /** @type {WeakMap<Source, Source>} */ | 
					
						
							|  |  |  | const cache = new WeakMap(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-18 21:47:24 +08:00
										 |  |  | const devtoolWarning = new RawSource(`/*
 | 
					
						
							|  |  |  |  * ATTENTION: An "eval-source-map" devtool has been used. | 
					
						
							|  |  |  |  * This devtool is not neither made for production nor for readable output files. | 
					
						
							|  |  |  |  * It uses "eval()" calls to create a separate source file with attached SourceMaps in the browser devtools. | 
					
						
							|  |  |  |  * If you are trying to read the output file, select a different devtool (https://webpack.js.org/configuration/devtool/)
 | 
					
						
							|  |  |  |  * or disable the default devtool with "devtool: false". | 
					
						
							|  |  |  |  * If you are looking for production-ready output files, see mode: "production" (https://webpack.js.org/configuration/mode/).
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | `);
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-13 11:32:24 +08:00
										 |  |  | class EvalSourceMapDevToolPlugin { | 
					
						
							| 
									
										
										
										
											2018-07-06 17:19:30 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2020-08-03 02:09:36 +08:00
										 |  |  | 	 * @param {SourceMapDevToolPluginOptions|string} inputOptions Options object | 
					
						
							| 
									
										
										
										
											2018-07-06 17:19:30 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2020-08-03 02:09:36 +08:00
										 |  |  | 	constructor(inputOptions) { | 
					
						
							|  |  |  | 		/** @type {SourceMapDevToolPluginOptions} */ | 
					
						
							|  |  |  | 		let options; | 
					
						
							|  |  |  | 		if (typeof inputOptions === "string") { | 
					
						
							| 
									
										
										
										
											2017-01-13 11:32:24 +08:00
										 |  |  | 			options = { | 
					
						
							| 
									
										
										
										
											2020-08-03 02:09:36 +08:00
										 |  |  | 				append: inputOptions | 
					
						
							| 
									
										
										
										
											2017-01-13 11:32:24 +08:00
										 |  |  | 			}; | 
					
						
							| 
									
										
										
										
											2020-08-03 02:09:36 +08:00
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			options = inputOptions; | 
					
						
							| 
									
										
										
										
											2017-01-13 11:32:24 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-10-02 17:19:44 +08:00
										 |  |  | 		this.sourceMapComment = | 
					
						
							|  |  |  | 			options.append || "//# sourceURL=[module]\n//# sourceMappingURL=[url]"; | 
					
						
							|  |  |  | 		this.moduleFilenameTemplate = | 
					
						
							|  |  |  | 			options.moduleFilenameTemplate || | 
					
						
							|  |  |  | 			"webpack://[namespace]/[resource-path]?[hash]"; | 
					
						
							|  |  |  | 		this.namespace = options.namespace || ""; | 
					
						
							| 
									
										
										
										
											2017-01-13 11:32:24 +08:00
										 |  |  | 		this.options = options; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-06 17:19:30 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2020-04-23 16:48:36 +08:00
										 |  |  | 	 * Apply the plugin | 
					
						
							| 
									
										
										
										
											2018-11-03 04:05:46 +08:00
										 |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							| 
									
										
										
										
											2018-07-06 17:19:30 +08:00
										 |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-01-13 11:32:24 +08:00
										 |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2017-02-05 07:23:50 +08:00
										 |  |  | 		const options = this.options; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		compiler.hooks.compilation.tap( | 
					
						
							|  |  |  | 			"EvalSourceMapDevToolPlugin", | 
					
						
							|  |  |  | 			compilation => { | 
					
						
							| 
									
										
										
										
											2019-10-02 17:19:44 +08:00
										 |  |  | 				const hooks = JavascriptModulesPlugin.getCompilationHooks(compilation); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				new SourceMapDevToolModuleOptionsPlugin(options).apply(compilation); | 
					
						
							| 
									
										
										
										
											2019-10-02 17:19:44 +08:00
										 |  |  | 				const matchModule = ModuleFilenameHelpers.matchObject.bind( | 
					
						
							|  |  |  | 					ModuleFilenameHelpers, | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					options | 
					
						
							| 
									
										
										
										
											2019-10-02 17:19:44 +08:00
										 |  |  | 				); | 
					
						
							|  |  |  | 				hooks.renderModuleContent.tap( | 
					
						
							|  |  |  | 					"EvalSourceMapDevToolPlugin", | 
					
						
							|  |  |  | 					(source, m, { runtimeTemplate, chunkGraph }) => { | 
					
						
							|  |  |  | 						const cachedSource = cache.get(source); | 
					
						
							|  |  |  | 						if (cachedSource !== undefined) { | 
					
						
							|  |  |  | 							return cachedSource; | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-11 22:25:03 +08:00
										 |  |  | 						if (m instanceof NormalModule) { | 
					
						
							|  |  |  | 							const module = /** @type {NormalModule} */ (m); | 
					
						
							|  |  |  | 							if (!matchModule(module.resource)) { | 
					
						
							|  |  |  | 								return source; | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						} else if (m instanceof ConcatenatedModule) { | 
					
						
							|  |  |  | 							const concatModule = /** @type {ConcatenatedModule} */ (m); | 
					
						
							|  |  |  | 							if (concatModule.rootModule instanceof NormalModule) { | 
					
						
							| 
									
										
										
										
											2020-03-29 06:10:15 +08:00
										 |  |  | 								const module = /** @type {NormalModule} */ (concatModule.rootModule); | 
					
						
							| 
									
										
										
										
											2019-11-11 22:25:03 +08:00
										 |  |  | 								if (!matchModule(module.resource)) { | 
					
						
							|  |  |  | 									return source; | 
					
						
							|  |  |  | 								} | 
					
						
							|  |  |  | 							} else { | 
					
						
							|  |  |  | 								return source; | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						} else { | 
					
						
							| 
									
										
										
										
											2019-10-02 17:19:44 +08:00
										 |  |  | 							return source; | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						/** @type {{ [key: string]: TODO; }} */ | 
					
						
							|  |  |  | 						let sourceMap; | 
					
						
							|  |  |  | 						let content; | 
					
						
							|  |  |  | 						if (source.sourceAndMap) { | 
					
						
							|  |  |  | 							const sourceAndMap = source.sourceAndMap(options); | 
					
						
							|  |  |  | 							sourceMap = sourceAndMap.map; | 
					
						
							|  |  |  | 							content = sourceAndMap.source; | 
					
						
							|  |  |  | 						} else { | 
					
						
							|  |  |  | 							sourceMap = source.map(options); | 
					
						
							|  |  |  | 							content = source.source(); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						if (!sourceMap) { | 
					
						
							|  |  |  | 							return source; | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						// Clone (flat) the sourcemap to ensure that the mutations below do not persist.
 | 
					
						
							| 
									
										
										
										
											2020-01-18 04:19:37 +08:00
										 |  |  | 						sourceMap = { ...sourceMap }; | 
					
						
							| 
									
										
										
										
											2019-11-11 22:25:03 +08:00
										 |  |  | 						const context = compiler.options.context; | 
					
						
							|  |  |  | 						const root = compiler.root; | 
					
						
							| 
									
										
										
										
											2019-10-02 17:19:44 +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-10-02 17:19:44 +08:00
										 |  |  | 							const module = compilation.findModule(source); | 
					
						
							|  |  |  | 							return module || source; | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 						let moduleFilenames = modules.map(module => { | 
					
						
							|  |  |  | 							return ModuleFilenameHelpers.createFilename( | 
					
						
							|  |  |  | 								module, | 
					
						
							|  |  |  | 								{ | 
					
						
							|  |  |  | 									moduleFilenameTemplate: this.moduleFilenameTemplate, | 
					
						
							|  |  |  | 									namespace: this.namespace | 
					
						
							|  |  |  | 								}, | 
					
						
							|  |  |  | 								{ | 
					
						
							|  |  |  | 									requestShortener: runtimeTemplate.requestShortener, | 
					
						
							|  |  |  | 									chunkGraph | 
					
						
							|  |  |  | 								} | 
					
						
							|  |  |  | 							); | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 						moduleFilenames = ModuleFilenameHelpers.replaceDuplicates( | 
					
						
							|  |  |  | 							moduleFilenames, | 
					
						
							|  |  |  | 							(filename, i, n) => { | 
					
						
							|  |  |  | 								for (let j = 0; j < n; j++) filename += "*"; | 
					
						
							|  |  |  | 								return filename; | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 						sourceMap.sources = moduleFilenames; | 
					
						
							|  |  |  | 						sourceMap.sourceRoot = options.sourceRoot || ""; | 
					
						
							| 
									
										
										
										
											2019-11-11 22:25:03 +08:00
										 |  |  | 						const moduleId = chunkGraph.getModuleId(m); | 
					
						
							| 
									
										
										
										
											2019-10-02 17:19:44 +08:00
										 |  |  | 						sourceMap.file = `${moduleId}.js`; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						const footer = | 
					
						
							|  |  |  | 							this.sourceMapComment.replace( | 
					
						
							|  |  |  | 								/\[url\]/g, | 
					
						
							|  |  |  | 								`data:application/json;charset=utf-8;base64,${Buffer.from( | 
					
						
							|  |  |  | 									JSON.stringify(sourceMap), | 
					
						
							|  |  |  | 									"utf8" | 
					
						
							|  |  |  | 								).toString("base64")}`
 | 
					
						
							|  |  |  | 							) + `\n//# sourceURL=webpack-internal:///${moduleId}\n`; // workaround for chrome bug
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						const evalSource = new RawSource( | 
					
						
							|  |  |  | 							`eval(${JSON.stringify(content + footer)});` | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						cache.set(source, evalSource); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						return evalSource; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				); | 
					
						
							| 
									
										
										
										
											2019-12-16 23:19:39 +08:00
										 |  |  | 				hooks.render.tap( | 
					
						
							|  |  |  | 					"EvalSourceMapDevToolPlugin", | 
					
						
							|  |  |  | 					source => new ConcatSource(devtoolWarning, source) | 
					
						
							|  |  |  | 				); | 
					
						
							| 
									
										
										
										
											2019-10-02 17:19:44 +08:00
										 |  |  | 				hooks.chunkHash.tap("EvalSourceMapDevToolPlugin", (chunk, hash) => { | 
					
						
							|  |  |  | 					hash.update("EvalSourceMapDevToolPlugin"); | 
					
						
							| 
									
										
										
										
											2019-12-18 21:47:24 +08:00
										 |  |  | 					hash.update("2"); | 
					
						
							| 
									
										
										
										
											2019-10-02 17:19:44 +08:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2015-04-05 07:52:30 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-13 18:40:16 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-01-13 11:32:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-13 18:40:16 +08:00
										 |  |  | module.exports = EvalSourceMapDevToolPlugin; |