webpack 4.37.0 → 4.38.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.
- package/lib/Chunk.js +5 -1
- package/lib/ChunkGroup.js +1 -1
- package/lib/Compilation.js +5 -467
- package/lib/ProgressPlugin.js +0 -1
- package/lib/SourceMapDevToolPlugin.js +25 -17
- package/lib/WebpackOptionsDefaulter.js +6 -1
- package/lib/buildChunkGraph.js +689 -0
- package/package.json +1 -1
package/lib/Chunk.js
CHANGED
@@ -355,7 +355,11 @@ class Chunk {
|
|
355
355
|
return this._modules.getFromUnorderedCache(getModulesIdent);
|
356
356
|
}
|
357
357
|
|
358
|
-
|
358
|
+
/**
|
359
|
+
* @param {string=} reason reason why chunk is removed
|
360
|
+
* @returns {void}
|
361
|
+
*/
|
362
|
+
remove(reason) {
|
359
363
|
// cleanup modules
|
360
364
|
// Array.from is used here to create a clone, because removeChunk modifies this._modules
|
361
365
|
for (const module of Array.from(this._modules)) {
|
package/lib/ChunkGroup.js
CHANGED
package/lib/Compilation.js
CHANGED
@@ -27,17 +27,16 @@ const HotUpdateChunkTemplate = require("./HotUpdateChunkTemplate");
|
|
27
27
|
const ModuleTemplate = require("./ModuleTemplate");
|
28
28
|
const RuntimeTemplate = require("./RuntimeTemplate");
|
29
29
|
const ChunkRenderError = require("./ChunkRenderError");
|
30
|
-
const AsyncDependencyToInitialChunkError = require("./AsyncDependencyToInitialChunkError");
|
31
30
|
const Stats = require("./Stats");
|
32
31
|
const Semaphore = require("./util/Semaphore");
|
33
32
|
const createHash = require("./util/createHash");
|
34
|
-
const Queue = require("./util/Queue");
|
35
33
|
const SortableSet = require("./util/SortableSet");
|
36
34
|
const GraphHelpers = require("./GraphHelpers");
|
37
35
|
const ModuleDependency = require("./dependencies/ModuleDependency");
|
38
36
|
const compareLocations = require("./compareLocations");
|
39
37
|
const { Logger, LogType } = require("./logging/Logger");
|
40
38
|
const ErrorHelpers = require("./ErrorHelpers");
|
39
|
+
const buildChunkGraph = require("./buildChunkGraph");
|
41
40
|
|
42
41
|
/** @typedef {import("./Module")} Module */
|
43
42
|
/** @typedef {import("./Compiler")} Compiler */
|
@@ -165,16 +164,6 @@ const byNameOrHash = (a, b) => {
|
|
165
164
|
return 0;
|
166
165
|
};
|
167
166
|
|
168
|
-
/**
|
169
|
-
* @template T
|
170
|
-
* @param {Set<T>} a first set
|
171
|
-
* @param {Set<T>} b second set
|
172
|
-
* @returns {number} cmp
|
173
|
-
*/
|
174
|
-
const bySetSize = (a, b) => {
|
175
|
-
return a.size - b.size;
|
176
|
-
};
|
177
|
-
|
178
167
|
/**
|
179
168
|
* @param {DependenciesBlockVariable[]} variables DepBlock Variables to iterate over
|
180
169
|
* @param {DepBlockVarDependenciesCallback} fn callback to apply on iterated elements
|
@@ -1295,7 +1284,10 @@ class Compilation extends Tapable {
|
|
1295
1284
|
|
1296
1285
|
this.assignDepth(module);
|
1297
1286
|
}
|
1298
|
-
|
1287
|
+
buildChunkGraph(
|
1288
|
+
this,
|
1289
|
+
/** @type {Entrypoint[]} */ (this.chunkGroups.slice())
|
1290
|
+
);
|
1299
1291
|
this.sortModules(this.modules);
|
1300
1292
|
this.hooks.afterChunks.call(this.chunks);
|
1301
1293
|
|
@@ -1583,460 +1575,6 @@ class Compilation extends Tapable {
|
|
1583
1575
|
return this.hooks.dependencyReference.call(ref, dependency, module);
|
1584
1576
|
}
|
1585
1577
|
|
1586
|
-
/**
|
1587
|
-
* This method creates the Chunk graph from the Module graph
|
1588
|
-
* @private
|
1589
|
-
* @param {TODO[]} inputChunkGroups chunk groups which are processed
|
1590
|
-
* @returns {void}
|
1591
|
-
*/
|
1592
|
-
processDependenciesBlocksForChunkGroups(inputChunkGroups) {
|
1593
|
-
// Process is splitting into two parts:
|
1594
|
-
// Part one traverse the module graph and builds a very basic chunks graph
|
1595
|
-
// in chunkDependencies.
|
1596
|
-
// Part two traverse every possible way through the basic chunk graph and
|
1597
|
-
// tracks the available modules. While traversing it connects chunks with
|
1598
|
-
// eachother and Blocks with Chunks. It stops traversing when all modules
|
1599
|
-
// for a chunk are already available. So it doesn't connect unneeded chunks.
|
1600
|
-
|
1601
|
-
/** @type {Map<ChunkGroup, {block: AsyncDependenciesBlock, chunkGroup: ChunkGroup, couldBeFiltered: boolean}[]>} */
|
1602
|
-
const chunkDependencies = new Map();
|
1603
|
-
const allCreatedChunkGroups = new Set();
|
1604
|
-
|
1605
|
-
// PREPARE
|
1606
|
-
/** @type {Map<DependenciesBlock, { modules: Module[], blocks: AsyncDependenciesBlock[]}>} */
|
1607
|
-
const blockInfoMap = new Map();
|
1608
|
-
|
1609
|
-
/**
|
1610
|
-
* @param {Dependency} d dependency to iterate over
|
1611
|
-
* @returns {void}
|
1612
|
-
*/
|
1613
|
-
const iteratorDependency = d => {
|
1614
|
-
// We skip Dependencies without Reference
|
1615
|
-
const ref = this.getDependencyReference(currentModule, d);
|
1616
|
-
if (!ref) {
|
1617
|
-
return;
|
1618
|
-
}
|
1619
|
-
// We skip Dependencies without Module pointer
|
1620
|
-
const refModule = ref.module;
|
1621
|
-
if (!refModule) {
|
1622
|
-
return;
|
1623
|
-
}
|
1624
|
-
// We skip weak Dependencies
|
1625
|
-
if (ref.weak) {
|
1626
|
-
return;
|
1627
|
-
}
|
1628
|
-
|
1629
|
-
blockInfoModules.add(refModule);
|
1630
|
-
};
|
1631
|
-
|
1632
|
-
/**
|
1633
|
-
* @param {AsyncDependenciesBlock} b blocks to prepare
|
1634
|
-
* @returns {void}
|
1635
|
-
*/
|
1636
|
-
const iteratorBlockPrepare = b => {
|
1637
|
-
blockInfoBlocks.push(b);
|
1638
|
-
blockQueue.push(b);
|
1639
|
-
};
|
1640
|
-
|
1641
|
-
/** @type {Module} */
|
1642
|
-
let currentModule;
|
1643
|
-
/** @type {DependenciesBlock} */
|
1644
|
-
let block;
|
1645
|
-
/** @type {DependenciesBlock[]} */
|
1646
|
-
let blockQueue;
|
1647
|
-
/** @type {Set<Module>} */
|
1648
|
-
let blockInfoModules;
|
1649
|
-
/** @type {AsyncDependenciesBlock[]} */
|
1650
|
-
let blockInfoBlocks;
|
1651
|
-
|
1652
|
-
for (const module of this.modules) {
|
1653
|
-
blockQueue = [module];
|
1654
|
-
currentModule = module;
|
1655
|
-
while (blockQueue.length > 0) {
|
1656
|
-
block = blockQueue.pop();
|
1657
|
-
blockInfoModules = new Set();
|
1658
|
-
blockInfoBlocks = [];
|
1659
|
-
|
1660
|
-
if (block.variables) {
|
1661
|
-
iterationBlockVariable(block.variables, iteratorDependency);
|
1662
|
-
}
|
1663
|
-
|
1664
|
-
if (block.dependencies) {
|
1665
|
-
iterationOfArrayCallback(block.dependencies, iteratorDependency);
|
1666
|
-
}
|
1667
|
-
|
1668
|
-
if (block.blocks) {
|
1669
|
-
iterationOfArrayCallback(block.blocks, iteratorBlockPrepare);
|
1670
|
-
}
|
1671
|
-
|
1672
|
-
const blockInfo = {
|
1673
|
-
modules: Array.from(blockInfoModules),
|
1674
|
-
blocks: blockInfoBlocks
|
1675
|
-
};
|
1676
|
-
blockInfoMap.set(block, blockInfo);
|
1677
|
-
}
|
1678
|
-
}
|
1679
|
-
|
1680
|
-
// PART ONE
|
1681
|
-
|
1682
|
-
/** @type {Map<ChunkGroup, { index: number, index2: number }>} */
|
1683
|
-
const chunkGroupCounters = new Map();
|
1684
|
-
for (const chunkGroup of inputChunkGroups) {
|
1685
|
-
chunkGroupCounters.set(chunkGroup, { index: 0, index2: 0 });
|
1686
|
-
}
|
1687
|
-
|
1688
|
-
let nextFreeModuleIndex = 0;
|
1689
|
-
let nextFreeModuleIndex2 = 0;
|
1690
|
-
|
1691
|
-
/** @type {Map<DependenciesBlock, ChunkGroup>} */
|
1692
|
-
const blockChunkGroups = new Map();
|
1693
|
-
|
1694
|
-
/** @type {Set<DependenciesBlock>} */
|
1695
|
-
const blocksWithNestedBlocks = new Set();
|
1696
|
-
|
1697
|
-
const ADD_AND_ENTER_MODULE = 0;
|
1698
|
-
const ENTER_MODULE = 1;
|
1699
|
-
const PROCESS_BLOCK = 2;
|
1700
|
-
const LEAVE_MODULE = 3;
|
1701
|
-
|
1702
|
-
/**
|
1703
|
-
* @typedef {Object} QueueItem
|
1704
|
-
* @property {number} action
|
1705
|
-
* @property {DependenciesBlock} block
|
1706
|
-
* @property {Module} module
|
1707
|
-
* @property {Chunk} chunk
|
1708
|
-
* @property {ChunkGroup} chunkGroup
|
1709
|
-
*/
|
1710
|
-
|
1711
|
-
/**
|
1712
|
-
* @param {ChunkGroup} chunkGroup chunk group
|
1713
|
-
* @returns {QueueItem} queue item
|
1714
|
-
*/
|
1715
|
-
const chunkGroupToQueueItem = chunkGroup => ({
|
1716
|
-
action: ENTER_MODULE,
|
1717
|
-
block: chunkGroup.chunks[0].entryModule,
|
1718
|
-
module: chunkGroup.chunks[0].entryModule,
|
1719
|
-
chunk: chunkGroup.chunks[0],
|
1720
|
-
chunkGroup
|
1721
|
-
});
|
1722
|
-
|
1723
|
-
// Start with the provided modules/chunks
|
1724
|
-
/** @type {QueueItem[]} */
|
1725
|
-
let queue = inputChunkGroups.map(chunkGroupToQueueItem).reverse();
|
1726
|
-
/** @type {QueueItem[]} */
|
1727
|
-
let queueDelayed = [];
|
1728
|
-
|
1729
|
-
/** @type {Module} */
|
1730
|
-
let module;
|
1731
|
-
/** @type {Chunk} */
|
1732
|
-
let chunk;
|
1733
|
-
/** @type {ChunkGroup} */
|
1734
|
-
let chunkGroup;
|
1735
|
-
|
1736
|
-
// For each async Block in graph
|
1737
|
-
/**
|
1738
|
-
* @param {AsyncDependenciesBlock} b iterating over each Async DepBlock
|
1739
|
-
* @returns {void}
|
1740
|
-
*/
|
1741
|
-
const iteratorBlock = b => {
|
1742
|
-
// 1. We create a chunk for this Block
|
1743
|
-
// but only once (blockChunkGroups map)
|
1744
|
-
let c = blockChunkGroups.get(b);
|
1745
|
-
if (c === undefined) {
|
1746
|
-
c = this.namedChunkGroups.get(b.chunkName);
|
1747
|
-
if (c && c.isInitial()) {
|
1748
|
-
this.errors.push(
|
1749
|
-
new AsyncDependencyToInitialChunkError(b.chunkName, module, b.loc)
|
1750
|
-
);
|
1751
|
-
c = chunkGroup;
|
1752
|
-
} else {
|
1753
|
-
c = this.addChunkInGroup(
|
1754
|
-
b.groupOptions || b.chunkName,
|
1755
|
-
module,
|
1756
|
-
b.loc,
|
1757
|
-
b.request
|
1758
|
-
);
|
1759
|
-
chunkGroupCounters.set(c, { index: 0, index2: 0 });
|
1760
|
-
blockChunkGroups.set(b, c);
|
1761
|
-
allCreatedChunkGroups.add(c);
|
1762
|
-
}
|
1763
|
-
} else {
|
1764
|
-
// TODO webpack 5 remove addOptions check
|
1765
|
-
if (c.addOptions) c.addOptions(b.groupOptions);
|
1766
|
-
c.addOrigin(module, b.loc, b.request);
|
1767
|
-
}
|
1768
|
-
|
1769
|
-
// 2. We store the Block+Chunk mapping as dependency for the chunk
|
1770
|
-
let deps = chunkDependencies.get(chunkGroup);
|
1771
|
-
if (!deps) chunkDependencies.set(chunkGroup, (deps = []));
|
1772
|
-
deps.push({
|
1773
|
-
block: b,
|
1774
|
-
chunkGroup: c,
|
1775
|
-
couldBeFiltered: true
|
1776
|
-
});
|
1777
|
-
|
1778
|
-
// 3. We enqueue the DependenciesBlock for traversal
|
1779
|
-
queueDelayed.push({
|
1780
|
-
action: PROCESS_BLOCK,
|
1781
|
-
block: b,
|
1782
|
-
module: module,
|
1783
|
-
chunk: c.chunks[0],
|
1784
|
-
chunkGroup: c
|
1785
|
-
});
|
1786
|
-
};
|
1787
|
-
|
1788
|
-
// Iterative traversal of the Module graph
|
1789
|
-
// Recursive would be simpler to write but could result in Stack Overflows
|
1790
|
-
while (queue.length) {
|
1791
|
-
while (queue.length) {
|
1792
|
-
const queueItem = queue.pop();
|
1793
|
-
module = queueItem.module;
|
1794
|
-
block = queueItem.block;
|
1795
|
-
chunk = queueItem.chunk;
|
1796
|
-
chunkGroup = queueItem.chunkGroup;
|
1797
|
-
|
1798
|
-
switch (queueItem.action) {
|
1799
|
-
case ADD_AND_ENTER_MODULE: {
|
1800
|
-
// We connect Module and Chunk when not already done
|
1801
|
-
if (chunk.addModule(module)) {
|
1802
|
-
module.addChunk(chunk);
|
1803
|
-
} else {
|
1804
|
-
// already connected, skip it
|
1805
|
-
break;
|
1806
|
-
}
|
1807
|
-
}
|
1808
|
-
// fallthrough
|
1809
|
-
case ENTER_MODULE: {
|
1810
|
-
if (chunkGroup !== undefined) {
|
1811
|
-
const index = chunkGroup.getModuleIndex(module);
|
1812
|
-
if (index === undefined) {
|
1813
|
-
chunkGroup.setModuleIndex(
|
1814
|
-
module,
|
1815
|
-
chunkGroupCounters.get(chunkGroup).index++
|
1816
|
-
);
|
1817
|
-
}
|
1818
|
-
}
|
1819
|
-
|
1820
|
-
if (module.index === null) {
|
1821
|
-
module.index = nextFreeModuleIndex++;
|
1822
|
-
}
|
1823
|
-
|
1824
|
-
queue.push({
|
1825
|
-
action: LEAVE_MODULE,
|
1826
|
-
block,
|
1827
|
-
module,
|
1828
|
-
chunk,
|
1829
|
-
chunkGroup
|
1830
|
-
});
|
1831
|
-
}
|
1832
|
-
// fallthrough
|
1833
|
-
case PROCESS_BLOCK: {
|
1834
|
-
// get prepared block info
|
1835
|
-
const blockInfo = blockInfoMap.get(block);
|
1836
|
-
|
1837
|
-
// Traverse all referenced modules
|
1838
|
-
for (let i = blockInfo.modules.length - 1; i >= 0; i--) {
|
1839
|
-
const refModule = blockInfo.modules[i];
|
1840
|
-
if (chunk.containsModule(refModule)) {
|
1841
|
-
// skip early if already connected
|
1842
|
-
continue;
|
1843
|
-
}
|
1844
|
-
// enqueue the add and enter to enter in the correct order
|
1845
|
-
// this is relevant with circular dependencies
|
1846
|
-
queue.push({
|
1847
|
-
action: ADD_AND_ENTER_MODULE,
|
1848
|
-
block: refModule,
|
1849
|
-
module: refModule,
|
1850
|
-
chunk,
|
1851
|
-
chunkGroup
|
1852
|
-
});
|
1853
|
-
}
|
1854
|
-
|
1855
|
-
// Traverse all Blocks
|
1856
|
-
iterationOfArrayCallback(blockInfo.blocks, iteratorBlock);
|
1857
|
-
|
1858
|
-
if (blockInfo.blocks.length > 0 && module !== block) {
|
1859
|
-
blocksWithNestedBlocks.add(block);
|
1860
|
-
}
|
1861
|
-
break;
|
1862
|
-
}
|
1863
|
-
case LEAVE_MODULE: {
|
1864
|
-
if (chunkGroup !== undefined) {
|
1865
|
-
const index = chunkGroup.getModuleIndex2(module);
|
1866
|
-
if (index === undefined) {
|
1867
|
-
chunkGroup.setModuleIndex2(
|
1868
|
-
module,
|
1869
|
-
chunkGroupCounters.get(chunkGroup).index2++
|
1870
|
-
);
|
1871
|
-
}
|
1872
|
-
}
|
1873
|
-
|
1874
|
-
if (module.index2 === null) {
|
1875
|
-
module.index2 = nextFreeModuleIndex2++;
|
1876
|
-
}
|
1877
|
-
break;
|
1878
|
-
}
|
1879
|
-
}
|
1880
|
-
}
|
1881
|
-
const tempQueue = queue;
|
1882
|
-
queue = queueDelayed.reverse();
|
1883
|
-
queueDelayed = tempQueue;
|
1884
|
-
}
|
1885
|
-
|
1886
|
-
// PART TWO
|
1887
|
-
/** @type {Set<Module>} */
|
1888
|
-
let newAvailableModules;
|
1889
|
-
|
1890
|
-
/**
|
1891
|
-
* @typedef {Object} ChunkGroupInfo
|
1892
|
-
* @property {Set<Module>} minAvailableModules current minimal set of modules available at this point
|
1893
|
-
* @property {Set<Module>[]} availableModulesToBeMerged enqueued updates to the minimal set of available modules
|
1894
|
-
*/
|
1895
|
-
|
1896
|
-
/** @type {Map<ChunkGroup, ChunkGroupInfo>} */
|
1897
|
-
const chunkGroupInfoMap = new Map();
|
1898
|
-
|
1899
|
-
/** @type {Queue<ChunkGroup>} */
|
1900
|
-
const queue2 = new Queue(inputChunkGroups);
|
1901
|
-
|
1902
|
-
for (const chunkGroup of inputChunkGroups) {
|
1903
|
-
chunkGroupInfoMap.set(chunkGroup, {
|
1904
|
-
minAvailableModules: undefined,
|
1905
|
-
availableModulesToBeMerged: [new Set()]
|
1906
|
-
});
|
1907
|
-
}
|
1908
|
-
|
1909
|
-
/**
|
1910
|
-
* Helper function to check if all modules of a chunk are available
|
1911
|
-
*
|
1912
|
-
* @param {ChunkGroup} chunkGroup the chunkGroup to scan
|
1913
|
-
* @param {Set<Module>} availableModules the comparitor set
|
1914
|
-
* @returns {boolean} return true if all modules of a chunk are available
|
1915
|
-
*/
|
1916
|
-
const areModulesAvailable = (chunkGroup, availableModules) => {
|
1917
|
-
for (const chunk of chunkGroup.chunks) {
|
1918
|
-
for (const module of chunk.modulesIterable) {
|
1919
|
-
if (!availableModules.has(module)) return false;
|
1920
|
-
}
|
1921
|
-
}
|
1922
|
-
return true;
|
1923
|
-
};
|
1924
|
-
|
1925
|
-
// For each edge in the basic chunk graph
|
1926
|
-
/**
|
1927
|
-
* @param {TODO} dep the dependency used for filtering
|
1928
|
-
* @returns {boolean} used to filter "edges" (aka Dependencies) that were pointing
|
1929
|
-
* to modules that are already available. Also filters circular dependencies in the chunks graph
|
1930
|
-
*/
|
1931
|
-
const filterFn = dep => {
|
1932
|
-
const depChunkGroup = dep.chunkGroup;
|
1933
|
-
if (!dep.couldBeFiltered) return true;
|
1934
|
-
if (blocksWithNestedBlocks.has(dep.block)) return true;
|
1935
|
-
if (areModulesAvailable(depChunkGroup, newAvailableModules)) {
|
1936
|
-
return false; // break all modules are already available
|
1937
|
-
}
|
1938
|
-
dep.couldBeFiltered = false;
|
1939
|
-
return true;
|
1940
|
-
};
|
1941
|
-
|
1942
|
-
// Iterative traversing of the basic chunk graph
|
1943
|
-
while (queue2.length) {
|
1944
|
-
chunkGroup = queue2.dequeue();
|
1945
|
-
const info = chunkGroupInfoMap.get(chunkGroup);
|
1946
|
-
const availableModulesToBeMerged = info.availableModulesToBeMerged;
|
1947
|
-
let minAvailableModules = info.minAvailableModules;
|
1948
|
-
|
1949
|
-
// 1. Get minimal available modules
|
1950
|
-
// It doesn't make sense to traverse a chunk again with more available modules.
|
1951
|
-
// This step calculates the minimal available modules and skips traversal when
|
1952
|
-
// the list didn't shrink.
|
1953
|
-
availableModulesToBeMerged.sort(bySetSize);
|
1954
|
-
let changed = false;
|
1955
|
-
for (const availableModules of availableModulesToBeMerged) {
|
1956
|
-
if (minAvailableModules === undefined) {
|
1957
|
-
minAvailableModules = new Set(availableModules);
|
1958
|
-
info.minAvailableModules = minAvailableModules;
|
1959
|
-
changed = true;
|
1960
|
-
} else {
|
1961
|
-
for (const m of minAvailableModules) {
|
1962
|
-
if (!availableModules.has(m)) {
|
1963
|
-
minAvailableModules.delete(m);
|
1964
|
-
changed = true;
|
1965
|
-
}
|
1966
|
-
}
|
1967
|
-
}
|
1968
|
-
}
|
1969
|
-
availableModulesToBeMerged.length = 0;
|
1970
|
-
if (!changed) continue;
|
1971
|
-
|
1972
|
-
// 2. Get the edges at this point of the graph
|
1973
|
-
const deps = chunkDependencies.get(chunkGroup);
|
1974
|
-
if (!deps) continue;
|
1975
|
-
if (deps.length === 0) continue;
|
1976
|
-
|
1977
|
-
// 3. Create a new Set of available modules at this points
|
1978
|
-
newAvailableModules = new Set(minAvailableModules);
|
1979
|
-
for (const chunk of chunkGroup.chunks) {
|
1980
|
-
for (const m of chunk.modulesIterable) {
|
1981
|
-
newAvailableModules.add(m);
|
1982
|
-
}
|
1983
|
-
}
|
1984
|
-
|
1985
|
-
// 4. Foreach remaining edge
|
1986
|
-
const nextChunkGroups = new Set();
|
1987
|
-
for (let i = 0; i < deps.length; i++) {
|
1988
|
-
const dep = deps[i];
|
1989
|
-
|
1990
|
-
// Filter inline, rather than creating a new array from `.filter()`
|
1991
|
-
if (!filterFn(dep)) {
|
1992
|
-
continue;
|
1993
|
-
}
|
1994
|
-
const depChunkGroup = dep.chunkGroup;
|
1995
|
-
const depBlock = dep.block;
|
1996
|
-
|
1997
|
-
// 5. Connect block with chunk
|
1998
|
-
GraphHelpers.connectDependenciesBlockAndChunkGroup(
|
1999
|
-
depBlock,
|
2000
|
-
depChunkGroup
|
2001
|
-
);
|
2002
|
-
|
2003
|
-
// 6. Connect chunk with parent
|
2004
|
-
GraphHelpers.connectChunkGroupParentAndChild(chunkGroup, depChunkGroup);
|
2005
|
-
|
2006
|
-
nextChunkGroups.add(depChunkGroup);
|
2007
|
-
}
|
2008
|
-
|
2009
|
-
// 7. Enqueue further traversal
|
2010
|
-
for (const nextChunkGroup of nextChunkGroups) {
|
2011
|
-
let nextInfo = chunkGroupInfoMap.get(nextChunkGroup);
|
2012
|
-
if (nextInfo === undefined) {
|
2013
|
-
nextInfo = {
|
2014
|
-
minAvailableModules: undefined,
|
2015
|
-
availableModulesToBeMerged: []
|
2016
|
-
};
|
2017
|
-
chunkGroupInfoMap.set(nextChunkGroup, nextInfo);
|
2018
|
-
}
|
2019
|
-
nextInfo.availableModulesToBeMerged.push(newAvailableModules);
|
2020
|
-
|
2021
|
-
// As queue deduplicates enqueued items this makes sure that a ChunkGroup
|
2022
|
-
// is not enqueued twice
|
2023
|
-
queue2.enqueue(nextChunkGroup);
|
2024
|
-
}
|
2025
|
-
}
|
2026
|
-
|
2027
|
-
// Remove all unconnected chunk groups
|
2028
|
-
for (const chunkGroup of allCreatedChunkGroups) {
|
2029
|
-
if (chunkGroup.getNumberOfParents() === 0) {
|
2030
|
-
for (const chunk of chunkGroup.chunks) {
|
2031
|
-
const idx = this.chunks.indexOf(chunk);
|
2032
|
-
if (idx >= 0) this.chunks.splice(idx, 1);
|
2033
|
-
chunk.remove("unconnected");
|
2034
|
-
}
|
2035
|
-
chunkGroup.remove("unconnected");
|
2036
|
-
}
|
2037
|
-
}
|
2038
|
-
}
|
2039
|
-
|
2040
1578
|
/**
|
2041
1579
|
*
|
2042
1580
|
* @param {Module} module module relationship for removal
|
package/lib/ProgressPlugin.js
CHANGED
@@ -263,7 +263,6 @@ class ProgressPlugin {
|
|
263
263
|
recordModules: "record modules",
|
264
264
|
recordChunks: "record chunks",
|
265
265
|
beforeHash: "hashing",
|
266
|
-
contentHash: "content hashing",
|
267
266
|
afterHash: "after hashing",
|
268
267
|
recordHash: "record hash",
|
269
268
|
beforeModuleAssets: "module assets processing",
|
@@ -49,27 +49,13 @@ const assetsCache = new WeakMap();
|
|
49
49
|
/**
|
50
50
|
* Creating {@link SourceMapTask} for given file
|
51
51
|
* @param {string} file current compiled file
|
52
|
+
* @param {Source} asset the asset
|
52
53
|
* @param {Chunk} chunk related chunk
|
53
54
|
* @param {SourceMapDevToolPluginOptions} options source map options
|
54
55
|
* @param {Compilation} compilation compilation instance
|
55
56
|
* @returns {SourceMapTask | undefined} created task instance or `undefined`
|
56
57
|
*/
|
57
|
-
const getTaskForFile = (file, chunk, options, compilation) => {
|
58
|
-
const asset = compilation.assets[file];
|
59
|
-
const cache = assetsCache.get(asset);
|
60
|
-
/**
|
61
|
-
* If presented in cache, reassigns assets. Cache assets already have source maps.
|
62
|
-
*/
|
63
|
-
if (cache && cache.file === file) {
|
64
|
-
for (const cachedFile in cache.assets) {
|
65
|
-
compilation.assets[cachedFile] = cache.assets[cachedFile];
|
66
|
-
/**
|
67
|
-
* Add file to chunk, if not presented there
|
68
|
-
*/
|
69
|
-
if (cachedFile !== file) chunk.files.push(cachedFile);
|
70
|
-
}
|
71
|
-
return;
|
72
|
-
}
|
58
|
+
const getTaskForFile = (file, asset, chunk, options, compilation) => {
|
73
59
|
let source, sourceMap;
|
74
60
|
/**
|
75
61
|
* Check if asset can build source map
|
@@ -189,13 +175,35 @@ class SourceMapDevToolPlugin {
|
|
189
175
|
reportProgress(0.0);
|
190
176
|
const tasks = [];
|
191
177
|
files.forEach(({ file, chunk }, idx) => {
|
178
|
+
const asset = compilation.assets[file];
|
179
|
+
const cache = assetsCache.get(asset);
|
180
|
+
/**
|
181
|
+
* If presented in cache, reassigns assets. Cache assets already have source maps.
|
182
|
+
*/
|
183
|
+
if (cache && cache.file === file) {
|
184
|
+
for (const cachedFile in cache.assets) {
|
185
|
+
compilation.assets[cachedFile] = cache.assets[cachedFile];
|
186
|
+
/**
|
187
|
+
* Add file to chunk, if not presented there
|
188
|
+
*/
|
189
|
+
if (cachedFile !== file) chunk.files.push(cachedFile);
|
190
|
+
}
|
191
|
+
return;
|
192
|
+
}
|
193
|
+
|
192
194
|
reportProgress(
|
193
195
|
(0.5 * idx) / files.length,
|
194
196
|
file,
|
195
197
|
"generate SourceMap"
|
196
198
|
);
|
197
199
|
/** @type {SourceMapTask | undefined} */
|
198
|
-
const task = getTaskForFile(
|
200
|
+
const task = getTaskForFile(
|
201
|
+
file,
|
202
|
+
asset,
|
203
|
+
chunk,
|
204
|
+
options,
|
205
|
+
compilation
|
206
|
+
);
|
199
207
|
|
200
208
|
if (task) {
|
201
209
|
const modules = task.sourceMap.sources.map(source => {
|
@@ -196,7 +196,12 @@ class WebpackOptionsDefaulter extends OptionsDefaulter {
|
|
196
196
|
);
|
197
197
|
|
198
198
|
this.set("optimization", "call", value => Object.assign({}, value));
|
199
|
-
|
199
|
+
// TODO webpack 5: Disable by default in a modes
|
200
|
+
this.set(
|
201
|
+
"optimization.removeAvailableModules",
|
202
|
+
"make",
|
203
|
+
options => options.mode !== "development"
|
204
|
+
);
|
200
205
|
this.set("optimization.removeEmptyChunks", true);
|
201
206
|
this.set("optimization.mergeDuplicateChunks", true);
|
202
207
|
this.set("optimization.flagIncludedChunks", "make", options =>
|
@@ -0,0 +1,689 @@
|
|
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 AsyncDependencyToInitialChunkError = require("./AsyncDependencyToInitialChunkError");
|
9
|
+
const GraphHelpers = require("./GraphHelpers");
|
10
|
+
|
11
|
+
/** @typedef {import("./AsyncDependenciesBlock")} AsyncDependenciesBlock */
|
12
|
+
/** @typedef {import("./Chunk")} Chunk */
|
13
|
+
/** @typedef {import("./ChunkGroup")} ChunkGroup */
|
14
|
+
/** @typedef {import("./Compilation")} Compilation */
|
15
|
+
/** @typedef {import("./DependenciesBlock")} DependenciesBlock */
|
16
|
+
/** @typedef {import("./Dependency")} Dependency */
|
17
|
+
/** @typedef {import("./Entrypoint")} Entrypoint */
|
18
|
+
/** @typedef {import("./Module")} Module */
|
19
|
+
|
20
|
+
/**
|
21
|
+
* @typedef {Object} QueueItem
|
22
|
+
* @property {number} action
|
23
|
+
* @property {DependenciesBlock} block
|
24
|
+
* @property {Module} module
|
25
|
+
* @property {Chunk} chunk
|
26
|
+
* @property {ChunkGroup} chunkGroup
|
27
|
+
*/
|
28
|
+
|
29
|
+
/**
|
30
|
+
* @typedef {Object} ChunkGroupInfo
|
31
|
+
* @property {Set<Module>} minAvailableModules current minimal set of modules available at this point
|
32
|
+
* @property {boolean} minAvailableModulesOwned true, if minAvailableModules is owned and can be modified
|
33
|
+
* @property {Set<Module>[]} availableModulesToBeMerged enqueued updates to the minimal set of available modules
|
34
|
+
* @property {QueueItem[]} skippedItems queue items that were skipped because module is already available in parent chunks (need to reconsider when minAvailableModules is shrinking)
|
35
|
+
* @property {Set<Module>} resultingAvailableModules set of modules available including modules from this chunk group
|
36
|
+
*/
|
37
|
+
|
38
|
+
/**
|
39
|
+
* @typedef {Object} ChunkGroupDep
|
40
|
+
* @property {AsyncDependenciesBlock} block referencing block
|
41
|
+
* @property {ChunkGroup} chunkGroup referenced chunk group
|
42
|
+
*/
|
43
|
+
|
44
|
+
/**
|
45
|
+
* @template T
|
46
|
+
* @param {Set<T>} a first set
|
47
|
+
* @param {Set<T>} b second set
|
48
|
+
* @returns {number} cmp
|
49
|
+
*/
|
50
|
+
const bySetSize = (a, b) => {
|
51
|
+
return b.size - a.size;
|
52
|
+
};
|
53
|
+
|
54
|
+
/**
|
55
|
+
* Extracts simplified info from the modules and their dependencies
|
56
|
+
* @param {Compilation} compilation the compilation
|
57
|
+
* @returns {Map<DependenciesBlock, { modules: Iterable<Module>, blocks: AsyncDependenciesBlock[]}>} the mapping block to modules and inner blocks
|
58
|
+
*/
|
59
|
+
const extraceBlockInfoMap = compilation => {
|
60
|
+
/** @type {Map<DependenciesBlock, { modules: Iterable<Module>, blocks: AsyncDependenciesBlock[]}>} */
|
61
|
+
const blockInfoMap = new Map();
|
62
|
+
|
63
|
+
/**
|
64
|
+
* @param {Dependency} d dependency to iterate over
|
65
|
+
* @returns {void}
|
66
|
+
*/
|
67
|
+
const iteratorDependency = d => {
|
68
|
+
// We skip Dependencies without Reference
|
69
|
+
const ref = compilation.getDependencyReference(currentModule, d);
|
70
|
+
if (!ref) {
|
71
|
+
return;
|
72
|
+
}
|
73
|
+
// We skip Dependencies without Module pointer
|
74
|
+
const refModule = ref.module;
|
75
|
+
if (!refModule) {
|
76
|
+
return;
|
77
|
+
}
|
78
|
+
// We skip weak Dependencies
|
79
|
+
if (ref.weak) {
|
80
|
+
return;
|
81
|
+
}
|
82
|
+
|
83
|
+
blockInfoModules.add(refModule);
|
84
|
+
};
|
85
|
+
|
86
|
+
/**
|
87
|
+
* @param {AsyncDependenciesBlock} b blocks to prepare
|
88
|
+
* @returns {void}
|
89
|
+
*/
|
90
|
+
const iteratorBlockPrepare = b => {
|
91
|
+
blockInfoBlocks.push(b);
|
92
|
+
blockQueue.push(b);
|
93
|
+
};
|
94
|
+
|
95
|
+
/** @type {Module} */
|
96
|
+
let currentModule;
|
97
|
+
/** @type {DependenciesBlock} */
|
98
|
+
let block;
|
99
|
+
/** @type {DependenciesBlock[]} */
|
100
|
+
let blockQueue;
|
101
|
+
/** @type {Set<Module>} */
|
102
|
+
let blockInfoModules;
|
103
|
+
/** @type {AsyncDependenciesBlock[]} */
|
104
|
+
let blockInfoBlocks;
|
105
|
+
|
106
|
+
for (const module of compilation.modules) {
|
107
|
+
blockQueue = [module];
|
108
|
+
currentModule = module;
|
109
|
+
while (blockQueue.length > 0) {
|
110
|
+
block = blockQueue.pop();
|
111
|
+
blockInfoModules = new Set();
|
112
|
+
blockInfoBlocks = [];
|
113
|
+
|
114
|
+
if (block.variables) {
|
115
|
+
for (const variable of block.variables) {
|
116
|
+
for (const dep of variable.dependencies) iteratorDependency(dep);
|
117
|
+
}
|
118
|
+
}
|
119
|
+
|
120
|
+
if (block.dependencies) {
|
121
|
+
for (const dep of block.dependencies) iteratorDependency(dep);
|
122
|
+
}
|
123
|
+
|
124
|
+
if (block.blocks) {
|
125
|
+
for (const b of block.blocks) iteratorBlockPrepare(b);
|
126
|
+
}
|
127
|
+
|
128
|
+
const blockInfo = {
|
129
|
+
modules: blockInfoModules,
|
130
|
+
blocks: blockInfoBlocks
|
131
|
+
};
|
132
|
+
blockInfoMap.set(block, blockInfo);
|
133
|
+
}
|
134
|
+
}
|
135
|
+
|
136
|
+
return blockInfoMap;
|
137
|
+
};
|
138
|
+
|
139
|
+
/**
|
140
|
+
*
|
141
|
+
* @param {Compilation} compilation the compilation
|
142
|
+
* @param {Entrypoint[]} inputChunkGroups input groups
|
143
|
+
* @param {Map<ChunkGroup, ChunkGroupInfo>} chunkGroupInfoMap mapping from chunk group to available modules
|
144
|
+
* @param {Map<ChunkGroup, ChunkGroupDep[]>} chunkDependencies dependencies for chunk groups
|
145
|
+
* @param {Set<DependenciesBlock>} blocksWithNestedBlocks flag for blocks that have nested blocks
|
146
|
+
* @param {Set<ChunkGroup>} allCreatedChunkGroups filled with all chunk groups that are created here
|
147
|
+
*/
|
148
|
+
const visitModules = (
|
149
|
+
compilation,
|
150
|
+
inputChunkGroups,
|
151
|
+
chunkGroupInfoMap,
|
152
|
+
chunkDependencies,
|
153
|
+
blocksWithNestedBlocks,
|
154
|
+
allCreatedChunkGroups
|
155
|
+
) => {
|
156
|
+
const logger = compilation.getLogger("webpack.buildChunkGraph.visitModules");
|
157
|
+
const { namedChunkGroups } = compilation;
|
158
|
+
|
159
|
+
logger.time("prepare");
|
160
|
+
const blockInfoMap = extraceBlockInfoMap(compilation);
|
161
|
+
|
162
|
+
/** @type {Map<ChunkGroup, { index: number, index2: number }>} */
|
163
|
+
const chunkGroupCounters = new Map();
|
164
|
+
for (const chunkGroup of inputChunkGroups) {
|
165
|
+
chunkGroupCounters.set(chunkGroup, {
|
166
|
+
index: 0,
|
167
|
+
index2: 0
|
168
|
+
});
|
169
|
+
}
|
170
|
+
|
171
|
+
let nextFreeModuleIndex = 0;
|
172
|
+
let nextFreeModuleIndex2 = 0;
|
173
|
+
|
174
|
+
/** @type {Map<DependenciesBlock, ChunkGroup>} */
|
175
|
+
const blockChunkGroups = new Map();
|
176
|
+
|
177
|
+
const ADD_AND_ENTER_MODULE = 0;
|
178
|
+
const ENTER_MODULE = 1;
|
179
|
+
const PROCESS_BLOCK = 2;
|
180
|
+
const LEAVE_MODULE = 3;
|
181
|
+
|
182
|
+
/**
|
183
|
+
* @param {QueueItem[]} queue the queue array (will be mutated)
|
184
|
+
* @param {ChunkGroup} chunkGroup chunk group
|
185
|
+
* @returns {QueueItem[]} the queue array again
|
186
|
+
*/
|
187
|
+
const reduceChunkGroupToQueueItem = (queue, chunkGroup) => {
|
188
|
+
for (const chunk of chunkGroup.chunks) {
|
189
|
+
const module = chunk.entryModule;
|
190
|
+
queue.push({
|
191
|
+
action: ENTER_MODULE,
|
192
|
+
block: module,
|
193
|
+
module,
|
194
|
+
chunk,
|
195
|
+
chunkGroup
|
196
|
+
});
|
197
|
+
}
|
198
|
+
chunkGroupInfoMap.set(chunkGroup, {
|
199
|
+
minAvailableModules: new Set(),
|
200
|
+
minAvailableModulesOwned: true,
|
201
|
+
availableModulesToBeMerged: [],
|
202
|
+
skippedItems: [],
|
203
|
+
resultingAvailableModules: undefined
|
204
|
+
});
|
205
|
+
return queue;
|
206
|
+
};
|
207
|
+
|
208
|
+
// Start with the provided modules/chunks
|
209
|
+
/** @type {QueueItem[]} */
|
210
|
+
let queue = inputChunkGroups
|
211
|
+
.reduce(reduceChunkGroupToQueueItem, [])
|
212
|
+
.reverse();
|
213
|
+
/** @type {Map<ChunkGroup, Set<ChunkGroup>>} */
|
214
|
+
const queueConnect = new Map();
|
215
|
+
/** @type {Set<ChunkGroupInfo>} */
|
216
|
+
const outdatedChunkGroupInfo = new Set();
|
217
|
+
/** @type {QueueItem[]} */
|
218
|
+
let queueDelayed = [];
|
219
|
+
|
220
|
+
logger.timeEnd("prepare");
|
221
|
+
|
222
|
+
/** @type {Module} */
|
223
|
+
let module;
|
224
|
+
/** @type {Chunk} */
|
225
|
+
let chunk;
|
226
|
+
/** @type {ChunkGroup} */
|
227
|
+
let chunkGroup;
|
228
|
+
/** @type {DependenciesBlock} */
|
229
|
+
let block;
|
230
|
+
/** @type {Set<Module>} */
|
231
|
+
let minAvailableModules;
|
232
|
+
/** @type {QueueItem[]} */
|
233
|
+
let skippedItems;
|
234
|
+
|
235
|
+
// For each async Block in graph
|
236
|
+
/**
|
237
|
+
* @param {AsyncDependenciesBlock} b iterating over each Async DepBlock
|
238
|
+
* @returns {void}
|
239
|
+
*/
|
240
|
+
const iteratorBlock = b => {
|
241
|
+
// 1. We create a chunk for this Block
|
242
|
+
// but only once (blockChunkGroups map)
|
243
|
+
let c = blockChunkGroups.get(b);
|
244
|
+
if (c === undefined) {
|
245
|
+
c = namedChunkGroups.get(b.chunkName);
|
246
|
+
if (c && c.isInitial()) {
|
247
|
+
compilation.errors.push(
|
248
|
+
new AsyncDependencyToInitialChunkError(b.chunkName, module, b.loc)
|
249
|
+
);
|
250
|
+
c = chunkGroup;
|
251
|
+
} else {
|
252
|
+
c = compilation.addChunkInGroup(
|
253
|
+
b.groupOptions || b.chunkName,
|
254
|
+
module,
|
255
|
+
b.loc,
|
256
|
+
b.request
|
257
|
+
);
|
258
|
+
chunkGroupCounters.set(c, { index: 0, index2: 0 });
|
259
|
+
blockChunkGroups.set(b, c);
|
260
|
+
allCreatedChunkGroups.add(c);
|
261
|
+
}
|
262
|
+
} else {
|
263
|
+
// TODO webpack 5 remove addOptions check
|
264
|
+
if (c.addOptions) c.addOptions(b.groupOptions);
|
265
|
+
c.addOrigin(module, b.loc, b.request);
|
266
|
+
}
|
267
|
+
|
268
|
+
// 2. We store the Block+Chunk mapping as dependency for the chunk
|
269
|
+
let deps = chunkDependencies.get(chunkGroup);
|
270
|
+
if (!deps) chunkDependencies.set(chunkGroup, (deps = []));
|
271
|
+
deps.push({
|
272
|
+
block: b,
|
273
|
+
chunkGroup: c
|
274
|
+
});
|
275
|
+
|
276
|
+
// 3. We create/update the chunk group info
|
277
|
+
let connectList = queueConnect.get(chunkGroup);
|
278
|
+
if (connectList === undefined) {
|
279
|
+
connectList = new Set();
|
280
|
+
queueConnect.set(chunkGroup, connectList);
|
281
|
+
}
|
282
|
+
connectList.add(c);
|
283
|
+
|
284
|
+
// 4. We enqueue the DependenciesBlock for traversal
|
285
|
+
queueDelayed.push({
|
286
|
+
action: PROCESS_BLOCK,
|
287
|
+
block: b,
|
288
|
+
module: module,
|
289
|
+
chunk: c.chunks[0],
|
290
|
+
chunkGroup: c
|
291
|
+
});
|
292
|
+
};
|
293
|
+
|
294
|
+
// Iterative traversal of the Module graph
|
295
|
+
// Recursive would be simpler to write but could result in Stack Overflows
|
296
|
+
while (queue.length) {
|
297
|
+
logger.time("visiting");
|
298
|
+
while (queue.length) {
|
299
|
+
const queueItem = queue.pop();
|
300
|
+
module = queueItem.module;
|
301
|
+
block = queueItem.block;
|
302
|
+
chunk = queueItem.chunk;
|
303
|
+
if (chunkGroup !== queueItem.chunkGroup) {
|
304
|
+
chunkGroup = queueItem.chunkGroup;
|
305
|
+
const chunkGroupInfo = chunkGroupInfoMap.get(chunkGroup);
|
306
|
+
minAvailableModules = chunkGroupInfo.minAvailableModules;
|
307
|
+
skippedItems = chunkGroupInfo.skippedItems;
|
308
|
+
}
|
309
|
+
|
310
|
+
switch (queueItem.action) {
|
311
|
+
case ADD_AND_ENTER_MODULE: {
|
312
|
+
if (minAvailableModules.has(module)) {
|
313
|
+
// already in parent chunks
|
314
|
+
// skip it for now, but enqueue for rechecking when minAvailableModules shrinks
|
315
|
+
skippedItems.push(queueItem);
|
316
|
+
break;
|
317
|
+
}
|
318
|
+
// We connect Module and Chunk when not already done
|
319
|
+
if (chunk.addModule(module)) {
|
320
|
+
module.addChunk(chunk);
|
321
|
+
} else {
|
322
|
+
// already connected, skip it
|
323
|
+
break;
|
324
|
+
}
|
325
|
+
}
|
326
|
+
// fallthrough
|
327
|
+
case ENTER_MODULE: {
|
328
|
+
if (chunkGroup !== undefined) {
|
329
|
+
const index = chunkGroup.getModuleIndex(module);
|
330
|
+
if (index === undefined) {
|
331
|
+
chunkGroup.setModuleIndex(
|
332
|
+
module,
|
333
|
+
chunkGroupCounters.get(chunkGroup).index++
|
334
|
+
);
|
335
|
+
}
|
336
|
+
}
|
337
|
+
|
338
|
+
if (module.index === null) {
|
339
|
+
module.index = nextFreeModuleIndex++;
|
340
|
+
}
|
341
|
+
|
342
|
+
queue.push({
|
343
|
+
action: LEAVE_MODULE,
|
344
|
+
block,
|
345
|
+
module,
|
346
|
+
chunk,
|
347
|
+
chunkGroup
|
348
|
+
});
|
349
|
+
}
|
350
|
+
// fallthrough
|
351
|
+
case PROCESS_BLOCK: {
|
352
|
+
// get prepared block info
|
353
|
+
const blockInfo = blockInfoMap.get(block);
|
354
|
+
|
355
|
+
// Buffer items because order need to be reverse to get indicies correct
|
356
|
+
const skipBuffer = [];
|
357
|
+
const queueBuffer = [];
|
358
|
+
// Traverse all referenced modules
|
359
|
+
for (const refModule of blockInfo.modules) {
|
360
|
+
if (chunk.containsModule(refModule)) {
|
361
|
+
// skip early if already connected
|
362
|
+
continue;
|
363
|
+
}
|
364
|
+
if (minAvailableModules.has(refModule)) {
|
365
|
+
// already in parent chunks, skip it for now
|
366
|
+
skipBuffer.push({
|
367
|
+
action: ADD_AND_ENTER_MODULE,
|
368
|
+
block: refModule,
|
369
|
+
module: refModule,
|
370
|
+
chunk,
|
371
|
+
chunkGroup
|
372
|
+
});
|
373
|
+
continue;
|
374
|
+
}
|
375
|
+
// enqueue the add and enter to enter in the correct order
|
376
|
+
// this is relevant with circular dependencies
|
377
|
+
queueBuffer.push({
|
378
|
+
action: ADD_AND_ENTER_MODULE,
|
379
|
+
block: refModule,
|
380
|
+
module: refModule,
|
381
|
+
chunk,
|
382
|
+
chunkGroup
|
383
|
+
});
|
384
|
+
}
|
385
|
+
// Add buffered items in reversed order
|
386
|
+
for (let i = skipBuffer.length - 1; i >= 0; i--) {
|
387
|
+
skippedItems.push(skipBuffer[i]);
|
388
|
+
}
|
389
|
+
for (let i = queueBuffer.length - 1; i >= 0; i--) {
|
390
|
+
queue.push(queueBuffer[i]);
|
391
|
+
}
|
392
|
+
|
393
|
+
// Traverse all Blocks
|
394
|
+
for (const block of blockInfo.blocks) iteratorBlock(block);
|
395
|
+
|
396
|
+
if (blockInfo.blocks.length > 0 && module !== block) {
|
397
|
+
blocksWithNestedBlocks.add(block);
|
398
|
+
}
|
399
|
+
break;
|
400
|
+
}
|
401
|
+
case LEAVE_MODULE: {
|
402
|
+
if (chunkGroup !== undefined) {
|
403
|
+
const index = chunkGroup.getModuleIndex2(module);
|
404
|
+
if (index === undefined) {
|
405
|
+
chunkGroup.setModuleIndex2(
|
406
|
+
module,
|
407
|
+
chunkGroupCounters.get(chunkGroup).index2++
|
408
|
+
);
|
409
|
+
}
|
410
|
+
}
|
411
|
+
|
412
|
+
if (module.index2 === null) {
|
413
|
+
module.index2 = nextFreeModuleIndex2++;
|
414
|
+
}
|
415
|
+
break;
|
416
|
+
}
|
417
|
+
}
|
418
|
+
}
|
419
|
+
logger.timeEnd("visiting");
|
420
|
+
|
421
|
+
if (queueConnect.size > 0) {
|
422
|
+
logger.time("calculating available modules");
|
423
|
+
|
424
|
+
// Figure out new parents for chunk groups
|
425
|
+
// to get new available modules for these children
|
426
|
+
for (const [chunkGroup, targets] of queueConnect) {
|
427
|
+
const info = chunkGroupInfoMap.get(chunkGroup);
|
428
|
+
let minAvailableModules = info.minAvailableModules;
|
429
|
+
|
430
|
+
// 1. Create a new Set of available modules at this points
|
431
|
+
const resultingAvailableModules = new Set(minAvailableModules);
|
432
|
+
for (const chunk of chunkGroup.chunks) {
|
433
|
+
for (const m of chunk.modulesIterable) {
|
434
|
+
resultingAvailableModules.add(m);
|
435
|
+
}
|
436
|
+
}
|
437
|
+
info.resultingAvailableModules = resultingAvailableModules;
|
438
|
+
|
439
|
+
// 2. Update chunk group info
|
440
|
+
for (const target of targets) {
|
441
|
+
let chunkGroupInfo = chunkGroupInfoMap.get(target);
|
442
|
+
if (chunkGroupInfo === undefined) {
|
443
|
+
chunkGroupInfo = {
|
444
|
+
minAvailableModules: undefined,
|
445
|
+
minAvailableModulesOwned: undefined,
|
446
|
+
availableModulesToBeMerged: [],
|
447
|
+
skippedItems: [],
|
448
|
+
resultingAvailableModules: undefined
|
449
|
+
};
|
450
|
+
chunkGroupInfoMap.set(target, chunkGroupInfo);
|
451
|
+
}
|
452
|
+
chunkGroupInfo.availableModulesToBeMerged.push(
|
453
|
+
resultingAvailableModules
|
454
|
+
);
|
455
|
+
outdatedChunkGroupInfo.add(chunkGroupInfo);
|
456
|
+
}
|
457
|
+
}
|
458
|
+
queueConnect.clear();
|
459
|
+
logger.timeEnd("calculating available modules");
|
460
|
+
|
461
|
+
if (outdatedChunkGroupInfo.size > 0) {
|
462
|
+
logger.time("merging available modules");
|
463
|
+
// Execute the merge
|
464
|
+
for (const info of outdatedChunkGroupInfo) {
|
465
|
+
const availableModulesToBeMerged = info.availableModulesToBeMerged;
|
466
|
+
let minAvailableModules = info.minAvailableModules;
|
467
|
+
|
468
|
+
// 1. Get minimal available modules
|
469
|
+
// It doesn't make sense to traverse a chunk again with more available modules.
|
470
|
+
// This step calculates the minimal available modules and skips traversal when
|
471
|
+
// the list didn't shrink.
|
472
|
+
if (availableModulesToBeMerged.length > 1) {
|
473
|
+
availableModulesToBeMerged.sort(bySetSize);
|
474
|
+
}
|
475
|
+
let changed = false;
|
476
|
+
for (const availableModules of availableModulesToBeMerged) {
|
477
|
+
if (minAvailableModules === undefined) {
|
478
|
+
minAvailableModules = availableModules;
|
479
|
+
info.minAvailableModules = minAvailableModules;
|
480
|
+
info.minAvailableModulesOwned = false;
|
481
|
+
changed = true;
|
482
|
+
} else {
|
483
|
+
if (info.minAvailableModulesOwned) {
|
484
|
+
// We own it and can modify it
|
485
|
+
for (const m of minAvailableModules) {
|
486
|
+
if (!availableModules.has(m)) {
|
487
|
+
minAvailableModules.delete(m);
|
488
|
+
changed = true;
|
489
|
+
}
|
490
|
+
}
|
491
|
+
} else {
|
492
|
+
for (const m of minAvailableModules) {
|
493
|
+
if (!availableModules.has(m)) {
|
494
|
+
// minAvailableModules need to be modified
|
495
|
+
// but we don't own it
|
496
|
+
// construct a new Set as intersection of minAvailableModules and availableModules
|
497
|
+
/** @type {Set<Module>} */
|
498
|
+
const newSet = new Set();
|
499
|
+
const iterator = minAvailableModules[Symbol.iterator]();
|
500
|
+
/** @type {IteratorResult<Module>} */
|
501
|
+
let it;
|
502
|
+
while (!(it = iterator.next()).done) {
|
503
|
+
const module = it.value;
|
504
|
+
if (module === m) break;
|
505
|
+
newSet.add(module);
|
506
|
+
}
|
507
|
+
while (!(it = iterator.next()).done) {
|
508
|
+
const module = it.value;
|
509
|
+
if (availableModules.has(module)) {
|
510
|
+
newSet.add(module);
|
511
|
+
}
|
512
|
+
}
|
513
|
+
minAvailableModules = newSet;
|
514
|
+
info.minAvailableModulesOwned = true;
|
515
|
+
info.minAvailableModules = newSet;
|
516
|
+
changed = true;
|
517
|
+
break;
|
518
|
+
}
|
519
|
+
}
|
520
|
+
}
|
521
|
+
}
|
522
|
+
}
|
523
|
+
availableModulesToBeMerged.length = 0;
|
524
|
+
if (!changed) continue;
|
525
|
+
|
526
|
+
// 2. Reconsider skipped items
|
527
|
+
for (const queueItem of info.skippedItems) {
|
528
|
+
queue.push(queueItem);
|
529
|
+
}
|
530
|
+
info.skippedItems.length = 0;
|
531
|
+
}
|
532
|
+
outdatedChunkGroupInfo.clear();
|
533
|
+
logger.timeEnd("merging available modules");
|
534
|
+
}
|
535
|
+
}
|
536
|
+
|
537
|
+
// Run queueDelayed when all items of the queue are processed
|
538
|
+
// This is important to get the global indicing correct
|
539
|
+
// Async blocks should be processed after all sync blocks are processed
|
540
|
+
if (queue.length === 0) {
|
541
|
+
const tempQueue = queue;
|
542
|
+
queue = queueDelayed.reverse();
|
543
|
+
queueDelayed = tempQueue;
|
544
|
+
}
|
545
|
+
}
|
546
|
+
};
|
547
|
+
|
548
|
+
/**
|
549
|
+
*
|
550
|
+
* @param {Set<DependenciesBlock>} blocksWithNestedBlocks flag for blocks that have nested blocks
|
551
|
+
* @param {Map<ChunkGroup, ChunkGroupDep[]>} chunkDependencies dependencies for chunk groups
|
552
|
+
* @param {Map<ChunkGroup, ChunkGroupInfo>} chunkGroupInfoMap mapping from chunk group to available modules
|
553
|
+
*/
|
554
|
+
const connectChunkGroups = (
|
555
|
+
blocksWithNestedBlocks,
|
556
|
+
chunkDependencies,
|
557
|
+
chunkGroupInfoMap
|
558
|
+
) => {
|
559
|
+
/** @type {Set<Module>} */
|
560
|
+
let resultingAvailableModules;
|
561
|
+
|
562
|
+
/**
|
563
|
+
* Helper function to check if all modules of a chunk are available
|
564
|
+
*
|
565
|
+
* @param {ChunkGroup} chunkGroup the chunkGroup to scan
|
566
|
+
* @param {Set<Module>} availableModules the comparitor set
|
567
|
+
* @returns {boolean} return true if all modules of a chunk are available
|
568
|
+
*/
|
569
|
+
const areModulesAvailable = (chunkGroup, availableModules) => {
|
570
|
+
for (const chunk of chunkGroup.chunks) {
|
571
|
+
for (const module of chunk.modulesIterable) {
|
572
|
+
if (!availableModules.has(module)) return false;
|
573
|
+
}
|
574
|
+
}
|
575
|
+
return true;
|
576
|
+
};
|
577
|
+
|
578
|
+
// For each edge in the basic chunk graph
|
579
|
+
/**
|
580
|
+
* @param {ChunkGroupDep} dep the dependency used for filtering
|
581
|
+
* @returns {boolean} used to filter "edges" (aka Dependencies) that were pointing
|
582
|
+
* to modules that are already available. Also filters circular dependencies in the chunks graph
|
583
|
+
*/
|
584
|
+
const filterFn = dep => {
|
585
|
+
const depChunkGroup = dep.chunkGroup;
|
586
|
+
// TODO is this needed?
|
587
|
+
if (blocksWithNestedBlocks.has(dep.block)) return true;
|
588
|
+
if (areModulesAvailable(depChunkGroup, resultingAvailableModules)) {
|
589
|
+
return false; // break all modules are already available
|
590
|
+
}
|
591
|
+
return true;
|
592
|
+
};
|
593
|
+
|
594
|
+
// For all deps, check if chunk groups need to be connected
|
595
|
+
for (const [chunkGroup, deps] of chunkDependencies) {
|
596
|
+
if (deps.length === 0) continue;
|
597
|
+
|
598
|
+
// 1. Get info from chunk group info map
|
599
|
+
const info = chunkGroupInfoMap.get(chunkGroup);
|
600
|
+
resultingAvailableModules = info.resultingAvailableModules;
|
601
|
+
|
602
|
+
// 2. Foreach edge
|
603
|
+
for (let i = 0; i < deps.length; i++) {
|
604
|
+
const dep = deps[i];
|
605
|
+
|
606
|
+
// Filter inline, rather than creating a new array from `.filter()`
|
607
|
+
// TODO check if inlining filterFn makes sense here
|
608
|
+
if (!filterFn(dep)) {
|
609
|
+
continue;
|
610
|
+
}
|
611
|
+
const depChunkGroup = dep.chunkGroup;
|
612
|
+
const depBlock = dep.block;
|
613
|
+
|
614
|
+
// 5. Connect block with chunk
|
615
|
+
GraphHelpers.connectDependenciesBlockAndChunkGroup(
|
616
|
+
depBlock,
|
617
|
+
depChunkGroup
|
618
|
+
);
|
619
|
+
|
620
|
+
// 6. Connect chunk with parent
|
621
|
+
GraphHelpers.connectChunkGroupParentAndChild(chunkGroup, depChunkGroup);
|
622
|
+
}
|
623
|
+
}
|
624
|
+
};
|
625
|
+
|
626
|
+
/**
|
627
|
+
* Remove all unconnected chunk groups
|
628
|
+
* @param {Compilation} compilation the compilation
|
629
|
+
* @param {Iterable<ChunkGroup>} allCreatedChunkGroups all chunk groups that where created before
|
630
|
+
*/
|
631
|
+
const cleanupUnconnectedGroups = (compilation, allCreatedChunkGroups) => {
|
632
|
+
for (const chunkGroup of allCreatedChunkGroups) {
|
633
|
+
if (chunkGroup.getNumberOfParents() === 0) {
|
634
|
+
for (const chunk of chunkGroup.chunks) {
|
635
|
+
const idx = compilation.chunks.indexOf(chunk);
|
636
|
+
if (idx >= 0) compilation.chunks.splice(idx, 1);
|
637
|
+
chunk.remove("unconnected");
|
638
|
+
}
|
639
|
+
chunkGroup.remove("unconnected");
|
640
|
+
}
|
641
|
+
}
|
642
|
+
};
|
643
|
+
|
644
|
+
/**
|
645
|
+
* This method creates the Chunk graph from the Module graph
|
646
|
+
* @param {Compilation} compilation the compilation
|
647
|
+
* @param {Entrypoint[]} inputChunkGroups chunk groups which are processed
|
648
|
+
* @returns {void}
|
649
|
+
*/
|
650
|
+
const buildChunkGraph = (compilation, inputChunkGroups) => {
|
651
|
+
// SHARED STATE
|
652
|
+
|
653
|
+
/** @type {Map<ChunkGroup, ChunkGroupDep[]>} */
|
654
|
+
const chunkDependencies = new Map();
|
655
|
+
|
656
|
+
/** @type {Set<ChunkGroup>} */
|
657
|
+
const allCreatedChunkGroups = new Set();
|
658
|
+
|
659
|
+
/** @type {Map<ChunkGroup, ChunkGroupInfo>} */
|
660
|
+
const chunkGroupInfoMap = new Map();
|
661
|
+
|
662
|
+
/** @type {Set<DependenciesBlock>} */
|
663
|
+
const blocksWithNestedBlocks = new Set();
|
664
|
+
|
665
|
+
// PART ONE
|
666
|
+
|
667
|
+
visitModules(
|
668
|
+
compilation,
|
669
|
+
inputChunkGroups,
|
670
|
+
chunkGroupInfoMap,
|
671
|
+
chunkDependencies,
|
672
|
+
blocksWithNestedBlocks,
|
673
|
+
allCreatedChunkGroups
|
674
|
+
);
|
675
|
+
|
676
|
+
// PART TWO
|
677
|
+
|
678
|
+
connectChunkGroups(
|
679
|
+
blocksWithNestedBlocks,
|
680
|
+
chunkDependencies,
|
681
|
+
chunkGroupInfoMap
|
682
|
+
);
|
683
|
+
|
684
|
+
// Cleaup work
|
685
|
+
|
686
|
+
cleanupUnconnectedGroups(compilation, allCreatedChunkGroups);
|
687
|
+
};
|
688
|
+
|
689
|
+
module.exports = buildChunkGraph;
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "webpack",
|
3
|
-
"version": "4.
|
3
|
+
"version": "4.38.0",
|
4
4
|
"author": "Tobias Koppers @sokra",
|
5
5
|
"description": "Packs CommonJs/AMD modules for the browser. Allows to split your codebase into multiple bundles, which can be loaded on demand. Support loaders to preprocess files, i.e. json, jsx, es7, css, less, ... and your custom stuff.",
|
6
6
|
"license": "MIT",
|