webpack 5.64.3 → 5.67.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.

Potentially problematic release.


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

Files changed (121) hide show
  1. package/README.md +1 -1
  2. package/lib/Cache.js +1 -1
  3. package/lib/CacheFacade.js +4 -11
  4. package/lib/Chunk.js +2 -0
  5. package/lib/CleanPlugin.js +1 -1
  6. package/lib/Compilation.js +91 -47
  7. package/lib/Compiler.js +57 -3
  8. package/lib/ContextModule.js +21 -17
  9. package/lib/DelegatedModule.js +1 -1
  10. package/lib/Dependency.js +10 -0
  11. package/lib/DependencyTemplate.js +9 -0
  12. package/lib/DependencyTemplates.js +1 -1
  13. package/lib/DllModule.js +1 -1
  14. package/lib/EvalDevToolModulePlugin.js +16 -1
  15. package/lib/EvalSourceMapDevToolPlugin.js +18 -1
  16. package/lib/ExternalModule.js +94 -54
  17. package/lib/ExternalModuleFactoryPlugin.js +1 -1
  18. package/lib/FileSystemInfo.js +29 -25
  19. package/lib/Generator.js +2 -0
  20. package/lib/HookWebpackError.js +1 -1
  21. package/lib/Module.js +25 -2
  22. package/lib/ModuleFilenameHelpers.js +5 -1
  23. package/lib/MultiCompiler.js +1 -1
  24. package/lib/MultiWatching.js +1 -1
  25. package/lib/NormalModule.js +9 -5
  26. package/lib/RawModule.js +1 -1
  27. package/lib/RuntimeGlobals.js +29 -1
  28. package/lib/RuntimeModule.js +1 -1
  29. package/lib/RuntimePlugin.js +50 -0
  30. package/lib/RuntimeTemplate.js +113 -2
  31. package/lib/Template.js +2 -1
  32. package/lib/WatchIgnorePlugin.js +14 -1
  33. package/lib/Watching.js +33 -19
  34. package/lib/WebpackOptionsApply.js +43 -2
  35. package/lib/asset/AssetGenerator.js +71 -30
  36. package/lib/asset/AssetModulesPlugin.js +3 -0
  37. package/lib/asset/RawDataUrlModule.js +148 -0
  38. package/lib/cache/ResolverCachePlugin.js +1 -1
  39. package/lib/cli.js +44 -3
  40. package/lib/config/browserslistTargetHandler.js +38 -1
  41. package/lib/config/defaults.js +79 -6
  42. package/lib/config/normalization.js +5 -0
  43. package/lib/config/target.js +10 -0
  44. package/lib/container/ContainerEntryModule.js +8 -5
  45. package/lib/container/FallbackModule.js +4 -4
  46. package/lib/container/RemoteModule.js +4 -2
  47. package/lib/css/CssExportsGenerator.js +139 -0
  48. package/lib/css/CssGenerator.js +109 -0
  49. package/lib/css/CssLoadingRuntimeModule.js +447 -0
  50. package/lib/css/CssModulesPlugin.js +461 -0
  51. package/lib/css/CssParser.js +618 -0
  52. package/lib/css/walkCssTokens.js +659 -0
  53. package/lib/debug/ProfilingPlugin.js +12 -10
  54. package/lib/dependencies/CreateScriptUrlDependency.js +12 -0
  55. package/lib/dependencies/CssExportDependency.js +85 -0
  56. package/lib/dependencies/CssImportDependency.js +75 -0
  57. package/lib/dependencies/CssLocalIdentifierDependency.js +119 -0
  58. package/lib/dependencies/CssSelfLocalIdentifierDependency.js +101 -0
  59. package/lib/dependencies/CssUrlDependency.js +132 -0
  60. package/lib/dependencies/LoaderPlugin.js +2 -2
  61. package/lib/dependencies/URLDependency.js +3 -8
  62. package/lib/esm/ModuleChunkFormatPlugin.js +74 -49
  63. package/lib/esm/ModuleChunkLoadingRuntimeModule.js +1 -1
  64. package/lib/hmr/LazyCompilationPlugin.js +45 -21
  65. package/lib/hmr/lazyCompilationBackend.js +4 -2
  66. package/lib/ids/DeterministicModuleIdsPlugin.js +55 -35
  67. package/lib/ids/HashedModuleIdsPlugin.js +9 -12
  68. package/lib/ids/IdHelpers.js +24 -10
  69. package/lib/ids/NamedModuleIdsPlugin.js +6 -9
  70. package/lib/ids/NaturalModuleIdsPlugin.js +10 -13
  71. package/lib/ids/OccurrenceModuleIdsPlugin.js +13 -10
  72. package/lib/ids/SyncModuleIdsPlugin.js +140 -0
  73. package/lib/index.js +8 -0
  74. package/lib/javascript/ArrayPushCallbackChunkFormatPlugin.js +2 -2
  75. package/lib/javascript/ChunkHelpers.js +33 -0
  76. package/lib/javascript/JavascriptGenerator.js +1 -0
  77. package/lib/javascript/JavascriptParser.js +16 -8
  78. package/lib/javascript/StartupHelpers.js +4 -28
  79. package/lib/library/AssignLibraryPlugin.js +31 -13
  80. package/lib/library/EnableLibraryPlugin.js +11 -0
  81. package/lib/node/NodeWatchFileSystem.js +85 -31
  82. package/lib/node/ReadFileChunkLoadingRuntimeModule.js +1 -1
  83. package/lib/node/RequireChunkLoadingRuntimeModule.js +1 -1
  84. package/lib/optimize/ConcatenatedModule.js +11 -5
  85. package/lib/runtime/CreateScriptRuntimeModule.js +36 -0
  86. package/lib/runtime/CreateScriptUrlRuntimeModule.js +9 -34
  87. package/lib/runtime/GetTrustedTypesPolicyRuntimeModule.js +76 -0
  88. package/lib/schemes/HttpUriPlugin.js +8 -8
  89. package/lib/sharing/ConsumeSharedModule.js +8 -2
  90. package/lib/sharing/ConsumeSharedRuntimeModule.js +25 -4
  91. package/lib/sharing/ProvideSharedModule.js +4 -2
  92. package/lib/sharing/utils.js +1 -1
  93. package/lib/stats/DefaultStatsFactoryPlugin.js +112 -67
  94. package/lib/stats/DefaultStatsPrinterPlugin.js +89 -24
  95. package/lib/util/AsyncQueue.js +1 -1
  96. package/lib/util/extractUrlAndGlobal.js +3 -0
  97. package/lib/util/fs.js +10 -0
  98. package/lib/util/hash/xxhash64.js +2 -2
  99. package/lib/util/internalSerializables.js +11 -0
  100. package/lib/web/JsonpChunkLoadingRuntimeModule.js +3 -3
  101. package/lib/webworker/ImportScriptsChunkLoadingPlugin.js +3 -11
  102. package/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +3 -2
  103. package/package.json +5 -12
  104. package/schemas/WebpackOptions.check.js +1 -1
  105. package/schemas/WebpackOptions.json +100 -1
  106. package/schemas/plugins/asset/AssetGeneratorOptions.check.js +1 -1
  107. package/schemas/plugins/asset/AssetResourceGeneratorOptions.check.js +1 -1
  108. package/schemas/plugins/container/ContainerPlugin.check.js +1 -1
  109. package/schemas/plugins/container/ContainerPlugin.json +2 -1
  110. package/schemas/plugins/container/ContainerReferencePlugin.check.js +1 -1
  111. package/schemas/plugins/container/ContainerReferencePlugin.json +1 -0
  112. package/schemas/plugins/container/ExternalsType.check.js +1 -1
  113. package/schemas/plugins/container/ModuleFederationPlugin.check.js +1 -1
  114. package/schemas/plugins/container/ModuleFederationPlugin.json +3 -1
  115. package/schemas/plugins/css/CssGeneratorOptions.check.d.ts +7 -0
  116. package/schemas/plugins/css/CssGeneratorOptions.check.js +6 -0
  117. package/schemas/plugins/css/CssGeneratorOptions.json +3 -0
  118. package/schemas/plugins/css/CssParserOptions.check.d.ts +7 -0
  119. package/schemas/plugins/css/CssParserOptions.check.js +6 -0
  120. package/schemas/plugins/css/CssParserOptions.json +3 -0
  121. package/types.d.ts +305 -58
@@ -0,0 +1,140 @@
1
+ /*
2
+ MIT License http://www.opensource.org/licenses/mit-license.php
3
+ Author Tobias Koppers @sokra
4
+ */
5
+
6
+ "use strict";
7
+
8
+ const { WebpackError } = require("..");
9
+ const { getUsedModuleIdsAndModules } = require("./IdHelpers");
10
+
11
+ /** @typedef {import("../Compiler")} Compiler */
12
+ /** @typedef {import("../Module")} Module */
13
+
14
+ const plugin = "SyncModuleIdsPlugin";
15
+
16
+ class SyncModuleIdsPlugin {
17
+ /**
18
+ * @param {Object} options options
19
+ * @param {string} options.path path to file
20
+ * @param {string=} options.context context for module names
21
+ * @param {function(Module): boolean} options.test selector for modules
22
+ * @param {"read" | "create" | "merge" | "update"=} options.mode operation mode (defaults to merge)
23
+ */
24
+ constructor({ path, context, test, mode }) {
25
+ this._path = path;
26
+ this._context = context;
27
+ this._test = test || (() => true);
28
+ const readAndWrite = !mode || mode === "merge" || mode === "update";
29
+ this._read = readAndWrite || mode === "read";
30
+ this._write = readAndWrite || mode === "create";
31
+ this._prune = mode === "update";
32
+ }
33
+
34
+ /**
35
+ * Apply the plugin
36
+ * @param {Compiler} compiler the compiler instance
37
+ * @returns {void}
38
+ */
39
+ apply(compiler) {
40
+ /** @type {Map<string, string | number>} */
41
+ let data;
42
+ let dataChanged = false;
43
+ if (this._read) {
44
+ compiler.hooks.readRecords.tapAsync(plugin, callback => {
45
+ const fs = compiler.intermediateFileSystem;
46
+ fs.readFile(this._path, (err, buffer) => {
47
+ if (err) {
48
+ if (err.code !== "ENOENT") {
49
+ return callback(err);
50
+ }
51
+ return callback();
52
+ }
53
+ const json = JSON.parse(buffer.toString());
54
+ data = new Map();
55
+ for (const key of Object.keys(json)) {
56
+ data.set(key, json[key]);
57
+ }
58
+ dataChanged = false;
59
+ return callback();
60
+ });
61
+ });
62
+ }
63
+ if (this._write) {
64
+ compiler.hooks.emitRecords.tapAsync(plugin, callback => {
65
+ if (!data || !dataChanged) return callback();
66
+ const json = {};
67
+ const sorted = Array.from(data).sort(([a], [b]) => (a < b ? -1 : 1));
68
+ for (const [key, value] of sorted) {
69
+ json[key] = value;
70
+ }
71
+ const fs = compiler.intermediateFileSystem;
72
+ fs.writeFile(this._path, JSON.stringify(json), callback);
73
+ });
74
+ }
75
+ compiler.hooks.thisCompilation.tap(plugin, compilation => {
76
+ const associatedObjectForCache = compiler.root;
77
+ const context = this._context || compiler.context;
78
+ if (this._read) {
79
+ compilation.hooks.reviveModules.tap(plugin, (_1, _2) => {
80
+ if (!data) return;
81
+ const { chunkGraph } = compilation;
82
+ const [usedIds, modules] = getUsedModuleIdsAndModules(
83
+ compilation,
84
+ this._test
85
+ );
86
+ for (const module of modules) {
87
+ const name = module.libIdent({
88
+ context,
89
+ associatedObjectForCache
90
+ });
91
+ if (!name) continue;
92
+ const id = data.get(name);
93
+ const idAsString = `${id}`;
94
+ if (usedIds.has(idAsString)) {
95
+ const err = new WebpackError(
96
+ `SyncModuleIdsPlugin: Unable to restore id '${id}' from '${this._path}' as it's already used.`
97
+ );
98
+ err.module = module;
99
+ compilation.errors.push(err);
100
+ }
101
+ chunkGraph.setModuleId(module, id);
102
+ usedIds.add(idAsString);
103
+ }
104
+ });
105
+ }
106
+ if (this._write) {
107
+ compilation.hooks.recordModules.tap(plugin, modules => {
108
+ const { chunkGraph } = compilation;
109
+ let oldData = data;
110
+ if (!oldData) {
111
+ oldData = data = new Map();
112
+ } else if (this._prune) {
113
+ data = new Map();
114
+ }
115
+ for (const module of modules) {
116
+ if (this._test(module)) {
117
+ const name = module.libIdent({
118
+ context,
119
+ associatedObjectForCache
120
+ });
121
+ if (!name) continue;
122
+ const id = chunkGraph.getModuleId(module);
123
+ if (id === null) continue;
124
+ const oldId = oldData.get(name);
125
+ if (oldId !== id) {
126
+ dataChanged = true;
127
+ } else if (data === oldData) {
128
+ continue;
129
+ }
130
+ data.set(name, id);
131
+ }
132
+ }
133
+ if (data.size !== oldData.size) dataChanged = true;
134
+ });
135
+ }
136
+ });
137
+ }
138
+ }
139
+
140
+ module.exports = SyncModuleIdsPlugin;
package/lib/index.js CHANGED
@@ -19,6 +19,7 @@ const memoize = require("./util/memoize");
19
19
  /** @typedef {import("../declarations/WebpackOptions").RuleSetRule} RuleSetRule */
20
20
  /** @typedef {import("../declarations/WebpackOptions").RuleSetUse} RuleSetUse */
21
21
  /** @typedef {import("../declarations/WebpackOptions").RuleSetUseItem} RuleSetUseItem */
22
+ /** @typedef {import("../declarations/WebpackOptions").StatsOptions} StatsOptions */
22
23
  /** @typedef {import("../declarations/WebpackOptions").WebpackOptions} Configuration */
23
24
  /** @typedef {import("../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptionsNormalized */
24
25
  /** @typedef {import("../declarations/WebpackOptions").WebpackPluginFunction} WebpackPluginFunction */
@@ -27,6 +28,8 @@ const memoize = require("./util/memoize");
27
28
  /** @typedef {import("./Compilation").AssetInfo} AssetInfo */
28
29
  /** @typedef {import("./MultiStats")} MultiStats */
29
30
  /** @typedef {import("./Parser").ParserState} ParserState */
31
+ /** @typedef {import("./ResolverFactory").ResolvePluginInstance} ResolvePluginInstance */
32
+ /** @typedef {import("./ResolverFactory").Resolver} Resolver */
30
33
  /** @typedef {import("./Watching")} Watching */
31
34
  /** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsAsset} StatsAsset */
32
35
  /** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsChunk} StatsChunk */
@@ -561,6 +564,11 @@ module.exports = mergeExports(fn, {
561
564
  get HttpUriPlugin() {
562
565
  return require("./schemes/HttpUriPlugin");
563
566
  }
567
+ },
568
+ ids: {
569
+ get SyncModuleIdsPlugin() {
570
+ return require("./ids/SyncModuleIdsPlugin");
571
+ }
564
572
  }
565
573
  }
566
574
  });
@@ -45,7 +45,7 @@ class ArrayPushCallbackChunkFormatPlugin {
45
45
  const { chunk, chunkGraph, runtimeTemplate } = renderContext;
46
46
  const hotUpdateChunk =
47
47
  chunk instanceof HotUpdateChunk ? chunk : null;
48
- const globalObject = runtimeTemplate.outputOptions.globalObject;
48
+ const globalObject = runtimeTemplate.globalObject;
49
49
  const source = new ConcatSource();
50
50
  const runtimeModules =
51
51
  chunkGraph.getChunkRuntimeModulesInOrder(chunk);
@@ -138,7 +138,7 @@ class ArrayPushCallbackChunkFormatPlugin {
138
138
  (chunk, hash, { chunkGraph, runtimeTemplate }) => {
139
139
  if (chunk.hasRuntime()) return;
140
140
  hash.update(
141
- `ArrayPushCallbackChunkFormatPlugin1${runtimeTemplate.outputOptions.chunkLoadingGlobal}${runtimeTemplate.outputOptions.hotUpdateGlobal}${runtimeTemplate.outputOptions.globalObject}`
141
+ `ArrayPushCallbackChunkFormatPlugin1${runtimeTemplate.outputOptions.chunkLoadingGlobal}${runtimeTemplate.outputOptions.hotUpdateGlobal}${runtimeTemplate.globalObject}`
142
142
  );
143
143
  const entries = Array.from(
144
144
  chunkGraph.getChunkEntryModulesWithChunkGroupIterable(chunk)
@@ -0,0 +1,33 @@
1
+ /*
2
+ MIT License http://www.opensource.org/licenses/mit-license.php
3
+ Author Tobias Koppers @sokra
4
+ */
5
+
6
+ "use strict";
7
+
8
+ const Entrypoint = require("../Entrypoint");
9
+
10
+ /** @typedef {import("../Chunk")} Chunk */
11
+
12
+ /**
13
+ * @param {Entrypoint} entrypoint a chunk group
14
+ * @param {Chunk} excludedChunk1 current chunk which is excluded
15
+ * @param {Chunk} excludedChunk2 runtime chunk which is excluded
16
+ * @returns {Set<Chunk>} chunks
17
+ */
18
+ const getAllChunks = (entrypoint, excludedChunk1, excludedChunk2) => {
19
+ const queue = new Set([entrypoint]);
20
+ const chunks = new Set();
21
+ for (const entrypoint of queue) {
22
+ for (const chunk of entrypoint.chunks) {
23
+ if (chunk === excludedChunk1) continue;
24
+ if (chunk === excludedChunk2) continue;
25
+ chunks.add(chunk);
26
+ }
27
+ for (const parent of entrypoint.parentsIterable) {
28
+ if (parent instanceof Entrypoint) queue.add(parent);
29
+ }
30
+ }
31
+ return chunks;
32
+ };
33
+ exports.getAllChunks = getAllChunks;
@@ -199,6 +199,7 @@ class JavascriptGenerator extends Generator {
199
199
  runtime: generateContext.runtime,
200
200
  runtimeRequirements: generateContext.runtimeRequirements,
201
201
  concatenationScope: generateContext.concatenationScope,
202
+ codeGenerationResults: generateContext.codeGenerationResults,
202
203
  initFragments
203
204
  };
204
205
 
@@ -935,6 +935,13 @@ class JavascriptParser extends Parser {
935
935
  .setString("undefined")
936
936
  .setRange(expr.range);
937
937
  });
938
+ this.hooks.evaluate.for("Identifier").tap("JavascriptParser", expr => {
939
+ if (/** @type {IdentifierNode} */ (expr).name === "undefined") {
940
+ return new BasicEvaluatedExpression()
941
+ .setUndefined()
942
+ .setRange(expr.range);
943
+ }
944
+ });
938
945
  /**
939
946
  * @param {string} exprType expression type name
940
947
  * @param {function(ExpressionNode): GetInfoResult | undefined} getInfo get info
@@ -1189,14 +1196,15 @@ class JavascriptParser extends Parser {
1189
1196
  const node = /** @type {TaggedTemplateExpressionNode} */ (_node);
1190
1197
  const tag = this.evaluateExpression(node.tag);
1191
1198
 
1192
- if (tag.isIdentifier() && tag.identifier !== "String.raw") return;
1193
- const { quasis, parts } = getSimplifiedTemplateResult(
1194
- "raw",
1195
- node.quasi
1196
- );
1197
- return new BasicEvaluatedExpression()
1198
- .setTemplateString(quasis, parts, "raw")
1199
- .setRange(node.range);
1199
+ if (tag.isIdentifier() && tag.identifier === "String.raw") {
1200
+ const { quasis, parts } = getSimplifiedTemplateResult(
1201
+ "raw",
1202
+ node.quasi
1203
+ );
1204
+ return new BasicEvaluatedExpression()
1205
+ .setTemplateString(quasis, parts, "raw")
1206
+ .setRange(node.range);
1207
+ }
1200
1208
  });
1201
1209
 
1202
1210
  this.hooks.evaluateCallExpressionMember
@@ -5,11 +5,10 @@
5
5
 
6
6
  "use strict";
7
7
 
8
- const Entrypoint = require("../Entrypoint");
9
8
  const RuntimeGlobals = require("../RuntimeGlobals");
10
9
  const Template = require("../Template");
11
10
  const { isSubset } = require("../util/SetHelpers");
12
- const { chunkHasJs } = require("./JavascriptModulesPlugin");
11
+ const { getAllChunks } = require("./ChunkHelpers");
13
12
 
14
13
  /** @typedef {import("../util/Hash")} Hash */
15
14
  /** @typedef {import("../Chunk")} Chunk */
@@ -19,30 +18,6 @@ const { chunkHasJs } = require("./JavascriptModulesPlugin");
19
18
  /** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
20
19
  /** @typedef {(string|number)[]} EntryItem */
21
20
 
22
- // TODO move to this file to ../javascript/ChunkHelpers.js
23
-
24
- /**
25
- * @param {Entrypoint} entrypoint a chunk group
26
- * @param {Chunk} excludedChunk1 current chunk which is excluded
27
- * @param {Chunk} excludedChunk2 runtime chunk which is excluded
28
- * @returns {Set<Chunk>} chunks
29
- */
30
- const getAllChunks = (entrypoint, excludedChunk1, excludedChunk2) => {
31
- const queue = new Set([entrypoint]);
32
- const chunks = new Set();
33
- for (const entrypoint of queue) {
34
- for (const chunk of entrypoint.chunks) {
35
- if (chunk === excludedChunk1) continue;
36
- if (chunk === excludedChunk2) continue;
37
- chunks.add(chunk);
38
- }
39
- for (const parent of entrypoint.parentsIterable) {
40
- if (parent instanceof Entrypoint) queue.add(parent);
41
- }
42
- }
43
- return chunks;
44
- };
45
-
46
21
  const EXPORT_PREFIX = "var __webpack_exports__ = ";
47
22
 
48
23
  /**
@@ -143,12 +118,13 @@ exports.updateHashForEntryStartup = (hash, chunkGraph, entries, chunk) => {
143
118
  /**
144
119
  * @param {Chunk} chunk the chunk
145
120
  * @param {ChunkGraph} chunkGraph the chunk graph
121
+ * @param {function(Chunk, ChunkGraph): boolean} filterFn filter function
146
122
  * @returns {Set<number | string>} initially fulfilled chunk ids
147
123
  */
148
- exports.getInitialChunkIds = (chunk, chunkGraph) => {
124
+ exports.getInitialChunkIds = (chunk, chunkGraph, filterFn) => {
149
125
  const initialChunkIds = new Set(chunk.ids);
150
126
  for (const c of chunk.getAllInitialChunks()) {
151
- if (c === chunk || chunkHasJs(c, chunkGraph)) continue;
127
+ if (c === chunk || filterFn(c, chunkGraph)) continue;
152
128
  for (const id of c.ids) initialChunkIds.add(id);
153
129
  }
154
130
  return initialChunkIds;
@@ -92,7 +92,7 @@ const accessWithInit = (accessor, existingLength, initLast = false) => {
92
92
  * @property {LibraryType} type
93
93
  * @property {string[] | "global"} prefix name prefix
94
94
  * @property {string | false} declare declare name as variable
95
- * @property {"error"|"copy"|"assign"} unnamed behavior for unnamed library name
95
+ * @property {"error"|"static"|"copy"|"assign"} unnamed behavior for unnamed library name
96
96
  * @property {"copy"|"assign"=} named behavior for named library name
97
97
  */
98
98
 
@@ -174,7 +174,7 @@ class AssignLibraryPlugin extends AbstractLibraryPlugin {
174
174
 
175
175
  _getPrefix(compilation) {
176
176
  return this.prefix === "global"
177
- ? [compilation.outputOptions.globalObject]
177
+ ? [compilation.runtimeTemplate.globalObject]
178
178
  : this.prefix;
179
179
  }
180
180
 
@@ -261,19 +261,42 @@ class AssignLibraryPlugin extends AbstractLibraryPlugin {
261
261
  * @param {LibraryContext<T>} libraryContext context
262
262
  * @returns {Source} source with library export
263
263
  */
264
- renderStartup(source, module, { chunk }, { options, compilation }) {
264
+ renderStartup(
265
+ source,
266
+ module,
267
+ { moduleGraph, chunk },
268
+ { options, compilation }
269
+ ) {
265
270
  const fullNameResolved = this._getResolvedFullName(
266
271
  options,
267
272
  chunk,
268
273
  compilation
269
274
  );
275
+ const staticExports = this.unnamed === "static";
270
276
  const exportAccess = options.export
271
277
  ? propertyAccess(
272
278
  Array.isArray(options.export) ? options.export : [options.export]
273
279
  )
274
280
  : "";
275
281
  const result = new ConcatSource(source);
276
- if (options.name ? this.named === "copy" : this.unnamed === "copy") {
282
+ if (staticExports) {
283
+ const exportsInfo = moduleGraph.getExportsInfo(module);
284
+ const exportTarget = accessWithInit(
285
+ fullNameResolved,
286
+ this._getPrefix(compilation).length,
287
+ true
288
+ );
289
+ for (const exportInfo of exportsInfo.orderedExports) {
290
+ if (!exportInfo.provided) continue;
291
+ const nameAccess = propertyAccess([exportInfo.name]);
292
+ result.add(
293
+ `${exportTarget}${nameAccess} = __webpack_exports__${exportAccess}${nameAccess};\n`
294
+ );
295
+ }
296
+ result.add(
297
+ `Object.defineProperty(${exportTarget}, "__esModule", { value: true });\n`
298
+ );
299
+ } else if (options.name ? this.named === "copy" : this.unnamed === "copy") {
277
300
  result.add(
278
301
  `var __webpack_export_target__ = ${accessWithInit(
279
302
  fullNameResolved,
@@ -325,15 +348,10 @@ class AssignLibraryPlugin extends AbstractLibraryPlugin {
325
348
  */
326
349
  chunkHash(chunk, hash, chunkHashContext, { options, compilation }) {
327
350
  hash.update("AssignLibraryPlugin");
328
- const prefix =
329
- this.prefix === "global"
330
- ? [compilation.outputOptions.globalObject]
331
- : this.prefix;
332
- const fullName = options.name ? prefix.concat(options.name) : prefix;
333
- const fullNameResolved = fullName.map(n =>
334
- compilation.getPath(n, {
335
- chunk
336
- })
351
+ const fullNameResolved = this._getResolvedFullName(
352
+ options,
353
+ chunk,
354
+ compilation
337
355
  );
338
356
  if (options.name ? this.named === "copy" : this.unnamed === "copy") {
339
357
  hash.update("copy");
@@ -167,6 +167,17 @@ class EnableLibraryPlugin {
167
167
  }).apply(compiler);
168
168
  break;
169
169
  }
170
+ case "commonjs-static": {
171
+ //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
172
+ const AssignLibraryPlugin = require("./AssignLibraryPlugin");
173
+ new AssignLibraryPlugin({
174
+ type,
175
+ prefix: ["exports"],
176
+ declare: false,
177
+ unnamed: "static"
178
+ }).apply(compiler);
179
+ break;
180
+ }
170
181
  case "commonjs2":
171
182
  case "commonjs-module": {
172
183
  //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
@@ -5,6 +5,7 @@
5
5
 
6
6
  "use strict";
7
7
 
8
+ const util = require("util");
8
9
  const Watchpack = require("watchpack");
9
10
 
10
11
  /** @typedef {import("../../declarations/WebpackOptions").WatchOptions} WatchOptions */
@@ -68,7 +69,22 @@ class NodeWatchFileSystem {
68
69
  if (callbackUndelayed) {
69
70
  this.watcher.once("change", callbackUndelayed);
70
71
  }
72
+
73
+ const fetchTimeInfo = () => {
74
+ const fileTimeInfoEntries = new Map();
75
+ const contextTimeInfoEntries = new Map();
76
+ if (this.watcher) {
77
+ this.watcher.collectTimeInfoEntries(
78
+ fileTimeInfoEntries,
79
+ contextTimeInfoEntries
80
+ );
81
+ }
82
+ return { fileTimeInfoEntries, contextTimeInfoEntries };
83
+ };
71
84
  this.watcher.once("aggregated", (changes, removals) => {
85
+ // pause emitting events (avoids clearing aggregated changes and removals on timeout)
86
+ this.watcher.pause();
87
+
72
88
  if (this.inputFileSystem && this.inputFileSystem.purge) {
73
89
  const fs = this.inputFileSystem;
74
90
  for (const item of changes) {
@@ -78,8 +94,14 @@ class NodeWatchFileSystem {
78
94
  fs.purge(item);
79
95
  }
80
96
  }
81
- const times = this.watcher.getTimeInfoEntries();
82
- callback(null, times, times, changes, removals);
97
+ const { fileTimeInfoEntries, contextTimeInfoEntries } = fetchTimeInfo();
98
+ callback(
99
+ null,
100
+ fileTimeInfoEntries,
101
+ contextTimeInfoEntries,
102
+ changes,
103
+ removals
104
+ );
83
105
  });
84
106
 
85
107
  this.watcher.watch({ files, directories, missing, startTime });
@@ -99,39 +121,71 @@ class NodeWatchFileSystem {
99
121
  this.watcher.pause();
100
122
  }
101
123
  },
102
- getAggregatedRemovals: () => {
103
- const items = this.watcher && this.watcher.aggregatedRemovals;
104
- if (items && this.inputFileSystem && this.inputFileSystem.purge) {
105
- const fs = this.inputFileSystem;
106
- for (const item of items) {
107
- fs.purge(item);
124
+ getAggregatedRemovals: util.deprecate(
125
+ () => {
126
+ const items = this.watcher && this.watcher.aggregatedRemovals;
127
+ if (items && this.inputFileSystem && this.inputFileSystem.purge) {
128
+ const fs = this.inputFileSystem;
129
+ for (const item of items) {
130
+ fs.purge(item);
131
+ }
108
132
  }
109
- }
110
- return items;
111
- },
112
- getAggregatedChanges: () => {
113
- const items = this.watcher && this.watcher.aggregatedChanges;
114
- if (items && this.inputFileSystem && this.inputFileSystem.purge) {
133
+ return items;
134
+ },
135
+ "Watcher.getAggregatedRemovals is deprecated in favor of Watcher.getInfo since that's more performant.",
136
+ "DEP_WEBPACK_WATCHER_GET_AGGREGATED_REMOVALS"
137
+ ),
138
+ getAggregatedChanges: util.deprecate(
139
+ () => {
140
+ const items = this.watcher && this.watcher.aggregatedChanges;
141
+ if (items && this.inputFileSystem && this.inputFileSystem.purge) {
142
+ const fs = this.inputFileSystem;
143
+ for (const item of items) {
144
+ fs.purge(item);
145
+ }
146
+ }
147
+ return items;
148
+ },
149
+ "Watcher.getAggregatedChanges is deprecated in favor of Watcher.getInfo since that's more performant.",
150
+ "DEP_WEBPACK_WATCHER_GET_AGGREGATED_CHANGES"
151
+ ),
152
+ getFileTimeInfoEntries: util.deprecate(
153
+ () => {
154
+ return fetchTimeInfo().fileTimeInfoEntries;
155
+ },
156
+ "Watcher.getFileTimeInfoEntries is deprecated in favor of Watcher.getInfo since that's more performant.",
157
+ "DEP_WEBPACK_WATCHER_FILE_TIME_INFO_ENTRIES"
158
+ ),
159
+ getContextTimeInfoEntries: util.deprecate(
160
+ () => {
161
+ return fetchTimeInfo().contextTimeInfoEntries;
162
+ },
163
+ "Watcher.getContextTimeInfoEntries is deprecated in favor of Watcher.getInfo since that's more performant.",
164
+ "DEP_WEBPACK_WATCHER_CONTEXT_TIME_INFO_ENTRIES"
165
+ ),
166
+ getInfo: () => {
167
+ const removals = this.watcher && this.watcher.aggregatedRemovals;
168
+ const changes = this.watcher && this.watcher.aggregatedChanges;
169
+ if (this.inputFileSystem && this.inputFileSystem.purge) {
115
170
  const fs = this.inputFileSystem;
116
- for (const item of items) {
117
- fs.purge(item);
171
+ if (removals) {
172
+ for (const item of removals) {
173
+ fs.purge(item);
174
+ }
175
+ }
176
+ if (changes) {
177
+ for (const item of changes) {
178
+ fs.purge(item);
179
+ }
118
180
  }
119
181
  }
120
- return items;
121
- },
122
- getFileTimeInfoEntries: () => {
123
- if (this.watcher) {
124
- return this.watcher.getTimeInfoEntries();
125
- } else {
126
- return new Map();
127
- }
128
- },
129
- getContextTimeInfoEntries: () => {
130
- if (this.watcher) {
131
- return this.watcher.getTimeInfoEntries();
132
- } else {
133
- return new Map();
134
- }
182
+ const { fileTimeInfoEntries, contextTimeInfoEntries } = fetchTimeInfo();
183
+ return {
184
+ changes,
185
+ removals,
186
+ fileTimeInfoEntries,
187
+ contextTimeInfoEntries
188
+ };
135
189
  }
136
190
  };
137
191
  }
@@ -46,7 +46,7 @@ class ReadFileChunkLoadingRuntimeModule extends RuntimeModule {
46
46
  );
47
47
  const conditionMap = chunkGraph.getChunkConditionMap(chunk, chunkHasJs);
48
48
  const hasJsMatcher = compileBooleanMatcher(conditionMap);
49
- const initialChunkIds = getInitialChunkIds(chunk, chunkGraph);
49
+ const initialChunkIds = getInitialChunkIds(chunk, chunkGraph, chunkHasJs);
50
50
 
51
51
  const outputName = this.compilation.getPath(
52
52
  getChunkFilenameTemplate(chunk, this.compilation.outputOptions),
@@ -46,7 +46,7 @@ class RequireChunkLoadingRuntimeModule extends RuntimeModule {
46
46
  );
47
47
  const conditionMap = chunkGraph.getChunkConditionMap(chunk, chunkHasJs);
48
48
  const hasJsMatcher = compileBooleanMatcher(conditionMap);
49
- const initialChunkIds = getInitialChunkIds(chunk, chunkGraph);
49
+ const initialChunkIds = getInitialChunkIds(chunk, chunkGraph, chunkHasJs);
50
50
 
51
51
  const outputName = this.compilation.getPath(
52
52
  getChunkFilenameTemplate(chunk, this.compilation.outputOptions),
@@ -39,6 +39,7 @@ const {
39
39
  /** @typedef {import("webpack-sources").Source} Source */
40
40
  /** @typedef {import("../../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
41
41
  /** @typedef {import("../ChunkGraph")} ChunkGraph */
42
+ /** @typedef {import("../CodeGenerationResults")} CodeGenerationResults */
42
43
  /** @typedef {import("../Compilation")} Compilation */
43
44
  /** @typedef {import("../Dependency")} Dependency */
44
45
  /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
@@ -1077,7 +1078,8 @@ class ConcatenatedModule extends Module {
1077
1078
  runtimeTemplate,
1078
1079
  moduleGraph,
1079
1080
  chunkGraph,
1080
- runtime: generationRuntime
1081
+ runtime: generationRuntime,
1082
+ codeGenerationResults
1081
1083
  }) {
1082
1084
  /** @type {Set<string>} */
1083
1085
  const runtimeRequirements = new Set();
@@ -1104,7 +1106,8 @@ class ConcatenatedModule extends Module {
1104
1106
  runtimeTemplate,
1105
1107
  moduleGraph,
1106
1108
  chunkGraph,
1107
- runtime
1109
+ runtime,
1110
+ codeGenerationResults
1108
1111
  );
1109
1112
  }
1110
1113
 
@@ -1634,6 +1637,7 @@ ${defineGetters}`
1634
1637
  * @param {ModuleGraph} moduleGraph moduleGraph
1635
1638
  * @param {ChunkGraph} chunkGraph chunkGraph
1636
1639
  * @param {RuntimeSpec} runtime runtime
1640
+ * @param {CodeGenerationResults} codeGenerationResults codeGenerationResults
1637
1641
  */
1638
1642
  _analyseModule(
1639
1643
  modulesMap,
@@ -1642,7 +1646,8 @@ ${defineGetters}`
1642
1646
  runtimeTemplate,
1643
1647
  moduleGraph,
1644
1648
  chunkGraph,
1645
- runtime
1649
+ runtime,
1650
+ codeGenerationResults
1646
1651
  ) {
1647
1652
  if (info.type === "concatenated") {
1648
1653
  const m = info.module;
@@ -1657,7 +1662,8 @@ ${defineGetters}`
1657
1662
  moduleGraph,
1658
1663
  chunkGraph,
1659
1664
  runtime,
1660
- concatenationScope
1665
+ concatenationScope,
1666
+ codeGenerationResults
1661
1667
  });
1662
1668
  const source = codeGenResult.sources.get("javascript");
1663
1669
  const data = codeGenResult.data;
@@ -1702,7 +1708,7 @@ ${defineGetters}`
1702
1708
  info.globalScope = globalScope;
1703
1709
  info.moduleScope = moduleScope;
1704
1710
  } catch (err) {
1705
- err.message += `\nwhile analysing module ${m.identifier()} for concatenation`;
1711
+ err.message += `\nwhile analyzing module ${m.identifier()} for concatenation`;
1706
1712
  throw err;
1707
1713
  }
1708
1714
  }