Skip to content

Commit cb47bb9

Browse files
authored
test: snapshot lazy barrel module graphs in config cases (#22399)
1 parent d37872d commit cb47bb9

28 files changed

Lines changed: 1203 additions & 143 deletions

File tree

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
"use strict";
2+
3+
const path = require("path");
4+
5+
/** @import { AsyncDependenciesBlock, Compilation, Compiler, Configuration, Dependency, Module, NormalModule } from "../../../../" */
6+
7+
/**
8+
* Collects the dependencies of a block and its nested blocks.
9+
* @param {Module | AsyncDependenciesBlock} block the block
10+
* @param {Dependency[]} dependencies collected dependencies
11+
* @returns {Dependency[]} collected dependencies
12+
*/
13+
const collectDependencies = (block, dependencies) => {
14+
for (const dependency of block.dependencies) dependencies.push(dependency);
15+
for (const child of block.blocks) collectDependencies(child, dependencies);
16+
return dependencies;
17+
};
18+
19+
/**
20+
* Groups the module dependencies of a module by request, in source order.
21+
* @param {Module} module the module
22+
* @returns {Map<string, Dependency[]>} request -> dependencies
23+
*/
24+
const groupByRequest = (module) => {
25+
/** @type {Map<string, Dependency[]>} */
26+
const groups = new Map();
27+
for (const dependency of collectDependencies(module, [])) {
28+
const request = /** @type {{ request?: string }} */ (dependency).request;
29+
if (typeof request !== "string") continue;
30+
const group = groups.get(request);
31+
if (group === undefined) {
32+
groups.set(request, [dependency]);
33+
} else {
34+
group.push(dependency);
35+
}
36+
}
37+
return groups;
38+
};
39+
40+
/**
41+
* Returns the sorted export names the dependencies of one edge forward, `*` for all.
42+
* @param {Dependency[]} dependencies dependencies of the edge
43+
* @returns {string} edge label, empty when nothing is forwarded
44+
*/
45+
const getForwardIds = (dependencies) => {
46+
/** @type {Set<string>} */
47+
const ids = new Set();
48+
for (const dependency of dependencies) {
49+
const id = dependency.getForwardId();
50+
if (id === true) return "*";
51+
if (id !== null) ids.add(id);
52+
}
53+
return [...ids].sort().join(", ");
54+
};
55+
56+
/**
57+
* Prints the module graph from the entries, each edge labeled with its forwarded ids. A side-effect-free module with edges is tagged `[barrel]`; an unresolved edge prints its request tagged `[raw]`, plus `[lazy]` when lazy barrel left it unbuilt.
58+
* @param {Compilation} compilation the compilation, read before seal
59+
* @returns {string} the printed graph
60+
*/
61+
const printLazyBarrelGraph = (compilation) => {
62+
const { moduleGraph, requestShortener } = compilation;
63+
/** @type {string[]} */
64+
const lines = [];
65+
/** @type {Set<Module>} */
66+
const seen = new Set();
67+
68+
/**
69+
* Prints a module, tagged `[barrel]` when side-effect-free with edges lazy barrel may defer.
70+
* @param {Module} module the module
71+
* @returns {string} the printed module
72+
*/
73+
const printModule = (module) => {
74+
const name = module.readableIdentifier(requestShortener);
75+
const { factoryMeta } = module;
76+
return factoryMeta !== undefined &&
77+
factoryMeta.sideEffectFree &&
78+
groupByRequest(module).size > 0
79+
? `${name} [barrel]`
80+
: name;
81+
};
82+
83+
/**
84+
* Prints the outgoing edges of a module below it.
85+
* @param {Module} module the module
86+
* @param {string} prefix tree prefix of the edge lines
87+
*/
88+
const walk = (module, prefix) => {
89+
const groups = [...groupByRequest(module)];
90+
for (let i = 0; i < groups.length; i++) {
91+
const [request, dependencies] = groups[i];
92+
const last = i === groups.length - 1;
93+
/** @type {Module | null} */
94+
let target = null;
95+
for (const dependency of dependencies) {
96+
target = moduleGraph.getModule(dependency);
97+
if (target !== null) break;
98+
}
99+
const ids = getForwardIds(dependencies);
100+
let line = `${prefix}${last ? "└─" : "├─"}${ids ? ` (${ids})` : ""} `;
101+
if (target === null) {
102+
line += `${request} [raw]`;
103+
if (dependencies.every((dependency) => dependency.isLazy())) {
104+
line += " [lazy]";
105+
}
106+
lines.push(line);
107+
continue;
108+
}
109+
line += printModule(target);
110+
if (seen.has(target)) {
111+
lines.push(`${line} (deduped)`);
112+
continue;
113+
}
114+
seen.add(target);
115+
lines.push(line);
116+
walk(target, `${prefix}${last ? " " : "│ "}`);
117+
}
118+
};
119+
120+
for (const { dependencies } of compilation.entries.values()) {
121+
for (const dependency of dependencies) {
122+
const module = moduleGraph.getModule(dependency);
123+
if (module === null || seen.has(module)) continue;
124+
seen.add(module);
125+
lines.push(printModule(module));
126+
walk(module, "");
127+
}
128+
}
129+
return lines.join("\n");
130+
};
131+
132+
/**
133+
* Creates the plugin asserting `lazyModules` stay unbuilt and snapshotting the module graph.
134+
* @param {string[]} lazyModules files, relative to the compiler context, that must never be built
135+
* @param {string=} hint snapshot hint, to tell configurations of one case apart
136+
* @returns {(compiler: Compiler) => void} plugin
137+
*/
138+
const createLazyBarrelPlugin = (lazyModules, hint) => (compiler) => {
139+
const lazyResources = lazyModules.map((file) =>
140+
path.resolve(compiler.context, file)
141+
);
142+
/** @type {Set<string>} */
143+
const built = new Set();
144+
compiler.hooks.thisCompilation.tap("createLazyBarrelTest", (compilation) => {
145+
compilation.hooks.buildModule.tap("createLazyBarrelTest", (module) => {
146+
built.add(/** @type {NormalModule} */ (module).resource);
147+
});
148+
// before seal: concatenation re-points connections
149+
compilation.hooks.finishModules.tap("createLazyBarrelTest", () => {
150+
const graph = printLazyBarrelGraph(compilation);
151+
if (hint === undefined) {
152+
expect(graph).toMatchSnapshot();
153+
} else {
154+
expect(graph).toMatchSnapshot(hint);
155+
}
156+
});
157+
});
158+
compiler.hooks.done.tap("createLazyBarrelTest", () => {
159+
for (const resource of lazyResources) {
160+
expect(built.has(resource)).toBe(false);
161+
}
162+
});
163+
};
164+
165+
/**
166+
* Applies the lazy barrel test plugin to a configuration.
167+
* @param {Configuration} options the configuration
168+
* @param {string[]} lazyModules files, relative to the case, that must never be built
169+
* @param {string=} hint snapshot hint
170+
* @returns {Configuration} the configuration with the plugin applied
171+
*/
172+
const applyLazyBarrelPlugin = (options, lazyModules, hint) => ({
173+
...options,
174+
plugins: [
175+
...(options.plugins || []),
176+
createLazyBarrelPlugin(lazyModules, hint)
177+
]
178+
});
179+
180+
/**
181+
* Creates the lazy barrel test configuration; an array builds each configuration, its snapshot named by `name` or index.
182+
* @template {Configuration | Configuration[]} T
183+
* @param {string[]} lazyModules files, relative to the case, that must never be built in any configuration
184+
* @param {T=} options extra configuration, or one per build
185+
* @returns {T} the configuration(s)
186+
*/
187+
const createLazyBarrelTest = (lazyModules, options) =>
188+
/** @type {T} */ (
189+
Array.isArray(options)
190+
? options.map((config, index) =>
191+
applyLazyBarrelPlugin(
192+
config,
193+
lazyModules,
194+
config.name === undefined ? String(index) : config.name
195+
)
196+
)
197+
: applyLazyBarrelPlugin(options || {}, lazyModules)
198+
);
199+
200+
module.exports = createLazyBarrelTest;
201+
module.exports.printLazyBarrelGraph = printLazyBarrelGraph;
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`ConfigCacheTestCases lazy-barrel basic basic should compile 1`] = `
4+
"./index.js [barrel]
5+
├─ (b, cc) ./named-barrel/index.js [barrel]
6+
│ ├─ (a) ./named-barrel/a.js
7+
│ ├─ (b, dd) ./b [raw] [lazy]
8+
│ └─ (c) ./named-barrel/c.js
9+
├─ (b, d) ./mixed-barrel/index.js [barrel]
10+
│ ├─ (default) ./a [raw] [lazy]
11+
│ ├─ (*) ./b [raw] [lazy]
12+
│ └─ (value) ./mixed-barrel/c.js
13+
├─ (b) ./star-barrel/index.js [barrel]
14+
│ ├─ (c) ./c [raw] [lazy]
15+
│ ├─ (*) ./star-barrel/a.js
16+
│ └─ (*) ./star-barrel/b.js
17+
├─ (a) ./nested-barrel/index.js [barrel]
18+
│ ├─ (a) ./nested-barrel/a.js [barrel]
19+
│ │ ├─ (b) ./nested-barrel/b.js
20+
│ │ └─ (c) ./c [raw] [lazy]
21+
│ └─ (c) ./c [raw] [lazy]
22+
├─ (inner) ./ns-barrel/index.js [barrel]
23+
│ ├─ (*) ./ns-barrel/inner.js [barrel]
24+
│ │ ├─ (p) ./ns-barrel/inner-p.js
25+
│ │ └─ (q) ./ns-barrel/inner-q.js
26+
│ └─ (x) ./other [raw] [lazy]
27+
├─ (one) ./shared-barrel/one.js [barrel]
28+
│ ├─ (a) ./shared-barrel/barrel.js [barrel]
29+
│ │ ├─ (a) ./shared-barrel/a.js
30+
│ │ ├─ (b) ./shared-barrel/b.js
31+
│ │ ├─ (c) ./shared-barrel/c.js
32+
│ │ └─ (d) ./d [raw] [lazy]
33+
│ └─ (bc) ./shared-barrel/bc.js [barrel]
34+
│ └─ (b, c) ./shared-barrel/barrel.js [barrel] (deduped)
35+
└─ (two) ./shared-barrel/two.js [barrel]
36+
└─ (b) ./shared-barrel/barrel.js [barrel] (deduped)"
37+
`;
38+
39+
exports[`ConfigCacheTestCases lazy-barrel basic basic should compile 2`] = `
40+
"./index.js [barrel]
41+
├─ (b, cc) ./named-barrel/index.js [barrel]
42+
│ ├─ (a) ./named-barrel/a.js
43+
│ ├─ (b, dd) ./b [raw] [lazy]
44+
│ └─ (c) ./named-barrel/c.js
45+
├─ (b, d) ./mixed-barrel/index.js [barrel]
46+
│ ├─ (default) ./a [raw] [lazy]
47+
│ ├─ (*) ./b [raw] [lazy]
48+
│ └─ (value) ./mixed-barrel/c.js
49+
├─ (b) ./star-barrel/index.js [barrel]
50+
│ ├─ (c) ./c [raw] [lazy]
51+
│ ├─ (*) ./star-barrel/a.js
52+
│ └─ (*) ./star-barrel/b.js
53+
├─ (a) ./nested-barrel/index.js [barrel]
54+
│ ├─ (a) ./nested-barrel/a.js [barrel]
55+
│ │ ├─ (b) ./nested-barrel/b.js
56+
│ │ └─ (c) ./c [raw] [lazy]
57+
│ └─ (c) ./c [raw] [lazy]
58+
├─ (inner) ./ns-barrel/index.js [barrel]
59+
│ ├─ (*) ./ns-barrel/inner.js [barrel]
60+
│ │ ├─ (p) ./ns-barrel/inner-p.js
61+
│ │ └─ (q) ./ns-barrel/inner-q.js
62+
│ └─ (x) ./other [raw] [lazy]
63+
├─ (one) ./shared-barrel/one.js [barrel]
64+
│ ├─ (a) ./shared-barrel/barrel.js [barrel]
65+
│ │ ├─ (a) ./shared-barrel/a.js
66+
│ │ ├─ (b) ./shared-barrel/b.js
67+
│ │ ├─ (c) ./shared-barrel/c.js
68+
│ │ └─ (d) ./d [raw] [lazy]
69+
│ └─ (bc) ./shared-barrel/bc.js [barrel]
70+
│ └─ (b, c) ./shared-barrel/barrel.js [barrel] (deduped)
71+
└─ (two) ./shared-barrel/two.js [barrel]
72+
└─ (b) ./shared-barrel/barrel.js [barrel] (deduped)"
73+
`;
74+
75+
exports[`ConfigCacheTestCases lazy-barrel basic basic should pre-compile to fill disk cache (1st) 1`] = `
76+
"./index.js [barrel]
77+
├─ (b, cc) ./named-barrel/index.js [barrel]
78+
│ ├─ (a) ./named-barrel/a.js
79+
│ ├─ (b, dd) ./b [raw] [lazy]
80+
│ └─ (c) ./named-barrel/c.js
81+
├─ (b, d) ./mixed-barrel/index.js [barrel]
82+
│ ├─ (default) ./a [raw] [lazy]
83+
│ ├─ (*) ./b [raw] [lazy]
84+
│ └─ (value) ./mixed-barrel/c.js
85+
├─ (b) ./star-barrel/index.js [barrel]
86+
│ ├─ (c) ./c [raw] [lazy]
87+
│ ├─ (*) ./star-barrel/a.js
88+
│ └─ (*) ./star-barrel/b.js
89+
├─ (a) ./nested-barrel/index.js [barrel]
90+
│ ├─ (a) ./nested-barrel/a.js [barrel]
91+
│ │ ├─ (b) ./nested-barrel/b.js
92+
│ │ └─ (c) ./c [raw] [lazy]
93+
│ └─ (c) ./c [raw] [lazy]
94+
├─ (inner) ./ns-barrel/index.js [barrel]
95+
│ ├─ (*) ./ns-barrel/inner.js [barrel]
96+
│ │ ├─ (p) ./ns-barrel/inner-p.js
97+
│ │ └─ (q) ./ns-barrel/inner-q.js
98+
│ └─ (x) ./other [raw] [lazy]
99+
├─ (one) ./shared-barrel/one.js [barrel]
100+
│ ├─ (a) ./shared-barrel/barrel.js [barrel]
101+
│ │ ├─ (a) ./shared-barrel/a.js
102+
│ │ ├─ (b) ./shared-barrel/b.js
103+
│ │ ├─ (c) ./shared-barrel/c.js
104+
│ │ └─ (d) ./d [raw] [lazy]
105+
│ └─ (bc) ./shared-barrel/bc.js [barrel]
106+
│ └─ (b, c) ./shared-barrel/barrel.js [barrel] (deduped)
107+
└─ (two) ./shared-barrel/two.js [barrel]
108+
└─ (b) ./shared-barrel/barrel.js [barrel] (deduped)"
109+
`;
110+
111+
exports[`ConfigCacheTestCases lazy-barrel basic basic should pre-compile to fill disk cache (2nd) 1`] = `
112+
"./index.js [barrel]
113+
├─ (b, cc) ./named-barrel/index.js [barrel]
114+
│ ├─ (a) ./named-barrel/a.js
115+
│ ├─ (b, dd) ./b [raw] [lazy]
116+
│ └─ (c) ./named-barrel/c.js
117+
├─ (b, d) ./mixed-barrel/index.js [barrel]
118+
│ ├─ (default) ./a [raw] [lazy]
119+
│ ├─ (*) ./b [raw] [lazy]
120+
│ └─ (value) ./mixed-barrel/c.js
121+
├─ (b) ./star-barrel/index.js [barrel]
122+
│ ├─ (c) ./c [raw] [lazy]
123+
│ ├─ (*) ./star-barrel/a.js
124+
│ └─ (*) ./star-barrel/b.js
125+
├─ (a) ./nested-barrel/index.js [barrel]
126+
│ ├─ (a) ./nested-barrel/a.js [barrel]
127+
│ │ ├─ (b) ./nested-barrel/b.js
128+
│ │ └─ (c) ./c [raw] [lazy]
129+
│ └─ (c) ./c [raw] [lazy]
130+
├─ (inner) ./ns-barrel/index.js [barrel]
131+
│ ├─ (*) ./ns-barrel/inner.js [barrel]
132+
│ │ ├─ (p) ./ns-barrel/inner-p.js
133+
│ │ └─ (q) ./ns-barrel/inner-q.js
134+
│ └─ (x) ./other [raw] [lazy]
135+
├─ (one) ./shared-barrel/one.js [barrel]
136+
│ ├─ (a) ./shared-barrel/barrel.js [barrel]
137+
│ │ ├─ (a) ./shared-barrel/a.js
138+
│ │ ├─ (b) ./shared-barrel/b.js
139+
│ │ ├─ (c) ./shared-barrel/c.js
140+
│ │ └─ (d) ./d [raw] [lazy]
141+
│ └─ (bc) ./shared-barrel/bc.js [barrel]
142+
│ └─ (b, c) ./shared-barrel/barrel.js [barrel] (deduped)
143+
└─ (two) ./shared-barrel/two.js [barrel]
144+
└─ (b) ./shared-barrel/barrel.js [barrel] (deduped)"
145+
`;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`ConfigTestCases lazy-barrel basic basic should compile 1`] = `
4+
"./index.js [barrel]
5+
├─ (b, cc) ./named-barrel/index.js [barrel]
6+
│ ├─ (a) ./named-barrel/a.js
7+
│ ├─ (b, dd) ./b [raw] [lazy]
8+
│ └─ (c) ./named-barrel/c.js
9+
├─ (b, d) ./mixed-barrel/index.js [barrel]
10+
│ ├─ (default) ./a [raw] [lazy]
11+
│ ├─ (*) ./b [raw] [lazy]
12+
│ └─ (value) ./mixed-barrel/c.js
13+
├─ (b) ./star-barrel/index.js [barrel]
14+
│ ├─ (c) ./c [raw] [lazy]
15+
│ ├─ (*) ./star-barrel/a.js
16+
│ └─ (*) ./star-barrel/b.js
17+
├─ (a) ./nested-barrel/index.js [barrel]
18+
│ ├─ (a) ./nested-barrel/a.js [barrel]
19+
│ │ ├─ (b) ./nested-barrel/b.js
20+
│ │ └─ (c) ./c [raw] [lazy]
21+
│ └─ (c) ./c [raw] [lazy]
22+
├─ (inner) ./ns-barrel/index.js [barrel]
23+
│ ├─ (*) ./ns-barrel/inner.js [barrel]
24+
│ │ ├─ (p) ./ns-barrel/inner-p.js
25+
│ │ └─ (q) ./ns-barrel/inner-q.js
26+
│ └─ (x) ./other [raw] [lazy]
27+
├─ (one) ./shared-barrel/one.js [barrel]
28+
│ ├─ (a) ./shared-barrel/barrel.js [barrel]
29+
│ │ ├─ (a) ./shared-barrel/a.js
30+
│ │ ├─ (b) ./shared-barrel/b.js
31+
│ │ ├─ (c) ./shared-barrel/c.js
32+
│ │ └─ (d) ./d [raw] [lazy]
33+
│ └─ (bc) ./shared-barrel/bc.js [barrel]
34+
│ └─ (b, c) ./shared-barrel/barrel.js [barrel] (deduped)
35+
└─ (two) ./shared-barrel/two.js [barrel]
36+
└─ (b) ./shared-barrel/barrel.js [barrel] (deduped)"
37+
`;

0 commit comments

Comments
 (0)