Skip to content

Commit

Permalink
fix(cli): load @rspack/dev-server on demand !breaking! will opt out…
Browse files Browse the repository at this point in the history
… process.env.WEBPACK_SERVE on build mode
  • Loading branch information
xc2 committed Apr 25, 2024
1 parent 0c04972 commit e628b15
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
}
}
};

0 comments on commit e628b15

Please sign in to comment.