webpack 5.54.0 → 5.56.1

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/lib/Compiler.js CHANGED
@@ -40,9 +40,10 @@ const { isSourceEqual } = require("./util/source");
40
40
  /** @typedef {import("../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
41
41
  /** @typedef {import("../declarations/WebpackOptions").WebpackPluginInstance} WebpackPluginInstance */
42
42
  /** @typedef {import("./Chunk")} Chunk */
43
+ /** @typedef {import("./Dependency")} Dependency */
43
44
  /** @typedef {import("./FileSystemInfo").FileSystemInfoEntry} FileSystemInfoEntry */
44
- /** @typedef {import("./MemCache")} MemCache */
45
45
  /** @typedef {import("./Module")} Module */
46
+ /** @typedef {import("./util/WeakTupleMap")} WeakTupleMap */
46
47
  /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
47
48
  /** @typedef {import("./util/fs").IntermediateFileSystem} IntermediateFileSystem */
48
49
  /** @typedef {import("./util/fs").OutputFileSystem} OutputFileSystem */
@@ -248,7 +249,7 @@ class Compiler {
248
249
 
249
250
  this.cache = new Cache();
250
251
 
251
- /** @type {WeakMap<Module, { hash: string, memCache: MemCache }> | undefined} */
252
+ /** @type {WeakMap<Module, { hash: string, references: WeakMap<Dependency, Module>, memCache: WeakTupleMap }> | undefined} */
252
253
  this.moduleMemCaches = undefined;
253
254
 
254
255
  this.compilerPath = "";
@@ -1035,9 +1036,9 @@ ${other}`);
1035
1036
  return !!this.parentCompilation;
1036
1037
  }
1037
1038
 
1038
- createCompilation() {
1039
+ createCompilation(params) {
1039
1040
  this._cleanupLastCompilation();
1040
- return (this._lastCompilation = new Compilation(this));
1041
+ return (this._lastCompilation = new Compilation(this, params));
1041
1042
  }
1042
1043
 
1043
1044
  /**
@@ -1045,7 +1046,7 @@ ${other}`);
1045
1046
  * @returns {Compilation} the created compilation
1046
1047
  */
1047
1048
  newCompilation(params) {
1048
- const compilation = this.createCompilation();
1049
+ const compilation = this.createCompilation(params);
1049
1050
  compilation.name = this.name;
1050
1051
  compilation.records = this.records;
1051
1052
  this.hooks.thisCompilation.call(compilation, params);
@@ -13,7 +13,7 @@ const {
13
13
  evaluateToString,
14
14
  toConstantDependency
15
15
  } = require("./javascript/JavascriptParserHelpers");
16
- const { provide } = require("./util/MapHelpers");
16
+ const createHash = require("./util/createHash");
17
17
 
18
18
  /** @typedef {import("estree").Expression} Expression */
19
19
  /** @typedef {import("./Compiler")} Compiler */
@@ -250,7 +250,7 @@ const toCacheVersion = code => {
250
250
  };
251
251
 
252
252
  const VALUE_DEP_PREFIX = "webpack/DefinePlugin ";
253
- const VALUE_DEP_MAIN = "webpack/DefinePlugin";
253
+ const VALUE_DEP_MAIN = "webpack/DefinePlugin_hash";
254
254
 
255
255
  class DefinePlugin {
256
256
  /**
@@ -286,12 +286,11 @@ class DefinePlugin {
286
286
  );
287
287
  const { runtimeTemplate } = compilation;
288
288
 
289
- const mainValue = /** @type {Set<string>} */ (
290
- provide(
291
- compilation.valueCacheVersions,
292
- VALUE_DEP_MAIN,
293
- () => new Set()
294
- )
289
+ const mainHash = createHash(compilation.outputOptions.hashFunction);
290
+ mainHash.update(
291
+ /** @type {string} */ (
292
+ compilation.valueCacheVersions.get(VALUE_DEP_MAIN)
293
+ ) || ""
295
294
  );
296
295
 
297
296
  /**
@@ -300,6 +299,7 @@ class DefinePlugin {
300
299
  * @returns {void}
301
300
  */
302
301
  const handler = parser => {
302
+ const mainValue = compilation.valueCacheVersions.get(VALUE_DEP_MAIN);
303
303
  parser.hooks.program.tap("DefinePlugin", () => {
304
304
  const { buildInfo } = parser.state.module;
305
305
  if (!buildInfo.valueDependencies)
@@ -565,7 +565,7 @@ class DefinePlugin {
565
565
  const code = definitions[key];
566
566
  const version = toCacheVersion(code);
567
567
  const name = VALUE_DEP_PREFIX + prefix + key;
568
- mainValue.add(name);
568
+ mainHash.update("|" + prefix + key);
569
569
  const oldVersion = compilation.valueCacheVersions.get(name);
570
570
  if (oldVersion === undefined) {
571
571
  compilation.valueCacheVersions.set(name, version);
@@ -589,6 +589,11 @@ class DefinePlugin {
589
589
  };
590
590
 
591
591
  walkDefinitionsForValues(definitions, "");
592
+
593
+ compilation.valueCacheVersions.set(
594
+ VALUE_DEP_MAIN,
595
+ /** @type {string} */ (mainHash.digest("hex").slice(0, 8))
596
+ );
592
597
  }
593
598
  );
594
599
  }
package/lib/Dependency.js CHANGED
@@ -78,6 +78,8 @@ const memoize = require("./util/memoize");
78
78
  * @property {boolean=} canMangle when false, referenced export can not be mangled, defaults to true
79
79
  */
80
80
 
81
+ const TRANSITIVE = Symbol("transitive");
82
+
81
83
  const getIgnoredModule = memoize(() => {
82
84
  const RawModule = require("./RawModule");
83
85
  return new RawModule("/* (ignored) */", `ignored`, `(ignored)`);
@@ -175,6 +177,13 @@ class Dependency {
175
177
  return null;
176
178
  }
177
179
 
180
+ /**
181
+ * @returns {boolean | TRANSITIVE} true, when changes to the referenced module could affect the referencing module; TRANSITIVE, when changes to the referenced module could affect referencing modules of the referencing module
182
+ */
183
+ couldAffectReferencingModule() {
184
+ return TRANSITIVE;
185
+ }
186
+
178
187
  /**
179
188
  * Returns the referenced module and export
180
189
  * @deprecated
@@ -322,4 +331,6 @@ Object.defineProperty(Dependency.prototype, "disconnect", {
322
331
  }
323
332
  });
324
333
 
334
+ Dependency.TRANSITIVE = TRANSITIVE;
335
+
325
336
  module.exports = Dependency;
@@ -28,7 +28,6 @@ class FlagDependencyExportsPlugin {
28
28
  compilation => {
29
29
  const moduleGraph = compilation.moduleGraph;
30
30
  const cache = compilation.getCache("FlagDependencyExportsPlugin");
31
- const { moduleMemCaches } = compilation;
32
31
  compilation.hooks.finishModules.tapAsync(
33
32
  "FlagDependencyExportsPlugin",
34
33
  (modules, callback) => {
@@ -42,6 +41,8 @@ class FlagDependencyExportsPlugin {
42
41
  let statNotCached = 0;
43
42
  let statQueueItemsProcessed = 0;
44
43
 
44
+ const { moduleMemCaches } = compilation;
45
+
45
46
  /** @type {Queue<Module>} */
46
47
  const queue = new Queue();
47
48
 
@@ -68,12 +69,10 @@ class FlagDependencyExportsPlugin {
68
69
  return callback();
69
70
  }
70
71
  const memCache = moduleMemCaches && moduleMemCaches.get(module);
71
- const memCacheValue = memCache && memCache.get(this, module);
72
+ const memCacheValue = memCache && memCache.get(this);
72
73
  if (memCacheValue !== undefined) {
73
74
  statRestoredFromMemCache++;
74
- moduleGraph
75
- .getExportsInfo(module)
76
- .restoreProvided(memCacheValue);
75
+ exportsInfo.restoreProvided(memCacheValue);
77
76
  return callback();
78
77
  }
79
78
  cache.get(
@@ -84,9 +83,7 @@ class FlagDependencyExportsPlugin {
84
83
 
85
84
  if (result !== undefined) {
86
85
  statRestoredFromCache++;
87
- moduleGraph
88
- .getExportsInfo(module)
89
- .restoreProvided(result);
86
+ exportsInfo.restoreProvided(result);
90
87
  } else {
91
88
  statNotCached++;
92
89
  // Without cached info enqueue module for determining the exports
@@ -102,9 +99,7 @@ class FlagDependencyExportsPlugin {
102
99
  if (err) return callback(err);
103
100
 
104
101
  /** @type {Set<Module>} */
105
- const modulesToStorePersistent = new Set();
106
- /** @type {Set<Module>} */
107
- const modulesToStoreTransient = new Set();
102
+ const modulesToStore = new Set();
108
103
 
109
104
  /** @type {Map<Module, Set<Module>>} */
110
105
  const dependencies = new Map();
@@ -338,9 +333,7 @@ class FlagDependencyExportsPlugin {
338
333
  }
339
334
 
340
335
  if (cacheable) {
341
- modulesToStorePersistent.add(module);
342
- } else {
343
- modulesToStoreTransient.add(module);
336
+ modulesToStore.add(module);
344
337
  }
345
338
 
346
339
  if (changed) {
@@ -366,7 +359,7 @@ class FlagDependencyExportsPlugin {
366
359
 
367
360
  logger.time("store provided exports into cache");
368
361
  asyncLib.each(
369
- modulesToStorePersistent,
362
+ modulesToStore,
370
363
  (module, callback) => {
371
364
  if (typeof module.buildInfo.hash !== "string") {
372
365
  // not cacheable
@@ -378,7 +371,7 @@ class FlagDependencyExportsPlugin {
378
371
  const memCache =
379
372
  moduleMemCaches && moduleMemCaches.get(module);
380
373
  if (memCache) {
381
- memCache.set(this, module, cachedData);
374
+ memCache.set(this, cachedData);
382
375
  }
383
376
  cache.store(
384
377
  module.identifier(),
@@ -389,17 +382,6 @@ class FlagDependencyExportsPlugin {
389
382
  },
390
383
  err => {
391
384
  logger.timeEnd("store provided exports into cache");
392
- if (moduleMemCaches) {
393
- for (const module of modulesToStoreTransient) {
394
- const memCache = moduleMemCaches.get(module);
395
- if (memCache) {
396
- const cachedData = moduleGraph
397
- .getExportsInfo(module)
398
- .getRestoreProvidedData();
399
- memCache.set(this, module, cachedData);
400
- }
401
- }
402
- }
403
385
  callback(err);
404
386
  }
405
387
  );
@@ -15,6 +15,7 @@
15
15
  * @property {Set<string>=} fileDependencies
16
16
  * @property {Set<string>=} contextDependencies
17
17
  * @property {Set<string>=} missingDependencies
18
+ * @property {boolean=} cacheable allow to use the unsafe cache
18
19
  */
19
20
 
20
21
  /**
@@ -79,19 +79,19 @@ class ModuleGraphModule {
79
79
  this.profile = undefined;
80
80
  /** @type {boolean} */
81
81
  this.async = false;
82
+ /** @type {ModuleGraphConnection[]} */
83
+ this._unassignedConnections = undefined;
82
84
  }
83
85
  }
84
86
 
85
87
  class ModuleGraph {
86
88
  constructor() {
87
- /** @type {Map<Dependency, ModuleGraphConnection>} */
88
- this._dependencyMap = new Map();
89
+ /** @type {WeakMap<Dependency, ModuleGraphConnection>} */
90
+ this._dependencyMap = new WeakMap();
89
91
  /** @type {Map<Module, ModuleGraphModule>} */
90
92
  this._moduleMap = new Map();
91
- /** @type {Map<Module, Set<ModuleGraphConnection>>} */
92
- this._originMap = new Map();
93
- /** @type {Map<any, Object>} */
94
- this._metaMap = new Map();
93
+ /** @type {WeakMap<any, Object>} */
94
+ this._metaMap = new WeakMap();
95
95
 
96
96
  // Caching
97
97
  this._cacheModuleGraphModuleKey1 = undefined;
@@ -103,6 +103,9 @@ class ModuleGraph {
103
103
 
104
104
  /** @type {WeakTupleMap<any[], any>} */
105
105
  this._cache = undefined;
106
+
107
+ /** @type {WeakMap<Module, WeakTupleMap<any, any>>} */
108
+ this._moduleMemCaches = undefined;
106
109
  }
107
110
 
108
111
  /**
@@ -168,14 +171,21 @@ class ModuleGraph {
168
171
  dependency.weak,
169
172
  dependency.getCondition(this)
170
173
  );
171
- this._dependencyMap.set(dependency, connection);
172
174
  const connections = this._getModuleGraphModule(module).incomingConnections;
173
175
  connections.add(connection);
174
- const mgm = this._getModuleGraphModule(originModule);
175
- if (mgm.outgoingConnections === undefined) {
176
- mgm.outgoingConnections = new Set();
176
+ if (originModule) {
177
+ const mgm = this._getModuleGraphModule(originModule);
178
+ if (mgm._unassignedConnections === undefined) {
179
+ mgm._unassignedConnections = [];
180
+ }
181
+ mgm._unassignedConnections.push(connection);
182
+ if (mgm.outgoingConnections === undefined) {
183
+ mgm.outgoingConnections = new Set();
184
+ }
185
+ mgm.outgoingConnections.add(connection);
186
+ } else {
187
+ this._dependencyMap.set(dependency, connection);
177
188
  }
178
- mgm.outgoingConnections.add(connection);
179
189
  }
180
190
 
181
191
  /**
@@ -184,7 +194,7 @@ class ModuleGraph {
184
194
  * @returns {void}
185
195
  */
186
196
  updateModule(dependency, module) {
187
- const connection = this._dependencyMap.get(dependency);
197
+ const connection = this.getConnection(dependency);
188
198
  if (connection.module === module) return;
189
199
  const newConnection = connection.clone();
190
200
  newConnection.module = module;
@@ -201,12 +211,12 @@ class ModuleGraph {
201
211
  * @returns {void}
202
212
  */
203
213
  removeConnection(dependency) {
204
- const connection = this._dependencyMap.get(dependency);
214
+ const connection = this.getConnection(dependency);
205
215
  const targetMgm = this._getModuleGraphModule(connection.module);
206
216
  targetMgm.incomingConnections.delete(connection);
207
217
  const originMgm = this._getModuleGraphModule(connection.originModule);
208
218
  originMgm.outgoingConnections.delete(connection);
209
- this._dependencyMap.delete(dependency);
219
+ this._dependencyMap.set(dependency, null);
210
220
  }
211
221
 
212
222
  /**
@@ -215,7 +225,7 @@ class ModuleGraph {
215
225
  * @returns {void}
216
226
  */
217
227
  addExplanation(dependency, explanation) {
218
- const connection = this._dependencyMap.get(dependency);
228
+ const connection = this.getConnection(dependency);
219
229
  connection.addExplanation(explanation);
220
230
  }
221
231
 
@@ -341,7 +351,7 @@ class ModuleGraph {
341
351
  * @returns {Module} the referenced module
342
352
  */
343
353
  getResolvedModule(dependency) {
344
- const connection = this._dependencyMap.get(dependency);
354
+ const connection = this.getConnection(dependency);
345
355
  return connection !== undefined ? connection.resolvedModule : null;
346
356
  }
347
357
 
@@ -351,7 +361,30 @@ class ModuleGraph {
351
361
  */
352
362
  getConnection(dependency) {
353
363
  const connection = this._dependencyMap.get(dependency);
354
- return connection;
364
+ if (connection === undefined) {
365
+ const module = this.getParentModule(dependency);
366
+ if (module !== undefined) {
367
+ const mgm = this._getModuleGraphModule(module);
368
+ if (
369
+ mgm._unassignedConnections &&
370
+ mgm._unassignedConnections.length !== 0
371
+ ) {
372
+ let foundConnection;
373
+ for (const connection of mgm._unassignedConnections) {
374
+ this._dependencyMap.set(connection.dependency, connection);
375
+ if (connection.dependency === dependency)
376
+ foundConnection = connection;
377
+ }
378
+ mgm._unassignedConnections.length = 0;
379
+ if (foundConnection !== undefined) {
380
+ return foundConnection;
381
+ }
382
+ }
383
+ }
384
+ this._dependencyMap.set(dependency, null);
385
+ return undefined;
386
+ }
387
+ return connection === null ? undefined : connection;
355
388
  }
356
389
 
357
390
  /**
@@ -359,7 +392,7 @@ class ModuleGraph {
359
392
  * @returns {Module} the referenced module
360
393
  */
361
394
  getModule(dependency) {
362
- const connection = this._dependencyMap.get(dependency);
395
+ const connection = this.getConnection(dependency);
363
396
  return connection !== undefined ? connection.module : null;
364
397
  }
365
398
 
@@ -368,7 +401,7 @@ class ModuleGraph {
368
401
  * @returns {Module} the referencing module
369
402
  */
370
403
  getOrigin(dependency) {
371
- const connection = this._dependencyMap.get(dependency);
404
+ const connection = this.getConnection(dependency);
372
405
  return connection !== undefined ? connection.originModule : null;
373
406
  }
374
407
 
@@ -377,7 +410,7 @@ class ModuleGraph {
377
410
  * @returns {Module} the original referencing module
378
411
  */
379
412
  getResolvedOrigin(dependency) {
380
- const connection = this._dependencyMap.get(dependency);
413
+ const connection = this.getConnection(dependency);
381
414
  return connection !== undefined ? connection.resolvedOriginModule : null;
382
415
  }
383
416
 
@@ -669,12 +702,17 @@ class ModuleGraph {
669
702
  return this._metaMap.get(thing);
670
703
  }
671
704
 
672
- freeze() {
705
+ /**
706
+ * @param {string=} cacheStage a persistent stage name for caching
707
+ */
708
+ freeze(cacheStage) {
673
709
  this._cache = new WeakTupleMap();
710
+ this._cacheStage = cacheStage;
674
711
  }
675
712
 
676
713
  unfreeze() {
677
714
  this._cache = undefined;
715
+ this._cacheStage = undefined;
678
716
  }
679
717
 
680
718
  /**
@@ -689,6 +727,37 @@ class ModuleGraph {
689
727
  return this._cache.provide(fn, ...args, () => fn(this, ...args));
690
728
  }
691
729
 
730
+ /**
731
+ * @param {WeakMap<Module, WeakTupleMap<any, any>>} moduleMemCaches mem caches for modules for better caching
732
+ */
733
+ setModuleMemCaches(moduleMemCaches) {
734
+ this._moduleMemCaches = moduleMemCaches;
735
+ }
736
+
737
+ /**
738
+ * @param {Dependency} dependency dependency
739
+ * @param {...any} args arguments, last argument is a function called with moduleGraph, dependency, ...args
740
+ * @returns {any} computed value or cached
741
+ */
742
+ dependencyCacheProvide(dependency, ...args) {
743
+ /** @type {(moduleGraph: ModuleGraph, dependency: Dependency, ...args: any[]) => any} */
744
+ const fn = args.pop();
745
+ if (this._moduleMemCaches && this._cacheStage) {
746
+ const memCache = this._moduleMemCaches.get(
747
+ this.getParentModule(dependency)
748
+ );
749
+ if (memCache !== undefined) {
750
+ return memCache.provide(dependency, this._cacheStage, ...args, () =>
751
+ fn(this, dependency, ...args)
752
+ );
753
+ }
754
+ }
755
+ if (this._cache === undefined) return fn(this, dependency, ...args);
756
+ return this._cache.provide(dependency, ...args, () =>
757
+ fn(this, dependency, ...args)
758
+ );
759
+ }
760
+
692
761
  // TODO remove in webpack 6
693
762
  /**
694
763
  * @param {Module} module the module
@@ -167,12 +167,6 @@ const deprecationChangedHookMessage = (name, hook) => {
167
167
  );
168
168
  };
169
169
 
170
- /** @type {WeakMap<ModuleDependency, ModuleFactoryResult & { module: { restoreFromUnsafeCache: Function }}>} */
171
- const unsafeCacheDependencies = new WeakMap();
172
-
173
- /** @type {WeakMap<Module, object>} */
174
- const unsafeCacheData = new WeakMap();
175
-
176
170
  const ruleSetCompiler = new RuleSetCompiler([
177
171
  new BasicMatcherRulePlugin("test", "resource"),
178
172
  new BasicMatcherRulePlugin("scheme"),
@@ -256,11 +250,6 @@ class NormalModuleFactory extends ModuleFactory {
256
250
  rules: options.rules
257
251
  }
258
252
  ]);
259
- this.unsafeCache = !!options.unsafeCache;
260
- this.cachePredicate =
261
- typeof options.unsafeCache === "function"
262
- ? options.unsafeCache
263
- : () => true;
264
253
  this.context = context || "";
265
254
  this.fs = fs;
266
255
  this._globalParserOptions = options.parser;
@@ -745,18 +734,6 @@ class NormalModuleFactory extends ModuleFactory {
745
734
  */
746
735
  create(data, callback) {
747
736
  const dependencies = /** @type {ModuleDependency[]} */ (data.dependencies);
748
- if (this.unsafeCache) {
749
- const cacheEntry = unsafeCacheDependencies.get(dependencies[0]);
750
- if (cacheEntry) {
751
- const { module } = cacheEntry;
752
- if (!this._restoredUnsafeCacheEntries.has(module)) {
753
- const data = unsafeCacheData.get(module);
754
- module.restoreFromUnsafeCache(data, this);
755
- this._restoredUnsafeCacheEntries.add(module);
756
- }
757
- return callback(null, cacheEntry);
758
- }
759
- }
760
737
  const context = data.context || this.context;
761
738
  const resolveOptions = data.resolveOptions || EMPTY_RESOLVE_OPTIONS;
762
739
  const dependency = dependencies[0];
@@ -788,7 +765,8 @@ class NormalModuleFactory extends ModuleFactory {
788
765
  return callback(err, {
789
766
  fileDependencies,
790
767
  missingDependencies,
791
- contextDependencies
768
+ contextDependencies,
769
+ cacheable: false
792
770
  });
793
771
  }
794
772
 
@@ -797,7 +775,8 @@ class NormalModuleFactory extends ModuleFactory {
797
775
  return callback(null, {
798
776
  fileDependencies,
799
777
  missingDependencies,
800
- contextDependencies
778
+ contextDependencies,
779
+ cacheable: resolveData.cacheable
801
780
  });
802
781
  }
803
782
 
@@ -814,7 +793,8 @@ class NormalModuleFactory extends ModuleFactory {
814
793
  return callback(err, {
815
794
  fileDependencies,
816
795
  missingDependencies,
817
- contextDependencies
796
+ contextDependencies,
797
+ cacheable: false
818
798
  });
819
799
  }
820
800
 
@@ -822,25 +802,10 @@ class NormalModuleFactory extends ModuleFactory {
822
802
  module,
823
803
  fileDependencies,
824
804
  missingDependencies,
825
- contextDependencies
805
+ contextDependencies,
806
+ cacheable: resolveData.cacheable
826
807
  };
827
808
 
828
- if (
829
- this.unsafeCache &&
830
- resolveData.cacheable &&
831
- module &&
832
- module.restoreFromUnsafeCache &&
833
- this.cachePredicate(module)
834
- ) {
835
- for (const d of dependencies) {
836
- unsafeCacheDependencies.set(d, factoryResult);
837
- }
838
- if (!unsafeCacheData.has(module)) {
839
- unsafeCacheData.set(module, module.getUnsafeCacheData());
840
- }
841
- this._restoredUnsafeCacheEntries.add(module);
842
- }
843
-
844
809
  callback(null, factoryResult);
845
810
  });
846
811
  });
@@ -18,6 +18,7 @@ const processExportInfo = require("./processExportInfo");
18
18
  /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
19
19
  /** @typedef {import("../Dependency").ExportsSpec} ExportsSpec */
20
20
  /** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
21
+ /** @typedef {import("../Dependency").TRANSITIVE} TRANSITIVE */
21
22
  /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
22
23
  /** @typedef {import("../Module")} Module */
23
24
  /** @typedef {import("../ModuleGraph")} ModuleGraph */
@@ -43,6 +44,13 @@ class CommonJsExportRequireDependency extends ModuleDependency {
43
44
  return "cjs export require";
44
45
  }
45
46
 
47
+ /**
48
+ * @returns {boolean | TRANSITIVE} true, when changes to the referenced module could affect the referencing module; TRANSITIVE, when changes to the referenced module could affect referencing modules of the referencing module
49
+ */
50
+ couldAffectReferencingModule() {
51
+ return Dependency.TRANSITIVE;
52
+ }
53
+
46
54
  /**
47
55
  * @param {ModuleGraph} moduleGraph the module graph
48
56
  * @returns {string[]} the imported id
@@ -332,15 +340,17 @@ CommonJsExportRequireDependency.Template = class CommonJsExportRequireDependency
332
340
  weak: dep.weak,
333
341
  runtimeRequirements
334
342
  });
335
- const ids = dep.getIds(moduleGraph);
336
- const usedImported = moduleGraph
337
- .getExportsInfo(importedModule)
338
- .getUsedName(ids, runtime);
339
- if (usedImported) {
340
- const comment = equals(usedImported, ids)
341
- ? ""
342
- : Template.toNormalComment(propertyAccess(ids)) + " ";
343
- requireExpr += `${comment}${propertyAccess(usedImported)}`;
343
+ if (importedModule) {
344
+ const ids = dep.getIds(moduleGraph);
345
+ const usedImported = moduleGraph
346
+ .getExportsInfo(importedModule)
347
+ .getUsedName(ids, runtime);
348
+ if (usedImported) {
349
+ const comment = equals(usedImported, ids)
350
+ ? ""
351
+ : Template.toNormalComment(propertyAccess(ids)) + " ";
352
+ requireExpr += `${comment}${propertyAccess(usedImported)}`;
353
+ }
344
354
  }
345
355
 
346
356
  switch (type) {
@@ -108,15 +108,17 @@ CommonJsFullRequireDependency.Template = class CommonJsFullRequireDependencyTemp
108
108
  weak: dep.weak,
109
109
  runtimeRequirements
110
110
  });
111
- const ids = dep.names;
112
- const usedImported = moduleGraph
113
- .getExportsInfo(importedModule)
114
- .getUsedName(ids, runtime);
115
- if (usedImported) {
116
- const comment = equals(usedImported, ids)
117
- ? ""
118
- : Template.toNormalComment(propertyAccess(ids)) + " ";
119
- requireExpr += `${comment}${propertyAccess(usedImported)}`;
111
+ if (importedModule) {
112
+ const ids = dep.names;
113
+ const usedImported = moduleGraph
114
+ .getExportsInfo(importedModule)
115
+ .getUsedName(ids, runtime);
116
+ if (usedImported) {
117
+ const comment = equals(usedImported, ids)
118
+ ? ""
119
+ : Template.toNormalComment(propertyAccess(ids)) + " ";
120
+ requireExpr += `${comment}${propertyAccess(usedImported)}`;
121
+ }
120
122
  }
121
123
  source.replace(dep.range[0], dep.range[1] - 1, requireExpr);
122
124
  }
@@ -11,6 +11,7 @@ const makeSerializable = require("../util/makeSerializable");
11
11
  const memoize = require("../util/memoize");
12
12
 
13
13
  /** @typedef {import("../ContextModule").ContextOptions} ContextOptions */
14
+ /** @typedef {import("../Dependency").TRANSITIVE} TRANSITIVE */
14
15
  /** @typedef {import("../ModuleGraph")} ModuleGraph */
15
16
  /** @typedef {import("../WebpackError")} WebpackError */
16
17
 
@@ -54,6 +55,13 @@ class ContextDependency extends Dependency {
54
55
  return "commonjs";
55
56
  }
56
57
 
58
+ /**
59
+ * @returns {boolean | TRANSITIVE} true, when changes to the referenced module could affect the referencing module; TRANSITIVE, when changes to the referenced module could affect referencing modules of the referencing module
60
+ */
61
+ couldAffectReferencingModule() {
62
+ return true;
63
+ }
64
+
57
65
  /**
58
66
  * @returns {string | null} an identifier to merge equal requests
59
67
  */