Skip to content

Commit 63013cc

Browse files
test: cover dev-server-like plugin hooks on Compiler and MultiCompiler
1 parent ab4cab0 commit 63013cc

1 file changed

Lines changed: 245 additions & 0 deletions

File tree

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
"use strict";
2+
3+
require("../helpers/warmup-webpack");
4+
5+
const path = require("path");
6+
const { Volume, createFsFromVolume } = require("memfs");
7+
const webpack = require("../..");
8+
const expectNoDeprecations = require("../helpers/expectNoDeprecations");
9+
10+
/** @typedef {import("../../").Compiler} Compiler */
11+
/** @typedef {import("../../").MultiCompiler} MultiCompiler */
12+
/** @typedef {import("../../").OutputFileSystem} OutputFileSystem */
13+
/** @typedef {import("memfs").IFs} IFs */
14+
15+
const fixtures = path.join(__dirname, "..", "fixtures");
16+
const outputPath = path.join(fixtures, "dist");
17+
const PLUGIN_NAME = "DevServerLikePlugin";
18+
19+
/**
20+
* @returns {IFs} an empty in-memory file system
21+
*/
22+
const createFs = () => createFsFromVolume(new Volume());
23+
24+
/**
25+
* @param {number} ms milliseconds
26+
* @returns {Promise<void>} resolves after `ms`
27+
*/
28+
const delay = (ms) =>
29+
new Promise((resolve) => {
30+
setTimeout(resolve, ms);
31+
});
32+
33+
// Taps hooks the way webpack-dev-server's `apply()` does, with no branching
34+
// on the compiler type, and records what each hook did.
35+
class DevServerLikePlugin {
36+
constructor() {
37+
this.fs = createFs();
38+
this.applied = 0;
39+
this.setupCalls = 0;
40+
this.listenCalls = 0;
41+
this.stopCalls = 0;
42+
this.invalidCalls = 0;
43+
this.doneCalls = 0;
44+
/** @type {boolean} */
45+
this.hasLogger = false;
46+
/** @type {boolean} */
47+
this.stopFinished = false;
48+
}
49+
50+
/**
51+
* @param {Compiler | MultiCompiler} compiler a compiler or a multi compiler
52+
* @returns {void}
53+
*/
54+
apply(compiler) {
55+
this.applied++;
56+
this.hasLogger =
57+
typeof compiler.getInfrastructureLogger(PLUGIN_NAME).info === "function";
58+
/** @type {Promise<void> | undefined} */
59+
let setupPromise;
60+
let inWatchMode = false;
61+
let listening = false;
62+
let stopped = false;
63+
const ensureSetup = () => {
64+
if (!setupPromise) {
65+
// Asynchronous like dev-server's setup(), which swaps the output fs.
66+
setupPromise = delay(10).then(() => {
67+
this.setupCalls++;
68+
compiler.outputFileSystem = /** @type {OutputFileSystem} */ (
69+
/** @type {unknown} */ (this.fs)
70+
);
71+
});
72+
}
73+
return setupPromise;
74+
};
75+
const { hooks } = /** @type {Compiler} */ (compiler);
76+
hooks.watchRun.tapPromise(PLUGIN_NAME, () => {
77+
inWatchMode = true;
78+
return ensureSetup();
79+
});
80+
hooks.invalid.tap(PLUGIN_NAME, () => {
81+
this.invalidCalls++;
82+
});
83+
hooks.done.tap(PLUGIN_NAME, () => {
84+
this.doneCalls++;
85+
if (listening || !inWatchMode) return;
86+
listening = true;
87+
this.listenCalls++;
88+
});
89+
hooks.shutdown.tapPromise(PLUGIN_NAME, () => {
90+
if (stopped) return Promise.resolve();
91+
stopped = true;
92+
this.stopCalls++;
93+
return delay(10).then(() => {
94+
this.stopFinished = true;
95+
});
96+
});
97+
}
98+
}
99+
100+
/**
101+
* @typedef {object} Mode
102+
* @property {string} name label of the mode
103+
* @property {string[]} bundles emitted bundle names
104+
* @property {number} invalidCallsPerInvalidate `invalid` calls per `watching.invalidate()`
105+
* @property {(plugin: DevServerLikePlugin) => Compiler | MultiCompiler} create creates the compiler
106+
*/
107+
108+
/** @type {Mode[]} */
109+
const modes = [
110+
{
111+
name: "Compiler with the plugin in `plugins`",
112+
bundles: ["a.js"],
113+
invalidCallsPerInvalidate: 1,
114+
create: (plugin) =>
115+
webpack({
116+
context: fixtures,
117+
entry: "./a.js",
118+
output: { path: outputPath, filename: "a.js" },
119+
plugins: [plugin]
120+
})
121+
},
122+
{
123+
name: "MultiCompiler with the plugin in top-level `plugins`",
124+
bundles: ["a.js", "b.js"],
125+
// `invalid` is a MultiHook, so the tap fires once per child.
126+
invalidCallsPerInvalidate: 2,
127+
create: (plugin) =>
128+
webpack(
129+
/** @type {import("../../").MultiConfiguration} */ (
130+
Object.assign(
131+
[
132+
{
133+
name: "a",
134+
context: fixtures,
135+
entry: "./a.js",
136+
output: { path: outputPath, filename: "a.js" }
137+
},
138+
{
139+
name: "b",
140+
context: fixtures,
141+
entry: "./b.js",
142+
output: { path: outputPath, filename: "b.js" }
143+
}
144+
],
145+
{ plugins: [plugin] }
146+
)
147+
)
148+
)
149+
}
150+
];
151+
152+
/**
153+
* @param {Compiler | MultiCompiler} compiler compiler
154+
* @returns {Promise<void>} resolves once the compiler is closed
155+
*/
156+
const close = (compiler) =>
157+
new Promise((resolve, reject) => {
158+
compiler.close((err) => (err ? reject(err) : resolve()));
159+
});
160+
161+
describe("plugin applied like webpack-dev-server", () => {
162+
expectNoDeprecations();
163+
164+
describe.each(modes)("$name", (mode) => {
165+
/** @type {DevServerLikePlugin} */
166+
let plugin;
167+
/** @type {Compiler | MultiCompiler} */
168+
let compiler;
169+
/** @type {IFs} */
170+
let originalFs;
171+
172+
beforeEach(() => {
173+
plugin = new DevServerLikePlugin();
174+
compiler = mode.create(plugin);
175+
originalFs = createFs();
176+
compiler.outputFileSystem = /** @type {OutputFileSystem} */ (
177+
/** @type {unknown} */ (originalFs)
178+
);
179+
});
180+
181+
it("is applied once with a logger", async () => {
182+
expect(plugin.applied).toBe(1);
183+
expect(plugin.hasLogger).toBe(true);
184+
await close(compiler);
185+
});
186+
187+
it("sets up on watchRun before emitting, listens once, and rebuilds on invalidate", async () => {
188+
/** @type {() => void} */
189+
let onFirstBuild = () => {};
190+
const firstBuild = new Promise((resolve) => {
191+
onFirstBuild = () => resolve(undefined);
192+
});
193+
let handled = 0;
194+
compiler.watch({}, (err) => {
195+
if (err) throw err;
196+
if (++handled === 1) onFirstBuild();
197+
});
198+
await firstBuild;
199+
200+
expect(plugin.setupCalls).toBe(1);
201+
expect(plugin.doneCalls).toBe(1);
202+
expect(plugin.listenCalls).toBe(1);
203+
// The awaited watchRun swapped the fs before anything was emitted.
204+
for (const bundle of mode.bundles) {
205+
expect(plugin.fs.existsSync(path.join(outputPath, bundle))).toBe(true);
206+
expect(originalFs.existsSync(path.join(outputPath, bundle))).toBe(
207+
false
208+
);
209+
}
210+
211+
const watching = /** @type {NonNullable<Compiler["watching"]>} */ (
212+
compiler.watching
213+
);
214+
await new Promise((resolve) => {
215+
watching.invalidate(() => resolve(undefined));
216+
});
217+
218+
expect(plugin.invalidCalls).toBe(mode.invalidCallsPerInvalidate);
219+
expect(plugin.doneCalls).toBe(2);
220+
expect(plugin.setupCalls).toBe(1);
221+
expect(plugin.listenCalls).toBe(1);
222+
223+
await close(compiler);
224+
expect(plugin.stopCalls).toBe(1);
225+
expect(plugin.stopFinished).toBe(true);
226+
});
227+
228+
it("stays passive for a one-shot run", async () => {
229+
await new Promise((resolve, reject) => {
230+
compiler.run((err) => (err ? reject(err) : resolve(undefined)));
231+
});
232+
233+
expect(plugin.doneCalls).toBe(1);
234+
expect(plugin.setupCalls).toBe(0);
235+
expect(plugin.listenCalls).toBe(0);
236+
for (const bundle of mode.bundles) {
237+
expect(originalFs.existsSync(path.join(outputPath, bundle))).toBe(true);
238+
}
239+
240+
await close(compiler);
241+
expect(plugin.stopCalls).toBe(1);
242+
expect(plugin.stopFinished).toBe(true);
243+
});
244+
});
245+
});

0 commit comments

Comments
 (0)