Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix live reload for child compiler scenario #3972

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Server {
static get DEFAULT_STATS() {
return {
all: false,
children: true,
hash: true,
warnings: true,
errors: true,
Expand Down Expand Up @@ -1179,7 +1180,17 @@ class Server {
stats.warningsFilter = compilerOptions.stats.warningsFilter;
}

return statsObj.toJson(stats);
const reduceFn = (statsJson, result = {}) => {
result.hash = `${result.hash || ""}|${statsJson.hash}`;
result.errors = [...(result.errors || []), ...statsJson.errors];
result.warnings = [...(result.warnings || []), ...statsJson.warnings];
(statsJson.children || []).forEach((child) => {
reduceFn(child, result);
});
return result;
};

return reduceFn(statsObj.toJson(stats));
}

setupHooks() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ Array [
"[HMR] Waiting for update signal from WDS...",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
"[webpack-dev-server] App updated. Recompiling...",
"[webpack-dev-server] App updated. Reloading...",
"[HMR] Waiting for update signal from WDS...",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

Expand Down Expand Up @@ -300,6 +305,9 @@ Array [
"[webpack-dev-server] App updated. Recompiling...",
"[webpack-dev-server] App updated. Reloading...",
"[webpack-dev-server] Live Reloading enabled.",
"[webpack-dev-server] App updated. Recompiling...",
"[webpack-dev-server] App updated. Reloading...",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

Expand All @@ -311,6 +319,9 @@ Array [
"[webpack-dev-server] App updated. Recompiling...",
"[webpack-dev-server] App updated. Reloading...",
"[webpack-dev-server] Live Reloading enabled.",
"[webpack-dev-server] App updated. Recompiling...",
"[webpack-dev-server] App updated. Reloading...",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

Expand All @@ -322,6 +333,9 @@ Array [
"[webpack-dev-server] App updated. Recompiling...",
"[webpack-dev-server] App updated. Reloading...",
"[webpack-dev-server] Live Reloading enabled.",
"[webpack-dev-server] App updated. Recompiling...",
"[webpack-dev-server] App updated. Reloading...",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

Expand Down Expand Up @@ -353,6 +367,10 @@ Array [
"[webpack-dev-server] App updated. Reloading...",
"[HMR] Waiting for update signal from WDS...",
"[webpack-dev-server] Live Reloading enabled.",
"[webpack-dev-server] App updated. Recompiling...",
"[webpack-dev-server] App updated. Reloading...",
"[HMR] Waiting for update signal from WDS...",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

Expand Down Expand Up @@ -389,6 +407,9 @@ Array [
"[webpack-dev-server] App updated. Recompiling...",
"[webpack-dev-server] App updated. Reloading...",
"[webpack-dev-server] Live Reloading enabled.",
"[webpack-dev-server] App updated. Recompiling...",
"[webpack-dev-server] App updated. Reloading...",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

Expand Down
73 changes: 46 additions & 27 deletions test/e2e/hot-and-live-reload.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,18 @@ const reloadConfig = require("../fixtures/reload-config/webpack.config");
const runBrowser = require("../helpers/run-browser");
const port = require("../ports-map")["hot-and-live-reload"];

const isWebpack5 = webpack.version.startsWith("5");

const cssFilePath = path.resolve(
__dirname,
"../fixtures/reload-config/main.css"
);

const jsFilePath = path.resolve(
__dirname,
"../fixtures/reload-config/child.js"
);

const INVALID_MESSAGE = "[webpack-dev-server] App updated. Recompiling...";

describe("hot and live reload", () => {
Expand Down Expand Up @@ -314,6 +321,8 @@ describe("hot and live reload", () => {
"body { background-color: rgb(0, 0, 255); }"
);

fs.writeFileSync(jsFilePath, "");

const webpackOptions = { ...reloadConfig, ...mode.webpackOptions };
const compiler = webpack(webpackOptions);
const testDevServerOptions = mode.options || {};
Expand Down Expand Up @@ -470,11 +479,6 @@ describe("hot and live reload", () => {

expect(backgroundColorBefore).toEqual("rgb(0, 0, 255)");

fs.writeFileSync(
cssFilePath,
"body { background-color: rgb(255, 0, 0); }"
);

let waitHot =
typeof testDevServerOptions.hot !== "undefined"
? testDevServerOptions.hot
Expand Down Expand Up @@ -523,29 +527,38 @@ describe("hot and live reload", () => {
waitLiveReload = false;
}

if (waitHot) {
await page.waitForFunction(
() =>
getComputedStyle(document.body)["background-color"] ===
"rgb(255, 0, 0)"
);

expect(doneHotUpdate).toBe(true);
} else if (waitLiveReload) {
await page.waitForNavigation({
waitUntil: "networkidle0",
});
} else if (webSocketServerLaunched) {
await new Promise((resolve) => {
const interval = setInterval(() => {
if (consoleMessages.includes(INVALID_MESSAGE)) {
clearInterval(interval);
const waitFunc = async () => {
if (waitHot) {
await page.waitForFunction(
() =>
getComputedStyle(document.body)["background-color"] ===
"rgb(255, 0, 0)"
);

resolve();
}
}, 100);
});
}
expect(doneHotUpdate).toBe(true);
} else if (waitLiveReload) {
await page.waitForNavigation({
waitUntil: "networkidle0",
});
} else if (webSocketServerLaunched) {
await new Promise((resolve) => {
const interval = setInterval(() => {
if (consoleMessages.includes(INVALID_MESSAGE)) {
clearInterval(interval);

resolve();
}
}, 100);
});
}
};

fs.writeFileSync(
cssFilePath,
"body { background-color: rgb(255, 0, 0); }"
);

await waitFunc();

const backgroundColorAfter = await page.evaluate(() => {
const body = document.body;
Expand All @@ -559,10 +572,16 @@ describe("hot and live reload", () => {
expect(backgroundColorAfter).toEqual("rgb(255, 0, 0)");
}

if (isWebpack5) {
fs.writeFileSync(jsFilePath, "console.log('hello from child.js');");
await waitFunc();
}

expect(consoleMessages).toMatchSnapshot("console messages");
expect(pageErrors).toMatchSnapshot("page errors");

fs.unlinkSync(cssFilePath);
fs.unlinkSync(jsFilePath);

await browser.close();
await server.stop();
Expand Down
41 changes: 41 additions & 0 deletions test/fixtures/reload-config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,39 @@

const webpack = require("webpack");

const { EntryOptionPlugin, Compilation } = webpack;

const isWebpack5 = webpack.version.startsWith("5");

class TestChildCompilerPlugin {
constructor(options) {
this.name = "TestChildCompilerPlugin";
this.options = webpack.config.getNormalizedWebpackOptions(options);
}

apply(compiler) {
compiler.hooks.thisCompilation.tap(this.name, (compilation) => {
compilation.hooks.processAssets.tapAsync(
{
name: this.name,
stage: Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE,
},
(assets, callback) => {
const child = compilation.createChildCompiler(this.name);
EntryOptionPlugin.applyEntryOption(
child,
compilation.compiler.context,
this.options.entry
);
child.runAsChild(() => {
callback();
});
}
);
});
}
}

module.exports = {
mode: "development",
context: __dirname,
Expand All @@ -30,4 +61,14 @@ module.exports = {
: {
level: "info",
},

plugins: isWebpack5
? [
new TestChildCompilerPlugin({
entry: {
child: "./child",
},
}),
]
: [],
};