webpack 4.1.0 → 4.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. package/README.md +719 -721
  2. package/bin/webpack.js +69 -10
  3. package/lib/APIPlugin.js +84 -84
  4. package/lib/AmdMainTemplatePlugin.js +75 -77
  5. package/lib/AsyncDependencyToInitialChunkError.js +21 -23
  6. package/lib/BannerPlugin.js +101 -101
  7. package/lib/Chunk.js +477 -469
  8. package/lib/ChunkTemplate.js +51 -53
  9. package/lib/Compilation.js +1858 -1851
  10. package/lib/Compiler.js +493 -478
  11. package/lib/ConcurrentCompilationError.js +19 -0
  12. package/lib/ContextModule.js +696 -685
  13. package/lib/ContextModuleFactory.js +245 -243
  14. package/lib/DefinePlugin.js +197 -197
  15. package/lib/DelegatedModule.js +101 -101
  16. package/lib/DependenciesBlockVariable.js +51 -52
  17. package/lib/Dependency.js +53 -52
  18. package/lib/DllModule.js +54 -54
  19. package/lib/DllModuleFactory.js +29 -29
  20. package/lib/EnvironmentPlugin.js +65 -67
  21. package/lib/EvalDevToolModuleTemplatePlugin.js +60 -60
  22. package/lib/EvalSourceMapDevToolModuleTemplatePlugin.js +105 -105
  23. package/lib/ExportPropertyMainTemplatePlugin.js +40 -40
  24. package/lib/ExternalModule.js +159 -159
  25. package/lib/FunctionModuleTemplatePlugin.js +98 -98
  26. package/lib/HotModuleReplacement.runtime.js +631 -631
  27. package/lib/HotModuleReplacementPlugin.js +407 -406
  28. package/lib/HotUpdateChunkTemplate.js +78 -80
  29. package/lib/JavascriptGenerator.js +228 -229
  30. package/lib/JavascriptModulesPlugin.js +184 -158
  31. package/lib/JsonGenerator.js +42 -42
  32. package/lib/MainTemplate.js +406 -402
  33. package/lib/Module.js +343 -340
  34. package/lib/ModuleBuildError.js +42 -42
  35. package/lib/ModuleError.js +28 -28
  36. package/lib/ModuleFilenameHelpers.js +166 -166
  37. package/lib/ModuleTemplate.js +77 -79
  38. package/lib/ModuleWarning.js +30 -30
  39. package/lib/MultiCompiler.js +271 -259
  40. package/lib/MultiModule.js +78 -75
  41. package/lib/MultiModuleFactory.js +23 -23
  42. package/lib/MultiWatching.js +38 -37
  43. package/lib/NoModeWarning.js +23 -21
  44. package/lib/NormalModule.js +478 -470
  45. package/lib/NormalModuleFactory.js +483 -481
  46. package/lib/OptionsDefaulter.js +80 -86
  47. package/lib/Parser.js +2074 -2071
  48. package/lib/ProgressPlugin.js +231 -231
  49. package/lib/RawModule.js +54 -55
  50. package/lib/RecordIdsPlugin.js +160 -160
  51. package/lib/RemovedPluginError.js +13 -13
  52. package/lib/ResolverFactory.js +64 -67
  53. package/lib/RuntimeTemplate.js +267 -297
  54. package/lib/SetVarMainTemplatePlugin.js +57 -57
  55. package/lib/SourceMapDevToolPlugin.js +302 -308
  56. package/lib/Stats.js +1234 -1212
  57. package/lib/Template.js +205 -205
  58. package/lib/TemplatedPathPlugin.js +170 -143
  59. package/lib/UmdMainTemplatePlugin.js +264 -269
  60. package/lib/Watching.js +193 -193
  61. package/lib/WebAssemblyParser.js +50 -54
  62. package/lib/WebpackOptionsApply.js +401 -401
  63. package/lib/WebpackOptionsDefaulter.js +337 -317
  64. package/lib/WebpackOptionsValidationError.js +316 -319
  65. package/lib/debug/ProfilingPlugin.js +409 -405
  66. package/lib/dependencies/AMDDefineDependencyParserPlugin.js +328 -311
  67. package/lib/dependencies/AMDRequireContextDependency.js +20 -20
  68. package/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js +270 -241
  69. package/lib/dependencies/HarmonyAcceptImportDependency.js +23 -23
  70. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +620 -606
  71. package/lib/dependencies/HarmonyExportSpecifierDependency.js +53 -53
  72. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +214 -214
  73. package/lib/dependencies/HarmonyImportSpecifierDependency.js +154 -156
  74. package/lib/dependencies/ImportDependenciesBlock.js +17 -17
  75. package/lib/dependencies/ImportDependency.js +34 -34
  76. package/lib/dependencies/ImportEagerDependency.js +32 -32
  77. package/lib/dependencies/ImportParserPlugin.js +175 -179
  78. package/lib/dependencies/ImportWeakDependency.js +34 -34
  79. package/lib/dependencies/JsonExportsDependency.js +25 -25
  80. package/lib/dependencies/ModuleDependency.js +20 -20
  81. package/lib/dependencies/NullDependency.js +20 -20
  82. package/lib/dependencies/RequireContextDependency.js +22 -22
  83. package/lib/dependencies/RequireIncludeDependency.js +40 -40
  84. package/lib/dependencies/WebpackMissingModule.js +20 -22
  85. package/lib/node/NodeChunkTemplatePlugin.js +31 -31
  86. package/lib/node/NodeHotUpdateChunkTemplatePlugin.js +36 -36
  87. package/lib/node/NodeMainTemplatePlugin.js +320 -273
  88. package/lib/node/ReadFileCompileWasmMainTemplatePlugin.js +113 -115
  89. package/lib/optimize/AggressiveSplittingPlugin.js +281 -281
  90. package/lib/optimize/ConcatenatedModule.js +1364 -1366
  91. package/lib/optimize/RemoveParentModulesPlugin.js +114 -114
  92. package/lib/optimize/SplitChunksPlugin.js +519 -491
  93. package/lib/performance/SizeLimitsPlugin.js +105 -105
  94. package/lib/util/TrackingSet.js +35 -35
  95. package/lib/util/objectToMap.js +10 -10
  96. package/lib/wasm/WasmModuleTemplatePlugin.js +106 -106
  97. package/lib/web/JsonpChunkTemplatePlugin.js +47 -47
  98. package/lib/web/JsonpExportMainTemplatePlugin.js +47 -47
  99. package/lib/web/JsonpHotUpdateChunkTemplatePlugin.js +39 -39
  100. package/lib/web/JsonpMainTemplatePlugin.js +425 -403
  101. package/lib/webpack.js +182 -179
  102. package/lib/webworker/WebWorkerChunkTemplatePlugin.js +35 -35
  103. package/lib/webworker/WebWorkerHotUpdateChunkTemplatePlugin.js +40 -40
  104. package/lib/webworker/WebWorkerMainTemplatePlugin.js +177 -154
  105. package/package.json +9 -8
  106. package/schemas/WebpackOptions.json +1973 -1951
  107. package/schemas/ajv.absolutePath.js +55 -29
  108. package/schemas/plugins/BannerPlugin.json +85 -85
  109. package/schemas/plugins/DllPlugin.json +28 -28
  110. package/schemas/plugins/DllReferencePlugin.json +99 -99
  111. package/schemas/plugins/HashedModuleIdsPlugin.json +24 -24
  112. package/schemas/plugins/LoaderOptionsPlugin.json +26 -26
  113. package/schemas/plugins/SourceMapDevToolPlugin.json +187 -187
  114. package/schemas/plugins/WatchIgnorePlugin.json +16 -16
  115. package/schemas/plugins/debug/ProfilingPlugin.json +12 -12
  116. package/schemas/plugins/optimize/AggressiveSplittingPlugin.json +22 -22
  117. package/schemas/plugins/optimize/LimitChunkCountPlugin.json +15 -15
  118. package/schemas/plugins/optimize/MinChunkSizePlugin.json +13 -13
@@ -1,308 +1,302 @@
1
- /*
2
- MIT License http://www.opensource.org/licenses/mit-license.php
3
- Author Tobias Koppers @sokra
4
- */
5
- "use strict";
6
-
7
- const path = require("path");
8
- const ConcatSource = require("webpack-sources").ConcatSource;
9
- const RawSource = require("webpack-sources").RawSource;
10
- const ModuleFilenameHelpers = require("./ModuleFilenameHelpers");
11
- const SourceMapDevToolModuleOptionsPlugin = require("./SourceMapDevToolModuleOptionsPlugin");
12
- const createHash = require("./util/createHash");
13
-
14
- const validateOptions = require("schema-utils");
15
- const schema = require("../schemas/plugins/SourceMapDevToolPlugin.json");
16
-
17
- const basename = name => {
18
- if (!name.includes("/")) return name;
19
- return name.substr(name.lastIndexOf("/") + 1);
20
- };
21
-
22
- const getTaskForFile = (file, chunk, options, compilation) => {
23
- const asset = compilation.assets[file];
24
- if (asset.__SourceMapDevToolFile === file && asset.__SourceMapDevToolData) {
25
- const data = asset.__SourceMapDevToolData;
26
- for (const cachedFile in data) {
27
- compilation.assets[cachedFile] = data[cachedFile];
28
- if (cachedFile !== file) chunk.files.push(cachedFile);
29
- }
30
- return;
31
- }
32
- let source, sourceMap;
33
- if (asset.sourceAndMap) {
34
- const sourceAndMap = asset.sourceAndMap(options);
35
- sourceMap = sourceAndMap.map;
36
- source = sourceAndMap.source;
37
- } else {
38
- sourceMap = asset.map(options);
39
- source = asset.source();
40
- }
41
- if (sourceMap) {
42
- return {
43
- chunk,
44
- file,
45
- asset,
46
- source,
47
- sourceMap,
48
- modules: undefined
49
- };
50
- }
51
- };
52
-
53
- class SourceMapDevToolPlugin {
54
- constructor(options) {
55
- if (arguments.length > 1)
56
- throw new Error(
57
- "SourceMapDevToolPlugin only takes one argument (pass an options object)"
58
- );
59
-
60
- validateOptions(schema, options || {}, "SourceMap DevTool Plugin");
61
-
62
- if (!options) options = {};
63
- this.sourceMapFilename = options.filename;
64
- this.sourceMappingURLComment =
65
- options.append === false
66
- ? false
67
- : options.append || "\n//# sourceMappingURL=[url]";
68
- this.moduleFilenameTemplate =
69
- options.moduleFilenameTemplate || "webpack://[namespace]/[resourcePath]";
70
- this.fallbackModuleFilenameTemplate =
71
- options.fallbackModuleFilenameTemplate ||
72
- "webpack://[namespace]/[resourcePath]?[hash]";
73
- this.namespace = options.namespace || "";
74
- this.options = options;
75
- }
76
-
77
- apply(compiler) {
78
- const sourceMapFilename = this.sourceMapFilename;
79
- const sourceMappingURLComment = this.sourceMappingURLComment;
80
- const moduleFilenameTemplate = this.moduleFilenameTemplate;
81
- const namespace = this.namespace;
82
- const fallbackModuleFilenameTemplate = this.fallbackModuleFilenameTemplate;
83
- const requestShortener = compiler.requestShortener;
84
- const options = this.options;
85
- options.test = options.test || /\.(js|css)($|\?)/i;
86
-
87
- const matchObject = ModuleFilenameHelpers.matchObject.bind(
88
- undefined,
89
- options
90
- );
91
-
92
- compiler.hooks.compilation.tap("SourceMapDevToolPlugin", compilation => {
93
- new SourceMapDevToolModuleOptionsPlugin(options).apply(compilation);
94
-
95
- compilation.hooks.afterOptimizeChunkAssets.tap(
96
- {
97
- name: "SourceMapDevToolPlugin",
98
- context: true
99
- },
100
- (context, chunks) => {
101
- const moduleToSourceNameMapping = new Map();
102
- const reportProgress =
103
- context && context.reportProgress
104
- ? context.reportProgress
105
- : () => {};
106
-
107
- const files = [];
108
- for (const chunk of chunks) {
109
- for (const file of chunk.files) {
110
- if (matchObject(file)) {
111
- files.push({
112
- file,
113
- chunk
114
- });
115
- }
116
- }
117
- }
118
-
119
- reportProgress(0.0);
120
- const tasks = [];
121
- files.forEach(({ file, chunk }, idx) => {
122
- reportProgress(
123
- 0.5 * idx / files.length,
124
- file,
125
- "generate SourceMap"
126
- );
127
- const task = getTaskForFile(file, chunk, options, compilation);
128
-
129
- if (task) {
130
- const modules = task.sourceMap.sources.map(source => {
131
- const module = compilation.findModule(source);
132
- return module || source;
133
- });
134
-
135
- for (let idx = 0; idx < modules.length; idx++) {
136
- const module = modules[idx];
137
- if (!moduleToSourceNameMapping.get(module)) {
138
- moduleToSourceNameMapping.set(
139
- module,
140
- ModuleFilenameHelpers.createFilename(
141
- module,
142
- {
143
- moduleFilenameTemplate: moduleFilenameTemplate,
144
- namespace: namespace
145
- },
146
- requestShortener
147
- )
148
- );
149
- }
150
- }
151
-
152
- task.modules = modules;
153
-
154
- tasks.push(task);
155
- }
156
- });
157
-
158
- reportProgress(0.5, "resolve sources");
159
- const usedNamesSet = new Set(moduleToSourceNameMapping.values());
160
- const conflictDetectionSet = new Set();
161
-
162
- // all modules in defined order (longest identifier first)
163
- const allModules = Array.from(moduleToSourceNameMapping.keys()).sort(
164
- (a, b) => {
165
- const ai = typeof a === "string" ? a : a.identifier();
166
- const bi = typeof b === "string" ? b : b.identifier();
167
- return ai.length - bi.length;
168
- }
169
- );
170
-
171
- // find modules with conflicting source names
172
- for (let idx = 0; idx < allModules.length; idx++) {
173
- const module = allModules[idx];
174
- let sourceName = moduleToSourceNameMapping.get(module);
175
- let hasName = conflictDetectionSet.has(sourceName);
176
- if (!hasName) {
177
- conflictDetectionSet.add(sourceName);
178
- continue;
179
- }
180
-
181
- // try the fallback name first
182
- sourceName = ModuleFilenameHelpers.createFilename(
183
- module,
184
- {
185
- moduleFilenameTemplate: fallbackModuleFilenameTemplate,
186
- namespace: namespace
187
- },
188
- requestShortener
189
- );
190
- hasName = usedNamesSet.has(sourceName);
191
- if (!hasName) {
192
- moduleToSourceNameMapping.set(module, sourceName);
193
- usedNamesSet.add(sourceName);
194
- continue;
195
- }
196
-
197
- // elsewise just append stars until we have a valid name
198
- while (hasName) {
199
- sourceName += "*";
200
- hasName = usedNamesSet.has(sourceName);
201
- }
202
- moduleToSourceNameMapping.set(module, sourceName);
203
- usedNamesSet.add(sourceName);
204
- }
205
- tasks.forEach((task, index) => {
206
- reportProgress(
207
- 0.5 + 0.5 * index / tasks.length,
208
- task.file,
209
- "attach SourceMap"
210
- );
211
- const chunk = task.chunk;
212
- const file = task.file;
213
- const asset = task.asset;
214
- const sourceMap = task.sourceMap;
215
- const source = task.source;
216
- const modules = task.modules;
217
- const moduleFilenames = modules.map(m =>
218
- moduleToSourceNameMapping.get(m)
219
- );
220
- sourceMap.sources = moduleFilenames;
221
- if (options.noSources) {
222
- sourceMap.sourcesContent = undefined;
223
- }
224
- sourceMap.sourceRoot = options.sourceRoot || "";
225
- sourceMap.file = file;
226
- asset.__SourceMapDevToolFile = file;
227
- asset.__SourceMapDevToolData = {};
228
- let currentSourceMappingURLComment = sourceMappingURLComment;
229
- if (
230
- currentSourceMappingURLComment !== false &&
231
- /\.css($|\?)/i.test(file)
232
- ) {
233
- currentSourceMappingURLComment = currentSourceMappingURLComment.replace(
234
- /^\n\/\/(.*)$/,
235
- "\n/*$1*/"
236
- );
237
- }
238
- const sourceMapString = JSON.stringify(sourceMap);
239
- if (sourceMapFilename) {
240
- let filename = file;
241
- let query = "";
242
- const idx = filename.indexOf("?");
243
- if (idx >= 0) {
244
- query = filename.substr(idx);
245
- filename = filename.substr(0, idx);
246
- }
247
- let sourceMapFile = compilation.getPath(sourceMapFilename, {
248
- chunk,
249
- filename: options.fileContext
250
- ? path.relative(options.fileContext, filename)
251
- : filename,
252
- query,
253
- basename: basename(filename)
254
- });
255
- if (sourceMapFile.includes("[contenthash]")) {
256
- sourceMapFile = sourceMapFile.replace(
257
- /\[contenthash\]/g,
258
- createHash("md4")
259
- .update(sourceMapString)
260
- .digest("hex")
261
- );
262
- }
263
- const sourceMapUrl = options.publicPath
264
- ? options.publicPath + sourceMapFile.replace(/\\/g, "/")
265
- : path
266
- .relative(path.dirname(file), sourceMapFile)
267
- .replace(/\\/g, "/");
268
- if (currentSourceMappingURLComment !== false) {
269
- asset.__SourceMapDevToolData[file] = compilation.assets[
270
- file
271
- ] = new ConcatSource(
272
- new RawSource(source),
273
- currentSourceMappingURLComment.replace(
274
- /\[url\]/g,
275
- sourceMapUrl
276
- )
277
- );
278
- }
279
- asset.__SourceMapDevToolData[sourceMapFile] = compilation.assets[
280
- sourceMapFile
281
- ] = new RawSource(sourceMapString);
282
- chunk.files.push(sourceMapFile);
283
- } else {
284
- asset.__SourceMapDevToolData[file] = compilation.assets[
285
- file
286
- ] = new ConcatSource(
287
- new RawSource(source),
288
- currentSourceMappingURLComment
289
- .replace(/\[map\]/g, () => sourceMapString)
290
- .replace(
291
- /\[url\]/g,
292
- () =>
293
- `data:application/json;charset=utf-8;base64,${Buffer.from(
294
- sourceMapString,
295
- "utf-8"
296
- ).toString("base64")}`
297
- )
298
- );
299
- }
300
- });
301
- reportProgress(1.0);
302
- }
303
- );
304
- });
305
- }
306
- }
307
-
308
- module.exports = SourceMapDevToolPlugin;
1
+ /*
2
+ MIT License http://www.opensource.org/licenses/mit-license.php
3
+ Author Tobias Koppers @sokra
4
+ */
5
+ "use strict";
6
+
7
+ const path = require("path");
8
+ const { ConcatSource, RawSource } = require("webpack-sources");
9
+ const ModuleFilenameHelpers = require("./ModuleFilenameHelpers");
10
+ const SourceMapDevToolModuleOptionsPlugin = require("./SourceMapDevToolModuleOptionsPlugin");
11
+ const createHash = require("./util/createHash");
12
+
13
+ const validateOptions = require("schema-utils");
14
+ const schema = require("../schemas/plugins/SourceMapDevToolPlugin.json");
15
+
16
+ const basename = name => {
17
+ if (!name.includes("/")) return name;
18
+ return name.substr(name.lastIndexOf("/") + 1);
19
+ };
20
+
21
+ const getTaskForFile = (file, chunk, options, compilation) => {
22
+ const asset = compilation.assets[file];
23
+ if (asset.__SourceMapDevToolFile === file && asset.__SourceMapDevToolData) {
24
+ const data = asset.__SourceMapDevToolData;
25
+ for (const cachedFile in data) {
26
+ compilation.assets[cachedFile] = data[cachedFile];
27
+ if (cachedFile !== file) chunk.files.push(cachedFile);
28
+ }
29
+ return;
30
+ }
31
+ let source, sourceMap;
32
+ if (asset.sourceAndMap) {
33
+ const sourceAndMap = asset.sourceAndMap(options);
34
+ sourceMap = sourceAndMap.map;
35
+ source = sourceAndMap.source;
36
+ } else {
37
+ sourceMap = asset.map(options);
38
+ source = asset.source();
39
+ }
40
+ if (sourceMap) {
41
+ return {
42
+ chunk,
43
+ file,
44
+ asset,
45
+ source,
46
+ sourceMap,
47
+ modules: undefined
48
+ };
49
+ }
50
+ };
51
+
52
+ class SourceMapDevToolPlugin {
53
+ constructor(options) {
54
+ if (arguments.length > 1)
55
+ throw new Error(
56
+ "SourceMapDevToolPlugin only takes one argument (pass an options object)"
57
+ );
58
+
59
+ validateOptions(schema, options || {}, "SourceMap DevTool Plugin");
60
+
61
+ if (!options) options = {};
62
+ this.sourceMapFilename = options.filename;
63
+ this.sourceMappingURLComment =
64
+ options.append === false
65
+ ? false
66
+ : options.append || "\n//# sourceMappingURL=[url]";
67
+ this.moduleFilenameTemplate =
68
+ options.moduleFilenameTemplate || "webpack://[namespace]/[resourcePath]";
69
+ this.fallbackModuleFilenameTemplate =
70
+ options.fallbackModuleFilenameTemplate ||
71
+ "webpack://[namespace]/[resourcePath]?[hash]";
72
+ this.namespace = options.namespace || "";
73
+ this.options = options;
74
+ }
75
+
76
+ apply(compiler) {
77
+ const sourceMapFilename = this.sourceMapFilename;
78
+ const sourceMappingURLComment = this.sourceMappingURLComment;
79
+ const moduleFilenameTemplate = this.moduleFilenameTemplate;
80
+ const namespace = this.namespace;
81
+ const fallbackModuleFilenameTemplate = this.fallbackModuleFilenameTemplate;
82
+ const requestShortener = compiler.requestShortener;
83
+ const options = this.options;
84
+ options.test = options.test || /\.(js|css)($|\?)/i;
85
+
86
+ const matchObject = ModuleFilenameHelpers.matchObject.bind(
87
+ undefined,
88
+ options
89
+ );
90
+
91
+ compiler.hooks.compilation.tap("SourceMapDevToolPlugin", compilation => {
92
+ new SourceMapDevToolModuleOptionsPlugin(options).apply(compilation);
93
+
94
+ compilation.hooks.afterOptimizeChunkAssets.tap(
95
+ {
96
+ name: "SourceMapDevToolPlugin",
97
+ context: true
98
+ },
99
+ (context, chunks) => {
100
+ const moduleToSourceNameMapping = new Map();
101
+ const reportProgress =
102
+ context && context.reportProgress
103
+ ? context.reportProgress
104
+ : () => {};
105
+
106
+ const files = [];
107
+ for (const chunk of chunks) {
108
+ for (const file of chunk.files) {
109
+ if (matchObject(file)) {
110
+ files.push({
111
+ file,
112
+ chunk
113
+ });
114
+ }
115
+ }
116
+ }
117
+
118
+ reportProgress(0.0);
119
+ const tasks = [];
120
+ files.forEach(({ file, chunk }, idx) => {
121
+ reportProgress(
122
+ 0.5 * idx / files.length,
123
+ file,
124
+ "generate SourceMap"
125
+ );
126
+ const task = getTaskForFile(file, chunk, options, compilation);
127
+
128
+ if (task) {
129
+ const modules = task.sourceMap.sources.map(source => {
130
+ const module = compilation.findModule(source);
131
+ return module || source;
132
+ });
133
+
134
+ for (let idx = 0; idx < modules.length; idx++) {
135
+ const module = modules[idx];
136
+ if (!moduleToSourceNameMapping.get(module)) {
137
+ moduleToSourceNameMapping.set(
138
+ module,
139
+ ModuleFilenameHelpers.createFilename(
140
+ module,
141
+ {
142
+ moduleFilenameTemplate: moduleFilenameTemplate,
143
+ namespace: namespace
144
+ },
145
+ requestShortener
146
+ )
147
+ );
148
+ }
149
+ }
150
+
151
+ task.modules = modules;
152
+
153
+ tasks.push(task);
154
+ }
155
+ });
156
+
157
+ reportProgress(0.5, "resolve sources");
158
+ const usedNamesSet = new Set(moduleToSourceNameMapping.values());
159
+ const conflictDetectionSet = new Set();
160
+
161
+ // all modules in defined order (longest identifier first)
162
+ const allModules = Array.from(moduleToSourceNameMapping.keys()).sort(
163
+ (a, b) => {
164
+ const ai = typeof a === "string" ? a : a.identifier();
165
+ const bi = typeof b === "string" ? b : b.identifier();
166
+ return ai.length - bi.length;
167
+ }
168
+ );
169
+
170
+ // find modules with conflicting source names
171
+ for (let idx = 0; idx < allModules.length; idx++) {
172
+ const module = allModules[idx];
173
+ let sourceName = moduleToSourceNameMapping.get(module);
174
+ let hasName = conflictDetectionSet.has(sourceName);
175
+ if (!hasName) {
176
+ conflictDetectionSet.add(sourceName);
177
+ continue;
178
+ }
179
+
180
+ // try the fallback name first
181
+ sourceName = ModuleFilenameHelpers.createFilename(
182
+ module,
183
+ {
184
+ moduleFilenameTemplate: fallbackModuleFilenameTemplate,
185
+ namespace: namespace
186
+ },
187
+ requestShortener
188
+ );
189
+ hasName = usedNamesSet.has(sourceName);
190
+ if (!hasName) {
191
+ moduleToSourceNameMapping.set(module, sourceName);
192
+ usedNamesSet.add(sourceName);
193
+ continue;
194
+ }
195
+
196
+ // elsewise just append stars until we have a valid name
197
+ while (hasName) {
198
+ sourceName += "*";
199
+ hasName = usedNamesSet.has(sourceName);
200
+ }
201
+ moduleToSourceNameMapping.set(module, sourceName);
202
+ usedNamesSet.add(sourceName);
203
+ }
204
+ tasks.forEach((task, index) => {
205
+ reportProgress(
206
+ 0.5 + 0.5 * index / tasks.length,
207
+ task.file,
208
+ "attach SourceMap"
209
+ );
210
+ const chunk = task.chunk;
211
+ const file = task.file;
212
+ const asset = task.asset;
213
+ const sourceMap = task.sourceMap;
214
+ const source = task.source;
215
+ const modules = task.modules;
216
+ const moduleFilenames = modules.map(m =>
217
+ moduleToSourceNameMapping.get(m)
218
+ );
219
+ sourceMap.sources = moduleFilenames;
220
+ if (options.noSources) {
221
+ sourceMap.sourcesContent = undefined;
222
+ }
223
+ sourceMap.sourceRoot = options.sourceRoot || "";
224
+ sourceMap.file = file;
225
+ asset.__SourceMapDevToolFile = file;
226
+ asset.__SourceMapDevToolData = {};
227
+ let currentSourceMappingURLComment = sourceMappingURLComment;
228
+ if (
229
+ currentSourceMappingURLComment !== false &&
230
+ /\.css($|\?)/i.test(file)
231
+ ) {
232
+ currentSourceMappingURLComment = currentSourceMappingURLComment.replace(
233
+ /^\n\/\/(.*)$/,
234
+ "\n/*$1*/"
235
+ );
236
+ }
237
+ const sourceMapString = JSON.stringify(sourceMap);
238
+ if (sourceMapFilename) {
239
+ let filename = file;
240
+ let query = "";
241
+ const idx = filename.indexOf("?");
242
+ if (idx >= 0) {
243
+ query = filename.substr(idx);
244
+ filename = filename.substr(0, idx);
245
+ }
246
+ let sourceMapFile = compilation.getPath(sourceMapFilename, {
247
+ chunk,
248
+ filename: options.fileContext
249
+ ? path.relative(options.fileContext, filename)
250
+ : filename,
251
+ query,
252
+ basename: basename(filename),
253
+ contentHash: createHash("md4")
254
+ .update(sourceMapString)
255
+ .digest("hex")
256
+ });
257
+ const sourceMapUrl = options.publicPath
258
+ ? options.publicPath + sourceMapFile.replace(/\\/g, "/")
259
+ : path
260
+ .relative(path.dirname(file), sourceMapFile)
261
+ .replace(/\\/g, "/");
262
+ if (currentSourceMappingURLComment !== false) {
263
+ asset.__SourceMapDevToolData[file] = compilation.assets[
264
+ file
265
+ ] = new ConcatSource(
266
+ new RawSource(source),
267
+ currentSourceMappingURLComment.replace(
268
+ /\[url\]/g,
269
+ sourceMapUrl
270
+ )
271
+ );
272
+ }
273
+ asset.__SourceMapDevToolData[sourceMapFile] = compilation.assets[
274
+ sourceMapFile
275
+ ] = new RawSource(sourceMapString);
276
+ chunk.files.push(sourceMapFile);
277
+ } else {
278
+ asset.__SourceMapDevToolData[file] = compilation.assets[
279
+ file
280
+ ] = new ConcatSource(
281
+ new RawSource(source),
282
+ currentSourceMappingURLComment
283
+ .replace(/\[map\]/g, () => sourceMapString)
284
+ .replace(
285
+ /\[url\]/g,
286
+ () =>
287
+ `data:application/json;charset=utf-8;base64,${Buffer.from(
288
+ sourceMapString,
289
+ "utf-8"
290
+ ).toString("base64")}`
291
+ )
292
+ );
293
+ }
294
+ });
295
+ reportProgress(1.0);
296
+ }
297
+ );
298
+ });
299
+ }
300
+ }
301
+
302
+ module.exports = SourceMapDevToolPlugin;