SourceMapDevToolPlugin.js
6.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
var path = require("path");
var RequestShortener = require("./RequestShortener");
var ConcatSource = require("webpack-sources").ConcatSource;
var RawSource = require("webpack-sources").RawSource;
var ModuleFilenameHelpers = require("./ModuleFilenameHelpers");
var SourceMapDevToolModuleOptionsPlugin = require("./SourceMapDevToolModuleOptionsPlugin");
function SourceMapDevToolPlugin(options) {
if(arguments.length > 1)
throw new Error("SourceMapDevToolPlugin only takes one argument (pass an options object)");
if(typeof options === "string") {
options = {
sourceMapFilename: options
};
}
if(!options) options = {};
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]";
this.options = options;
}
module.exports = SourceMapDevToolPlugin;
SourceMapDevToolPlugin.prototype.apply = function(compiler) {
var sourceMapFilename = this.sourceMapFilename;
var sourceMappingURLComment = this.sourceMappingURLComment;
var moduleFilenameTemplate = this.moduleFilenameTemplate;
var fallbackModuleFilenameTemplate = this.fallbackModuleFilenameTemplate;
var requestShortener = new RequestShortener(compiler.context);
var options = this.options;
options.test = options.test || /\.(js|css)($|\?)/i;
compiler.plugin("compilation", function(compilation) {
new SourceMapDevToolModuleOptionsPlugin(options).apply(compilation);
compilation.plugin("after-optimize-chunk-assets", function(chunks) {
var allModules = [];
var allModuleFilenames = [];
var tasks = [];
chunks.forEach(function(chunk) {
chunk.files.filter(ModuleFilenameHelpers.matchObject.bind(undefined, options)).map(function(file) {
var asset = this.assets[file];
if(asset.__SourceMapDevToolData) {
var data = asset.__SourceMapDevToolData;
for(var cachedFile in data) {
this.assets[cachedFile] = data[cachedFile];
if(cachedFile !== file)
chunk.files.push(cachedFile);
}
return;
}
var source;
var sourceMap;
if(asset.sourceAndMap) {
var sourceAndMap = asset.sourceAndMap(options);
sourceMap = sourceAndMap.map;
source = sourceAndMap.source;
} else {
sourceMap = asset.map(options);
source = asset.source();
}
if(sourceMap) {
return {
chunk: chunk,
file: file,
asset: asset,
source: source,
sourceMap: sourceMap
};
}
}, 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);
}, this);
}, this);
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];
a = !a ? "" : typeof a === "string" ? a : a.identifier();
b = !b ? "" : typeof b === "string" ? b : b.identifier();
return a.length - b.length;
});
allModuleFilenames = ModuleFilenameHelpers.replaceDuplicates(allModuleFilenames, function(filename, i, n) {
for(var j = 0; j < n; j++)
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;
var source = task.source;
var moduleFilenames = task.moduleFilenames;
var modules = task.modules;
sourceMap.sources = moduleFilenames;
if(sourceMap.sourcesContent && !options.noSources) {
sourceMap.sourcesContent = sourceMap.sourcesContent.map(function(content, i) {
return content + "\n\n\n" + ModuleFilenameHelpers.createFooter(modules[i], requestShortener);
});
} else {
sourceMap.sourcesContent = undefined;
}
sourceMap.sourceRoot = options.sourceRoot || "";
sourceMap.file = file;
asset.__SourceMapDevToolData = {};
var currentSourceMappingURLComment = sourceMappingURLComment;
if(currentSourceMappingURLComment !== false && /\.css($|\?)/i.test(file)) {
currentSourceMappingURLComment = currentSourceMappingURLComment.replace(/^\n\/\/(.*)$/, "\n/*$1*/");
}
if(sourceMapFilename) {
var filename = file,
query = "";
var idx = filename.indexOf("?");
if(idx >= 0) {
query = filename.substr(idx);
filename = filename.substr(0, idx);
}
var sourceMapFile = this.getPath(sourceMapFilename, {
chunk: chunk,
filename: filename,
query: query,
basename: basename(filename)
});
var sourceMapUrl = path.relative(path.dirname(file), sourceMapFile).replace(/\\/g, "/");
if(currentSourceMappingURLComment !== false) {
asset.__SourceMapDevToolData[file] = this.assets[file] = new ConcatSource(new RawSource(source), currentSourceMappingURLComment.replace(/\[url\]/g, sourceMapUrl));
}
asset.__SourceMapDevToolData[sourceMapFile] = this.assets[sourceMapFile] = new RawSource(JSON.stringify(sourceMap));
chunk.files.push(sourceMapFile);
} else {
asset.__SourceMapDevToolData[file] = this.assets[file] = new ConcatSource(new RawSource(source), currentSourceMappingURLComment
.replace(/\[map\]/g, function() {
return JSON.stringify(sourceMap);
})
.replace(/\[url\]/g, function() {
return "data:application/json;charset=utf-8;base64," +
new Buffer(JSON.stringify(sourceMap), "utf8").toString("base64"); //eslint-disable-line
})
);
}
}, this);
});
});
};
function basename(name) {
if(name.indexOf("/") < 0) return name;
return name.substr(name.lastIndexOf("/") + 1);
}