| 
									
										
										
										
											2013-03-26 23:54:41 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | var RequestShortener = require("./RequestShortener"); | 
					
						
							|  |  |  | var Template = require("./Template"); | 
					
						
							|  |  |  | var ConcatSource = require("webpack-core/lib/ConcatSource"); | 
					
						
							|  |  |  | var RawSource = require("webpack-core/lib/RawSource"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-18 06:21:49 +08:00
										 |  |  | function SourceMapDevToolPlugin(sourceMapFilename, sourceMappingURLComment) { | 
					
						
							| 
									
										
										
										
											2013-03-26 23:54:41 +08:00
										 |  |  | 	this.sourceMapFilename = sourceMapFilename; | 
					
						
							| 
									
										
										
										
											2014-05-26 15:45:42 +08:00
										 |  |  | 	this.sourceMappingURLComment = sourceMappingURLComment || "\n//# sourceMappingURL=[url]"; | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2013-12-18 06:21:49 +08:00
										 |  |  | 	var requestShortener = new RequestShortener(compiler.context); | 
					
						
							| 
									
										
										
										
											2013-03-26 23:54:41 +08:00
										 |  |  | 	compiler.plugin("compilation", function(compilation) { | 
					
						
							| 
									
										
										
										
											2013-10-14 19:59:44 +08:00
										 |  |  | 		compilation.plugin("build-module", function(module) { | 
					
						
							|  |  |  | 			module.useSourceMap = true; | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-03-26 23:54:41 +08:00
										 |  |  | 		compilation.plugin("after-optimize-chunk-assets", function(chunks) { | 
					
						
							|  |  |  | 			chunks.forEach(function(chunk) { | 
					
						
							|  |  |  | 				chunk.files.slice().forEach(function(file) { | 
					
						
							|  |  |  | 					var asset = this.assets[file]; | 
					
						
							| 
									
										
										
										
											2013-07-04 17:55:37 +08:00
										 |  |  | 					if(asset.__SourceMapDevTool_Data) { | 
					
						
							|  |  |  | 						var data = asset.__SourceMapDevTool_Data; | 
					
						
							|  |  |  | 						for(var file in data) { | 
					
						
							|  |  |  | 							this.assets[file] = data[file]; | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						return; | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2013-03-26 23:54:41 +08:00
										 |  |  | 					var sourceMap = asset.map(); | 
					
						
							|  |  |  | 					if(sourceMap) { | 
					
						
							| 
									
										
										
										
											2013-04-02 15:14:19 +08:00
										 |  |  | 						for(var i = 0; i < sourceMap.sources.length; i++) { | 
					
						
							|  |  |  | 							var source = sourceMap.sources[i]; | 
					
						
							| 
									
										
										
										
											2013-03-26 23:54:41 +08:00
										 |  |  | 							var str; | 
					
						
							|  |  |  | 							var module = compilation.findModule(source); | 
					
						
							|  |  |  | 							if(module) | 
					
						
							|  |  |  | 								str = module.readableIdentifier(requestShortener); | 
					
						
							|  |  |  | 							else | 
					
						
							|  |  |  | 								str = requestShortener.shorten(source); | 
					
						
							| 
									
										
										
										
											2014-02-12 15:57:26 +08:00
										 |  |  | 							str = str.split("!"); | 
					
						
							|  |  |  | 							str = str.pop() + (str.length > 0 ? " " + str.join("!") : ""); | 
					
						
							| 
									
										
										
										
											2013-04-02 15:14:19 +08:00
										 |  |  | 							var idx; | 
					
						
							| 
									
										
										
										
											2014-06-18 03:50:19 +08:00
										 |  |  | 							while((idx = sourceMap.sources.indexOf(str)) && (idx >= 0) && (idx < i)) { | 
					
						
							| 
									
										
										
										
											2013-04-02 15:14:19 +08:00
										 |  |  | 								str += "*"; | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 							sourceMap.sources[i] = str; | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2013-03-26 23:54:41 +08:00
										 |  |  | 						sourceMap.sourceRoot = "webpack-module://"; | 
					
						
							| 
									
										
										
										
											2013-07-04 17:55:37 +08:00
										 |  |  | 						asset.__SourceMapDevTool_Data = {}; | 
					
						
							| 
									
										
										
										
											2013-03-26 23:54:41 +08:00
										 |  |  | 						if(sourceMapFilename) { | 
					
						
							| 
									
										
										
										
											2014-04-04 01:46:53 +08:00
										 |  |  | 							var filename = file, query = ""; | 
					
						
							|  |  |  | 							var idx = filename.indexOf("?"); | 
					
						
							|  |  |  | 							if(idx >= 0) { | 
					
						
							|  |  |  | 								query = filename.substr(idx); | 
					
						
							|  |  |  | 								filename = filename.substr(0, idx); | 
					
						
							|  |  |  | 							} | 
					
						
							| 
									
										
										
										
											2013-03-26 23:54:41 +08:00
										 |  |  | 							var sourceMapFile = sourceMapFilename | 
					
						
							| 
									
										
										
										
											2014-04-04 01:46:53 +08:00
										 |  |  | 								.replace(Template.REGEXP_FILE, filename) | 
					
						
							|  |  |  | 								.replace(Template.REGEXP_QUERY, query) | 
					
						
							|  |  |  | 								.replace(Template.REGEXP_FILEBASE, basename(filename)) | 
					
						
							| 
									
										
										
										
											2013-03-26 23:54:41 +08:00
										 |  |  | 								.replace(Template.REGEXP_HASH, this.hash) | 
					
						
							|  |  |  | 								.replace(Template.REGEXP_ID, chunk.id); | 
					
						
							| 
									
										
										
										
											2013-07-04 20:08:47 +08:00
										 |  |  | 							asset.__SourceMapDevTool_Data[file] = this.assets[file] = new ConcatSource(asset, sourceMappingURLComment.replace(/\[url\]/g, sourceMapFile)); | 
					
						
							| 
									
										
										
										
											2013-07-04 17:55:37 +08:00
										 |  |  | 							asset.__SourceMapDevTool_Data[sourceMapFile] = this.assets[sourceMapFile] = new RawSource(JSON.stringify(sourceMap)); | 
					
						
							| 
									
										
										
										
											2013-03-26 23:54:41 +08:00
										 |  |  | 							chunk.files.push(sourceMapFile); | 
					
						
							|  |  |  | 						} else { | 
					
						
							| 
									
										
										
										
											2014-03-21 23:16:31 +08:00
										 |  |  | 							asset.__SourceMapDevTool_Data[file] = this.assets[file] = new ConcatSource(asset, sourceMappingURLComment.replace(/\[url\]/g, "data:application/json;base64," + new Buffer(JSON.stringify(sourceMap)).toString("base64"))); | 
					
						
							| 
									
										
										
										
											2013-03-26 23:54:41 +08:00
										 |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				}, this); | 
					
						
							|  |  |  | 			}, this); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function basename(name) { | 
					
						
							|  |  |  | 	if(name.indexOf("/") < 0) return name; | 
					
						
							|  |  |  | 	return name.substr(name.lastIndexOf("/")+1); | 
					
						
							| 
									
										
										
										
											2014-06-17 22:35:31 +08:00
										 |  |  | } |