| 
									
										
										
										
											2013-03-26 23:54:41 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2014-07-17 02:57:10 +08:00
										 |  |  | var path = require("path"); | 
					
						
							| 
									
										
										
										
											2013-03-26 23:54:41 +08:00
										 |  |  | var RequestShortener = require("./RequestShortener"); | 
					
						
							|  |  |  | var ConcatSource = require("webpack-core/lib/ConcatSource"); | 
					
						
							|  |  |  | var RawSource = require("webpack-core/lib/RawSource"); | 
					
						
							| 
									
										
										
										
											2014-07-18 19:31:50 +08:00
										 |  |  | var ModuleFilenameHelpers = require("./ModuleFilenameHelpers"); | 
					
						
							| 
									
										
										
										
											2015-04-05 07:52:30 +08:00
										 |  |  | var SourceMapDevToolModuleOptionsPlugin = require("./SourceMapDevToolModuleOptionsPlugin"); | 
					
						
							| 
									
										
										
										
											2013-03-26 23:54:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-19 08:11:29 +08:00
										 |  |  | function SourceMapDevToolPlugin(options, sourceMappingURLComment, moduleFilenameTemplate, fallbackModuleFilenameTemplate) { | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 	if(!options || typeof options !== "object") { | 
					
						
							| 
									
										
										
										
											2015-02-19 08:11:29 +08:00
										 |  |  | 		this.sourceMapFilename = options; | 
					
						
							|  |  |  | 		this.sourceMappingURLComment = sourceMappingURLComment === false ? false : sourceMappingURLComment || "\n//# sourceMappingURL=[url]"; | 
					
						
							|  |  |  | 		this.moduleFilenameTemplate = moduleFilenameTemplate || "webpack:///[resourcePath]"; | 
					
						
							|  |  |  | 		this.fallbackModuleFilenameTemplate = fallbackModuleFilenameTemplate || "webpack:///[resourcePath]?[hash]"; | 
					
						
							| 
									
										
										
										
											2015-03-19 23:30:52 +08:00
										 |  |  | 		this.options = {}; | 
					
						
							| 
									
										
										
										
											2015-02-19 08:11:29 +08:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		this.sourceMapFilename = options.filename; | 
					
						
							|  |  |  | 		this.sourceMappingURLComment = options.append === false ? false : options.append || "\n//# sourceMappingURL=[url]"; | 
					
						
							|  |  |  | 		this.moduleFilenameTemplate = options.moduleFilenameTemplate || "webpack:///[resourcePath]"; | 
					
						
							|  |  |  | 		this.fallbackModuleFilenameTemplate = options.fallbackModuleFilenameTemplate || "webpack:///[resourcePath]?[hash]"; | 
					
						
							| 
									
										
										
										
											2015-03-19 23:30:52 +08:00
										 |  |  | 		this.options = options; | 
					
						
							| 
									
										
										
										
											2015-02-19 08:11:29 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-03-26 23:54:41 +08:00
										 |  |  | } | 
					
						
							|  |  |  | module.exports = SourceMapDevToolPlugin; | 
					
						
							|  |  |  | SourceMapDevToolPlugin.prototype.apply = function(compiler) { | 
					
						
							|  |  |  | 	var sourceMapFilename = this.sourceMapFilename; | 
					
						
							| 
									
										
										
										
											2013-07-04 20:08:47 +08:00
										 |  |  | 	var sourceMappingURLComment = this.sourceMappingURLComment; | 
					
						
							| 
									
										
										
										
											2014-07-18 19:31:50 +08:00
										 |  |  | 	var moduleFilenameTemplate = this.moduleFilenameTemplate; | 
					
						
							|  |  |  | 	var fallbackModuleFilenameTemplate = this.fallbackModuleFilenameTemplate; | 
					
						
							| 
									
										
										
										
											2013-12-18 06:21:49 +08:00
										 |  |  | 	var requestShortener = new RequestShortener(compiler.context); | 
					
						
							| 
									
										
										
										
											2015-03-19 23:30:52 +08:00
										 |  |  | 	var options = this.options; | 
					
						
							| 
									
										
										
										
											2015-04-14 03:17:29 +08:00
										 |  |  | 	options.test = options.test || /\.(js|css)($|\?)/i; | 
					
						
							| 
									
										
										
										
											2013-03-26 23:54:41 +08:00
										 |  |  | 	compiler.plugin("compilation", function(compilation) { | 
					
						
							| 
									
										
										
										
											2015-04-05 07:52:30 +08:00
										 |  |  | 		new SourceMapDevToolModuleOptionsPlugin(options).apply(compilation); | 
					
						
							| 
									
										
										
										
											2013-03-26 23:54:41 +08:00
										 |  |  | 		compilation.plugin("after-optimize-chunk-assets", function(chunks) { | 
					
						
							| 
									
										
										
										
											2014-07-26 23:54:00 +08:00
										 |  |  | 			var allModules = []; | 
					
						
							|  |  |  | 			var allModuleFilenames = []; | 
					
						
							|  |  |  | 			var tasks = []; | 
					
						
							| 
									
										
										
										
											2013-03-26 23:54:41 +08:00
										 |  |  | 			chunks.forEach(function(chunk) { | 
					
						
							| 
									
										
										
										
											2015-03-21 02:00:39 +08:00
										 |  |  | 				chunk.files.filter(ModuleFilenameHelpers.matchObject.bind(undefined, options)).map(function(file) { | 
					
						
							| 
									
										
										
										
											2013-03-26 23:54:41 +08:00
										 |  |  | 					var asset = this.assets[file]; | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 					if(asset.__SourceMapDevToolData) { | 
					
						
							| 
									
										
										
										
											2015-04-24 05:55:50 +08:00
										 |  |  | 						var data = asset.__SourceMapDevToolData; | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 						for(var cachedFile in data) { | 
					
						
							| 
									
										
										
										
											2014-09-24 18:50:11 +08:00
										 |  |  | 							this.assets[cachedFile] = data[cachedFile]; | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 							if(cachedFile !== file) | 
					
						
							| 
									
										
										
										
											2014-09-24 18:50:11 +08:00
										 |  |  | 								chunk.files.push(cachedFile); | 
					
						
							| 
									
										
										
										
											2013-07-04 17:55:37 +08:00
										 |  |  | 						} | 
					
						
							|  |  |  | 						return; | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 					if(asset.sourceAndMap) { | 
					
						
							| 
									
										
										
										
											2015-04-05 07:52:30 +08:00
										 |  |  | 						var sourceAndMap = asset.sourceAndMap(options); | 
					
						
							| 
									
										
										
										
											2015-04-03 18:38:56 +08:00
										 |  |  | 						var sourceMap = sourceAndMap.map; | 
					
						
							|  |  |  | 						var source = sourceAndMap.source; | 
					
						
							|  |  |  | 					} else { | 
					
						
							| 
									
										
										
										
											2015-04-05 07:52:30 +08:00
										 |  |  | 						var sourceMap = asset.map(options); | 
					
						
							| 
									
										
										
										
											2015-04-03 18:38:56 +08:00
										 |  |  | 						var source = asset.source(); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 					if(sourceMap) { | 
					
						
							| 
									
										
										
										
											2014-07-26 23:54:00 +08:00
										 |  |  | 						return { | 
					
						
							|  |  |  | 							chunk: chunk, | 
					
						
							|  |  |  | 							file: file, | 
					
						
							|  |  |  | 							asset: asset, | 
					
						
							| 
									
										
										
										
											2015-04-03 18:38:56 +08:00
										 |  |  | 							source: source, | 
					
						
							| 
									
										
										
										
											2014-07-26 23:54:00 +08:00
										 |  |  | 							sourceMap: sourceMap | 
					
						
							| 
									
										
										
										
											2015-04-24 05:55:50 +08:00
										 |  |  | 						}; | 
					
						
							| 
									
										
										
										
											2013-03-26 23:54:41 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2014-07-26 23:54:00 +08:00
										 |  |  | 				}, this).filter(Boolean).map(function(task) { | 
					
						
							|  |  |  | 					var modules = task.sourceMap.sources.map(function(source) { | 
					
						
							|  |  |  | 						var module = compilation.findModule(source); | 
					
						
							|  |  |  | 						return module || source; | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 					var moduleFilenames = modules.map(function(module) { | 
					
						
							|  |  |  | 						return ModuleFilenameHelpers.createFilename(module, moduleFilenameTemplate, requestShortener); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 					task.modules = modules; | 
					
						
							|  |  |  | 					task.moduleFilenames = moduleFilenames; | 
					
						
							|  |  |  | 					return task; | 
					
						
							|  |  |  | 				}, this).forEach(function(task) { | 
					
						
							|  |  |  | 					allModules = allModules.concat(task.modules); | 
					
						
							|  |  |  | 					allModuleFilenames = allModuleFilenames.concat(task.moduleFilenames); | 
					
						
							|  |  |  | 					tasks.push(task); | 
					
						
							| 
									
										
										
										
											2013-03-26 23:54:41 +08:00
										 |  |  | 				}, this); | 
					
						
							|  |  |  | 			}, this); | 
					
						
							| 
									
										
										
										
											2014-07-26 23:54:00 +08:00
										 |  |  | 			allModuleFilenames = ModuleFilenameHelpers.replaceDuplicates(allModuleFilenames, function(filename, i) { | 
					
						
							|  |  |  | 				return ModuleFilenameHelpers.createFilename(allModules[i], fallbackModuleFilenameTemplate, requestShortener); | 
					
						
							|  |  |  | 			}, function(ai, bi) { | 
					
						
							|  |  |  | 				var a = allModules[ai]; | 
					
						
							|  |  |  | 				var b = allModules[bi]; | 
					
						
							| 
									
										
										
										
											2015-04-08 21:10:09 +08:00
										 |  |  | 				a = !a ? "" : typeof a === "string" ? a : a.identifier(); | 
					
						
							|  |  |  | 				b = !b ? "" : typeof b === "string" ? b : b.identifier(); | 
					
						
							| 
									
										
										
										
											2014-07-26 23:54:00 +08:00
										 |  |  | 				return a.length - b.length; | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 			allModuleFilenames = ModuleFilenameHelpers.replaceDuplicates(allModuleFilenames, function(filename, i, n) { | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 				for(var j = 0; j < n; j++) | 
					
						
							| 
									
										
										
										
											2014-07-26 23:54:00 +08:00
										 |  |  | 					filename += "*"; | 
					
						
							|  |  |  | 				return filename; | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 			tasks.forEach(function(task) { | 
					
						
							|  |  |  | 				task.moduleFilenames = allModuleFilenames.slice(0, task.moduleFilenames.length); | 
					
						
							|  |  |  | 				allModuleFilenames = allModuleFilenames.slice(task.moduleFilenames.length); | 
					
						
							|  |  |  | 			}, this); | 
					
						
							|  |  |  | 			tasks.forEach(function(task) { | 
					
						
							|  |  |  | 				var chunk = task.chunk; | 
					
						
							|  |  |  | 				var file = task.file; | 
					
						
							|  |  |  | 				var asset = task.asset; | 
					
						
							|  |  |  | 				var sourceMap = task.sourceMap; | 
					
						
							| 
									
										
										
										
											2015-04-03 18:38:56 +08:00
										 |  |  | 				var source = task.source; | 
					
						
							| 
									
										
										
										
											2014-07-26 23:54:00 +08:00
										 |  |  | 				var moduleFilenames = task.moduleFilenames; | 
					
						
							|  |  |  | 				var modules = task.modules; | 
					
						
							|  |  |  | 				sourceMap.sources = moduleFilenames; | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 				if(sourceMap.sourcesContent) { | 
					
						
							| 
									
										
										
										
											2014-07-26 23:54:00 +08:00
										 |  |  | 					sourceMap.sourcesContent = sourceMap.sourcesContent.map(function(content, i) { | 
					
						
							|  |  |  | 						return content + "\n\n\n" + ModuleFilenameHelpers.createFooter(modules[i], requestShortener); | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2015-02-19 08:11:29 +08:00
										 |  |  | 				} else { | 
					
						
							|  |  |  | 					sourceMap.sourcesContent = undefined; | 
					
						
							| 
									
										
										
										
											2014-07-26 23:54:00 +08:00
										 |  |  | 				} | 
					
						
							|  |  |  | 				sourceMap.sourceRoot = ""; | 
					
						
							|  |  |  | 				sourceMap.file = file; | 
					
						
							| 
									
										
										
										
											2015-04-24 05:55:50 +08:00
										 |  |  | 				asset.__SourceMapDevToolData = {}; | 
					
						
							| 
									
										
										
										
											2014-09-08 04:54:38 +08:00
										 |  |  | 				var currentSourceMappingURLComment = sourceMappingURLComment; | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 				if(currentSourceMappingURLComment !== false && /\.css($|\?)/i.test(file)) { | 
					
						
							| 
									
										
										
										
											2014-09-08 04:54:38 +08:00
										 |  |  | 					currentSourceMappingURLComment = currentSourceMappingURLComment.replace(/^\n\/\/(.*)$/, "\n/*$1*/"); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 				if(sourceMapFilename) { | 
					
						
							| 
									
										
										
										
											2015-07-13 06:20:09 +08:00
										 |  |  | 					var filename = file, | 
					
						
							|  |  |  | 						query = ""; | 
					
						
							| 
									
										
										
										
											2014-07-26 23:54:00 +08:00
										 |  |  | 					var idx = filename.indexOf("?"); | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 					if(idx >= 0) { | 
					
						
							| 
									
										
										
										
											2014-07-26 23:54:00 +08:00
										 |  |  | 						query = filename.substr(idx); | 
					
						
							|  |  |  | 						filename = filename.substr(0, idx); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2014-08-22 19:51:24 +08:00
										 |  |  | 					var sourceMapFile = this.getPath(sourceMapFilename, { | 
					
						
							|  |  |  | 						chunk: chunk, | 
					
						
							|  |  |  | 						filename: filename, | 
					
						
							|  |  |  | 						query: query, | 
					
						
							|  |  |  | 						basename: basename(filename) | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2014-07-26 23:54:00 +08:00
										 |  |  | 					var sourceMapUrl = path.relative(path.dirname(file), sourceMapFile).replace(/\\/g, "/"); | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 					if(currentSourceMappingURLComment !== false) { | 
					
						
							| 
									
										
										
										
											2015-04-24 05:55:50 +08:00
										 |  |  | 						asset.__SourceMapDevToolData[file] = this.assets[file] = new ConcatSource(new RawSource(source), currentSourceMappingURLComment.replace(/\[url\]/g, sourceMapUrl)); | 
					
						
							| 
									
										
										
										
											2014-08-28 21:13:05 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2015-04-24 05:55:50 +08:00
										 |  |  | 					asset.__SourceMapDevToolData[sourceMapFile] = this.assets[sourceMapFile] = new RawSource(JSON.stringify(sourceMap)); | 
					
						
							| 
									
										
										
										
											2014-07-26 23:54:00 +08:00
										 |  |  | 					chunk.files.push(sourceMapFile); | 
					
						
							|  |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2015-04-24 05:55:50 +08:00
										 |  |  | 					asset.__SourceMapDevToolData[file] = this.assets[file] = new ConcatSource(new RawSource(source), currentSourceMappingURLComment | 
					
						
							| 
									
										
										
										
											2015-02-19 08:11:29 +08:00
										 |  |  | 						.replace(/\[map\]/g, function() { | 
					
						
							|  |  |  | 							return JSON.stringify(sourceMap); | 
					
						
							|  |  |  | 						}) | 
					
						
							|  |  |  | 						.replace(/\[url\]/g, function() { | 
					
						
							|  |  |  | 							return "data:application/json;base64," + | 
					
						
							|  |  |  | 								new Buffer(JSON.stringify(sourceMap)).toString("base64"); | 
					
						
							|  |  |  | 						}) | 
					
						
							|  |  |  | 					); | 
					
						
							| 
									
										
										
										
											2014-07-26 23:54:00 +08:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			}, this); | 
					
						
							| 
									
										
										
										
											2013-03-26 23:54:41 +08:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function basename(name) { | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 	if(name.indexOf("/") < 0) return name; | 
					
						
							| 
									
										
										
										
											2015-04-24 05:55:50 +08:00
										 |  |  | 	return name.substr(name.lastIndexOf("/") + 1); | 
					
						
							| 
									
										
										
										
											2014-06-17 22:35:31 +08:00
										 |  |  | } |