| 
									
										
										
										
											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-10-02 17:19:44 +08:00
										 |  |  | const { RawSource } = require("webpack-sources"); | 
					
						
							|  |  |  | 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 */ | 
					
						
							| 
									
										
										
										
											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(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-13 11:32:24 +08:00
										 |  |  | class EvalSourceMapDevToolPlugin { | 
					
						
							| 
									
										
										
										
											2018-07-06 17:19:30 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {TODO} options Options object | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	constructor(options = {}) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		if (typeof options === "string") { | 
					
						
							| 
									
										
										
										
											2017-01-13 11:32:24 +08:00
										 |  |  | 			options = { | 
					
						
							|  |  |  | 				append: options | 
					
						
							|  |  |  | 			}; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							|  |  |  | 								const module = | 
					
						
							|  |  |  | 									/** @type {NormalModule} */ (concatModule.rootModule); | 
					
						
							|  |  |  | 								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.
 | 
					
						
							|  |  |  | 						sourceMap = Object.keys(sourceMap).reduce((obj, key) => { | 
					
						
							|  |  |  | 							obj[key] = sourceMap[key]; | 
					
						
							|  |  |  | 							return obj; | 
					
						
							|  |  |  | 						}, {}); | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 				hooks.chunkHash.tap("EvalSourceMapDevToolPlugin", (chunk, hash) => { | 
					
						
							|  |  |  | 					hash.update("EvalSourceMapDevToolPlugin"); | 
					
						
							|  |  |  | 					hash.update("1"); | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											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; |