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