Skip to content

Commit e5e0f44

Browse files
committed
add algorithm to extract graph roots
show only graph roots in chunks by default
1 parent adb7076 commit e5e0f44

File tree

35 files changed

+569
-298
lines changed

35 files changed

+569
-298
lines changed

declarations/WebpackOptions.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,6 +1267,10 @@ export interface StatsOptions {
12671267
* add the origins of chunks and chunk merging info
12681268
*/
12691269
chunkOrigins?: boolean;
1270+
/**
1271+
* add root modules information to chunk information
1272+
*/
1273+
chunkRootModules?: boolean;
12701274
/**
12711275
* add chunk information
12721276
*/

lib/ChunkGraph.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const {
1515
compareSelect,
1616
compareIds
1717
} = require("./util/comparators");
18+
const findGraphRoots = require("./util/findGraphRoots");
1819

1920
/** @typedef {import("./AsyncDependenciesBlock")} AsyncDependenciesBlock */
2021
/** @typedef {import("./Chunk")} Chunk */
@@ -155,6 +156,8 @@ class ChunkGraph {
155156
this._blockChunkGroups = new WeakMap();
156157
/** @private @type {ModuleGraph} */
157158
this.moduleGraph = moduleGraph;
159+
160+
this._getGraphRoots = this._getGraphRoots.bind(this);
158161
}
159162

160163
/**
@@ -185,6 +188,22 @@ class ChunkGraph {
185188
return c;
186189
}
187190

191+
/**
192+
* @param {SortableSet<Module>} set the sortable Set to get the roots of
193+
* @returns {Module[]} the graph roots
194+
*/
195+
_getGraphRoots(set) {
196+
const { moduleGraph } = this;
197+
return Array.from(
198+
findGraphRoots(set, module => {
199+
return moduleGraph
200+
.getOutgoingConnections(module)
201+
.map(connection => connection.module)
202+
.filter(Boolean);
203+
})
204+
).sort(compareModulesByIdentifier);
205+
}
206+
188207
/**
189208
* @param {Chunk} chunk the new chunk
190209
* @param {Module} module the module
@@ -534,6 +553,15 @@ class ChunkGraph {
534553
return cgc.modules.getFromUnorderedCache(getModulesSizes);
535554
}
536555

556+
/**
557+
* @param {Chunk} chunk the chunk
558+
* @returns {Module[]} root modules of the chunks (ordered by identifer)
559+
*/
560+
getChunkRootModules(chunk) {
561+
const cgc = this._getChunkGraphChunk(chunk);
562+
return cgc.modules.getFromUnorderedCache(this._getGraphRoots);
563+
}
564+
537565
/**
538566
* @param {Chunk} chunk the chunk
539567
* @param {ChunkSizeOptions} options options object

lib/ModuleGraph.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ class ModuleGraph {
300300
* @param {Module} module the module
301301
* @returns {ModuleGraphConnection[]} list of outgoing connections
302302
*/
303-
getOutgoingConnection(module) {
303+
getOutgoingConnections(module) {
304304
const connections = this._getModuleGraphModule(module).outgoingConnections;
305305
return Array.from(connections);
306306
}

lib/Stats.js

Lines changed: 67 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,22 @@ class Stats {
170170
!forToString
171171
);
172172
const showChunks = optionOrLocalFallback(options.chunks, !forToString);
173-
const showChunkModules = optionOrLocalFallback(options.chunkModules, true);
173+
const showChunkModules = optionOrLocalFallback(
174+
options.chunkModules,
175+
!forToString
176+
);
177+
const showChunkRootModules = optionOrLocalFallback(
178+
options.chunkRootModules,
179+
forToString ? !showChunkModules : true
180+
);
174181
const showChunkOrigins = optionOrLocalFallback(
175182
options.chunkOrigins,
176183
!forToString
177184
);
178185
const showModules = optionOrLocalFallback(options.modules, true);
179186
const showNestedModules = optionOrLocalFallback(
180187
options.nestedModules,
181-
true
188+
!forToString
182189
);
183190
const showOrphanModules = optionOrLocalFallback(
184191
options.orphanModules,
@@ -793,6 +800,25 @@ class Stats {
793800
);
794801
}
795802
}
803+
if (showChunkRootModules) {
804+
const rootModules = chunkGraph.getChunkRootModules(chunk);
805+
obj.rootModules = rootModules
806+
.slice()
807+
.sort(sortRealModules)
808+
.filter(createModuleFilter("root-of-chunk"))
809+
.map(m => fnModule(m));
810+
obj.filteredRootModules = rootModules.length - obj.rootModules.length;
811+
obj.nonRootModules =
812+
chunkGraph.getNumberOfChunkModules(chunk) - rootModules.length;
813+
if (sortModules) {
814+
obj.rootModules.sort(
815+
concatComparators(
816+
sortByField(sortModules),
817+
keepOriginalOrder(obj.rootModules)
818+
)
819+
);
820+
}
821+
}
796822
if (showChunkOrigins) {
797823
const originsKeySet = new Set();
798824
obj.origins = Array.from(chunk.groupsIterable, g => g.origins)
@@ -1134,21 +1160,6 @@ class Stats {
11341160
processChunkGroups(outputChunkGroups, "Chunk Group");
11351161
}
11361162

1137-
const modulesByIdentifier = {};
1138-
if (obj.modules) {
1139-
for (const module of obj.modules) {
1140-
modulesByIdentifier[`$${module.identifier}`] = module;
1141-
}
1142-
} else if (obj.chunks) {
1143-
for (const chunk of obj.chunks) {
1144-
if (chunk.modules) {
1145-
for (const module of chunk.modules) {
1146-
modulesByIdentifier[`$${module.identifier}`] = module;
1147-
}
1148-
}
1149-
}
1150-
}
1151-
11521163
const processSizes = sizes => {
11531164
const keys = Object.keys(sizes);
11541165
if (keys.length > 1) {
@@ -1341,11 +1352,16 @@ class Stats {
13411352
newline();
13421353
}
13431354
if (module.modules) {
1344-
processModulesList(module, prefix + "| ");
1355+
processModulesList(module, prefix + "| ", "nested module");
13451356
}
13461357
};
13471358

1348-
const processModulesList = (obj, prefix) => {
1359+
const processModulesList = (
1360+
obj,
1361+
prefix,
1362+
itemType = "module",
1363+
dependentItemType = "dependent module"
1364+
) => {
13491365
if (obj.modules) {
13501366
let maxModuleId = 0;
13511367
for (const module of obj.modules) {
@@ -1397,7 +1413,19 @@ class Stats {
13971413
if (obj.modules.length > 0) colors.normal(" + ");
13981414
colors.normal(obj.filteredModules);
13991415
if (obj.modules.length > 0) colors.normal(" hidden");
1400-
colors.normal(obj.filteredModules !== 1 ? " modules" : " module");
1416+
colors.normal(` ${itemType}${obj.filteredModules !== 1 ? "s" : ""}`);
1417+
newline();
1418+
}
1419+
if (obj.dependentModules > 0) {
1420+
const additional = obj.modules.length > 0 || obj.filteredModules > 0;
1421+
colors.normal(prefix);
1422+
colors.normal(" ");
1423+
if (additional) colors.normal(" + ");
1424+
colors.normal(obj.dependentModules);
1425+
if (additional) colors.normal(" hidden");
1426+
colors.normal(
1427+
` ${dependentItemType}${obj.dependentModules !== 1 ? "s" : ""}`
1428+
);
14011429
newline();
14021430
}
14031431
}
@@ -1473,9 +1501,8 @@ class Stats {
14731501
colors.normal("[");
14741502
colors.normal(origin.moduleId);
14751503
colors.normal("] ");
1476-
const module = modulesByIdentifier[`$${origin.module}`];
1477-
if (module) {
1478-
colors.bold(module.name);
1504+
if (origin.moduleName) {
1505+
colors.bold(origin.moduleName);
14791506
colors.normal(" ");
14801507
}
14811508
}
@@ -1485,7 +1512,21 @@ class Stats {
14851512
newline();
14861513
}
14871514
}
1488-
processModulesList(chunk, " ");
1515+
const hasRootModules =
1516+
chunk.rootModules ||
1517+
chunk.filteredRootModules ||
1518+
chunk.nonRootModules;
1519+
processModulesList(
1520+
{
1521+
modules: chunk.rootModules,
1522+
filteredModules: chunk.filteredRootModules,
1523+
dependentModules: chunk.nonRootModules
1524+
},
1525+
" ",
1526+
"root module",
1527+
"dependent module"
1528+
);
1529+
processModulesList(chunk, hasRootModules ? " | " : " ", "chunk module");
14891530
}
14901531
}
14911532

@@ -1552,6 +1593,7 @@ class Stats {
15521593
modules: false,
15531594
chunks: true,
15541595
chunkModules: true,
1596+
chunkRootModules: false,
15551597
chunkOrigins: true,
15561598
depth: true,
15571599
env: true,
@@ -1572,6 +1614,7 @@ class Stats {
15721614
chunkGroups: true,
15731615
chunks: true,
15741616
chunkModules: false,
1617+
chunkRootModules: false,
15751618
chunkOrigins: true,
15761619
depth: true,
15771620
usedExports: true,

0 commit comments

Comments
 (0)