Skip to content

Commit f2e5d92

Browse files
authored
feat(rspack-cli): support mjs config (#2168)
1 parent c4c20a3 commit f2e5d92

File tree

9 files changed

+144
-16
lines changed

9 files changed

+144
-16
lines changed

.changeset/lemon-clocks-boil.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@rspack/cli": patch
3+
---
4+
5+
support mjs config

packages/rspack-cli/src/globals.d.ts

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
** https://github.com/jorgebucaran/colorette/pull/96
3+
** colorette is broken in moduileResolution:nodenext,even it's fixed it's never published
4+
*/
5+
declare module "colorette" {
6+
type Color = (text: string | number) => string;
7+
8+
interface Colorette {
9+
reset: Color;
10+
bold: Color;
11+
dim: Color;
12+
italic: Color;
13+
underline: Color;
14+
inverse: Color;
15+
hidden: Color;
16+
strikethrough: Color;
17+
black: Color;
18+
red: Color;
19+
green: Color;
20+
yellow: Color;
21+
blue: Color;
22+
magenta: Color;
23+
cyan: Color;
24+
white: Color;
25+
gray: Color;
26+
bgBlack: Color;
27+
bgRed: Color;
28+
bgGreen: Color;
29+
bgYellow: Color;
30+
bgBlue: Color;
31+
bgMagenta: Color;
32+
bgCyan: Color;
33+
bgWhite: Color;
34+
blackBright: Color;
35+
redBright: Color;
36+
greenBright: Color;
37+
yellowBright: Color;
38+
blueBright: Color;
39+
magentaBright: Color;
40+
cyanBright: Color;
41+
whiteBright: Color;
42+
bgBlackBright: Color;
43+
bgRedBright: Color;
44+
bgGreenBright: Color;
45+
bgYellowBright: Color;
46+
bgBlueBright: Color;
47+
bgMagentaBright: Color;
48+
bgCyanBright: Color;
49+
bgWhiteBright: Color;
50+
}
51+
52+
const reset: Color;
53+
const bold: Color;
54+
const dim: Color;
55+
const italic: Color;
56+
const underline: Color;
57+
const inverse: Color;
58+
const hidden: Color;
59+
const strikethrough: Color;
60+
const black: Color;
61+
const red: Color;
62+
const green: Color;
63+
const yellow: Color;
64+
const blue: Color;
65+
const magenta: Color;
66+
const cyan: Color;
67+
const white: Color;
68+
const gray: Color;
69+
const bgBlack: Color;
70+
const bgRed: Color;
71+
const bgGreen: Color;
72+
const bgYellow: Color;
73+
const bgBlue: Color;
74+
const bgMagenta: Color;
75+
const bgCyan: Color;
76+
const bgWhite: Color;
77+
const blackBright: Color;
78+
const redBright: Color;
79+
const greenBright: Color;
80+
const yellowBright: Color;
81+
const blueBright: Color;
82+
const magentaBright: Color;
83+
const cyanBright: Color;
84+
const whiteBright: Color;
85+
const bgBlackBright: Color;
86+
const bgRedBright: Color;
87+
const bgGreenBright: Color;
88+
const bgYellowBright: Color;
89+
const bgBlueBright: Color;
90+
const bgMagentaBright: Color;
91+
const bgCyanBright: Color;
92+
const bgWhiteBright: Color;
93+
94+
const isColorSupported: boolean;
95+
96+
function createColors(options?: { useColor: boolean }): Colorette;
97+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ export class RspackCLI {
186186
async loadConfig(
187187
options: RspackCLIOptions
188188
): Promise<RspackOptions | MultiRspackOptions> {
189-
let loadedConfig = loadRspackConfig(options);
189+
let loadedConfig = await loadRspackConfig(options);
190190
if (options.configName) {
191191
const notFoundConfigNames: string[] = [];
192192

packages/rspack-cli/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Colorette } from "colorette";
22
import { RspackCLI } from "./rspack-cli";
33
import type { DevServer } from "@rspack/core";
44
export type { Configuration } from "@rspack/core";
5+
56
export interface IRspackCLI {
67
runRspack(): Promise<void>;
78
}

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,23 @@ export type LoadedRspackConfig =
1616
argv: Record<string, any>
1717
) => RspackOptions | MultiRspackOptions);
1818

19-
export function loadRspackConfig(
19+
export async function loadRspackConfig(
2020
options: RspackCLIOptions
21-
): LoadedRspackConfig {
21+
): Promise<LoadedRspackConfig> {
2222
let loadedConfig: LoadedRspackConfig;
2323
// if we pass config paras
2424
if (options.config) {
2525
const resolvedConfigPath = path.resolve(process.cwd(), options.config);
2626
if (!fs.existsSync(resolvedConfigPath)) {
2727
throw new Error(`config file "${resolvedConfigPath}" not exists`);
2828
}
29-
loadedConfig = requireWithAdditionalExtension(resolvedConfigPath);
29+
loadedConfig = await requireWithAdditionalExtension(resolvedConfigPath);
3030
} else {
3131
let defaultConfigPath = findFileWithSupportedExtensions(
3232
path.resolve(process.cwd(), defaultConfig)
3333
);
3434
if (defaultConfigPath != null) {
35-
loadedConfig = requireWithAdditionalExtension(defaultConfigPath);
35+
loadedConfig = await requireWithAdditionalExtension(defaultConfigPath);
3636
} else {
3737
let entry: Record<string, string> = {};
3838
if (options.entry) {
@@ -68,7 +68,7 @@ function findFileWithSupportedExtensions(basePath: string): string | null {
6868
}
6969

7070
let hasRegisteredTS = false;
71-
function requireWithAdditionalExtension(resolvedPath: string) {
71+
async function requireWithAdditionalExtension(resolvedPath: string) {
7272
if (resolvedPath.endsWith("ts") && !hasRegisteredTS) {
7373
hasRegisteredTS = true;
7474
let tsNode: any;
@@ -79,5 +79,12 @@ function requireWithAdditionalExtension(resolvedPath: string) {
7979
}
8080
tsNode.register({ transpileOnly: true });
8181
}
82-
return require(resolvedPath);
82+
let loadedConfig;
83+
if (resolvedPath.endsWith("ts")) {
84+
loadedConfig = require(resolvedPath);
85+
} else {
86+
// dynamic import can handle both cjs & mjs
87+
loadedConfig = (await import(resolvedPath)).default;
88+
}
89+
return loadedConfig;
8390
}

packages/rspack-cli/tests/build/basic/basic.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,15 @@ describe("build command", () => {
8585
expect(stderr).toBeFalsy();
8686
expect(stdout).toBeTruthy();
8787
});
88+
it("should work with mjs configuration ", async () => {
89+
const { exitCode, stderr, stdout } = await run(__dirname, [
90+
"./src/index.js",
91+
"./src/other.js",
92+
"--config",
93+
"./entry.config.mjs"
94+
]);
95+
expect(exitCode).toBe(0);
96+
expect(stderr).toBeFalsy();
97+
expect(stdout).toBeTruthy();
98+
});
8899
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import url from "url";
2+
const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
3+
export default {
4+
context: __dirname,
5+
mode: "development",
6+
entry: "./src/entry.js"
7+
};

packages/rspack-cli/tsconfig.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
{
22
"extends": "../../tsconfig.base.json",
3-
"include": [
4-
"src"
5-
],
3+
"include": ["src"],
64
"compilerOptions": {
75
"outDir": "dist",
8-
"rootDir": "src"
6+
"rootDir": "src",
7+
"moduleResolution": "nodenext"
98
},
109
"ts-node": {
1110
"transpileOnly": true

packages/rspack-dev-server/src/server.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,10 @@ export class RspackDevServer extends WebpackDevServer {
261261
}
262262
if (!clientImplementationFound) {
263263
throw new Error(
264-
`${!isKnownWebSocketServerImplementation
265-
? "When you use custom web socket implementation you must explicitly specify client.webSocketTransport. "
266-
: ""
264+
`${
265+
!isKnownWebSocketServerImplementation
266+
? "When you use custom web socket implementation you must explicitly specify client.webSocketTransport. "
267+
: ""
267268
}client.webSocketTransport must be a string denoting a default implementation (e.g. 'sockjs', 'ws') or a full path to a JS file via require.resolve(...) which exports a class `
268269
);
269270
}
@@ -283,7 +284,7 @@ export class RspackDevServer extends WebpackDevServer {
283284
if (mode === "production") {
284285
this.logger.warn(
285286
"Hot Module Replacement (HMR) is enabled for the production build. \n" +
286-
"Make sure to disable HMR for production by setting `devServer.hot` to `false` in the configuration."
287+
"Make sure to disable HMR for production by setting `devServer.hot` to `false` in the configuration."
287288
);
288289
}
289290
compiler.options.devServer ??= {};
@@ -295,7 +296,7 @@ export class RspackDevServer extends WebpackDevServer {
295296
if (mode === "production") {
296297
this.logger.warn(
297298
"React Refresh runtime should not be included in the production bundle.\n" +
298-
"Make sure to disable React Refresh for production by setting `builtins.react.refresh` to `false` in the configuration."
299+
"Make sure to disable React Refresh for production by setting `builtins.react.refresh` to `false` in the configuration."
299300
);
300301
} else {
301302
this.logger.warn(

0 commit comments

Comments
 (0)