webpack 5.58.0 → 5.59.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/ChunkGraph.js CHANGED
@@ -6,7 +6,6 @@
6
6
  "use strict";
7
7
 
8
8
  const util = require("util");
9
- const ChunkCombination = require("./ChunkCombination");
10
9
  const Entrypoint = require("./Entrypoint");
11
10
  const ModuleGraphConnection = require("./ModuleGraphConnection");
12
11
  const { first } = require("./util/SetHelpers");
@@ -41,8 +40,6 @@ const {
41
40
  /** @type {ReadonlySet<string>} */
42
41
  const EMPTY_SET = new Set();
43
42
 
44
- const EMPTY_RUNTIME_SPEC_SET = new RuntimeSpecSet();
45
-
46
43
  const ZERO_BIG_INT = BigInt(0);
47
44
 
48
45
  const compareModuleIterables = compareIterables(compareModulesByIdentifier);
@@ -180,7 +177,8 @@ const isAvailableChunk = (a, b) => {
180
177
 
181
178
  class ChunkGraphModule {
182
179
  constructor() {
183
- this.chunkCombination = ChunkCombination.empty;
180
+ /** @type {SortableSet<Chunk>} */
181
+ this.chunks = new SortableSet();
184
182
  /** @type {Set<Chunk> | undefined} */
185
183
  this.entryInChunks = undefined;
186
184
  /** @type {Set<Chunk> | undefined} */
@@ -303,7 +301,7 @@ class ChunkGraph {
303
301
  connectChunkAndModule(chunk, module) {
304
302
  const cgm = this._getChunkGraphModule(module);
305
303
  const cgc = this._getChunkGraphChunk(chunk);
306
- cgm.chunkCombination = cgm.chunkCombination.with(chunk);
304
+ cgm.chunks.add(chunk);
307
305
  cgc.modules.add(module);
308
306
  }
309
307
 
@@ -316,7 +314,7 @@ class ChunkGraph {
316
314
  const cgm = this._getChunkGraphModule(module);
317
315
  const cgc = this._getChunkGraphChunk(chunk);
318
316
  cgc.modules.delete(module);
319
- cgm.chunkCombination = cgm.chunkCombination.without(chunk);
317
+ cgm.chunks.delete(chunk);
320
318
  }
321
319
 
322
320
  /**
@@ -327,7 +325,7 @@ class ChunkGraph {
327
325
  const cgc = this._getChunkGraphChunk(chunk);
328
326
  for (const module of cgc.modules) {
329
327
  const cgm = this._getChunkGraphModule(module);
330
- cgm.chunkCombination = cgm.chunkCombination.without(chunk);
328
+ cgm.chunks.delete(chunk);
331
329
  }
332
330
  cgc.modules.clear();
333
331
  chunk.disconnectFromGroups();
@@ -394,13 +392,13 @@ class ChunkGraph {
394
392
  const oldCgm = this._getChunkGraphModule(oldModule);
395
393
  const newCgm = this._getChunkGraphModule(newModule);
396
394
 
397
- for (const chunk of oldCgm.chunkCombination._chunks) {
395
+ for (const chunk of oldCgm.chunks) {
398
396
  const cgc = this._getChunkGraphChunk(chunk);
399
397
  cgc.modules.delete(oldModule);
400
398
  cgc.modules.add(newModule);
401
- newCgm.chunkCombination = newCgm.chunkCombination.with(chunk);
399
+ newCgm.chunks.add(chunk);
402
400
  }
403
- oldCgm.chunkCombination = ChunkCombination.empty;
401
+ oldCgm.chunks.clear();
404
402
 
405
403
  if (oldCgm.entryInChunks !== undefined) {
406
404
  if (newCgm.entryInChunks === undefined) {
@@ -487,22 +485,13 @@ class ChunkGraph {
487
485
  return cgm.entryInChunks !== undefined;
488
486
  }
489
487
 
490
- /**
491
- * @param {Module} module the module
492
- * @returns {ChunkCombination} chunk combination (do not modify)
493
- */
494
- getModuleChunkCombination(module) {
495
- const cgm = this._getChunkGraphModule(module);
496
- return cgm.chunkCombination;
497
- }
498
-
499
488
  /**
500
489
  * @param {Module} module the module
501
490
  * @returns {Iterable<Chunk>} iterable of chunks (do not modify)
502
491
  */
503
492
  getModuleChunksIterable(module) {
504
493
  const cgm = this._getChunkGraphModule(module);
505
- return cgm.chunkCombination._chunks;
494
+ return cgm.chunks;
506
495
  }
507
496
 
508
497
  /**
@@ -512,9 +501,8 @@ class ChunkGraph {
512
501
  */
513
502
  getOrderedModuleChunksIterable(module, sortFn) {
514
503
  const cgm = this._getChunkGraphModule(module);
515
- const chunks = cgm.chunkCombination._chunks;
516
- chunks.sortWith(sortFn);
517
- return chunks;
504
+ cgm.chunks.sortWith(sortFn);
505
+ return cgm.chunks;
518
506
  }
519
507
 
520
508
  /**
@@ -523,7 +511,7 @@ class ChunkGraph {
523
511
  */
524
512
  getModuleChunks(module) {
525
513
  const cgm = this._getChunkGraphModule(module);
526
- return cgm.chunkCombination.getChunks();
514
+ return cgm.chunks.getFromCache(getArray);
527
515
  }
528
516
 
529
517
  /**
@@ -532,7 +520,7 @@ class ChunkGraph {
532
520
  */
533
521
  getNumberOfModuleChunks(module) {
534
522
  const cgm = this._getChunkGraphModule(module);
535
- return cgm.chunkCombination.size;
523
+ return cgm.chunks.size;
536
524
  }
537
525
 
538
526
  /**
@@ -541,10 +529,7 @@ class ChunkGraph {
541
529
  */
542
530
  getModuleRuntimes(module) {
543
531
  const cgm = this._getChunkGraphModule(module);
544
- if (cgm.chunkCombination.size === 0) return EMPTY_RUNTIME_SPEC_SET;
545
- return cgm.chunkCombination._chunks.getFromUnorderedCache(
546
- getModuleRuntimes
547
- );
532
+ return cgm.chunks.getFromUnorderedCache(getModuleRuntimes);
548
533
  }
549
534
 
550
535
  /**
@@ -908,7 +893,8 @@ class ChunkGraph {
908
893
  // Merge runtime
909
894
  chunkA.runtime = mergeRuntime(chunkA.runtime, chunkB.runtime);
910
895
 
911
- for (const module of this.getChunkModulesIterable(chunkB)) {
896
+ // getChunkModules is used here to create a clone, because disconnectChunkAndModule modifies
897
+ for (const module of this.getChunkModules(chunkB)) {
912
898
  this.disconnectChunkAndModule(chunkB, module);
913
899
  this.connectChunkAndModule(chunkA, module);
914
900
  }
@@ -416,10 +416,10 @@ const byLocation = compareSelect(err => err.loc, compareLocations);
416
416
 
417
417
  const compareErrors = concatComparators(byModule, byLocation, byMessage);
418
418
 
419
- /** @type {WeakMap<Dependency, Module & { restoreFromUnsafeCache: Function }>} */
419
+ /** @type {WeakMap<Dependency, Module & { restoreFromUnsafeCache: Function } | null>} */
420
420
  const unsafeCacheDependencies = new WeakMap();
421
421
 
422
- /** @type {WeakMap<Module, object>} */
422
+ /** @type {WeakMap<Module & { restoreFromUnsafeCache: Function }, object>} */
423
423
  const unsafeCacheData = new WeakMap();
424
424
 
425
425
  class Compilation {
@@ -1023,8 +1023,10 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
1023
1023
  this.usedModuleIds = null;
1024
1024
  /** @type {boolean} */
1025
1025
  this.needAdditionalPass = false;
1026
- /** @type {Set<Module>} */
1027
- this._restoredUnsafeCacheEntries = new Set();
1026
+ /** @type {Set<Module & { restoreFromUnsafeCache: Function }>} */
1027
+ this._restoredUnsafeCacheModuleEntries = new Set();
1028
+ /** @type {Map<string, Module & { restoreFromUnsafeCache: Function }>} */
1029
+ this._restoredUnsafeCacheEntries = new Map();
1028
1030
  /** @type {WeakSet<Module>} */
1029
1031
  this.builtModules = new WeakSet();
1030
1032
  /** @type {WeakSet<Module>} */
@@ -1424,9 +1426,7 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
1424
1426
  * @returns {void}
1425
1427
  */
1426
1428
  _processModuleDependencies(module, callback) {
1427
- /**
1428
- * @type {Array<{factory: ModuleFactory, dependencies: Dependency[], originModule: Module|null}>}
1429
- */
1429
+ /** @type {Array<{factory: ModuleFactory, dependencies: Dependency[], originModule: Module|null}>} */
1430
1430
  const sortedDependencies = [];
1431
1431
 
1432
1432
  /** @type {DependenciesBlock} */
@@ -1447,7 +1447,46 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
1447
1447
  /** @type {Dependency[]} */
1448
1448
  let listCacheValue;
1449
1449
 
1450
- const unsafeRestoredModules = new Set();
1450
+ let inProgressSorting = 1;
1451
+ let inProgressTransitive = 1;
1452
+
1453
+ const onDependenciesSorted = err => {
1454
+ if (err) return callback(err);
1455
+
1456
+ // early exit without changing parallelism back and forth
1457
+ if (sortedDependencies.length === 0 && inProgressTransitive === 1) {
1458
+ return callback();
1459
+ }
1460
+
1461
+ // This is nested so we need to allow one additional task
1462
+ this.processDependenciesQueue.increaseParallelism();
1463
+
1464
+ for (const item of sortedDependencies) {
1465
+ inProgressTransitive++;
1466
+ this.handleModuleCreation(item, err => {
1467
+ // In V8, the Error objects keep a reference to the functions on the stack. These warnings &
1468
+ // errors are created inside closures that keep a reference to the Compilation, so errors are
1469
+ // leaking the Compilation object.
1470
+ if (err && this.bail) {
1471
+ if (inProgressTransitive <= 0) return;
1472
+ inProgressTransitive = -1;
1473
+ // eslint-disable-next-line no-self-assign
1474
+ err.stack = err.stack;
1475
+ onTransitiveTasksFinished(err);
1476
+ return;
1477
+ }
1478
+ if (--inProgressTransitive === 0) onTransitiveTasksFinished();
1479
+ });
1480
+ }
1481
+ if (--inProgressTransitive === 0) onTransitiveTasksFinished();
1482
+ };
1483
+
1484
+ const onTransitiveTasksFinished = err => {
1485
+ if (err) return callback(err);
1486
+ this.processDependenciesQueue.decreaseParallelism();
1487
+
1488
+ return callback();
1489
+ };
1451
1490
 
1452
1491
  /**
1453
1492
  * @param {Dependency} dep dependency
@@ -1458,34 +1497,111 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
1458
1497
  this.moduleGraph.setParents(dep, currentBlock, module, index);
1459
1498
  if (this._unsafeCache) {
1460
1499
  try {
1461
- const cachedModule = unsafeCacheDependencies.get(dep);
1462
- if (cachedModule === null) return;
1463
- if (cachedModule !== undefined) {
1464
- if (!this._restoredUnsafeCacheEntries.has(cachedModule)) {
1465
- const data = unsafeCacheData.get(cachedModule);
1466
- cachedModule.restoreFromUnsafeCache(
1467
- data,
1468
- this.params.normalModuleFactory,
1469
- this.params
1500
+ const unsafeCachedModule = unsafeCacheDependencies.get(dep);
1501
+ if (unsafeCachedModule === null) return;
1502
+ if (unsafeCachedModule !== undefined) {
1503
+ if (
1504
+ this._restoredUnsafeCacheModuleEntries.has(unsafeCachedModule)
1505
+ ) {
1506
+ this._handleExistingModuleFromUnsafeCache(
1507
+ module,
1508
+ dep,
1509
+ unsafeCachedModule
1510
+ );
1511
+ return;
1512
+ }
1513
+ const identifier = unsafeCachedModule.identifier();
1514
+ const cachedModule =
1515
+ this._restoredUnsafeCacheEntries.get(identifier);
1516
+ if (cachedModule !== undefined) {
1517
+ // update unsafe cache to new module
1518
+ unsafeCacheDependencies.set(dep, cachedModule);
1519
+ this._handleExistingModuleFromUnsafeCache(
1520
+ module,
1521
+ dep,
1522
+ cachedModule
1470
1523
  );
1471
- this._restoredUnsafeCacheEntries.add(cachedModule);
1472
- if (!this.modules.has(cachedModule)) {
1473
- this._handleNewModuleFromUnsafeCache(module, dep, cachedModule);
1474
- unsafeRestoredModules.add(cachedModule);
1524
+ return;
1525
+ }
1526
+ inProgressSorting++;
1527
+ this._modulesCache.get(identifier, null, (err, cachedModule) => {
1528
+ if (err) {
1529
+ if (inProgressSorting <= 0) return;
1530
+ inProgressSorting = -1;
1531
+ onDependenciesSorted(err);
1475
1532
  return;
1476
1533
  }
1477
- }
1478
- this._handleExistingModuleFromUnsafeCache(
1479
- module,
1480
- dep,
1481
- cachedModule
1482
- );
1534
+ try {
1535
+ if (!this._restoredUnsafeCacheEntries.has(identifier)) {
1536
+ const data = unsafeCacheData.get(cachedModule);
1537
+ if (data === undefined) {
1538
+ processDependencyForResolving(dep);
1539
+ if (--inProgressSorting === 0) onDependenciesSorted();
1540
+ return;
1541
+ }
1542
+ if (cachedModule !== unsafeCachedModule) {
1543
+ unsafeCacheDependencies.set(dep, cachedModule);
1544
+ }
1545
+ cachedModule.restoreFromUnsafeCache(
1546
+ data,
1547
+ this.params.normalModuleFactory,
1548
+ this.params
1549
+ );
1550
+ this._restoredUnsafeCacheEntries.set(
1551
+ identifier,
1552
+ cachedModule
1553
+ );
1554
+ this._restoredUnsafeCacheModuleEntries.add(cachedModule);
1555
+ if (!this.modules.has(cachedModule)) {
1556
+ inProgressTransitive++;
1557
+ this._handleNewModuleFromUnsafeCache(
1558
+ module,
1559
+ dep,
1560
+ cachedModule,
1561
+ err => {
1562
+ if (err) {
1563
+ if (inProgressTransitive <= 0) return;
1564
+ inProgressTransitive = -1;
1565
+ onTransitiveTasksFinished(err);
1566
+ }
1567
+ if (--inProgressTransitive === 0)
1568
+ return onTransitiveTasksFinished();
1569
+ }
1570
+ );
1571
+ if (--inProgressSorting === 0) onDependenciesSorted();
1572
+ return;
1573
+ }
1574
+ }
1575
+ if (unsafeCachedModule !== cachedModule) {
1576
+ unsafeCacheDependencies.set(dep, cachedModule);
1577
+ }
1578
+ this._handleExistingModuleFromUnsafeCache(
1579
+ module,
1580
+ dep,
1581
+ cachedModule
1582
+ ); // a3
1583
+ } catch (err) {
1584
+ if (inProgressSorting <= 0) return;
1585
+ inProgressSorting = -1;
1586
+ onDependenciesSorted(err);
1587
+ return;
1588
+ }
1589
+ if (--inProgressSorting === 0) onDependenciesSorted();
1590
+ });
1483
1591
  return;
1484
1592
  }
1485
1593
  } catch (e) {
1486
1594
  console.error(e);
1487
1595
  }
1488
1596
  }
1597
+ processDependencyForResolving(dep);
1598
+ };
1599
+
1600
+ /**
1601
+ * @param {Dependency} dep dependency
1602
+ * @returns {void}
1603
+ */
1604
+ const processDependencyForResolving = dep => {
1489
1605
  const resourceIdent = dep.getResourceIdentifier();
1490
1606
  if (resourceIdent !== undefined && resourceIdent !== null) {
1491
1607
  const category = dep.category;
@@ -1570,68 +1686,10 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
1570
1686
  return callback(e);
1571
1687
  }
1572
1688
 
1573
- if (sortedDependencies.length === 0 && unsafeRestoredModules.size === 0) {
1574
- callback();
1575
- return;
1576
- }
1577
-
1578
- // This is nested so we need to allow one additional task
1579
- this.processDependenciesQueue.increaseParallelism();
1580
-
1581
- const processSortedDependency = (item, callback) => {
1582
- this.handleModuleCreation(item, err => {
1583
- // In V8, the Error objects keep a reference to the functions on the stack. These warnings &
1584
- // errors are created inside closures that keep a reference to the Compilation, so errors are
1585
- // leaking the Compilation object.
1586
- if (err && this.bail) {
1587
- // eslint-disable-next-line no-self-assign
1588
- err.stack = err.stack;
1589
- return callback(err);
1590
- }
1591
- callback();
1592
- });
1593
- };
1594
-
1595
- const processUnsafeRestoredModule = (item, callback) => {
1596
- this._handleModuleBuildAndDependencies(module, item, true, callback);
1597
- };
1598
-
1599
- const finalCallback = err => {
1600
- this.processDependenciesQueue.decreaseParallelism();
1601
-
1602
- return callback(err);
1603
- };
1604
-
1605
- if (sortedDependencies.length === 0) {
1606
- asyncLib.forEach(
1607
- unsafeRestoredModules,
1608
- processUnsafeRestoredModule,
1609
- finalCallback
1610
- );
1611
- } else if (unsafeRestoredModules.size === 0) {
1612
- asyncLib.forEach(
1613
- sortedDependencies,
1614
- processSortedDependency,
1615
- finalCallback
1616
- );
1617
- } else {
1618
- asyncLib.parallel(
1619
- [
1620
- cb =>
1621
- asyncLib.forEach(
1622
- unsafeRestoredModules,
1623
- processUnsafeRestoredModule,
1624
- cb
1625
- ),
1626
- cb =>
1627
- asyncLib.forEach(sortedDependencies, processSortedDependency, cb)
1628
- ],
1629
- finalCallback
1630
- );
1631
- }
1689
+ if (--inProgressSorting === 0) onDependenciesSorted();
1632
1690
  }
1633
1691
 
1634
- _handleNewModuleFromUnsafeCache(originModule, dependency, module) {
1692
+ _handleNewModuleFromUnsafeCache(originModule, dependency, module, callback) {
1635
1693
  const moduleGraph = this.moduleGraph;
1636
1694
 
1637
1695
  moduleGraph.setResolvedModule(originModule, dependency, module);
@@ -1644,6 +1702,13 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
1644
1702
  this._modules.set(module.identifier(), module);
1645
1703
  this.modules.add(module);
1646
1704
  ModuleGraph.setModuleGraphForModule(module, this.moduleGraph);
1705
+
1706
+ this._handleModuleBuildAndDependencies(
1707
+ originModule,
1708
+ module,
1709
+ true,
1710
+ callback
1711
+ );
1647
1712
  }
1648
1713
 
1649
1714
  _handleExistingModuleFromUnsafeCache(originModule, dependency, module) {
@@ -1747,20 +1812,24 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
1747
1812
  /** @type {any} */ (module).restoreFromUnsafeCache &&
1748
1813
  this._unsafeCachePredicate(module)
1749
1814
  ) {
1815
+ const unsafeCacheableModule =
1816
+ /** @type {Module & { restoreFromUnsafeCache: Function }} */ (
1817
+ module
1818
+ );
1750
1819
  for (let i = 0; i < dependencies.length; i++) {
1751
1820
  const dependency = dependencies[i];
1752
1821
  moduleGraph.setResolvedModule(
1753
1822
  connectOrigin ? originModule : null,
1754
1823
  dependency,
1755
- module
1756
- );
1757
- unsafeCacheDependencies.set(
1758
- dependency,
1759
- /** @type {any} */ (module)
1824
+ unsafeCacheableModule
1760
1825
  );
1826
+ unsafeCacheDependencies.set(dependency, unsafeCacheableModule);
1761
1827
  }
1762
- if (!unsafeCacheData.has(module)) {
1763
- unsafeCacheData.set(module, module.getUnsafeCacheData());
1828
+ if (!unsafeCacheData.has(unsafeCacheableModule)) {
1829
+ unsafeCacheData.set(
1830
+ unsafeCacheableModule,
1831
+ unsafeCacheableModule.getUnsafeCacheData()
1832
+ );
1764
1833
  }
1765
1834
  } else {
1766
1835
  applyFactoryResultDependencies();
@@ -1814,7 +1883,7 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
1814
1883
  creatingModuleDuringBuildSet
1815
1884
  );
1816
1885
  }
1817
- creatingModuleDuringBuildSet.add(originModule);
1886
+ creatingModuleDuringBuildSet.add(module);
1818
1887
 
1819
1888
  // When building is blocked by another module
1820
1889
  // search for a cycle, cancel the cycle by throwing
@@ -3633,7 +3702,7 @@ Or do you want to use the entrypoints '${name}' and '${runtime}' independently o
3633
3702
  this.moduleGraph.removeConnection(dep);
3634
3703
 
3635
3704
  if (this.chunkGraph) {
3636
- for (const chunk of this.chunkGraph.getModuleChunksIterable(
3705
+ for (const chunk of this.chunkGraph.getModuleChunks(
3637
3706
  originalModule
3638
3707
  )) {
3639
3708
  this.patchChunksAfterReasonRemoval(originalModule, chunk);
@@ -4618,21 +4687,18 @@ This prevents using hashes of each other and should be avoided.`);
4618
4687
  * @returns {void}
4619
4688
  */
4620
4689
  (module, push, callback) => {
4621
- this.addModuleQueue.waitFor(module, err => {
4690
+ this.buildQueue.waitFor(module, err => {
4622
4691
  if (err) return callback(err);
4623
- this.buildQueue.waitFor(module, err => {
4692
+ this.processDependenciesQueue.waitFor(module, err => {
4624
4693
  if (err) return callback(err);
4625
- this.processDependenciesQueue.waitFor(module, err => {
4626
- if (err) return callback(err);
4627
- for (const {
4628
- module: m
4629
- } of this.moduleGraph.getOutgoingConnections(module)) {
4630
- const size = modules.size;
4631
- modules.add(m);
4632
- if (modules.size !== size) push(m);
4633
- }
4634
- callback();
4635
- });
4694
+ for (const { module: m } of this.moduleGraph.getOutgoingConnections(
4695
+ module
4696
+ )) {
4697
+ const size = modules.size;
4698
+ modules.add(m);
4699
+ if (modules.size !== size) push(m);
4700
+ }
4701
+ callback();
4636
4702
  });
4637
4703
  });
4638
4704
  },
package/lib/Compiler.js CHANGED
@@ -219,9 +219,9 @@ class Compiler {
219
219
  /** @type {string|null} */
220
220
  this.recordsOutputPath = null;
221
221
  this.records = {};
222
- /** @type {Set<string>} */
222
+ /** @type {Set<string | RegExp>} */
223
223
  this.managedPaths = new Set();
224
- /** @type {Set<string>} */
224
+ /** @type {Set<string | RegExp>} */
225
225
  this.immutablePaths = new Set();
226
226
 
227
227
  /** @type {ReadonlySet<string>} */
@@ -151,7 +151,7 @@ const stringifyObj = (
151
151
  case false:
152
152
  return arr ? `;${code}` : `;(${code})`;
153
153
  default:
154
- return `Object(${code})`;
154
+ return `/*#__PURE__*/Object(${code})`;
155
155
  }
156
156
  };
157
157