Skip to content

Commit fa2bae1

Browse files
authored
fix(cli): failed to run preview with TS config file (#9468)
* fix(CLI): failed to run preview with ts config file * fix: port
1 parent 9cf027f commit fa2bae1

File tree

11 files changed

+154
-72
lines changed

11 files changed

+154
-72
lines changed

packages/rspack-cli/src/commands/build.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { RspackCLI } from "../cli";
55
import type { RspackCommand } from "../types";
66
import {
77
commonOptions,
8+
commonOptionsForBuildAndServe,
89
ensureEnvObject,
910
setBuiltinEnvArg
1011
} from "../utils/options";
@@ -14,8 +15,8 @@ export class BuildCommand implements RspackCommand {
1415
cli.program.command(
1516
["build", "$0", "bundle", "b"],
1617
"run the rspack build",
17-
yargs =>
18-
commonOptions(yargs).options({
18+
yargs => {
19+
commonOptionsForBuildAndServe(commonOptions(yargs)).options({
1920
analyze: {
2021
type: "boolean",
2122
default: false,
@@ -29,7 +30,8 @@ export class BuildCommand implements RspackCommand {
2930
default: false,
3031
describe: "capture timing information for each module"
3132
}
32-
}),
33+
});
34+
},
3335
async options => {
3436
const env = ensureEnvObject(options);
3537
if (options.watch) {

packages/rspack-cli/src/commands/preview.ts

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,41 @@ import {
55
type RspackOptions,
66
rspack
77
} from "@rspack/core";
8+
import type yargs from "yargs";
89

910
import type { RspackCLI } from "../cli";
1011
import type { RspackCommand, RspackPreviewCLIOptions } from "../types";
11-
import { previewOptions } from "../utils/options";
12+
import { commonOptions } from "../utils/options";
13+
14+
const previewOptions = (yargs: yargs.Argv) => {
15+
yargs.positional("dir", {
16+
type: "string",
17+
describe: "directory want to preview"
18+
});
19+
return commonOptions(yargs).options({
20+
publicPath: {
21+
type: "string",
22+
describe: "static resource server path"
23+
},
24+
port: {
25+
type: "number",
26+
describe: "preview server port"
27+
},
28+
host: {
29+
type: "string",
30+
describe: "preview server host"
31+
},
32+
open: {
33+
type: "boolean",
34+
describe: "open browser"
35+
},
36+
// same as devServer.server
37+
server: {
38+
type: "string",
39+
describe: "Configuration items for the server."
40+
}
41+
});
42+
};
1243

1344
const defaultRoot = "dist";
1445
export class PreviewCommand implements RspackCommand {
@@ -18,14 +49,7 @@ export class PreviewCommand implements RspackCommand {
1849
"run the rspack server for build output",
1950
previewOptions,
2051
async options => {
21-
// config、configName are necessary for loadConfig
22-
const rspackOptions = {
23-
config: options.config,
24-
configName: options.configName,
25-
argv: {
26-
...options
27-
}
28-
};
52+
const rspackOptions = { ...options, argv: { ...options } };
2953
const { RspackDevServer } = await import("@rspack/dev-server");
3054

3155
let config = await cli.loadConfig(rspackOptions);

packages/rspack-cli/src/commands/serve.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { RspackCLI } from "../cli";
55
import type { RspackCommand } from "../types";
66
import {
77
commonOptions,
8+
commonOptionsForBuildAndServe,
89
ensureEnvObject,
910
setBuiltinEnvArg
1011
} from "../utils/options";
@@ -15,7 +16,7 @@ export class ServeCommand implements RspackCommand {
1516
["serve", "server", "s", "dev"],
1617
"run the rspack dev server.",
1718
yargs =>
18-
commonOptions(yargs).options({
19+
commonOptionsForBuildAndServe(commonOptions(yargs)).options({
1920
hot: {
2021
coerce: arg => {
2122
if (typeof arg === "boolean" || arg === "only") {

packages/rspack-cli/src/utils/options.ts

Lines changed: 29 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,36 @@
11
import type yargs from "yargs";
2+
3+
/**
4+
* Apply common options for all commands
5+
*/
26
export const commonOptions = (yargs: yargs.Argv) => {
7+
return yargs.options({
8+
config: {
9+
g: true,
10+
type: "string",
11+
describe: "config file",
12+
alias: "c"
13+
},
14+
configName: {
15+
type: "array",
16+
string: true,
17+
describe: "Name of the configuration to use."
18+
},
19+
configLoader: {
20+
type: "string",
21+
default: "register",
22+
describe:
23+
"Specify the loader to load the config file, can be `native` or `register`."
24+
}
25+
});
26+
};
27+
28+
/**
29+
* Apply common options for `build` and `serve` commands
30+
*/
31+
export const commonOptionsForBuildAndServe = (yargs: yargs.Argv) => {
332
return yargs
433
.options({
5-
config: {
6-
g: true,
7-
type: "string",
8-
describe: "config file",
9-
alias: "c"
10-
},
1134
entry: {
1235
type: "array",
1336
string: true,
@@ -39,64 +62,11 @@ export const commonOptions = (yargs: yargs.Argv) => {
3962
default: false,
4063
describe: "devtool",
4164
alias: "d"
42-
},
43-
configName: {
44-
type: "array",
45-
string: true,
46-
describe: "Name of the configuration to use."
47-
},
48-
configLoader: {
49-
type: "string",
50-
default: "register",
51-
describe:
52-
"Specify the loader to load the config file, can be `native` or `register`."
5365
}
5466
})
5567
.alias({ v: "version", h: "help" });
5668
};
5769

58-
export const previewOptions = (yargs: yargs.Argv) => {
59-
return yargs
60-
.positional("dir", {
61-
type: "string",
62-
describe: "directory want to preview"
63-
})
64-
.options({
65-
publicPath: {
66-
type: "string",
67-
describe: "static resource server path"
68-
},
69-
config: {
70-
g: true,
71-
type: "string",
72-
describe: "config file",
73-
alias: "c"
74-
},
75-
port: {
76-
type: "number",
77-
describe: "preview server port"
78-
},
79-
host: {
80-
type: "string",
81-
describe: "preview server host"
82-
},
83-
open: {
84-
type: "boolean",
85-
describe: "open browser"
86-
},
87-
// same as devServer.server
88-
server: {
89-
type: "string",
90-
describe: "Configuration items for the server."
91-
},
92-
configName: {
93-
type: "array",
94-
string: true,
95-
describe: "Name of the configuration to use."
96-
}
97-
});
98-
};
99-
10070
export function normalizeEnv(argv: yargs.Arguments) {
10171
function parseValue(previous: Record<string, unknown>, value: string) {
10272
const [allKeys, val] = value.split(/=(.+)/, 2);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {
2+
getRandomPort,
3+
normalizeStderr,
4+
runWatch
5+
} from "../../utils/test-utils";
6+
7+
describe("should run preview command as expected", () => {
8+
it("should work", async () => {
9+
const port = await getRandomPort();
10+
const { stderr } = await runWatch(
11+
__dirname,
12+
["preview", "--port", port.toString()],
13+
{
14+
killString: /localhost/
15+
}
16+
);
17+
18+
expect(normalizeStderr(stderr)).toContain("Project is running at");
19+
});
20+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
mode: "production"
3+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("hello world");
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {
2+
getRandomPort,
3+
normalizeStderr,
4+
runWatch
5+
} from "../../utils/test-utils";
6+
7+
describe("should run preview command with ts config file as expected", () => {
8+
it("should work", async () => {
9+
const port = await getRandomPort();
10+
const { stderr } = await runWatch(
11+
__dirname,
12+
["preview", "--port", port.toString()],
13+
{
14+
killString: /localhost/
15+
}
16+
);
17+
18+
expect(normalizeStderr(stderr)).toContain("Project is running at");
19+
});
20+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
mode: "production"
3+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("hello world");

0 commit comments

Comments
 (0)