Skip to content

Commit 11d99c0

Browse files
test: issue 3204 (#3691)
1 parent 76112b7 commit 11d99c0

File tree

6 files changed

+222
-9
lines changed

6 files changed

+222
-9
lines changed

test/e2e/lazy-compilation.test.js

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,18 @@
22

33
const webpack = require("webpack");
44
const Server = require("../../lib/Server");
5-
const config = require("../fixtures/client-config/webpack.config");
5+
const lazyCompilationSingleEntryConfig = require("../fixtures/lazy-compilation-single-entry/webpack.config");
6+
const lazyCompilationMultipleEntriesConfig = require("../fixtures/lazy-compilation-multiple-entries/webpack.config");
67
const runBrowser = require("../helpers/run-browser");
78
const port = require("../ports-map")["lazy-compilation"];
9+
// const isWebpack5 = require("../helpers/isWebpack5");
10+
11+
// const itOnlyWebpack5 = isWebpack5 ? it : it.skip;
812

913
describe("lazy compilation", () => {
1014
// TODO jest freeze due webpack do not close `eventsource`, we should uncomment this after fix it on webpack side
11-
it.skip(`should work`, async () => {
12-
const compiler = webpack({
13-
...config,
14-
experiments: {
15-
lazyCompilation: true,
16-
},
17-
});
15+
it.skip(`should work with single entry`, async () => {
16+
const compiler = webpack(lazyCompilationSingleEntryConfig);
1817
const server = new Server({ port }, compiler);
1918

2019
await server.start();
@@ -32,7 +31,7 @@ describe("lazy compilation", () => {
3231
pageErrors.push(error);
3332
});
3433

35-
await page.goto(`http://127.0.0.1:${port}/main`, {
34+
await page.goto(`http://127.0.0.1:${port}/test.html`, {
3635
waitUntil: "domcontentloaded",
3736
});
3837
await new Promise((resolve) => {
@@ -51,4 +50,58 @@ describe("lazy compilation", () => {
5150
await browser.close();
5251
await server.stop();
5352
});
53+
54+
it.skip(`should work with multiple entries`, async () => {
55+
const compiler = webpack(lazyCompilationMultipleEntriesConfig);
56+
const server = new Server({ port }, compiler);
57+
58+
await server.start();
59+
60+
const { page, browser } = await runBrowser();
61+
62+
const pageErrors = [];
63+
const consoleMessages = [];
64+
65+
page
66+
.on("console", (message) => {
67+
consoleMessages.push(message.text());
68+
})
69+
.on("pageerror", (error) => {
70+
pageErrors.push(error);
71+
});
72+
73+
await page.goto(`http://127.0.0.1:${port}/test-one.html`, {
74+
waitUntil: "domcontentloaded",
75+
});
76+
await new Promise((resolve) => {
77+
const interval = setInterval(() => {
78+
console.log(consoleMessages);
79+
if (consoleMessages.includes("One.")) {
80+
clearInterval(interval);
81+
82+
resolve();
83+
}
84+
}, 100);
85+
});
86+
87+
await page.goto(`http://127.0.0.1:${port}/test-two.html`, {
88+
waitUntil: "domcontentloaded",
89+
});
90+
await new Promise((resolve) => {
91+
const interval = setInterval(() => {
92+
console.log(consoleMessages);
93+
if (consoleMessages.includes("Two.")) {
94+
clearInterval(interval);
95+
96+
resolve();
97+
}
98+
}, 100);
99+
});
100+
101+
expect(consoleMessages).toMatchSnapshot("console messages");
102+
expect(pageErrors).toMatchSnapshot("page errors");
103+
104+
await browser.close();
105+
await server.stop();
106+
});
54107
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use strict";
2+
3+
console.log("One.");
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use strict";
2+
3+
console.log("Two.");
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
"use strict";
2+
3+
const webpack = require("webpack");
4+
5+
const isWebpack5 = webpack.version.startsWith("5");
6+
7+
const oneHTMLContent = `
8+
<!doctype html>
9+
<html>
10+
<head>
11+
<meta charset='UTF-8'>
12+
<title>test</title>
13+
<script src="one.js"></script>
14+
</head>
15+
<body></body>
16+
</html>
17+
`;
18+
const twoHTMLContent = `
19+
<!doctype html>
20+
<html>
21+
<head>
22+
<meta charset='UTF-8'>
23+
<title>test</title>
24+
<script src="two.js"></script>
25+
</head>
26+
<body></body>
27+
</html>
28+
`;
29+
30+
module.exports = {
31+
devtool: false,
32+
mode: "development",
33+
context: __dirname,
34+
stats: "none",
35+
entry: {
36+
one: "./one.js",
37+
two: "./two.js",
38+
},
39+
output: {
40+
path: "/",
41+
},
42+
experiments: {
43+
lazyCompilation: true,
44+
},
45+
infrastructureLogging: isWebpack5
46+
? {
47+
level: "info",
48+
stream: {
49+
write: () => {},
50+
},
51+
}
52+
: {
53+
level: "info",
54+
},
55+
plugins: [
56+
{
57+
apply(compiler) {
58+
const pluginName = "html-generator-plugin-test";
59+
const oneFilename = "test-one.html";
60+
const twoFilename = "test-two.html";
61+
62+
compiler.hooks.thisCompilation.tap(pluginName, (compilation) => {
63+
const { RawSource } = compiler.webpack.sources;
64+
65+
compilation.hooks.processAssets.tap(
66+
{
67+
name: pluginName,
68+
stage:
69+
compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL,
70+
},
71+
() => {
72+
const oneSource = new RawSource(oneHTMLContent);
73+
74+
compilation.emitAsset(oneFilename, oneSource);
75+
76+
const twoSource = new RawSource(twoHTMLContent);
77+
78+
compilation.emitAsset(twoFilename, twoSource);
79+
}
80+
);
81+
});
82+
},
83+
},
84+
],
85+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use strict";
2+
3+
console.log("Hey.");
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"use strict";
2+
3+
const webpack = require("webpack");
4+
5+
const isWebpack5 = webpack.version.startsWith("5");
6+
7+
const HTMLContent = `
8+
<!doctype html>
9+
<html>
10+
<head>
11+
<meta charset='UTF-8'>
12+
<title>test</title>
13+
<script src="main.js"></script>
14+
</head>
15+
<body></body>
16+
</html>
17+
`;
18+
19+
module.exports = {
20+
devtool: false,
21+
mode: "development",
22+
context: __dirname,
23+
stats: "none",
24+
entry: "./entry.js",
25+
output: {
26+
path: "/",
27+
},
28+
experiments: {
29+
lazyCompilation: true,
30+
},
31+
infrastructureLogging: isWebpack5
32+
? {
33+
level: "info",
34+
stream: {
35+
write: () => {},
36+
},
37+
}
38+
: {
39+
level: "info",
40+
},
41+
plugins: [
42+
{
43+
apply(compiler) {
44+
const pluginName = "html-generator-plugin-test";
45+
const filename = "test.html";
46+
47+
compiler.hooks.thisCompilation.tap(pluginName, (compilation) => {
48+
const { RawSource } = compiler.webpack.sources;
49+
50+
compilation.hooks.processAssets.tap(
51+
{
52+
name: pluginName,
53+
stage:
54+
compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL,
55+
},
56+
() => {
57+
const source = new RawSource(HTMLContent);
58+
59+
compilation.emitAsset(filename, source);
60+
}
61+
);
62+
});
63+
},
64+
},
65+
],
66+
};

0 commit comments

Comments
 (0)