webpack 5.41.1 → 5.44.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 (40) hide show
  1. package/lib/Compiler.js +14 -1
  2. package/lib/ExternalModule.js +23 -0
  3. package/lib/ExternalModuleFactoryPlugin.js +1 -1
  4. package/lib/FlagDependencyUsagePlugin.js +5 -1
  5. package/lib/NormalModuleFactory.js +13 -2
  6. package/lib/Template.js +1 -0
  7. package/lib/WebpackOptionsApply.js +2 -1
  8. package/lib/asset/AssetGenerator.js +2 -2
  9. package/lib/cache/PackFileCacheStrategy.js +26 -15
  10. package/lib/config/defaults.js +1 -0
  11. package/lib/config/normalization.js +1 -0
  12. package/lib/container/ContainerPlugin.js +4 -1
  13. package/lib/container/ModuleFederationPlugin.js +1 -0
  14. package/lib/dependencies/WorkerPlugin.js +1 -1
  15. package/lib/esm/ExportWebpackRequireRuntimeModule.js +29 -0
  16. package/lib/esm/ModuleChunkFormatPlugin.js +91 -11
  17. package/lib/esm/ModuleChunkLoadingPlugin.js +13 -0
  18. package/lib/esm/ModuleChunkLoadingRuntimeModule.js +46 -37
  19. package/lib/hmr/lazyCompilationBackend.js +5 -2
  20. package/lib/json/JsonData.js +41 -0
  21. package/lib/json/JsonGenerator.js +8 -2
  22. package/lib/json/JsonParser.js +2 -1
  23. package/lib/optimize/ConcatenatedModule.js +16 -0
  24. package/lib/optimize/RuntimeChunkPlugin.js +1 -1
  25. package/lib/rules/RuleSetCompiler.js +2 -2
  26. package/lib/serialization/BinaryMiddleware.js +26 -21
  27. package/lib/serialization/FileMiddleware.js +82 -6
  28. package/lib/util/internalSerializables.js +1 -0
  29. package/lib/util/makeSerializable.js +0 -1
  30. package/package.json +13 -13
  31. package/schemas/WebpackOptions.check.js +1 -1
  32. package/schemas/WebpackOptions.json +15 -3
  33. package/schemas/plugins/container/ContainerPlugin.check.js +1 -1
  34. package/schemas/plugins/container/ContainerPlugin.json +15 -0
  35. package/schemas/plugins/container/ContainerReferencePlugin.check.js +1 -1
  36. package/schemas/plugins/container/ContainerReferencePlugin.json +2 -1
  37. package/schemas/plugins/container/ExternalsType.check.js +1 -1
  38. package/schemas/plugins/container/ModuleFederationPlugin.check.js +1 -1
  39. package/schemas/plugins/container/ModuleFederationPlugin.json +17 -1
  40. package/types.d.ts +26 -6
package/lib/Compiler.js CHANGED
@@ -260,6 +260,8 @@ class Compiler {
260
260
 
261
261
  /** @type {Compilation} */
262
262
  this._lastCompilation = undefined;
263
+ /** @type {NormalModuleFactory} */
264
+ this._lastNormalModuleFactory = undefined;
263
265
 
264
266
  /** @private @type {WeakMap<Source, { sizeOnlySource: SizeOnlySource, writtenTo: Map<string, number> }>} */
265
267
  this._assetEmittingSourceCache = new WeakMap();
@@ -375,6 +377,14 @@ class Compiler {
375
377
  }
376
378
  }
377
379
 
380
+ // TODO webpack 6: solve this in a better way
381
+ _cleanupLastNormalModuleFactory() {
382
+ if (this._lastNormalModuleFactory !== undefined) {
383
+ this._lastNormalModuleFactory.cleanupForCache();
384
+ this._lastNormalModuleFactory = undefined;
385
+ }
386
+ }
387
+
378
388
  /**
379
389
  * @param {WatchOptions} watchOptions the watcher's options
380
390
  * @param {Callback<Stats>} handler signals when the call finishes
@@ -1036,6 +1046,7 @@ ${other}`);
1036
1046
  }
1037
1047
 
1038
1048
  createNormalModuleFactory() {
1049
+ this._cleanupLastNormalModuleFactory();
1039
1050
  const normalModuleFactory = new NormalModuleFactory({
1040
1051
  context: this.options.context,
1041
1052
  fs: this.inputFileSystem,
@@ -1044,6 +1055,7 @@ ${other}`);
1044
1055
  associatedObjectForCache: this.root,
1045
1056
  layers: this.options.experiments.layers
1046
1057
  });
1058
+ this._lastNormalModuleFactory = normalModuleFactory;
1047
1059
  this.hooks.normalModuleFactory.call(normalModuleFactory);
1048
1060
  return normalModuleFactory;
1049
1061
  }
@@ -1122,8 +1134,9 @@ ${other}`);
1122
1134
  if (err) return callback(err);
1123
1135
  // Get rid of reference to last compilation to avoid leaking memory
1124
1136
  // We can't run this._cleanupLastCompilation() as the Stats to this compilation
1125
- // might be still in use. We try to get rid for the reference to the cache instead.
1137
+ // might be still in use. We try to get rid of the reference to the cache instead.
1126
1138
  this._lastCompilation = undefined;
1139
+ this._lastNormalModuleFactory = undefined;
1127
1140
  this.cache.shutdown(callback);
1128
1141
  });
1129
1142
  }
@@ -16,6 +16,7 @@ const StaticExportsDependency = require("./dependencies/StaticExportsDependency"
16
16
  const extractUrlAndGlobal = require("./util/extractUrlAndGlobal");
17
17
  const makeSerializable = require("./util/makeSerializable");
18
18
  const propertyAccess = require("./util/propertyAccess");
19
+ const { register } = require("./util/serialization");
19
20
 
20
21
  /** @typedef {import("webpack-sources").Source} Source */
21
22
  /** @typedef {import("../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
@@ -169,6 +170,8 @@ class ModuleExternalInitFragment extends InitFragment {
169
170
  `external module import ${id}`
170
171
  );
171
172
  this._identifier = identifier;
173
+ this._id = id;
174
+ this._request = request;
172
175
  }
173
176
 
174
177
  getNamespaceIdentifier() {
@@ -176,6 +179,21 @@ class ModuleExternalInitFragment extends InitFragment {
176
179
  }
177
180
  }
178
181
 
182
+ register(
183
+ ModuleExternalInitFragment,
184
+ "webpack/lib/ExternalModule",
185
+ "ModuleExternalInitFragment",
186
+ {
187
+ serialize(obj, { write }) {
188
+ write(obj._id);
189
+ write(obj._request);
190
+ },
191
+ deserialize({ read }) {
192
+ return new ModuleExternalInitFragment(read(), read());
193
+ }
194
+ }
195
+ );
196
+
179
197
  const generateModuleRemapping = (input, exportsInfo, runtime) => {
180
198
  if (exportsInfo.otherExportsInfo.getUsed(runtime) === UsageState.Unused) {
181
199
  const properties = [];
@@ -429,6 +447,11 @@ class ExternalModule extends Module {
429
447
  this.buildInfo.strict = false;
430
448
  break;
431
449
  case "system":
450
+ if (!Array.isArray(request) || request.length === 1) {
451
+ this.buildMeta.exportsType = "namespace";
452
+ canMangle = true;
453
+ }
454
+ break;
432
455
  case "module":
433
456
  if (this.buildInfo.module) {
434
457
  if (!Array.isArray(request) || request.length === 1) {
@@ -12,7 +12,7 @@ const { resolveByProperty, cachedSetProperty } = require("./util/cleverMerge");
12
12
  /** @typedef {import("../declarations/WebpackOptions").Externals} Externals */
13
13
  /** @typedef {import("./NormalModuleFactory")} NormalModuleFactory */
14
14
 
15
- const UNSPECIFIED_EXTERNAL_TYPE_REGEXP = /^[a-z0-9]+ /;
15
+ const UNSPECIFIED_EXTERNAL_TYPE_REGEXP = /^[a-z0-9-]+ /;
16
16
  const EMPTY_RESOLVE_OPTIONS = {};
17
17
 
18
18
  // TODO webpack 6 remove this
@@ -180,7 +180,11 @@ class FlagDependencyUsagePlugin {
180
180
  b.groupOptions &&
181
181
  b.groupOptions.entryOptions
182
182
  ) {
183
- processModule(b, b.groupOptions.entryOptions.runtime, true);
183
+ processModule(
184
+ b,
185
+ b.groupOptions.entryOptions.runtime || undefined,
186
+ true
187
+ );
184
188
  } else {
185
189
  queue.enqueue(b);
186
190
  }
@@ -13,8 +13,10 @@ const {
13
13
  SyncHook,
14
14
  HookMap
15
15
  } = require("tapable");
16
+ const ChunkGraph = require("./ChunkGraph");
16
17
  const Module = require("./Module");
17
18
  const ModuleFactory = require("./ModuleFactory");
19
+ const ModuleGraph = require("./ModuleGraph");
18
20
  const NormalModule = require("./NormalModule");
19
21
  const BasicEffectRulePlugin = require("./rules/BasicEffectRulePlugin");
20
22
  const BasicMatcherRulePlugin = require("./rules/BasicMatcherRulePlugin");
@@ -257,8 +259,8 @@ class NormalModuleFactory extends ModuleFactory {
257
259
  this.parserCache = new Map();
258
260
  /** @type {Map<string, WeakMap<Object, Generator>>} */
259
261
  this.generatorCache = new Map();
260
- /** @type {WeakSet<Module>} */
261
- this._restoredUnsafeCacheEntries = new WeakSet();
262
+ /** @type {Set<Module>} */
263
+ this._restoredUnsafeCacheEntries = new Set();
262
264
 
263
265
  const cacheParseResource = parseResource.bindCache(
264
266
  associatedObjectForCache
@@ -661,6 +663,14 @@ class NormalModuleFactory extends ModuleFactory {
661
663
  );
662
664
  }
663
665
 
666
+ cleanupForCache() {
667
+ for (const module of this._restoredUnsafeCacheEntries) {
668
+ ChunkGraph.clearChunkGraphForModule(module);
669
+ ModuleGraph.clearModuleGraphForModule(module);
670
+ module.cleanupForCache();
671
+ }
672
+ }
673
+
664
674
  /**
665
675
  * @param {ModuleFactoryCreateData} data data object
666
676
  * @param {function(Error=, ModuleFactoryResult=): void} callback callback
@@ -756,6 +766,7 @@ class NormalModuleFactory extends ModuleFactory {
756
766
  if (!unsafeCacheData.has(module)) {
757
767
  unsafeCacheData.set(module, module.getUnsafeCacheData());
758
768
  }
769
+ this._restoredUnsafeCacheEntries.add(module);
759
770
  }
760
771
 
761
772
  callback(null, factoryResult);
package/lib/Template.js CHANGED
@@ -379,6 +379,7 @@ class Template {
379
379
  source.add(Template.toNormalComment(module.identifier()) + "\n");
380
380
  if (!module.shouldIsolate()) {
381
381
  source.add(runtimeSource);
382
+ source.add("\n\n");
382
383
  } else if (renderContext.runtimeTemplate.supportsArrowFunction()) {
383
384
  source.add("(() => {\n");
384
385
  if (renderContext.useStrict) source.add('\t"use strict";\n');
@@ -572,7 +572,8 @@ class WebpackOptionsApply extends OptionsApply {
572
572
  snapshot: options.snapshot,
573
573
  maxAge: cacheOptions.maxAge,
574
574
  profile: cacheOptions.profile,
575
- allowCollectingMemory: cacheOptions.allowCollectingMemory
575
+ allowCollectingMemory: cacheOptions.allowCollectingMemory,
576
+ compression: cacheOptions.compression
576
577
  }),
577
578
  cacheOptions.idleTimeout,
578
579
  cacheOptions.idleTimeoutForInitialStore,
@@ -265,7 +265,7 @@ class AssetGenerator extends Generator {
265
265
  * @returns {Set<string>} available types (do not mutate)
266
266
  */
267
267
  getTypes(module) {
268
- if (module.buildInfo.dataUrl || this.emit === false) {
268
+ if ((module.buildInfo && module.buildInfo.dataUrl) || this.emit === false) {
269
269
  return JS_TYPES;
270
270
  } else {
271
271
  return JS_AND_ASSET_TYPES;
@@ -289,7 +289,7 @@ class AssetGenerator extends Generator {
289
289
  return originalSource.size();
290
290
  }
291
291
  default:
292
- if (module.buildInfo.dataUrl) {
292
+ if (module.buildInfo && module.buildInfo.dataUrl) {
293
293
  const originalSource = module.originalSource();
294
294
 
295
295
  if (!originalSource) {
@@ -848,6 +848,7 @@ class PackFileCacheStrategy {
848
848
  * @param {number} options.maxAge max age of cache items
849
849
  * @param {boolean} options.profile track and log detailed timing information for individual cache items
850
850
  * @param {boolean} options.allowCollectingMemory allow to collect unused memory created during deserialization
851
+ * @param {false | "gzip" | "brotli"} options.compression compression used
851
852
  */
852
853
  constructor({
853
854
  compiler,
@@ -859,7 +860,8 @@ class PackFileCacheStrategy {
859
860
  snapshot,
860
861
  maxAge,
861
862
  profile,
862
- allowCollectingMemory
863
+ allowCollectingMemory,
864
+ compression
863
865
  }) {
864
866
  this.fileSerializer = createFileSerializer(fs);
865
867
  this.fileSystemInfo = new FileSystemInfo(fs, {
@@ -875,6 +877,13 @@ class PackFileCacheStrategy {
875
877
  this.maxAge = maxAge;
876
878
  this.profile = profile;
877
879
  this.allowCollectingMemory = allowCollectingMemory;
880
+ this.compression = compression;
881
+ this._extension =
882
+ compression === "brotli"
883
+ ? ".pack.br"
884
+ : compression === "gzip"
885
+ ? ".pack.gz"
886
+ : ".pack";
878
887
  this.snapshot = snapshot;
879
888
  /** @type {Set<string>} */
880
889
  this.buildDependencies = new Set();
@@ -916,8 +925,8 @@ class PackFileCacheStrategy {
916
925
  logger.time("restore cache container");
917
926
  return this.fileSerializer
918
927
  .deserialize(null, {
919
- filename: `${cacheLocation}/index.pack`,
920
- extension: ".pack",
928
+ filename: `${cacheLocation}/index${this._extension}`,
929
+ extension: `${this._extension}`,
921
930
  logger,
922
931
  profile,
923
932
  retainedBuffer: this.allowCollectingMemory
@@ -927,11 +936,13 @@ class PackFileCacheStrategy {
927
936
  .catch(err => {
928
937
  if (err.code !== "ENOENT") {
929
938
  logger.warn(
930
- `Restoring pack failed from ${cacheLocation}.pack: ${err}`
939
+ `Restoring pack failed from ${cacheLocation}${this._extension}: ${err}`
931
940
  );
932
941
  logger.debug(err.stack);
933
942
  } else {
934
- logger.debug(`No pack exists at ${cacheLocation}.pack: ${err}`);
943
+ logger.debug(
944
+ `No pack exists at ${cacheLocation}${this._extension}: ${err}`
945
+ );
935
946
  }
936
947
  return undefined;
937
948
  })
@@ -940,14 +951,14 @@ class PackFileCacheStrategy {
940
951
  if (!packContainer) return undefined;
941
952
  if (!(packContainer instanceof PackContainer)) {
942
953
  logger.warn(
943
- `Restored pack from ${cacheLocation}.pack, but contained content is unexpected.`,
954
+ `Restored pack from ${cacheLocation}${this._extension}, but contained content is unexpected.`,
944
955
  packContainer
945
956
  );
946
957
  return undefined;
947
958
  }
948
959
  if (packContainer.version !== version) {
949
960
  logger.log(
950
- `Restored pack from ${cacheLocation}.pack, but version doesn't match.`
961
+ `Restored pack from ${cacheLocation}${this._extension}, but version doesn't match.`
951
962
  );
952
963
  return undefined;
953
964
  }
@@ -959,14 +970,14 @@ class PackFileCacheStrategy {
959
970
  (err, valid) => {
960
971
  if (err) {
961
972
  logger.log(
962
- `Restored pack from ${cacheLocation}.pack, but checking snapshot of build dependencies errored: ${err}.`
973
+ `Restored pack from ${cacheLocation}${this._extension}, but checking snapshot of build dependencies errored: ${err}.`
963
974
  );
964
975
  logger.debug(err.stack);
965
976
  return resolve(false);
966
977
  }
967
978
  if (!valid) {
968
979
  logger.log(
969
- `Restored pack from ${cacheLocation}.pack, but build dependencies have changed.`
980
+ `Restored pack from ${cacheLocation}${this._extension}, but build dependencies have changed.`
970
981
  );
971
982
  return resolve(false);
972
983
  }
@@ -981,7 +992,7 @@ class PackFileCacheStrategy {
981
992
  (err, valid) => {
982
993
  if (err) {
983
994
  logger.log(
984
- `Restored pack from ${cacheLocation}.pack, but checking snapshot of resolving of build dependencies errored: ${err}.`
995
+ `Restored pack from ${cacheLocation}${this._extension}, but checking snapshot of resolving of build dependencies errored: ${err}.`
985
996
  );
986
997
  logger.debug(err.stack);
987
998
  return resolve(false);
@@ -1001,7 +1012,7 @@ class PackFileCacheStrategy {
1001
1012
  (err, valid) => {
1002
1013
  if (err) {
1003
1014
  logger.log(
1004
- `Restored pack from ${cacheLocation}.pack, but resolving of build dependencies errored: ${err}.`
1015
+ `Restored pack from ${cacheLocation}${this._extension}, but resolving of build dependencies errored: ${err}.`
1005
1016
  );
1006
1017
  logger.debug(err.stack);
1007
1018
  return resolve(false);
@@ -1012,7 +1023,7 @@ class PackFileCacheStrategy {
1012
1023
  return resolve(true);
1013
1024
  }
1014
1025
  logger.log(
1015
- `Restored pack from ${cacheLocation}.pack, but build dependencies resolve to different locations.`
1026
+ `Restored pack from ${cacheLocation}${this._extension}, but build dependencies resolve to different locations.`
1016
1027
  );
1017
1028
  return resolve(false);
1018
1029
  }
@@ -1052,7 +1063,7 @@ class PackFileCacheStrategy {
1052
1063
  })
1053
1064
  .catch(err => {
1054
1065
  this.logger.warn(
1055
- `Restoring pack from ${cacheLocation}.pack failed: ${err}`
1066
+ `Restoring pack from ${cacheLocation}${this._extension} failed: ${err}`
1056
1067
  );
1057
1068
  this.logger.debug(err.stack);
1058
1069
  return new Pack(logger, this.maxAge);
@@ -1236,8 +1247,8 @@ class PackFileCacheStrategy {
1236
1247
  );
1237
1248
  return this.fileSerializer
1238
1249
  .serialize(content, {
1239
- filename: `${this.cacheLocation}/index.pack`,
1240
- extension: ".pack",
1250
+ filename: `${this.cacheLocation}/index${this._extension}`,
1251
+ extension: `${this._extension}`,
1241
1252
  logger: this.logger,
1242
1253
  profile: this.profile
1243
1254
  })
@@ -298,6 +298,7 @@ const applyCacheDefaults = (cache, { name, mode, development }) => {
298
298
  );
299
299
  D(cache, "hashAlgorithm", "md4");
300
300
  D(cache, "store", "pack");
301
+ D(cache, "compression", development ? false : "gzip");
301
302
  D(cache, "profile", false);
302
303
  D(cache, "idleTimeout", 60000);
303
304
  D(cache, "idleTimeoutForInitialStore", 5000);
@@ -137,6 +137,7 @@ const getNormalizedWebpackOptions = config => {
137
137
  cacheDirectory: cache.cacheDirectory,
138
138
  cacheLocation: cache.cacheLocation,
139
139
  hashAlgorithm: cache.hashAlgorithm,
140
+ compression: cache.compression,
140
141
  idleTimeout: cache.idleTimeout,
141
142
  idleTimeoutForInitialStore: cache.idleTimeoutForInitialStore,
142
143
  idleTimeoutAfterLargeChanges: cache.idleTimeoutAfterLargeChanges,
@@ -39,6 +39,7 @@ class ContainerPlugin {
39
39
  type: "var",
40
40
  name: options.name
41
41
  },
42
+ runtime: options.runtime,
42
43
  filename: options.filename || undefined,
43
44
  exposes: parseOptions(
44
45
  options.exposes,
@@ -60,7 +61,8 @@ class ContainerPlugin {
60
61
  * @returns {void}
61
62
  */
62
63
  apply(compiler) {
63
- const { name, exposes, shareScope, filename, library } = this._options;
64
+ const { name, exposes, shareScope, filename, library, runtime } =
65
+ this._options;
64
66
 
65
67
  compiler.options.output.enabledLibraryTypes.push(library.type);
66
68
 
@@ -73,6 +75,7 @@ class ContainerPlugin {
73
75
  {
74
76
  name,
75
77
  filename,
78
+ runtime,
76
79
  library
77
80
  },
78
81
  error => {
@@ -64,6 +64,7 @@ class ModuleFederationPlugin {
64
64
  name: options.name,
65
65
  library,
66
66
  filename: options.filename,
67
+ runtime: options.runtime,
67
68
  exposes: options.exposes
68
69
  }).apply(compiler);
69
70
  }
@@ -270,7 +270,7 @@ class WorkerPlugin {
270
270
  entryOptions.name = options.name;
271
271
  }
272
272
 
273
- if (!entryOptions.runtime) {
273
+ if (entryOptions.runtime === undefined) {
274
274
  let i = workerIndexMap.get(parser.state) || 0;
275
275
  workerIndexMap.set(parser.state, i + 1);
276
276
  let name = `${cachedContextify(
@@ -0,0 +1,29 @@
1
+ /*
2
+ MIT License http://www.opensource.org/licenses/mit-license.php
3
+ */
4
+
5
+ "use strict";
6
+
7
+ const RuntimeModule = require("../RuntimeModule");
8
+
9
+ class ExportWebpackRequireRuntimeModule extends RuntimeModule {
10
+ constructor() {
11
+ super("export webpack runtime", RuntimeModule.STAGE_ATTACH);
12
+ }
13
+
14
+ /**
15
+ * @returns {boolean} true, if the runtime module should get it's own scope
16
+ */
17
+ shouldIsolate() {
18
+ return false;
19
+ }
20
+
21
+ /**
22
+ * @returns {string} runtime code
23
+ */
24
+ generate() {
25
+ return "export default __webpack_require__;";
26
+ }
27
+ }
28
+
29
+ module.exports = ExportWebpackRequireRuntimeModule;
@@ -5,13 +5,18 @@
5
5
 
6
6
  "use strict";
7
7
 
8
- const { ConcatSource } = require("webpack-sources");
8
+ const { ConcatSource, RawSource } = require("webpack-sources");
9
9
  const { RuntimeGlobals } = require("..");
10
10
  const HotUpdateChunk = require("../HotUpdateChunk");
11
11
  const Template = require("../Template");
12
12
  const {
13
- getCompilationHooks
13
+ getCompilationHooks,
14
+ getChunkFilenameTemplate
14
15
  } = require("../javascript/JavascriptModulesPlugin");
16
+ const {
17
+ generateEntryStartup,
18
+ updateHashForEntryStartup
19
+ } = require("../javascript/StartupHelpers");
15
20
 
16
21
  /** @typedef {import("../Compiler")} Compiler */
17
22
 
@@ -30,8 +35,9 @@ class ModuleChunkFormatPlugin {
30
35
  (chunk, set) => {
31
36
  if (chunk.hasRuntime()) return;
32
37
  if (compilation.chunkGraph.getNumberOfEntryModules(chunk) > 0) {
33
- set.add(RuntimeGlobals.onChunksLoaded);
34
38
  set.add(RuntimeGlobals.require);
39
+ set.add(RuntimeGlobals.startupEntrypoint);
40
+ set.add(RuntimeGlobals.externalInstallChunk);
35
41
  }
36
42
  }
37
43
  );
@@ -39,7 +45,7 @@ class ModuleChunkFormatPlugin {
39
45
  hooks.renderChunk.tap(
40
46
  "ModuleChunkFormatPlugin",
41
47
  (modules, renderContext) => {
42
- const { chunk, chunkGraph } = renderContext;
48
+ const { chunk, chunkGraph, runtimeTemplate } = renderContext;
43
49
  const hotUpdateChunk =
44
50
  chunk instanceof HotUpdateChunk ? chunk : null;
45
51
  const source = new ConcatSource();
@@ -68,9 +74,84 @@ class ModuleChunkFormatPlugin {
68
74
  chunkGraph.getChunkEntryModulesWithChunkGroupIterable(chunk)
69
75
  );
70
76
  if (entries.length > 0) {
71
- throw new Error(
72
- "Entry modules in chunk is not implemented for module chunk format yet"
77
+ const runtimeChunk = entries[0][1].getRuntimeChunk();
78
+ const currentOutputName = compilation
79
+ .getPath(
80
+ getChunkFilenameTemplate(chunk, compilation.outputOptions),
81
+ {
82
+ chunk,
83
+ contentHashType: "javascript"
84
+ }
85
+ )
86
+ .split("/");
87
+ const runtimeOutputName = compilation
88
+ .getPath(
89
+ getChunkFilenameTemplate(
90
+ runtimeChunk,
91
+ compilation.outputOptions
92
+ ),
93
+ {
94
+ chunk: runtimeChunk,
95
+ contentHashType: "javascript"
96
+ }
97
+ )
98
+ .split("/");
99
+
100
+ // remove filename, we only need the directory
101
+ const outputFilename = currentOutputName.pop();
102
+
103
+ // remove common parts
104
+ while (
105
+ currentOutputName.length > 0 &&
106
+ runtimeOutputName.length > 0 &&
107
+ currentOutputName[0] === runtimeOutputName[0]
108
+ ) {
109
+ currentOutputName.shift();
110
+ runtimeOutputName.shift();
111
+ }
112
+
113
+ // create final path
114
+ const runtimePath =
115
+ (currentOutputName.length > 0
116
+ ? "../".repeat(currentOutputName.length)
117
+ : "./") + runtimeOutputName.join("/");
118
+
119
+ const entrySource = new ConcatSource();
120
+ entrySource.add(source);
121
+ entrySource.add(";\n\n// load runtime\n");
122
+ entrySource.add(
123
+ `import __webpack_require__ from ${JSON.stringify(
124
+ runtimePath
125
+ )};\n`
126
+ );
127
+ entrySource.add(
128
+ `import * as __webpack_self_exports__ from ${JSON.stringify(
129
+ "./" + outputFilename
130
+ )};\n`
131
+ );
132
+ entrySource.add(
133
+ `${RuntimeGlobals.externalInstallChunk}(__webpack_self_exports__);\n`
134
+ );
135
+ const startupSource = new RawSource(
136
+ generateEntryStartup(
137
+ chunkGraph,
138
+ runtimeTemplate,
139
+ entries,
140
+ chunk,
141
+ false
142
+ )
143
+ );
144
+ entrySource.add(
145
+ hooks.renderStartup.call(
146
+ startupSource,
147
+ entries[entries.length - 1][0],
148
+ {
149
+ ...renderContext,
150
+ inlined: false
151
+ }
152
+ )
73
153
  );
154
+ return entrySource;
74
155
  }
75
156
  }
76
157
  return source;
@@ -82,11 +163,10 @@ class ModuleChunkFormatPlugin {
82
163
  if (chunk.hasRuntime()) return;
83
164
  hash.update("ModuleChunkFormatPlugin");
84
165
  hash.update("1");
85
- // TODO
86
- // const entries = Array.from(
87
- // chunkGraph.getChunkEntryModulesWithChunkGroupIterable(chunk)
88
- // );
89
- // updateHashForEntryStartup(hash, chunkGraph, entries, chunk);
166
+ const entries = Array.from(
167
+ chunkGraph.getChunkEntryModulesWithChunkGroupIterable(chunk)
168
+ );
169
+ updateHashForEntryStartup(hash, chunkGraph, entries, chunk);
90
170
  }
91
171
  );
92
172
  }
@@ -6,6 +6,7 @@
6
6
  "use strict";
7
7
 
8
8
  const RuntimeGlobals = require("../RuntimeGlobals");
9
+ const ExportWebpackRequireRuntimeModule = require("./ExportWebpackRequireRuntimeModule");
9
10
  const ModuleChunkLoadingRuntimeModule = require("./ModuleChunkLoadingRuntimeModule");
10
11
 
11
12
  /** @typedef {import("../Compiler")} Compiler */
@@ -45,9 +46,21 @@ class ModuleChunkLoadingPlugin {
45
46
  compilation.hooks.runtimeRequirementInTree
46
47
  .for(RuntimeGlobals.baseURI)
47
48
  .tap("ModuleChunkLoadingPlugin", handler);
49
+ compilation.hooks.runtimeRequirementInTree
50
+ .for(RuntimeGlobals.externalInstallChunk)
51
+ .tap("ModuleChunkLoadingPlugin", handler);
48
52
  compilation.hooks.runtimeRequirementInTree
49
53
  .for(RuntimeGlobals.onChunksLoaded)
50
54
  .tap("ModuleChunkLoadingPlugin", handler);
55
+ compilation.hooks.runtimeRequirementInTree
56
+ .for(RuntimeGlobals.externalInstallChunk)
57
+ .tap("ModuleChunkLoadingPlugin", (chunk, set) => {
58
+ if (!isEnabledForChunk(chunk)) return;
59
+ compilation.addRuntimeModule(
60
+ chunk,
61
+ new ExportWebpackRequireRuntimeModule()
62
+ );
63
+ });
51
64
 
52
65
  compilation.hooks.runtimeRequirementInTree
53
66
  .for(RuntimeGlobals.ensureChunkHandlers)