webpack 4.13.0 → 4.16.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 (93) hide show
  1. package/bin/webpack.js +7 -2
  2. package/hot/dev-server.js +2 -2
  3. package/hot/only-dev-server.js +2 -2
  4. package/hot/poll.js +5 -2
  5. package/hot/signal.js +2 -2
  6. package/lib/AsyncDependenciesBlock.js +44 -0
  7. package/lib/AutomaticPrefetchPlugin.js +2 -2
  8. package/lib/Chunk.js +56 -6
  9. package/lib/ChunkGroup.js +2 -2
  10. package/lib/ChunkTemplate.js +14 -2
  11. package/lib/CommentCompilationWarning.js +3 -3
  12. package/lib/CompatibilityPlugin.js +1 -1
  13. package/lib/Compilation.js +494 -36
  14. package/lib/Compiler.js +57 -4
  15. package/lib/ContextModule.js +23 -16
  16. package/lib/DelegatedModule.js +9 -1
  17. package/lib/DelegatedModuleFactoryPlugin.js +7 -1
  18. package/lib/DependenciesBlock.js +36 -3
  19. package/lib/DependenciesBlockVariable.js +22 -0
  20. package/lib/Dependency.js +33 -6
  21. package/lib/DllEntryPlugin.js +4 -1
  22. package/lib/DynamicEntryPlugin.js +21 -1
  23. package/lib/EntryOptionPlugin.js +12 -0
  24. package/lib/Entrypoint.js +1 -1
  25. package/lib/EnvironmentPlugin.js +8 -1
  26. package/lib/ExtendedAPIPlugin.js +8 -4
  27. package/lib/ExternalModuleFactoryPlugin.js +1 -1
  28. package/lib/FlagDependencyUsagePlugin.js +18 -13
  29. package/lib/Generator.js +1 -1
  30. package/lib/GraphHelpers.js +2 -1
  31. package/lib/HotModuleReplacement.runtime.js +8 -5
  32. package/lib/HotModuleReplacementPlugin.js +115 -117
  33. package/lib/IgnorePlugin.js +1 -1
  34. package/lib/MainTemplate.js +19 -4
  35. package/lib/Module.js +9 -3
  36. package/lib/ModuleReason.js +8 -0
  37. package/lib/MultiEntryPlugin.js +25 -3
  38. package/lib/NormalModule.js +5 -23
  39. package/lib/RuleSet.js +3 -3
  40. package/lib/RuntimeTemplate.js +4 -0
  41. package/lib/SingleEntryPlugin.js +20 -1
  42. package/lib/Stats.js +12 -5
  43. package/lib/Template.js +4 -1
  44. package/lib/UmdMainTemplatePlugin.js +12 -12
  45. package/lib/UseStrictPlugin.js +1 -1
  46. package/lib/WebpackError.js +4 -0
  47. package/lib/WebpackOptionsApply.js +92 -10
  48. package/lib/WebpackOptionsDefaulter.js +23 -6
  49. package/lib/WebpackOptionsValidationError.js +0 -1
  50. package/lib/compareLocations.js +13 -17
  51. package/lib/debug/ProfilingPlugin.js +5 -7
  52. package/lib/dependencies/AMDDefineDependencyParserPlugin.js +4 -6
  53. package/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js +0 -2
  54. package/lib/dependencies/DependencyReference.js +4 -0
  55. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +18 -8
  56. package/lib/dependencies/LoaderDependency.js +3 -0
  57. package/lib/dependencies/LoaderPlugin.js +21 -2
  58. package/lib/dependencies/ModuleDependency.js +3 -0
  59. package/lib/dependencies/MultiEntryDependency.js +5 -0
  60. package/lib/dependencies/SingleEntryDependency.js +3 -0
  61. package/lib/dependencies/SystemPlugin.js +1 -1
  62. package/lib/formatLocation.js +55 -41
  63. package/lib/node/NodeMainTemplateAsync.runtime.js +1 -1
  64. package/lib/node/NodeMainTemplatePlugin.js +2 -2
  65. package/lib/node/NodeSourcePlugin.js +1 -1
  66. package/lib/optimize/ConcatenatedModule.js +24 -8
  67. package/lib/optimize/ModuleConcatenationPlugin.js +29 -14
  68. package/lib/optimize/NaturalChunkOrderPlugin.js +41 -0
  69. package/lib/optimize/OccurrenceChunkOrderPlugin.js +61 -0
  70. package/lib/optimize/OccurrenceModuleOrderPlugin.js +103 -0
  71. package/lib/optimize/OccurrenceOrderPlugin.js +2 -0
  72. package/lib/optimize/SplitChunksPlugin.js +168 -18
  73. package/lib/util/Semaphore.js +12 -0
  74. package/lib/util/SetHelpers.js +4 -4
  75. package/lib/util/SortableSet.js +1 -1
  76. package/lib/util/cachedMerge.js +1 -1
  77. package/lib/util/createHash.js +15 -0
  78. package/lib/util/deterministicGrouping.js +251 -0
  79. package/lib/util/identifier.js +27 -0
  80. package/lib/wasm/WasmFinalizeExportsPlugin.js +5 -2
  81. package/lib/wasm/WasmMainTemplatePlugin.js +10 -4
  82. package/lib/wasm/WebAssemblyGenerator.js +12 -12
  83. package/lib/wasm/WebAssemblyInInitialChunkError.js +88 -0
  84. package/lib/wasm/WebAssemblyModulesPlugin.js +28 -0
  85. package/lib/web/JsonpMainTemplatePlugin.js +1 -1
  86. package/lib/web/WebEnvironmentPlugin.js +18 -18
  87. package/lib/webpack.js +7 -0
  88. package/lib/webpack.web.js +2 -2
  89. package/lib/webworker/WebWorkerMainTemplatePlugin.js +1 -1
  90. package/package.json +21 -11
  91. package/schemas/WebpackOptions.json +70 -4
  92. package/schemas/plugins/optimize/OccurrenceOrderChunkIdsPlugin.json +10 -0
  93. package/schemas/plugins/optimize/OccurrenceOrderModuleIdsPlugin.json +10 -0
@@ -265,6 +265,10 @@ module.exports = class RuntimeTemplate {
265
265
 
266
266
  if (exportName) {
267
267
  const used = module.isUsed(exportName);
268
+ if (!used) {
269
+ const comment = Template.toNormalComment(`unused export ${exportName}`);
270
+ return `${comment} undefined`;
271
+ }
268
272
  const comment =
269
273
  used !== exportName ? Template.toNormalComment(exportName) + " " : "";
270
274
  const access = `${importVar}[${comment}${JSON.stringify(used)}]`;
@@ -5,13 +5,27 @@
5
5
  "use strict";
6
6
  const SingleEntryDependency = require("./dependencies/SingleEntryDependency");
7
7
 
8
+ /** @typedef {import("./Compiler")} Compiler */
9
+
8
10
  class SingleEntryPlugin {
11
+ /**
12
+ * An entry plugin which will handle
13
+ * creation of the SingleEntryDependency
14
+ *
15
+ * @param {string} context context path
16
+ * @param {string} entry entry path
17
+ * @param {string} name entry key name
18
+ */
9
19
  constructor(context, entry, name) {
10
20
  this.context = context;
11
21
  this.entry = entry;
12
22
  this.name = name;
13
23
  }
14
24
 
25
+ /**
26
+ * @param {Compiler} compiler the compiler instance
27
+ * @returns {void}
28
+ */
15
29
  apply(compiler) {
16
30
  compiler.hooks.compilation.tap(
17
31
  "SingleEntryPlugin",
@@ -34,9 +48,14 @@ class SingleEntryPlugin {
34
48
  );
35
49
  }
36
50
 
51
+ /**
52
+ * @param {string} entry entry request
53
+ * @param {string} name entry name
54
+ * @returns {SingleEntryDependency} the dependency
55
+ */
37
56
  static createDependency(entry, name) {
38
57
  const dep = new SingleEntryDependency(entry);
39
- dep.loc = name;
58
+ dep.loc = { name };
40
59
  return dep;
41
60
  }
42
61
  }
package/lib/Stats.js CHANGED
@@ -15,6 +15,12 @@ const optionsOrFallback = (...args) => {
15
15
  return optionValues.find(optionValue => typeof optionValue !== "undefined");
16
16
  };
17
17
 
18
+ const compareId = (a, b) => {
19
+ if (a < b) return -1;
20
+ if (a > b) return 1;
21
+ return 0;
22
+ };
23
+
18
24
  class Stats {
19
25
  constructor(compilation) {
20
26
  this.compilation = compilation;
@@ -108,10 +114,11 @@ class Stats {
108
114
  if (typeof item === "string") {
109
115
  const regExp = new RegExp(
110
116
  `[\\\\/]${item.replace(
117
+ // eslint-disable-next-line no-useless-escape
111
118
  /[-[\]{}()*+?.\\^$|]/g,
112
119
  "\\$&"
113
120
  )}([\\\\/]|$|!|\\?)`
114
- ); // eslint-disable-line no-useless-escape
121
+ );
115
122
  return ident => regExp.test(ident);
116
123
  }
117
124
  if (item && typeof item === "object" && typeof item.test === "function") {
@@ -542,7 +549,7 @@ class Stats {
542
549
  }
543
550
  return obj;
544
551
  })
545
- .sort((a, b) => a.moduleId - b.moduleId);
552
+ .sort(compareId);
546
553
  }
547
554
  if (showUsedExports) {
548
555
  if (module.used === true) {
@@ -613,9 +620,9 @@ class Stats {
613
620
  names: chunk.name ? [chunk.name] : [],
614
621
  files: chunk.files.slice(),
615
622
  hash: chunk.renderedHash,
616
- siblings: Array.from(siblings).sort(),
617
- parents: Array.from(parents).sort(),
618
- children: Array.from(children).sort(),
623
+ siblings: Array.from(siblings).sort(compareId),
624
+ parents: Array.from(parents).sort(compareId),
625
+ children: Array.from(children).sort(compareId),
619
626
  childrenByOrder: childIdByOrder
620
627
  };
621
628
  if (showChunkModules) {
package/lib/Template.js CHANGED
@@ -22,6 +22,8 @@ const COMMENT_END_REGEX = /\*\//g;
22
22
  const PATH_NAME_NORMALIZE_REPLACE_REGEX = /[^a-zA-Z0-9_!§$()=\-^°]+/g;
23
23
  const MATCH_PADDED_HYPHENS_REPLACE_REGEX = /^-|-$/g;
24
24
 
25
+ /** @typedef {import("webpack-sources").Source} Source */
26
+
25
27
  /**
26
28
  * @typedef {Object} HasId
27
29
  * @property {number | string} id
@@ -230,6 +232,7 @@ class Template {
230
232
  source.add("[]");
231
233
  return source;
232
234
  }
235
+ /** @type {{id: string|number, source: Source|string}[]} */
233
236
  var allModules = modules.map(module => {
234
237
  return {
235
238
  id: module.id,
@@ -241,7 +244,7 @@ class Template {
241
244
  if (removedModules && removedModules.length > 0) {
242
245
  for (const id of removedModules) {
243
246
  allModules.push({
244
- id: id,
247
+ id,
245
248
  source: "false"
246
249
  });
247
250
  }
@@ -107,14 +107,14 @@ class UmdMainTemplatePlugin {
107
107
  requiredExternals = externals;
108
108
  }
109
109
 
110
- function replaceKeys(str) {
110
+ const replaceKeys = str => {
111
111
  return mainTemplate.getAssetPath(str, {
112
112
  hash,
113
113
  chunk
114
114
  });
115
- }
115
+ };
116
116
 
117
- function externalsDepsArray(modules) {
117
+ const externalsDepsArray = modules => {
118
118
  return `[${replaceKeys(
119
119
  modules
120
120
  .map(m =>
@@ -124,9 +124,9 @@ class UmdMainTemplatePlugin {
124
124
  )
125
125
  .join(", ")
126
126
  )}]`;
127
- }
127
+ };
128
128
 
129
- function externalsRootArray(modules) {
129
+ const externalsRootArray = modules => {
130
130
  return replaceKeys(
131
131
  modules
132
132
  .map(m => {
@@ -136,9 +136,9 @@ class UmdMainTemplatePlugin {
136
136
  })
137
137
  .join(", ")
138
138
  );
139
- }
139
+ };
140
140
 
141
- function externalsRequireArray(type) {
141
+ const externalsRequireArray = type => {
142
142
  return replaceKeys(
143
143
  externals
144
144
  .map(m => {
@@ -166,20 +166,20 @@ class UmdMainTemplatePlugin {
166
166
  })
167
167
  .join(", ")
168
168
  );
169
- }
169
+ };
170
170
 
171
- function externalsArguments(modules) {
171
+ const externalsArguments = modules => {
172
172
  return modules
173
173
  .map(
174
174
  m =>
175
175
  `__WEBPACK_EXTERNAL_MODULE_${Template.toIdentifier(`${m.id}`)}__`
176
176
  )
177
177
  .join(", ");
178
- }
178
+ };
179
179
 
180
- function libraryName(library) {
180
+ const libraryName = library => {
181
181
  return JSON.stringify(replaceKeys([].concat(library).pop()));
182
- }
182
+ };
183
183
 
184
184
  let amdFactory;
185
185
  if (optionalExternals.length > 0) {
@@ -6,7 +6,7 @@
6
6
 
7
7
  const ConstDependency = require("./dependencies/ConstDependency");
8
8
 
9
- /** @typedef {import("./Compiler.js")} Compiler */
9
+ /** @typedef {import("./Compiler")} Compiler */
10
10
 
11
11
  class UseStrictPlugin {
12
12
  /**
@@ -13,6 +13,10 @@ class WebpackError extends Error {
13
13
  super(message);
14
14
 
15
15
  this.details = undefined;
16
+ this.missing = undefined;
17
+ this.origin = undefined;
18
+ this.dependencies = undefined;
19
+ this.module = undefined;
16
20
 
17
21
  Error.captureStackTrace(this, this.constructor);
18
22
  }
@@ -46,7 +46,9 @@ const RemoveParentModulesPlugin = require("./optimize/RemoveParentModulesPlugin"
46
46
  const RemoveEmptyChunksPlugin = require("./optimize/RemoveEmptyChunksPlugin");
47
47
  const MergeDuplicateChunksPlugin = require("./optimize/MergeDuplicateChunksPlugin");
48
48
  const FlagIncludedChunksPlugin = require("./optimize/FlagIncludedChunksPlugin");
49
- const OccurrenceOrderPlugin = require("./optimize/OccurrenceOrderPlugin");
49
+ const OccurrenceChunkOrderPlugin = require("./optimize/OccurrenceChunkOrderPlugin");
50
+ const OccurrenceModuleOrderPlugin = require("./optimize/OccurrenceModuleOrderPlugin");
51
+ const NaturalChunkOrderPlugin = require("./optimize/NaturalChunkOrderPlugin");
50
52
  const SideEffectsFlagPlugin = require("./optimize/SideEffectsFlagPlugin");
51
53
  const FlagDependencyUsagePlugin = require("./FlagDependencyUsagePlugin");
52
54
  const FlagDependencyExportsPlugin = require("./FlagDependencyExportsPlugin");
@@ -171,9 +173,13 @@ class WebpackOptionsApply extends OptionsApply {
171
173
  break;
172
174
  case "electron-renderer":
173
175
  JsonpTemplatePlugin = require("./web/JsonpTemplatePlugin");
176
+ FetchCompileWasmTemplatePlugin = require("./web/FetchCompileWasmTemplatePlugin");
174
177
  NodeTargetPlugin = require("./node/NodeTargetPlugin");
175
178
  ExternalsPlugin = require("./ExternalsPlugin");
176
179
  new JsonpTemplatePlugin().apply(compiler);
180
+ new FetchCompileWasmTemplatePlugin({
181
+ mangleImports: options.optimization.mangleWasmImports
182
+ }).apply(compiler);
177
183
  new FunctionModulePlugin().apply(compiler);
178
184
  new NodeTargetPlugin().apply(compiler);
179
185
  new ExternalsPlugin("commonjs", [
@@ -325,9 +331,6 @@ class WebpackOptionsApply extends OptionsApply {
325
331
  if (options.optimization.flagIncludedChunks) {
326
332
  new FlagIncludedChunksPlugin().apply(compiler);
327
333
  }
328
- if (options.optimization.occurrenceOrder) {
329
- new OccurrenceOrderPlugin(true).apply(compiler);
330
- }
331
334
  if (options.optimization.sideEffects) {
332
335
  new SideEffectsFlagPlugin().apply(compiler);
333
336
  }
@@ -352,14 +355,93 @@ class WebpackOptionsApply extends OptionsApply {
352
355
  if (options.optimization.checkWasmTypes) {
353
356
  new WasmFinalizeExportsPlugin().apply(compiler);
354
357
  }
355
- if (options.optimization.namedModules) {
356
- new NamedModulesPlugin().apply(compiler);
358
+ let moduleIds = options.optimization.moduleIds;
359
+ if (moduleIds === undefined) {
360
+ // TODO webpack 5 remove all these options
361
+ if (options.optimization.occurrenceOrder) {
362
+ moduleIds = "size";
363
+ }
364
+ if (options.optimization.namedModules) {
365
+ moduleIds = "named";
366
+ }
367
+ if (options.optimization.hashedModuleIds) {
368
+ moduleIds = "hashed";
369
+ }
370
+ if (moduleIds === undefined) {
371
+ moduleIds = "natural";
372
+ }
357
373
  }
358
- if (options.optimization.hashedModuleIds) {
359
- new HashedModuleIdsPlugin().apply(compiler);
374
+ if (moduleIds) {
375
+ switch (moduleIds) {
376
+ case "natural":
377
+ // TODO webpack 5: see hint in Compilation.sortModules
378
+ break;
379
+ case "named":
380
+ new NamedModulesPlugin().apply(compiler);
381
+ break;
382
+ case "hashed":
383
+ new HashedModuleIdsPlugin().apply(compiler);
384
+ break;
385
+ case "size":
386
+ new OccurrenceModuleOrderPlugin({
387
+ prioritiseInitial: true
388
+ }).apply(compiler);
389
+ break;
390
+ case "total-size":
391
+ new OccurrenceModuleOrderPlugin({
392
+ prioritiseInitial: false
393
+ }).apply(compiler);
394
+ break;
395
+ default:
396
+ throw new Error(
397
+ `webpack bug: moduleIds: ${moduleIds} is not implemented`
398
+ );
399
+ }
400
+ }
401
+ let chunkIds = options.optimization.chunkIds;
402
+ if (chunkIds === undefined) {
403
+ // TODO webpack 5 remove all these options
404
+ if (options.optimization.occurrenceOrder) {
405
+ // This looks weird but it's for backward-compat
406
+ // This bug already existed before adding this feature
407
+ chunkIds = "total-size";
408
+ }
409
+ if (options.optimization.namedChunks) {
410
+ chunkIds = "named";
411
+ }
412
+ if (chunkIds === undefined) {
413
+ chunkIds = "natural";
414
+ }
360
415
  }
361
- if (options.optimization.namedChunks) {
362
- new NamedChunksPlugin().apply(compiler);
416
+ if (chunkIds) {
417
+ switch (chunkIds) {
418
+ case "natural":
419
+ new NaturalChunkOrderPlugin().apply(compiler);
420
+ break;
421
+ case "named":
422
+ // TODO webapck 5: for backward-compat this need to have OccurrenceChunkOrderPlugin too
423
+ // The NamedChunksPlugin doesn't give every chunk a name
424
+ // This should be fixed, and the OccurrenceChunkOrderPlugin should be removed here.
425
+ new OccurrenceChunkOrderPlugin({
426
+ prioritiseInitial: false
427
+ }).apply(compiler);
428
+ new NamedChunksPlugin().apply(compiler);
429
+ break;
430
+ case "size":
431
+ new OccurrenceChunkOrderPlugin({
432
+ prioritiseInitial: true
433
+ }).apply(compiler);
434
+ break;
435
+ case "total-size":
436
+ new OccurrenceChunkOrderPlugin({
437
+ prioritiseInitial: false
438
+ }).apply(compiler);
439
+ break;
440
+ default:
441
+ throw new Error(
442
+ `webpack bug: chunkIds: ${chunkIds} is not implemented`
443
+ );
444
+ }
363
445
  }
364
446
  if (options.optimization.nodeEnv) {
365
447
  new DefinePlugin({
@@ -17,6 +17,16 @@ const isWebLikeTarget = options => {
17
17
  return options.target === "web" || options.target === "webworker";
18
18
  };
19
19
 
20
+ const getDevtoolNamespace = library => {
21
+ // if options.output.library is a string
22
+ if (Array.isArray(library)) {
23
+ return library.join(".");
24
+ } else if (typeof library === "object") {
25
+ return getDevtoolNamespace(library.root);
26
+ }
27
+ return library || "";
28
+ };
29
+
20
30
  class WebpackOptionsDefaulter extends OptionsDefaulter {
21
31
  constructor() {
22
32
  super();
@@ -136,12 +146,7 @@ class WebpackOptionsDefaulter extends OptionsDefaulter {
136
146
  }
137
147
  });
138
148
  this.set("output.devtoolNamespace", "make", options => {
139
- if (Array.isArray(options.output.library)) {
140
- return options.output.library.join(".");
141
- } else if (typeof options.output.library === "object") {
142
- return options.output.library.root || "";
143
- }
144
- return options.output.library || "";
149
+ return getDevtoolNamespace(options.output.library);
145
150
  });
146
151
  this.set("output.libraryTarget", "var");
147
152
  this.set("output.path", path.join(process.cwd(), "dist"));
@@ -201,6 +206,9 @@ class WebpackOptionsDefaulter extends OptionsDefaulter {
201
206
  this.set("optimization.flagIncludedChunks", "make", options =>
202
207
  isProductionLikeMode(options)
203
208
  );
209
+ // TODO webpack 5 add `moduleIds: "named"` default for development
210
+ // TODO webpack 5 add `moduleIds: "size"` default for production
211
+ // TODO webpack 5 remove optimization.occurrenceOrder
204
212
  this.set("optimization.occurrenceOrder", "make", options =>
205
213
  isProductionLikeMode(options)
206
214
  );
@@ -215,6 +223,9 @@ class WebpackOptionsDefaulter extends OptionsDefaulter {
215
223
  isProductionLikeMode(options)
216
224
  );
217
225
  this.set("optimization.splitChunks", {});
226
+ this.set("optimization.splitChunks.hidePathInfo", "make", options => {
227
+ return isProductionLikeMode(options);
228
+ });
218
229
  this.set("optimization.splitChunks.chunks", "async");
219
230
  this.set("optimization.splitChunks.minSize", "make", options => {
220
231
  return isProductionLikeMode(options) ? 30000 : 10000;
@@ -230,11 +241,13 @@ class WebpackOptionsDefaulter extends OptionsDefaulter {
230
241
  this.set("optimization.splitChunks.name", true);
231
242
  this.set("optimization.splitChunks.cacheGroups", {});
232
243
  this.set("optimization.splitChunks.cacheGroups.default", {
244
+ automaticNamePrefix: "",
233
245
  reuseExistingChunk: true,
234
246
  minChunks: 2,
235
247
  priority: -20
236
248
  });
237
249
  this.set("optimization.splitChunks.cacheGroups.vendors", {
250
+ automaticNamePrefix: "vendors",
238
251
  test: /[\\/]node_modules[\\/]/,
239
252
  priority: -10
240
253
  });
@@ -258,12 +271,16 @@ class WebpackOptionsDefaulter extends OptionsDefaulter {
258
271
  isProductionLikeMode(options)
259
272
  );
260
273
  this.set("optimization.mangleWasmImports", false);
274
+ // TODO webpack 5 remove optimization.namedModules
261
275
  this.set(
262
276
  "optimization.namedModules",
263
277
  "make",
264
278
  options => options.mode === "development"
265
279
  );
266
280
  this.set("optimization.hashedModuleIds", false);
281
+ // TODO webpack 5 add `chunkIds: "named"` default for development
282
+ // TODO webpack 5 add `chunkIds: "size"` default for production
283
+ // TODO webpack 5 remove optimization.namedChunks
267
284
  this.set(
268
285
  "optimization.namedChunks",
269
286
  "make",
@@ -332,7 +332,6 @@ class WebpackOptionsValidationError extends WebpackError {
332
332
  }
333
333
  return baseMessage;
334
334
  } else {
335
- // eslint-disable-line no-fallthrough
336
335
  return `${dataPath} ${err.message} (${JSON.stringify(
337
336
  err,
338
337
  null,
@@ -4,23 +4,13 @@
4
4
  */
5
5
  "use strict";
6
6
 
7
- /**
8
- * @typedef {Object} LineAndColumn
9
- * @property {number=} line
10
- * @property {number=} column
11
- */
12
-
13
- /**
14
- * @typedef {Object} NodeLocation
15
- * @property {LineAndColumn=} start
16
- * @property {LineAndColumn=} end
17
- * @property {number=} index
18
- */
7
+ /** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */
19
8
 
9
+ // TODO webpack 5 remove string type from a and b
20
10
  /**
21
11
  * Compare two locations
22
- * @param {string|NodeLocation} a A location node
23
- * @param {string|NodeLocation} b A location node
12
+ * @param {string|DependencyLocation} a A location node
13
+ * @param {string|DependencyLocation} b A location node
24
14
  * @returns {-1|0|1} sorting comparator value
25
15
  */
26
16
  module.exports = (a, b) => {
@@ -38,7 +28,7 @@ module.exports = (a, b) => {
38
28
  if (typeof b === "string") {
39
29
  return -1;
40
30
  } else if (typeof b === "object") {
41
- if (a.start && b.start) {
31
+ if ("start" in a && "start" in b) {
42
32
  const ap = a.start;
43
33
  const bp = b.start;
44
34
  if (ap.line < bp.line) return -1;
@@ -46,8 +36,14 @@ module.exports = (a, b) => {
46
36
  if (ap.column < bp.column) return -1;
47
37
  if (ap.column > bp.column) return 1;
48
38
  }
49
- if (a.index < b.index) return -1;
50
- if (a.index > b.index) return 1;
39
+ if ("name" in a && "name" in b) {
40
+ if (a.name < b.name) return -1;
41
+ if (a.name > b.name) return 1;
42
+ }
43
+ if ("index" in a && "index" in b) {
44
+ if (a.index < b.index) return -1;
45
+ if (a.index > b.index) return 1;
46
+ }
51
47
  return 0;
52
48
  } else {
53
49
  return 0;
@@ -5,7 +5,8 @@ const schema = require("../../schemas/plugins/debug/ProfilingPlugin.json");
5
5
  let inspector = undefined;
6
6
 
7
7
  try {
8
- inspector = require("inspector"); // eslint-disable-line node/no-missing-require
8
+ // eslint-disable-next-line node/no-missing-require
9
+ inspector = require("inspector");
9
10
  } catch (e) {
10
11
  console.log("Unable to CPU profile in < node 8.0");
11
12
  }
@@ -84,7 +85,7 @@ class Profiler {
84
85
  * @param {string} outputPath The location where to write the log.
85
86
  * @returns {Trace} The trace object
86
87
  */
87
- function createTrace(outputPath) {
88
+ const createTrace = outputPath => {
88
89
  const trace = new Tracer({
89
90
  noStream: true
90
91
  });
@@ -136,10 +137,10 @@ function createTrace(outputPath) {
136
137
  callback();
137
138
  });
138
139
  // Tear down the readable trace stream.
139
- trace.destroy();
140
+ trace.push(null);
140
141
  }
141
142
  };
142
- }
143
+ };
143
144
 
144
145
  const pluginName = "ProfilingPlugin";
145
146
 
@@ -354,7 +355,6 @@ const makeNewProfiledTapFn = (hookName, tracer, { name, type, fn }) => {
354
355
  switch (type) {
355
356
  case "promise":
356
357
  return (...args) => {
357
- // eslint-disable-line
358
358
  const id = ++tracer.counter;
359
359
  tracer.trace.begin({
360
360
  name,
@@ -373,7 +373,6 @@ const makeNewProfiledTapFn = (hookName, tracer, { name, type, fn }) => {
373
373
  };
374
374
  case "async":
375
375
  return (...args) => {
376
- // eslint-disable-line
377
376
  const id = ++tracer.counter;
378
377
  tracer.trace.begin({
379
378
  name,
@@ -392,7 +391,6 @@ const makeNewProfiledTapFn = (hookName, tracer, { name, type, fn }) => {
392
391
  };
393
392
  case "sync":
394
393
  return (...args) => {
395
- // eslint-disable-line
396
394
  const id = ++tracer.counter;
397
395
  // Do not instrument ourself due to the CPU
398
396
  // profile needing to be the last event in the trace.
@@ -23,17 +23,17 @@ const isBoundFunctionExpression = expr => {
23
23
  return true;
24
24
  };
25
25
 
26
- function isUnboundFunctionExpression(expr) {
26
+ const isUnboundFunctionExpression = expr => {
27
27
  if (expr.type === "FunctionExpression") return true;
28
28
  if (expr.type === "ArrowFunctionExpression") return true;
29
29
  return false;
30
- }
30
+ };
31
31
 
32
- function isCallable(expr) {
32
+ const isCallable = expr => {
33
33
  if (isUnboundFunctionExpression(expr)) return true;
34
34
  if (isBoundFunctionExpression(expr)) return true;
35
35
  return false;
36
- }
36
+ };
37
37
 
38
38
  class AMDDefineDependencyParserPlugin {
39
39
  constructor(options) {
@@ -80,7 +80,6 @@ class AMDDefineDependencyParserPlugin {
80
80
  request
81
81
  ))
82
82
  ) {
83
- // eslint-disable-line no-cond-assign
84
83
  dep = new LocalModuleDependency(localModule, undefined, false);
85
84
  dep.loc = expr.loc;
86
85
  parser.state.current.addDependency(dep);
@@ -121,7 +120,6 @@ class AMDDefineDependencyParserPlugin {
121
120
  namedModule
122
121
  ))
123
122
  ) {
124
- // eslint-disable-line no-cond-assign
125
123
  dep = new LocalModuleDependency(localModule, param.range, false);
126
124
  } else {
127
125
  dep = this.newRequireItemDependency(param.string, param.range);
@@ -79,7 +79,6 @@ class AMDRequireDependenciesBlockParserPlugin {
79
79
  request
80
80
  ))
81
81
  ) {
82
- // eslint-disable-line no-cond-assign
83
82
  dep = new LocalModuleDependency(localModule, undefined, false);
84
83
  dep.loc = expr.loc;
85
84
  parser.state.current.addDependency(dep);
@@ -127,7 +126,6 @@ class AMDRequireDependenciesBlockParserPlugin {
127
126
  param.string
128
127
  ))
129
128
  ) {
130
- // eslint-disable-line no-cond-assign
131
129
  dep = new LocalModuleDependency(localModule, param.range, false);
132
130
  } else {
133
131
  dep = this.newRequireItemDependency(param.string, param.range);
@@ -7,6 +7,9 @@
7
7
  /** @typedef {import("../Module")} Module */
8
8
 
9
9
  class DependencyReference {
10
+ // TODO webpack 5: module must be dynamic, you must pass a function returning a module
11
+ // This is needed to remove the hack in ConcatenatedModule
12
+ // The problem is that the `module` in Dependency could be replaced i. e. because of Scope Hoisting
10
13
  /**
11
14
  *
12
15
  * @param {Module} module the referenced module
@@ -15,6 +18,7 @@ class DependencyReference {
15
18
  * @param {number} order the order information or NaN if don't care
16
19
  */
17
20
  constructor(module, importedNames, weak = false, order = NaN) {
21
+ // TODO webpack 5: make it a getter
18
22
  this.module = module;
19
23
  // true: full object
20
24
  // false: only sideeffects/no export
@@ -244,7 +244,8 @@ class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {
244
244
  return new DependencyReference(
245
245
  mode.module,
246
246
  Array.from(mode.map.values()),
247
- false
247
+ false,
248
+ this.sourceOrder
248
249
  );
249
250
 
250
251
  case "dynamic-reexport":
@@ -602,10 +603,10 @@ HarmonyExportImportedSpecifierDependency.Template = class HarmonyExportImportedS
602
603
 
603
604
  getReexportStatement(module, key, name, valueKey) {
604
605
  const exportsName = module.exportsArgument;
605
- const returnValue = this.getReturnValue(valueKey);
606
+ const returnValue = this.getReturnValue(name, valueKey);
606
607
  return `__webpack_require__.d(${exportsName}, ${JSON.stringify(
607
608
  key
608
- )}, function() { return ${name}${returnValue}; });\n`;
609
+ )}, function() { return ${returnValue}; });\n`;
609
610
  }
610
611
 
611
612
  getReexportFakeNamespaceObjectStatement(module, key, name) {
@@ -616,20 +617,29 @@ HarmonyExportImportedSpecifierDependency.Template = class HarmonyExportImportedS
616
617
  }
617
618
 
618
619
  getConditionalReexportStatement(module, key, name, valueKey) {
620
+ if (valueKey === false) {
621
+ return "/* unused export */\n";
622
+ }
619
623
  const exportsName = module.exportsArgument;
620
- const returnValue = this.getReturnValue(valueKey);
624
+ const returnValue = this.getReturnValue(name, valueKey);
621
625
  return `if(__webpack_require__.o(${name}, ${JSON.stringify(
622
626
  valueKey
623
627
  )})) __webpack_require__.d(${exportsName}, ${JSON.stringify(
624
628
  key
625
- )}, function() { return ${name}${returnValue}; });\n`;
629
+ )}, function() { return ${returnValue}; });\n`;
626
630
  }
627
631
 
628
- getReturnValue(valueKey) {
632
+ getReturnValue(name, valueKey) {
629
633
  if (valueKey === null) {
630
- return "_default.a";
634
+ return `${name}_default.a`;
635
+ }
636
+ if (valueKey === "") {
637
+ return name;
638
+ }
639
+ if (valueKey === false) {
640
+ return "/* unused export */ undefined";
631
641
  }
632
642
 
633
- return valueKey && "[" + JSON.stringify(valueKey) + "]";
643
+ return `${name}[${JSON.stringify(valueKey)}]`;
634
644
  }
635
645
  };