Skip to content

Commit

Permalink
fix(cli): failed to run preview with TS config file (#9468)
Browse files Browse the repository at this point in the history
* fix(CLI): failed to run preview with ts config file

* fix: port
  • Loading branch information
chenjiahan authored Feb 26, 2025
1 parent 9cf027f commit fa2bae1
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 72 deletions.
8 changes: 5 additions & 3 deletions packages/rspack-cli/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { RspackCLI } from "../cli";
import type { RspackCommand } from "../types";
import {
commonOptions,
commonOptionsForBuildAndServe,
ensureEnvObject,
setBuiltinEnvArg
} from "../utils/options";
Expand All @@ -14,8 +15,8 @@ export class BuildCommand implements RspackCommand {
cli.program.command(
["build", "$0", "bundle", "b"],
"run the rspack build",
yargs =>
commonOptions(yargs).options({
yargs => {
commonOptionsForBuildAndServe(commonOptions(yargs)).options({
analyze: {
type: "boolean",
default: false,
Expand All @@ -29,7 +30,8 @@ export class BuildCommand implements RspackCommand {
default: false,
describe: "capture timing information for each module"
}
}),
});
},
async options => {
const env = ensureEnvObject(options);
if (options.watch) {
Expand Down
42 changes: 33 additions & 9 deletions packages/rspack-cli/src/commands/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,41 @@ import {
type RspackOptions,
rspack
} from "@rspack/core";
import type yargs from "yargs";

import type { RspackCLI } from "../cli";
import type { RspackCommand, RspackPreviewCLIOptions } from "../types";
import { previewOptions } from "../utils/options";
import { commonOptions } from "../utils/options";

const previewOptions = (yargs: yargs.Argv) => {
yargs.positional("dir", {
type: "string",
describe: "directory want to preview"
});
return commonOptions(yargs).options({
publicPath: {
type: "string",
describe: "static resource server path"
},
port: {
type: "number",
describe: "preview server port"
},
host: {
type: "string",
describe: "preview server host"
},
open: {
type: "boolean",
describe: "open browser"
},
// same as devServer.server
server: {
type: "string",
describe: "Configuration items for the server."
}
});
};

const defaultRoot = "dist";
export class PreviewCommand implements RspackCommand {
Expand All @@ -18,14 +49,7 @@ export class PreviewCommand implements RspackCommand {
"run the rspack server for build output",
previewOptions,
async options => {
// config、configName are necessary for loadConfig
const rspackOptions = {
config: options.config,
configName: options.configName,
argv: {
...options
}
};
const rspackOptions = { ...options, argv: { ...options } };
const { RspackDevServer } = await import("@rspack/dev-server");

let config = await cli.loadConfig(rspackOptions);
Expand Down
3 changes: 2 additions & 1 deletion packages/rspack-cli/src/commands/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { RspackCLI } from "../cli";
import type { RspackCommand } from "../types";
import {
commonOptions,
commonOptionsForBuildAndServe,
ensureEnvObject,
setBuiltinEnvArg
} from "../utils/options";
Expand All @@ -15,7 +16,7 @@ export class ServeCommand implements RspackCommand {
["serve", "server", "s", "dev"],
"run the rspack dev server.",
yargs =>
commonOptions(yargs).options({
commonOptionsForBuildAndServe(commonOptions(yargs)).options({
hot: {
coerce: arg => {
if (typeof arg === "boolean" || arg === "only") {
Expand Down
88 changes: 29 additions & 59 deletions packages/rspack-cli/src/utils/options.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,36 @@
import type yargs from "yargs";

/**
* Apply common options for all commands
*/
export const commonOptions = (yargs: yargs.Argv) => {
return yargs.options({
config: {
g: true,
type: "string",
describe: "config file",
alias: "c"
},
configName: {
type: "array",
string: true,
describe: "Name of the configuration to use."
},
configLoader: {
type: "string",
default: "register",
describe:
"Specify the loader to load the config file, can be `native` or `register`."
}
});
};

/**
* Apply common options for `build` and `serve` commands
*/
export const commonOptionsForBuildAndServe = (yargs: yargs.Argv) => {
return yargs
.options({
config: {
g: true,
type: "string",
describe: "config file",
alias: "c"
},
entry: {
type: "array",
string: true,
Expand Down Expand Up @@ -39,64 +62,11 @@ export const commonOptions = (yargs: yargs.Argv) => {
default: false,
describe: "devtool",
alias: "d"
},
configName: {
type: "array",
string: true,
describe: "Name of the configuration to use."
},
configLoader: {
type: "string",
default: "register",
describe:
"Specify the loader to load the config file, can be `native` or `register`."
}
})
.alias({ v: "version", h: "help" });
};

export const previewOptions = (yargs: yargs.Argv) => {
return yargs
.positional("dir", {
type: "string",
describe: "directory want to preview"
})
.options({
publicPath: {
type: "string",
describe: "static resource server path"
},
config: {
g: true,
type: "string",
describe: "config file",
alias: "c"
},
port: {
type: "number",
describe: "preview server port"
},
host: {
type: "string",
describe: "preview server host"
},
open: {
type: "boolean",
describe: "open browser"
},
// same as devServer.server
server: {
type: "string",
describe: "Configuration items for the server."
},
configName: {
type: "array",
string: true,
describe: "Name of the configuration to use."
}
});
};

export function normalizeEnv(argv: yargs.Arguments) {
function parseValue(previous: Record<string, unknown>, value: string) {
const [allKeys, val] = value.split(/=(.+)/, 2);
Expand Down
20 changes: 20 additions & 0 deletions packages/rspack-cli/tests/preview/basic/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {
getRandomPort,
normalizeStderr,
runWatch
} from "../../utils/test-utils";

describe("should run preview command as expected", () => {
it("should work", async () => {
const port = await getRandomPort();
const { stderr } = await runWatch(
__dirname,
["preview", "--port", port.toString()],
{
killString: /localhost/
}
);

expect(normalizeStderr(stderr)).toContain("Project is running at");
});
});
3 changes: 3 additions & 0 deletions packages/rspack-cli/tests/preview/basic/rspack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
mode: "production"
};
1 change: 1 addition & 0 deletions packages/rspack-cli/tests/preview/basic/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("hello world");
20 changes: 20 additions & 0 deletions packages/rspack-cli/tests/preview/ts-config-file/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {
getRandomPort,
normalizeStderr,
runWatch
} from "../../utils/test-utils";

describe("should run preview command with ts config file as expected", () => {
it("should work", async () => {
const port = await getRandomPort();
const { stderr } = await runWatch(
__dirname,
["preview", "--port", port.toString()],
{
killString: /localhost/
}
);

expect(normalizeStderr(stderr)).toContain("Project is running at");
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
mode: "production"
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("hello world");
37 changes: 37 additions & 0 deletions packages/rspack-cli/tests/utils/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
const os = require("os");
const { stripVTControlCharacters: stripAnsi } = require("node:util");
const path = require("path");
const net = require("node:net");
const fs = require("fs");
const execa = require("execa");
const { exec } = require("child_process");
Expand Down Expand Up @@ -394,6 +395,42 @@ const uniqueDirectoryForTest = async () => {
return result;
};

function isPortAvailable(port: number) {
try {
const server = net.createServer().listen(port);
return new Promise(resolve => {
server.on("listening", () => {
server.close();
resolve(true);
});

server.on("error", () => {
resolve(false);
});
});
} catch (err) {
return false;
}
}

const portMap = new Map();

// Available port ranges: 1024 ~ 65535
// `10080` is not available in macOS CI, `> 50000` get 'permission denied' in Windows.
// so we use `15000` ~ `45000`.
export async function getRandomPort(
defaultPort = Math.ceil(Math.random() * 30000) + 15000
) {
let port = defaultPort;
while (true) {
if (!portMap.get(port) && (await isPortAvailable(port))) {
portMap.set(port, 1);
return port;
}
port++;
}
}

export {
run,
runAndGetProcess,
Expand Down

2 comments on commit fa2bae1

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented on fa2bae1 Feb 26, 2025

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 (2025-02-26 50bdb20) Current Change
10000_big_production-mode_disable-minimize + exec 36.8 s ± 476 ms 37.8 s ± 1.15 s +2.53 %
10000_development-mode + exec 1.7 s ± 20 ms 1.69 s ± 9.6 ms -0.62 %
10000_development-mode_hmr + exec 674 ms ± 19 ms 676 ms ± 23 ms +0.31 %
10000_production-mode + exec 2.21 s ± 66 ms 2.16 s ± 37 ms -2.42 %
10000_production-mode_persistent-cold + exec 2.34 s ± 55 ms 2.31 s ± 60 ms -1.03 %
10000_production-mode_persistent-hot + exec 1.62 s ± 60 ms 1.68 s ± 231 ms +3.51 %
arco-pro_development-mode + exec 1.71 s ± 122 ms 1.75 s ± 115 ms +2.24 %
arco-pro_development-mode_hmr + exec 375 ms ± 2.1 ms 377 ms ± 4.9 ms +0.50 %
arco-pro_production-mode + exec 3.55 s ± 173 ms 3.48 s ± 146 ms -2.05 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.64 s ± 178 ms 3.59 s ± 209 ms -1.45 %
arco-pro_production-mode_persistent-cold + exec 3.66 s ± 57 ms 3.59 s ± 212 ms -1.88 %
arco-pro_production-mode_persistent-hot + exec 2.24 s ± 125 ms 2.29 s ± 61 ms +2.41 %
arco-pro_production-mode_traverse-chunk-modules + exec 3.61 s ± 181 ms 3.56 s ± 167 ms -1.24 %
large-dyn-imports_development-mode + exec 1.96 s ± 22 ms 1.97 s ± 21 ms +0.27 %
large-dyn-imports_production-mode + exec 2.02 s ± 29 ms 2.02 s ± 31 ms -0.05 %
threejs_development-mode_10x + exec 1.45 s ± 43 ms 1.46 s ± 34 ms +1.16 %
threejs_development-mode_10x_hmr + exec 786 ms ± 8.4 ms 814 ms ± 18 ms +3.63 %
threejs_production-mode_10x + exec 5.04 s ± 169 ms 5.16 s ± 413 ms +2.49 %
threejs_production-mode_10x_persistent-cold + exec 5.15 s ± 294 ms 5.11 s ± 82 ms -0.80 %
threejs_production-mode_10x_persistent-hot + exec 4.46 s ± 215 ms 4.42 s ± 41 ms -1.04 %
10000_big_production-mode_disable-minimize + rss memory 8667 MiB ± 41.1 MiB 8723 MiB ± 57.6 MiB +0.64 %
10000_development-mode + rss memory 655 MiB ± 19.2 MiB 667 MiB ± 29.1 MiB +1.89 %
10000_development-mode_hmr + rss memory 1201 MiB ± 64.3 MiB 1285 MiB ± 313 MiB +7.00 %
10000_production-mode + rss memory 617 MiB ± 6.87 MiB 640 MiB ± 31.5 MiB +3.70 %
10000_production-mode_persistent-cold + rss memory 732 MiB ± 6 MiB 751 MiB ± 16.3 MiB +2.66 %
10000_production-mode_persistent-hot + rss memory 699 MiB ± 31.9 MiB 737 MiB ± 16.7 MiB +5.45 %
arco-pro_development-mode + rss memory 571 MiB ± 31 MiB 584 MiB ± 23.4 MiB +2.21 %
arco-pro_development-mode_hmr + rss memory 650 MiB ± 51.6 MiB 678 MiB ± 96.3 MiB +4.30 %
arco-pro_production-mode + rss memory 709 MiB ± 41.3 MiB 722 MiB ± 8.61 MiB +1.75 %
arco-pro_production-mode_generate-package-json-webpack-plugin + rss memory 725 MiB ± 15.4 MiB 724 MiB ± 16.6 MiB -0.09 %
arco-pro_production-mode_persistent-cold + rss memory 804 MiB ± 25.7 MiB 788 MiB ± 26.8 MiB -1.99 %
arco-pro_production-mode_persistent-hot + rss memory 655 MiB ± 23.2 MiB 669 MiB ± 15.6 MiB +2.15 %
arco-pro_production-mode_traverse-chunk-modules + rss memory 731 MiB ± 23.4 MiB 722 MiB ± 21.2 MiB -1.33 %
large-dyn-imports_development-mode + rss memory 650 MiB ± 5.99 MiB 664 MiB ± 5.3 MiB +2.19 %
large-dyn-imports_production-mode + rss memory 529 MiB ± 6.89 MiB 544 MiB ± 6.28 MiB +2.76 %
threejs_development-mode_10x + rss memory 571 MiB ± 14.4 MiB 567 MiB ± 20.4 MiB -0.62 %
threejs_development-mode_10x_hmr + rss memory 1212 MiB ± 166 MiB 1227 MiB ± 15.8 MiB +1.32 %
threejs_production-mode_10x + rss memory 858 MiB ± 32.6 MiB 851 MiB ± 55.9 MiB -0.79 %
threejs_production-mode_10x_persistent-cold + rss memory 963 MiB ± 58.5 MiB 968 MiB ± 37.3 MiB +0.52 %
threejs_production-mode_10x_persistent-hot + rss memory 815 MiB ± 40.3 MiB 847 MiB ± 23.8 MiB +3.81 %

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented on fa2bae1 Feb 26, 2025

Choose a reason for hiding this comment

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

📝 Ecosystem CI detail: Open

suite result
modernjs ❌ failure
rspress ✅ success
rslib ✅ success
rsbuild ✅ success
rsdoctor ❌ failure
examples ✅ success
devserver ✅ success
nuxt ✅ success

Please sign in to comment.