Skip to content

Commit

Permalink
fix(cli): load @rspack/dev-server on demand (#6370)
Browse files Browse the repository at this point in the history
fix(cli): load `@rspack/dev-server` on demand to prevent process.env.WEBPACK_SERVE being set on build mode
  • Loading branch information
xc2 committed May 7, 2024
1 parent b3d7aca commit 76f4c16
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/rspack-cli/src/commands/preview.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { RspackCLI } from "../rspack-cli";
import { RspackDevServer } from "@rspack/dev-server";
import { RspackCommand, RspackPreviewCLIOptions } from "../types";
import { previewOptions } from "../utils/options";
import {
Expand All @@ -26,6 +25,7 @@ export class PreviewCommand implements RspackCommand {
...options
}
};
const { RspackDevServer } = await import("@rspack/dev-server");

let config = await cli.loadConfig(rspackOptions);
config = await getPreviewConfig(config, options);
Expand Down
15 changes: 13 additions & 2 deletions packages/rspack-cli/src/commands/serve.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { RspackCLI } from "../rspack-cli";
import { RspackDevServer } from "@rspack/dev-server";
import type { RspackDevServer as RspackDevServerType } from "@rspack/dev-server";
import { RspackCommand } from "../types";
import {
commonOptions,
Expand All @@ -22,6 +22,17 @@ export class ServeCommand implements RspackCommand {
...options
}
};
/**
* webpack-dev-server will set `process.env.WEBPACK_SERVE` to true
* when its module is imported, so we have to lazy load the package
* to make sure the envvar is not set on build mode.
* when run in serve mode, we have to load the package before config
* module is imported so that the envvar `process.env.WEBPACK_SERVE`
* got in config module could be `true`.
* related issue: https://github.com/web-infra-dev/rspack/issues/6359
*/
const { RspackDevServer } = await import("@rspack/dev-server");

const compiler = await cli.createCompiler(rspackOptions, "serve");
if (!compiler) return;
const compilers = cli.isMultipleCompiler(compiler)
Expand All @@ -32,7 +43,7 @@ export class ServeCommand implements RspackCommand {
);

const usedPorts: number[] = [];
const servers: RspackDevServer[] = [];
const servers: RspackDevServerType[] = [];

/**
* Webpack uses an Array of compilerForDevServer,
Expand Down
1 change: 1 addition & 0 deletions packages/rspack-cli/tests/build/issue-6359/entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
log(DEFINE_ME);
16 changes: 16 additions & 0 deletions packages/rspack-cli/tests/build/issue-6359/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { readFile, run, runWatch } from "../../utils/test-utils";
import { resolve } from "path";

it("should not have `process.env.WEBPACK_SERVE` set on build mode", async () => {
await run(__dirname, []);
const mainJs = await readFile(resolve(__dirname, "dist/main.js"), "utf-8");

expect(mainJs).toContain("WEBPACK_SERVE=<EMPTY>");
});

it("should have `process.env.WEBPACK_SERVE` set on serve mode", async () => {
await runWatch(__dirname, ["serve"], { killString: /rspack compiled/i });
const mainJs = await readFile(resolve(__dirname, "dist/main.js"), "utf-8");

expect(mainJs).toContain("WEBPACK_SERVE=true");
});
22 changes: 22 additions & 0 deletions packages/rspack-cli/tests/build/issue-6359/rspack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { WEBPACK_SERVE } = process.env;
module.exports = /** @type {import('@rspack/cli').Configuration} */ {
mode: "production",
entry: "./entry.js",
output: { clean: true },
plugins: [
{
apply(compiler) {
new compiler.webpack.DefinePlugin({
DEFINE_ME: JSON.stringify(
`WEBPACK_SERVE=${WEBPACK_SERVE ?? "<EMPTY>"}`
)
}).apply(compiler);
}
}
],
devServer: {
devMiddleware: {
writeToDisk: true
}
}
};

2 comments on commit 76f4c16

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result
modernjs, self-hosted, Linux, ci, ec2-linux ❌ failure
_selftest, ubuntu-latest ✅ success
nx, ubuntu-latest ❌ failure
rspress, ubuntu-latest ❌ failure
rsbuild, ubuntu-latest ❌ failure
compat, ubuntu-latest ❌ failure
examples, ubuntu-latest ❌ failure

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2024-05-06 42527c5) Current Change
10000_development-mode + exec 2.77 s ± 40 ms 2.73 s ± 16 ms -1.52 %
10000_development-mode_hmr + exec 684 ms ± 3.9 ms 697 ms ± 4.1 ms +2.01 %
10000_production-mode + exec 2.5 s ± 26 ms 2.51 s ± 15 ms +0.37 %
arco-pro_development-mode + exec 2.61 s ± 43 ms 2.5 s ± 67 ms -4.21 %
arco-pro_development-mode_hmr + exec 429 ms ± 2.9 ms 429 ms ± 3.3 ms -0.02 %
arco-pro_development-mode_hmr_intercept-plugin + exec 441 ms ± 2 ms 440 ms ± 3.3 ms -0.16 %
arco-pro_development-mode_intercept-plugin + exec 3.37 s ± 64 ms 3.31 s ± 99 ms -1.90 %
arco-pro_production-mode + exec 4.08 s ± 57 ms 4.06 s ± 57 ms -0.60 %
arco-pro_production-mode_intercept-plugin + exec 4.87 s ± 55 ms 4.82 s ± 67 ms -1.13 %
threejs_development-mode_10x + exec 2.09 s ± 23 ms 1.98 s ± 28 ms -4.94 %
threejs_development-mode_10x_hmr + exec 760 ms ± 16 ms 767 ms ± 8.4 ms +0.88 %
threejs_production-mode_10x + exec 5.16 s ± 26 ms 5.17 s ± 27 ms +0.15 %

Please sign in to comment.