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 Template = require("./Template");
|
|
|
|
var ConcatSource = require("webpack-core/lib/ConcatSource");
|
|
|
|
var RawSource = require("webpack-core/lib/RawSource");
|
|
|
|
|
2014-07-18 06:58:01 +08:00
|
|
|
function SourceMapDevToolPlugin(sourceMapFilename, sourceMappingURLComment, moduleFilename) {
|
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]";
|
2014-07-18 06:58:01 +08:00
|
|
|
this.moduleFilename = moduleFilename || "[name]?[loaders]";
|
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 06:58:01 +08:00
|
|
|
var moduleFilename = this.moduleFilename;
|
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];
|
2014-07-18 06:58:01 +08:00
|
|
|
var str, name, loaders;
|
2013-03-26 23:54:41 +08:00
|
|
|
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("!");
|
2014-07-18 06:58:01 +08:00
|
|
|
name = str.pop();
|
|
|
|
loaders = str.join("!");
|
|
|
|
str = moduleFilename
|
|
|
|
.replace(/\[name\]/gi, name)
|
|
|
|
.replace(/\[loaders\]/gi, loaders)
|
|
|
|
.replace(/\[id\]/gi, module && module.id || '');
|
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);
|
2014-07-17 02:57:10 +08:00
|
|
|
var sourceMapUrl = path.relative(path.dirname(file), sourceMapFile).replace(/\\/g, "/");
|
|
|
|
asset.__SourceMapDevTool_Data[file] = this.assets[file] = new ConcatSource(asset, sourceMappingURLComment.replace(/\[url\]/g, sourceMapUrl));
|
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
|
|
|
}
|
2014-07-17 02:57:10 +08:00
|
|
|
|