@rspack/cli 1.0.14 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Dynamically import files. It will make sure it's not being compiled away by TS/Rollup.
2
+ * Dynamically import files. It will make sure it's not being compiled away by TS/Rslib.
3
3
  */
4
4
  export declare const dynamicImport: Function;
5
5
  declare const crossImport: <T = any>(path: string, cwd?: string) => Promise<T>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rspack/cli",
3
- "version": "1.0.14",
3
+ "version": "1.1.0",
4
4
  "description": "CLI for rspack",
5
5
  "homepage": "https://rspack.dev",
6
6
  "bugs": "https://github.com/web-infra-dev/rspack/issues",
@@ -11,13 +11,17 @@
11
11
  },
12
12
  "license": "MIT",
13
13
  "exports": {
14
- ".": "./dist/index.js",
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "import": "./dist/index.mjs",
17
+ "require": "./dist/index.js"
18
+ },
15
19
  "./package.json": "./package.json"
16
20
  },
17
21
  "main": "./dist/index.js",
18
22
  "types": "./dist/index.d.ts",
19
23
  "bin": {
20
- "rspack": "./bin/rspack"
24
+ "rspack": "./bin/rspack.js"
21
25
  },
22
26
  "files": [
23
27
  "bin",
@@ -25,9 +29,9 @@
25
29
  ],
26
30
  "dependencies": {
27
31
  "@discoveryjs/json-ext": "^0.5.7",
28
- "@rspack/dev-server": "1.0.5",
32
+ "@rspack/dev-server": "1.0.9",
29
33
  "colorette": "2.0.19",
30
- "exit-hook": "^3.2.0",
34
+ "exit-hook": "^4.0.0",
31
35
  "interpret": "^3.1.1",
32
36
  "rechoir": "^0.8.0",
33
37
  "semver": "^7.6.2",
@@ -35,18 +39,19 @@
35
39
  "yargs": "17.6.2"
36
40
  },
37
41
  "devDependencies": {
42
+ "@rslib/core": "0.0.15",
38
43
  "@types/interpret": "^1.1.3",
39
44
  "@types/rechoir": "^0.6.1",
40
45
  "@types/semver": "^7.5.6",
41
46
  "@types/webpack-bundle-analyzer": "^4.6.0",
42
- "@types/yargs": "17.0.32",
47
+ "@types/yargs": "17.0.33",
43
48
  "concat-stream": "^2.0.0",
44
49
  "cross-env": "^7.0.3",
45
50
  "execa": "^5.0.0",
46
51
  "internal-ip": "6.2.0",
47
52
  "ts-node": "^10.9.2",
48
53
  "typescript": "^5.6.3",
49
- "@rspack/core": "1.0.14"
54
+ "@rspack/core": "1.1.0"
50
55
  },
51
56
  "peerDependencies": {
52
57
  "@rspack/core": "^1.0.0-alpha || ^1.x"
@@ -56,8 +61,8 @@
56
61
  "provenance": true
57
62
  },
58
63
  "scripts": {
59
- "build": "tsc -b ./tsconfig.build.json",
60
- "dev": "tsc -b -w",
64
+ "build": "rslib build",
65
+ "dev": "rslib build -w",
61
66
  "test": "cross-env jest --colors"
62
67
  }
63
68
  }
package/bin/rspack DELETED
@@ -1,3 +0,0 @@
1
- #!/usr/bin/env node
2
- const runCLI = require("../dist/bootstrap").runCLI;
3
- runCLI(process.argv);
@@ -1 +0,0 @@
1
- export declare function runCLI(argv: string[]): Promise<void>;
package/dist/bootstrap.js DELETED
@@ -1,8 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.runCLI = runCLI;
4
- const cli_1 = require("./cli");
5
- async function runCLI(argv) {
6
- const cli = new cli_1.RspackCLI();
7
- await cli.run(argv);
8
- }
package/dist/cli.js DELETED
@@ -1,257 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- var __importDefault = (this && this.__importDefault) || function (mod) {
26
- return (mod && mod.__esModule) ? mod : { "default": mod };
27
- };
28
- Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.RspackCLI = void 0;
30
- exports.defineConfig = defineConfig;
31
- exports.definePlugin = definePlugin;
32
- const node_path_1 = __importDefault(require("node:path"));
33
- const node_util_1 = __importDefault(require("node:util"));
34
- const core_1 = require("@rspack/core");
35
- const rspackCore = __importStar(require("@rspack/core"));
36
- const semver_1 = __importDefault(require("semver"));
37
- const yargs_1 = __importDefault(require("yargs"));
38
- const helpers_1 = require("yargs/helpers");
39
- const build_1 = require("./commands/build");
40
- const preview_1 = require("./commands/preview");
41
- const serve_1 = require("./commands/serve");
42
- const loadConfig_1 = require("./utils/loadConfig");
43
- const options_1 = require("./utils/options");
44
- class RspackCLI {
45
- constructor() {
46
- this.colors = this.createColors();
47
- this.program = (0, yargs_1.default)();
48
- }
49
- async createCompiler(options, rspackCommand, callback) {
50
- process.env.RSPACK_CONFIG_VALIDATE ??= "loose";
51
- process.env.WATCHPACK_WATCHER_LIMIT =
52
- process.env.WATCHPACK_WATCHER_LIMIT || "20";
53
- const nodeEnv = process?.env?.NODE_ENV;
54
- const rspackCommandDefaultEnv = rspackCommand === "build" ? "production" : "development";
55
- if (typeof options.nodeEnv === "string") {
56
- process.env.NODE_ENV = nodeEnv || options.nodeEnv;
57
- }
58
- else {
59
- process.env.NODE_ENV = nodeEnv || rspackCommandDefaultEnv;
60
- }
61
- let config = await this.loadConfig(options);
62
- config = await this.buildConfig(config, options, rspackCommand);
63
- const isWatch = Array.isArray(config)
64
- ? config.some(i => i.watch)
65
- : config.watch;
66
- let compiler;
67
- try {
68
- compiler = (0, core_1.rspack)(config, isWatch ? callback : undefined);
69
- }
70
- catch (e) {
71
- // Aligned with webpack-cli
72
- // See: https://github.com/webpack/webpack-cli/blob/eea6adf7d34dfbfd3b5b784ece4a4664834f5a6a/packages/webpack-cli/src/webpack-cli.ts#L2394
73
- if (e instanceof core_1.ValidationError) {
74
- this.getLogger().error(e.message);
75
- process.exit(2);
76
- }
77
- else if (e instanceof Error) {
78
- callback?.(e);
79
- return null;
80
- }
81
- throw e;
82
- }
83
- return compiler;
84
- }
85
- createColors(useColor) {
86
- const { createColors, isColorSupported } = require("colorette");
87
- const shouldUseColor = useColor || isColorSupported;
88
- return {
89
- ...createColors({ useColor: shouldUseColor }),
90
- isColorSupported: shouldUseColor
91
- };
92
- }
93
- getLogger() {
94
- return {
95
- error: val => console.error(`[rspack-cli] ${this.colors.red(node_util_1.default.format(val))}`),
96
- warn: val => console.warn(`[rspack-cli] ${this.colors.yellow(val)}`),
97
- info: val => console.info(`[rspack-cli] ${this.colors.cyan(val)}`),
98
- success: val => console.log(`[rspack-cli] ${this.colors.green(val)}`),
99
- log: val => console.log(`[rspack-cli] ${val}`),
100
- raw: val => console.log(val)
101
- };
102
- }
103
- async run(argv) {
104
- if (semver_1.default.lt(semver_1.default.clean(process.version), "14.0.0")) {
105
- this.getLogger().warn(`Minimum recommended Node.js version is 14.0.0, current version is ${process.version}`);
106
- }
107
- this.program.showHelpOnFail(false);
108
- this.program.usage("[options]");
109
- this.program.scriptName("rspack");
110
- this.program.strictCommands(true).strict(true);
111
- this.program.middleware(options_1.normalizeEnv);
112
- this.registerCommands();
113
- await this.program.parseAsync((0, helpers_1.hideBin)(argv));
114
- }
115
- async registerCommands() {
116
- const builtinCommands = [
117
- new build_1.BuildCommand(),
118
- new serve_1.ServeCommand(),
119
- new preview_1.PreviewCommand()
120
- ];
121
- for (const command of builtinCommands) {
122
- command.apply(this);
123
- }
124
- }
125
- async buildConfig(item, options, command) {
126
- const isBuild = command === "build";
127
- const isServe = command === "serve";
128
- const commandDefaultEnv = isBuild
129
- ? "production"
130
- : "development";
131
- const internalBuildConfig = async (item) => {
132
- if (options.entry) {
133
- item.entry = {
134
- main: options.entry.map(x => node_path_1.default.resolve(process.cwd(), x))[0] // Fix me when entry supports array
135
- };
136
- }
137
- // to set output.path
138
- item.output = item.output || {};
139
- if (options["output-path"]) {
140
- item.output.path = node_path_1.default.resolve(process.cwd(), options["output-path"]);
141
- }
142
- if (options.analyze) {
143
- const { BundleAnalyzerPlugin } = await import("webpack-bundle-analyzer");
144
- (item.plugins ??= []).push({
145
- name: "rspack-bundle-analyzer",
146
- apply(compiler) {
147
- new BundleAnalyzerPlugin({
148
- generateStatsFile: true
149
- }).apply(compiler);
150
- }
151
- });
152
- }
153
- if (options.profile) {
154
- item.profile = true;
155
- }
156
- if (process.env.RSPACK_PROFILE) {
157
- const { applyProfile } = await import("./utils/profile.js");
158
- await applyProfile(process.env.RSPACK_PROFILE, item);
159
- }
160
- // cli --watch overrides the watch config
161
- if (options.watch) {
162
- item.watch = options.watch;
163
- }
164
- // auto set default mode if user config don't set it
165
- if (!item.mode) {
166
- item.mode = commandDefaultEnv ?? "none";
167
- }
168
- // user parameters always has highest priority than default mode and config mode
169
- if (options.mode) {
170
- item.mode = options.mode;
171
- }
172
- // false is also a valid value for sourcemap, so don't override it
173
- if (typeof item.devtool === "undefined") {
174
- item.devtool = isBuild ? "source-map" : "cheap-module-source-map";
175
- }
176
- if (isServe) {
177
- const installed = (item.plugins ||= []).find(item => item instanceof rspackCore.ProgressPlugin);
178
- if (!installed) {
179
- (item.plugins ??= []).push(new rspackCore.ProgressPlugin());
180
- }
181
- }
182
- if (typeof item.stats === "undefined") {
183
- item.stats = { preset: "errors-warnings", timings: true };
184
- }
185
- else if (typeof item.stats === "boolean") {
186
- item.stats = item.stats ? { preset: "normal" } : { preset: "none" };
187
- }
188
- else if (typeof item.stats === "string") {
189
- item.stats = {
190
- preset: item.stats
191
- };
192
- }
193
- if (this.colors.isColorSupported &&
194
- typeof item.stats.colors === "undefined") {
195
- item.stats.colors = true;
196
- }
197
- return item;
198
- };
199
- if (Array.isArray(item)) {
200
- return Promise.all(item.map(internalBuildConfig));
201
- }
202
- return internalBuildConfig(item);
203
- }
204
- async loadConfig(options) {
205
- let loadedConfig = (await (0, loadConfig_1.loadRspackConfig)(options));
206
- if (options.configName) {
207
- const notFoundConfigNames = [];
208
- loadedConfig = options.configName.map((configName) => {
209
- let found;
210
- if (Array.isArray(loadedConfig)) {
211
- found = loadedConfig.find(options => options.name === configName);
212
- }
213
- else {
214
- found =
215
- loadedConfig.name === configName
216
- ? loadedConfig
217
- : undefined;
218
- }
219
- if (!found) {
220
- notFoundConfigNames.push(configName);
221
- }
222
- // WARNING: if config is not found, the program will exit
223
- // so assert here is okay to avoid runtime filtering
224
- return found;
225
- });
226
- if (notFoundConfigNames.length > 0) {
227
- this.getLogger().error(notFoundConfigNames
228
- .map(configName => `Configuration with the name "${configName}" was not found.`)
229
- .join(" "));
230
- process.exit(2);
231
- }
232
- }
233
- if (typeof loadedConfig === "function") {
234
- loadedConfig = loadedConfig(options.argv?.env, options.argv);
235
- // if return promise we should await its result
236
- if (typeof loadedConfig.then === "function") {
237
- loadedConfig = await loadedConfig;
238
- }
239
- }
240
- return loadedConfig;
241
- }
242
- isMultipleCompiler(compiler) {
243
- return Boolean(compiler.compilers);
244
- }
245
- isWatch(compiler) {
246
- return Boolean(this.isMultipleCompiler(compiler)
247
- ? compiler.compilers.some(compiler => compiler.options.watch)
248
- : compiler.options.watch);
249
- }
250
- }
251
- exports.RspackCLI = RspackCLI;
252
- function defineConfig(config) {
253
- return config;
254
- }
255
- function definePlugin(plugin) {
256
- return plugin;
257
- }
@@ -1,132 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.BuildCommand = void 0;
27
- const fs = __importStar(require("node:fs"));
28
- const options_1 = require("../utils/options");
29
- class BuildCommand {
30
- async apply(cli) {
31
- cli.program.command(["build", "$0", "bundle", "b"], "run the rspack build", yargs => (0, options_1.commonOptions)(yargs).options({
32
- analyze: {
33
- type: "boolean",
34
- default: false,
35
- describe: "analyze"
36
- },
37
- json: {
38
- describe: "emit stats json"
39
- },
40
- profile: {
41
- type: "boolean",
42
- default: false,
43
- describe: "capture timing information for each module"
44
- }
45
- }), async (options) => {
46
- const env = (0, options_1.ensureEnvObject)(options);
47
- if (options.watch) {
48
- (0, options_1.setBuiltinEnvArg)(env, "WATCH", true);
49
- }
50
- else {
51
- (0, options_1.setBuiltinEnvArg)(env, "BUNDLE", true);
52
- (0, options_1.setBuiltinEnvArg)(env, "BUILD", true);
53
- }
54
- const logger = cli.getLogger();
55
- let createJsonStringifyStream;
56
- if (options.json) {
57
- const jsonExt = await import("@discoveryjs/json-ext");
58
- createJsonStringifyStream = jsonExt.default.stringifyStream;
59
- }
60
- const errorHandler = (error, stats) => {
61
- if (error) {
62
- logger.error(error);
63
- process.exit(2);
64
- }
65
- if (stats?.hasErrors()) {
66
- process.exitCode = 1;
67
- }
68
- if (!compiler || !stats) {
69
- return;
70
- }
71
- const statsOptions = cli.isMultipleCompiler(compiler)
72
- ? {
73
- children: compiler.compilers.map(compiler => compiler.options ? compiler.options.stats : undefined)
74
- }
75
- : compiler.options
76
- ? compiler.options.stats
77
- : undefined;
78
- if (options.json && createJsonStringifyStream) {
79
- const handleWriteError = (error) => {
80
- logger.error(error);
81
- process.exit(2);
82
- };
83
- if (options.json === true) {
84
- createJsonStringifyStream(stats.toJson(statsOptions))
85
- .on("error", handleWriteError)
86
- .pipe(process.stdout)
87
- .on("error", handleWriteError)
88
- .on("close", () => process.stdout.write("\n"));
89
- }
90
- else if (typeof options.json === "string") {
91
- createJsonStringifyStream(stats.toJson(statsOptions))
92
- .on("error", handleWriteError)
93
- .pipe(fs.createWriteStream(options.json))
94
- .on("error", handleWriteError)
95
- // Use stderr to logging
96
- .on("close", () => {
97
- process.stderr.write(`[rspack-cli] ${cli.colors.green(`stats are successfully stored as json to ${options.json}`)}\n`);
98
- });
99
- }
100
- }
101
- else {
102
- const printedStats = stats.toString(statsOptions);
103
- // Avoid extra empty line when `stats: 'none'`
104
- if (printedStats) {
105
- logger.raw(printedStats);
106
- }
107
- }
108
- };
109
- const rspackOptions = { ...options, argv: { ...options } };
110
- const compiler = await cli.createCompiler(rspackOptions, "build", errorHandler);
111
- if (!compiler || cli.isWatch(compiler)) {
112
- return;
113
- }
114
- compiler.run((error, stats) => {
115
- // If there is a compilation error, the close method should not be called,
116
- // Otherwise Rspack may generate invalid caches.
117
- if (error || stats?.hasErrors()) {
118
- errorHandler(error, stats);
119
- }
120
- else {
121
- compiler.close(closeErr => {
122
- if (closeErr) {
123
- logger.error(closeErr);
124
- }
125
- errorHandler(error, stats);
126
- });
127
- }
128
- });
129
- });
130
- }
131
- }
132
- exports.BuildCommand = BuildCommand;
@@ -1,73 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.PreviewCommand = void 0;
7
- const node_path_1 = __importDefault(require("node:path"));
8
- const core_1 = require("@rspack/core");
9
- const options_1 = require("../utils/options");
10
- const defaultRoot = "dist";
11
- class PreviewCommand {
12
- async apply(cli) {
13
- cli.program.command(["preview [dir]", "preview", "p"], "run the rspack server for build output", options_1.previewOptions, async (options) => {
14
- // config、configName are necessary for loadConfig
15
- const rspackOptions = {
16
- config: options.config,
17
- configName: options.configName,
18
- argv: {
19
- ...options
20
- }
21
- };
22
- const { RspackDevServer } = await import("@rspack/dev-server");
23
- let config = await cli.loadConfig(rspackOptions);
24
- config = await getPreviewConfig(config, options);
25
- if (!Array.isArray(config)) {
26
- config = [config];
27
- }
28
- config = config;
29
- // find the possible devServer config
30
- config = config.find(item => item.devServer) || config[0];
31
- const devServerOptions = config.devServer;
32
- try {
33
- const compiler = (0, core_1.rspack)({ entry: {} });
34
- if (!compiler)
35
- return;
36
- const server = new RspackDevServer(devServerOptions, compiler);
37
- await server.start();
38
- }
39
- catch (error) {
40
- const logger = cli.getLogger();
41
- logger.error(error);
42
- process.exit(2);
43
- }
44
- });
45
- }
46
- }
47
- exports.PreviewCommand = PreviewCommand;
48
- // get the devServerOptions from the config
49
- async function getPreviewConfig(item, options) {
50
- const internalPreviewConfig = async (item) => {
51
- // all of the options that a preview static server needs(maybe not all)
52
- item.devServer = {
53
- static: {
54
- directory: options.dir
55
- ? node_path_1.default.join(item.context ?? process.cwd(), options.dir)
56
- : item.output?.path ??
57
- node_path_1.default.join(item.context ?? process.cwd(), defaultRoot),
58
- publicPath: options.publicPath ?? "/"
59
- },
60
- port: options.port ?? 8080,
61
- proxy: item.devServer?.proxy,
62
- host: options.host ?? item.devServer?.host,
63
- open: options.open ?? item.devServer?.open,
64
- server: options.server ?? item.devServer?.server,
65
- historyApiFallback: item.devServer?.historyApiFallback
66
- };
67
- return item;
68
- };
69
- if (Array.isArray(item)) {
70
- return Promise.all(item.map(internalPreviewConfig));
71
- }
72
- return internalPreviewConfig(item);
73
- }
@@ -1,125 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ServeCommand = void 0;
4
- const options_1 = require("../utils/options");
5
- class ServeCommand {
6
- async apply(cli) {
7
- cli.program.command(["serve", "server", "s", "dev"], "run the rspack dev server.", yargs => (0, options_1.commonOptions)(yargs).options({
8
- hot: {
9
- coerce: arg => {
10
- if (typeof arg === "boolean" || arg === "only") {
11
- return arg;
12
- }
13
- if (arg === "false") {
14
- return false;
15
- }
16
- return true;
17
- },
18
- describe: "enables hot module replacement"
19
- },
20
- port: {
21
- type: "number",
22
- coerce: arg => (Number.isInteger(arg) ? arg : undefined),
23
- describe: "allows to specify a port to use"
24
- },
25
- host: {
26
- type: "string",
27
- describe: "allows to specify a hostname to use"
28
- }
29
- }), async (options) => {
30
- (0, options_1.setBuiltinEnvArg)((0, options_1.ensureEnvObject)(options), "SERVE", true);
31
- const rspackOptions = {
32
- ...options,
33
- argv: {
34
- ...options
35
- }
36
- };
37
- /**
38
- * webpack-dev-server will set `process.env.WEBPACK_SERVE` to true
39
- * when its module is imported, so we have to lazy load the package
40
- * to make sure the envvar is not set on build mode.
41
- * when run in serve mode, we have to load the package before config
42
- * module is imported so that the envvar `process.env.WEBPACK_SERVE`
43
- * got in config module could be `true`.
44
- * related issue: https://github.com/web-infra-dev/rspack/issues/6359
45
- */
46
- const { RspackDevServer } = await import("@rspack/dev-server");
47
- const compiler = await cli.createCompiler(rspackOptions, "serve");
48
- if (!compiler)
49
- return;
50
- const compilers = cli.isMultipleCompiler(compiler)
51
- ? compiler.compilers
52
- : [compiler];
53
- const possibleCompilers = compilers.filter((compiler) => compiler.options.devServer);
54
- const usedPorts = [];
55
- const servers = [];
56
- /**
57
- * Webpack uses an Array of compilerForDevServer,
58
- * however according to it's doc https://webpack.js.org/configuration/dev-server/#devserverhot
59
- * It should use only the first one
60
- *
61
- * Choose the one for configure devServer
62
- */
63
- const compilerForDevServer = possibleCompilers.length > 0 ? possibleCompilers[0] : compilers[0];
64
- /**
65
- * Rspack relies on devServer.hot to enable HMR
66
- */
67
- for (const compiler of compilers) {
68
- const devServer = (compiler.options.devServer ??= {});
69
- devServer.hot = options.hot ?? devServer.hot ?? true;
70
- if (devServer.client !== false) {
71
- if (devServer.client === true || devServer.client == null) {
72
- devServer.client = {};
73
- }
74
- devServer.client = {
75
- overlay: {
76
- errors: true,
77
- warnings: false
78
- },
79
- ...devServer.client
80
- };
81
- }
82
- }
83
- const result = (compilerForDevServer.options.devServer ??= {});
84
- /**
85
- * Enable this to tell Rspack that we need to enable React Refresh by default
86
- */
87
- result.hot = options.hot ?? result.hot ?? true;
88
- result.host = options.host || result.host;
89
- result.port = options.port || result.port;
90
- if (result.client !== false) {
91
- if (result.client === true || result.client == null) {
92
- result.client = {};
93
- }
94
- result.client = {
95
- overlay: {
96
- errors: true,
97
- warnings: false
98
- },
99
- ...result.client
100
- };
101
- }
102
- const devServerOptions = result;
103
- if (devServerOptions.port) {
104
- const portNumber = Number(devServerOptions.port);
105
- if (!Number.isNaN(portNumber)) {
106
- if (usedPorts.find(port => portNumber === port)) {
107
- throw new Error("Unique ports must be specified for each devServer option in your rspack configuration. Alternatively, run only 1 devServer config using the --config-name flag to specify your desired config.");
108
- }
109
- usedPorts.push(portNumber);
110
- }
111
- }
112
- try {
113
- const server = new RspackDevServer(devServerOptions, compiler);
114
- await server.start();
115
- servers.push(server);
116
- }
117
- catch (error) {
118
- const logger = cli.getLogger();
119
- logger.error(error);
120
- process.exit(2);
121
- }
122
- });
123
- }
124
- }
125
- exports.ServeCommand = ServeCommand;