webpack 5.20.1 → 5.21.2

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.

Potentially problematic release.


This version of webpack might be problematic. Click here for more details.

package/bin/webpack.js CHANGED
File without changes
package/lib/APIPlugin.js CHANGED
@@ -33,6 +33,12 @@ const REPLACEMENTS = {
33
33
  type: "string",
34
34
  assign: true
35
35
  },
36
+ __webpack_base_uri__: {
37
+ expr: RuntimeGlobals.baseURI,
38
+ req: [RuntimeGlobals.baseURI],
39
+ type: "string",
40
+ assign: true
41
+ },
36
42
  __webpack_modules__: {
37
43
  expr: RuntimeGlobals.moduleFactories,
38
44
  req: [RuntimeGlobals.moduleFactories],
@@ -1872,6 +1872,7 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
1872
1872
  });
1873
1873
  }
1874
1874
 
1875
+ this.processDependenciesQueue.invalidate(module);
1875
1876
  this.processModuleDependencies(module, err => {
1876
1877
  if (err) return callback(err);
1877
1878
  this.removeReasonsOfDependencyBlock(module, {
package/lib/Generator.js CHANGED
@@ -28,6 +28,7 @@
28
28
  * @property {RuntimeSpec} runtime the runtime
29
29
  * @property {ConcatenationScope=} concatenationScope when in concatenated module, information about other concatenated modules
30
30
  * @property {string} type which kind of code should be generated
31
+ * @property {function(): Map<string, any>=} getData get access to the code generation data
31
32
  */
32
33
 
33
34
  /**
@@ -21,12 +21,21 @@ class ModuleDependencyError extends WebpackError {
21
21
  super(err.message);
22
22
 
23
23
  this.name = "ModuleDependencyError";
24
- this.details = err.stack.split("\n").slice(1).join("\n");
24
+ this.details =
25
+ err && !(/** @type {any} */ (err).hideStack)
26
+ ? err.stack.split("\n").slice(1).join("\n")
27
+ : undefined;
25
28
  this.module = module;
26
29
  this.loc = loc;
30
+ /** error is not (de)serialized, so it might be undefined after deserialization */
27
31
  this.error = err;
28
32
 
29
33
  Error.captureStackTrace(this, this.constructor);
34
+
35
+ if (err && /** @type {any} */ (err).hideStack) {
36
+ this.stack =
37
+ err.stack.split("\n").slice(1).join("\n") + "\n\n" + this.stack;
38
+ }
30
39
  }
31
40
  }
32
41
 
@@ -21,13 +21,21 @@ class ModuleDependencyWarning extends WebpackError {
21
21
  super(err ? err.message : "");
22
22
 
23
23
  this.name = "ModuleDependencyWarning";
24
- this.details = err && err.stack.split("\n").slice(1).join("\n");
24
+ this.details =
25
+ err && !(/** @type {any} */ (err).hideStack)
26
+ ? err.stack.split("\n").slice(1).join("\n")
27
+ : undefined;
25
28
  this.module = module;
26
29
  this.loc = loc;
27
30
  /** error is not (de)serialized, so it might be undefined after deserialization */
28
31
  this.error = err;
29
32
 
30
33
  Error.captureStackTrace(this, this.constructor);
34
+
35
+ if (err && /** @type {any} */ (err).hideStack) {
36
+ this.stack =
37
+ err.stack.split("\n").slice(1).join("\n") + "\n\n" + this.stack;
38
+ }
31
39
  }
32
40
  }
33
41
 
package/lib/MultiStats.js CHANGED
@@ -7,7 +7,10 @@
7
7
 
8
8
  const identifierUtils = require("./util/identifier");
9
9
 
10
+ /** @typedef {import("../declarations/WebpackOptions").StatsOptions} StatsOptions */
10
11
  /** @typedef {import("./Stats")} Stats */
12
+ /** @typedef {import("./stats/DefaultStatsFactoryPlugin").KnownStatsCompilation} KnownStatsCompilation */
13
+ /** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsCompilation} StatsCompilation */
11
14
 
12
15
  const indent = (str, prefix) => {
13
16
  const rem = str.replace(/\n([^\n])/g, "\n" + prefix + "$1");
@@ -70,8 +73,13 @@ class MultiStats {
70
73
  };
71
74
  }
72
75
 
76
+ /**
77
+ * @param {any} options stats options
78
+ * @returns {StatsCompilation} json output
79
+ */
73
80
  toJson(options) {
74
81
  options = this._createChildOptions(options, { forToString: false });
82
+ /** @type {KnownStatsCompilation} */
75
83
  const obj = {};
76
84
  obj.children = this.stats.map((stat, idx) => {
77
85
  const obj = stat.toJson(options.children[idx]);
@@ -8,13 +8,13 @@
8
8
  const WebpackError = require("./WebpackError");
9
9
 
10
10
  module.exports = class NoModeWarning extends WebpackError {
11
- constructor(modules) {
11
+ constructor() {
12
12
  super();
13
13
 
14
14
  this.name = "NoModeWarning";
15
15
  this.message =
16
16
  "configuration\n" +
17
- "The 'mode' option has not been set, webpack will fallback to 'production' for this value. " +
17
+ "The 'mode' option has not been set, webpack will fallback to 'production' for this value.\n" +
18
18
  "Set 'mode' option to 'development' or 'production' to enable defaults for each environment.\n" +
19
19
  "You can also set it to 'none' to disable any default behavior. " +
20
20
  "Learn more: https://webpack.js.org/configuration/mode/";
@@ -993,6 +993,13 @@ class NormalModule extends Module {
993
993
  runtimeRequirements.add(RuntimeGlobals.thisAsExports);
994
994
  }
995
995
 
996
+ /** @type {Map<string, any>} */
997
+ let data;
998
+ const getData = () => {
999
+ if (data === undefined) data = new Map();
1000
+ return data;
1001
+ };
1002
+
996
1003
  const sources = new Map();
997
1004
  for (const type of this.generator.getTypes(this)) {
998
1005
  const source = this.error
@@ -1007,6 +1014,7 @@ class NormalModule extends Module {
1007
1014
  runtimeRequirements,
1008
1015
  runtime,
1009
1016
  concatenationScope,
1017
+ getData,
1010
1018
  type
1011
1019
  });
1012
1020
 
@@ -1018,7 +1026,8 @@ class NormalModule extends Module {
1018
1026
  /** @type {CodeGenerationResult} */
1019
1027
  const resultEntry = {
1020
1028
  sources,
1021
- runtimeRequirements
1029
+ runtimeRequirements,
1030
+ data
1022
1031
  };
1023
1032
  return resultEntry;
1024
1033
  }
@@ -47,6 +47,7 @@ const GLOBALS_ON_REQUIRE = [
47
47
  RuntimeGlobals.moduleFactoriesAddOnly,
48
48
  RuntimeGlobals.interceptModuleExecution,
49
49
  RuntimeGlobals.publicPath,
50
+ RuntimeGlobals.baseURI,
50
51
  RuntimeGlobals.scriptNonce,
51
52
  RuntimeGlobals.uncaughtErrorHandler,
52
53
  RuntimeGlobals.asyncModule,
package/lib/Stats.js CHANGED
@@ -5,7 +5,9 @@
5
5
 
6
6
  "use strict";
7
7
 
8
+ /** @typedef {import("../declarations/WebpackOptions").StatsOptions} StatsOptions */
8
9
  /** @typedef {import("./Compilation")} Compilation */
10
+ /** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsCompilation} StatsCompilation */
9
11
 
10
12
  class Stats {
11
13
  /**
@@ -47,6 +49,10 @@ class Stats {
47
49
  );
48
50
  }
49
51
 
52
+ /**
53
+ * @param {(string|StatsOptions)=} options stats options
54
+ * @returns {StatsCompilation} json output
55
+ */
50
56
  toJson(options) {
51
57
  options = this.compilation.createStatsOptions(options, {
52
58
  forToString: false
@@ -47,7 +47,7 @@ class AssetGenerator extends Generator {
47
47
  */
48
48
  generate(
49
49
  module,
50
- { runtime, chunkGraph, runtimeTemplate, runtimeRequirements, type }
50
+ { runtime, chunkGraph, runtimeTemplate, runtimeRequirements, type, getData }
51
51
  ) {
52
52
  switch (type) {
53
53
  case "asset":
@@ -146,6 +146,15 @@ class AssetGenerator extends Generator {
146
146
  sourceFilename,
147
147
  ...info
148
148
  };
149
+ if (getData) {
150
+ // Due to code generation caching module.buildInfo.XXX can't used to store such information
151
+ // It need to be stored in the code generation results instead, where it's cached too
152
+ // TODO webpack 6 For back-compat reasons we also store in on module.buildInfo
153
+ const data = getData();
154
+ data.set("fullContentHash", fullHash);
155
+ data.set("filename", filename);
156
+ data.set("assetInfo", info);
157
+ }
149
158
 
150
159
  runtimeRequirements.add(RuntimeGlobals.publicPath); // add __webpack_require__.p
151
160
 
@@ -145,14 +145,23 @@ class AssetModulesPlugin {
145
145
  );
146
146
  if (modules) {
147
147
  for (const module of modules) {
148
+ const codeGenResult = codeGenerationResults.get(
149
+ module,
150
+ chunk.runtime
151
+ );
148
152
  result.push({
149
- render: () =>
150
- codeGenerationResults.getSource(module, chunk.runtime, type),
151
- filename: module.buildInfo.filename,
152
- info: module.buildInfo.assetInfo,
153
+ render: () => codeGenResult.sources.get(type),
154
+ filename:
155
+ module.buildInfo.filename ||
156
+ codeGenResult.data.get("filename"),
157
+ info:
158
+ module.buildInfo.assetInfo ||
159
+ codeGenResult.data.get("assetInfo"),
153
160
  auxiliary: true,
154
161
  identifier: `assetModule${chunkGraph.getModuleId(module)}`,
155
- hash: module.buildInfo.fullContentHash
162
+ hash:
163
+ module.buildInfo.fullContentHash ||
164
+ codeGenResult.data.get("fullContentHash")
156
165
  });
157
166
  }
158
167
  }
@@ -127,9 +127,15 @@ class IdleFileCachePlugin {
127
127
  });
128
128
  return;
129
129
  }
130
- currentIdlePromise = currentIdlePromise.then(() =>
131
- strategy.afterAllStored()
132
- );
130
+ currentIdlePromise = currentIdlePromise
131
+ .then(() => strategy.afterAllStored())
132
+ .catch(err => {
133
+ const logger = compiler.getInfrastructureLogger(
134
+ "IdleFileCachePlugin"
135
+ );
136
+ logger.warn(`Background tasks during idle failed: ${err.message}`);
137
+ logger.debug(err.stack);
138
+ });
133
139
  isInitialStore = false;
134
140
  }
135
141
  };
@@ -275,6 +275,7 @@ const applyCacheDefaults = (cache, { name, mode }) => {
275
275
  dir = undefined;
276
276
  break;
277
277
  }
278
+ dir = parent;
278
279
  }
279
280
  if (!dir) {
280
281
  return path.resolve(cwd, ".cache/webpack");
@@ -128,9 +128,11 @@ class HarmonyImportDependency extends ModuleDependency {
128
128
  if (exportInfo.provided === false) {
129
129
  // We are sure that it's not provided
130
130
  const providedExports = exportsInfo.getProvidedExports();
131
- const moreInfo = Array.isArray(providedExports)
132
- ? ` (possible exports: ${providedExports.join(", ")})`
133
- : " (possible exports unknown)";
131
+ const moreInfo = !Array.isArray(providedExports)
132
+ ? " (possible exports unknown)"
133
+ : providedExports.length === 0
134
+ ? " (module has no exports)"
135
+ : ` (possible exports: ${providedExports.join(", ")})`;
134
136
  return [
135
137
  new HarmonyLinkingError(
136
138
  `export ${ids
@@ -111,7 +111,9 @@ class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
111
111
  */
112
112
  getReferencedExports(moduleGraph, runtime) {
113
113
  let ids = this.getIds(moduleGraph);
114
- if (ids.length > 0 && ids[0] === "default") {
114
+ if (ids.length === 0) return Dependency.EXPORTS_OBJECT_REFERENCED;
115
+ let namespaceObjectAsContext = this.namespaceObjectAsContext;
116
+ if (ids[0] === "default") {
115
117
  const selfModule = moduleGraph.getParentModule(this);
116
118
  const importedModule = moduleGraph.getModule(this);
117
119
  switch (
@@ -124,13 +126,18 @@ class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
124
126
  case "default-with-named":
125
127
  if (ids.length === 1) return Dependency.EXPORTS_OBJECT_REFERENCED;
126
128
  ids = ids.slice(1);
129
+ namespaceObjectAsContext = true;
127
130
  break;
128
131
  case "dynamic":
129
132
  return Dependency.EXPORTS_OBJECT_REFERENCED;
130
133
  }
131
134
  }
132
135
 
133
- if (this.namespaceObjectAsContext) {
136
+ if (
137
+ this.call &&
138
+ !this.directImport &&
139
+ (namespaceObjectAsContext || ids.length > 1)
140
+ ) {
134
141
  if (ids.length === 1) return Dependency.EXPORTS_OBJECT_REFERENCED;
135
142
  ids = ids.slice(0, -1);
136
143
  }
package/lib/index.js CHANGED
@@ -26,6 +26,7 @@ const memoize = require("./util/memoize");
26
26
  /** @typedef {import("./Compilation").Asset} Asset */
27
27
  /** @typedef {import("./Compilation").AssetInfo} AssetInfo */
28
28
  /** @typedef {import("./Parser").ParserState} ParserState */
29
+ /** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsCompilation} StatsCompilation */
29
30
 
30
31
  /**
31
32
  * @template {Function} T
@@ -38,14 +38,13 @@ const stringifySafe = data => {
38
38
  const createObjectForExportsInfo = (data, exportsInfo, runtime) => {
39
39
  if (exportsInfo.otherExportsInfo.getUsed(runtime) !== UsageState.Unused)
40
40
  return data;
41
- const reducedData = Array.isArray(data) ? [] : {};
42
- for (const exportInfo of exportsInfo.exports) {
43
- if (exportInfo.name in reducedData) return data;
44
- }
41
+ const isArray = Array.isArray(data);
42
+ const reducedData = isArray ? [] : {};
45
43
  for (const key of Object.keys(data)) {
46
44
  const exportInfo = exportsInfo.getReadOnlyExportInfo(key);
47
45
  const used = exportInfo.getUsed(runtime);
48
46
  if (used === UsageState.Unused) continue;
47
+
49
48
  let value;
50
49
  if (used === UsageState.OnlyPropertiesUsed && exportInfo.exportsInfo) {
51
50
  value = createObjectForExportsInfo(
@@ -59,7 +58,13 @@ const createObjectForExportsInfo = (data, exportsInfo, runtime) => {
59
58
  const name = exportInfo.getUsedName(key, runtime);
60
59
  reducedData[name] = value;
61
60
  }
62
- if (Array.isArray(reducedData)) {
61
+ if (isArray) {
62
+ let arrayLengthWhenUsed =
63
+ exportsInfo.getReadOnlyExportInfo("length").getUsed(runtime) !==
64
+ UsageState.Unused
65
+ ? data.length
66
+ : undefined;
67
+
63
68
  let sizeObjectMinusArray = 0;
64
69
  for (let i = 0; i < reducedData.length; i++) {
65
70
  if (reducedData[i] === undefined) {
@@ -68,8 +73,24 @@ const createObjectForExportsInfo = (data, exportsInfo, runtime) => {
68
73
  sizeObjectMinusArray += `${i}`.length + 3;
69
74
  }
70
75
  }
71
- if (sizeObjectMinusArray < 0) return Object.assign({}, reducedData);
72
- for (let i = 0; i < reducedData.length; i++) {
76
+ if (arrayLengthWhenUsed !== undefined) {
77
+ sizeObjectMinusArray +=
78
+ `${arrayLengthWhenUsed}`.length +
79
+ 8 -
80
+ (arrayLengthWhenUsed - reducedData.length) * 2;
81
+ }
82
+ if (sizeObjectMinusArray < 0)
83
+ return Object.assign(
84
+ arrayLengthWhenUsed === undefined
85
+ ? {}
86
+ : { length: arrayLengthWhenUsed },
87
+ reducedData
88
+ );
89
+ const generatedLength =
90
+ arrayLengthWhenUsed !== undefined
91
+ ? Math.max(arrayLengthWhenUsed, reducedData.length)
92
+ : reducedData.length;
93
+ for (let i = 0; i < generatedLength; i++) {
73
94
  if (reducedData[i] === undefined) {
74
95
  reducedData[i] = 0;
75
96
  }
@@ -208,12 +208,14 @@ class ObjectMiddleware extends SerializerMiddleware {
208
208
  }
209
209
 
210
210
  static getSerializerFor(object) {
211
- let c = object.constructor;
212
- if (!c) {
213
- if (Object.getPrototypeOf(object) === null) {
214
- // Object created with Object.create(null)
215
- c = null;
216
- } else {
211
+ const proto = Object.getPrototypeOf(object);
212
+ let c;
213
+ if (proto === null) {
214
+ // Object created with Object.create(null)
215
+ c = null;
216
+ } else {
217
+ c = proto.constructor;
218
+ if (!c) {
217
219
  throw new Error(
218
220
  "Serialization of objects with prototype without valid constructor property not possible"
219
221
  );