Skip to content

Commit 71e9241

Browse files
authored
feat(CLI): add --config-loader option (#1225)
1 parent d09f7d6 commit 71e9241

File tree

13 files changed

+110
-23
lines changed

13 files changed

+110
-23
lines changed

packages/core/src/cli/commands.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { RsbuildMode } from '@rsbuild/core';
22
import cac, { type CAC } from 'cac';
3+
import type { ConfigLoader } from '../config';
34
import { logger } from '../utils/logger';
45
import { build } from './build';
56
import { init } from './init';
@@ -13,6 +14,7 @@ export type CommonOptions = {
1314
envDir?: string;
1415
envMode?: string;
1516
lib?: string[];
17+
configLoader?: ConfigLoader;
1618
};
1719

1820
export type BuildOptions = CommonOptions & {
@@ -39,6 +41,13 @@ const applyCommonOptions = (cli: CAC) => {
3941
'--env-mode <mode>',
4042
'specify the env mode to load the `.env.[mode]` file',
4143
)
44+
.option(
45+
'--config-loader <loader>',
46+
'Set the config file loader (jiti | native)',
47+
{
48+
default: 'jiti',
49+
},
50+
)
4251
.option('--env-dir <dir>', 'specify the directory to load `.env` files')
4352
.option(
4453
'--lib <id>',

packages/core/src/cli/init.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export async function init(options: CommonOptions): Promise<{
3131
cwd: root,
3232
path: options.config,
3333
envMode: options.envMode,
34+
loader: options.configLoader,
3435
});
3536

3637
config.source ||= {};

packages/core/src/config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,18 @@ const resolveConfigPath = (root: string, customConfig?: string): string => {
119119
throw new Error(`${DEFAULT_CONFIG_NAME} not found in ${root}`);
120120
};
121121

122+
export type ConfigLoader = 'jiti' | 'native';
123+
122124
export async function loadConfig({
123125
cwd = process.cwd(),
124126
path,
125127
envMode,
128+
loader,
126129
}: {
127130
cwd?: string;
128131
path?: string;
129132
envMode?: string;
133+
loader?: ConfigLoader;
130134
}): Promise<{
131135
content: RslibConfig;
132136
filePath: string;
@@ -136,6 +140,7 @@ export async function loadConfig({
136140
cwd: dirname(configFilePath),
137141
path: configFilePath,
138142
envMode,
143+
loader,
139144
});
140145

141146
return { content: content as RslibConfig, filePath: configFilePath };

tests/integration/cli/build/build.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,27 @@ describe('build command', async () => {
8282
`);
8383
});
8484

85+
test('--config-loader', async () => {
86+
// Skip Node.js <= 22.18
87+
if (!process.features.typescript) {
88+
return;
89+
}
90+
91+
await fse.remove(path.join(__dirname, 'dist'));
92+
runCliSync('build --config-loader native', {
93+
cwd: __dirname,
94+
});
95+
96+
const files = await globContentJSON(path.join(__dirname, 'dist'));
97+
const fileNames = Object.keys(files).sort();
98+
expect(fileNames).toMatchInlineSnapshot(`
99+
[
100+
"<ROOT>/tests/integration/cli/build/dist/cjs/index.cjs",
101+
"<ROOT>/tests/integration/cli/build/dist/esm/index.js",
102+
]
103+
`);
104+
});
105+
85106
test('--root', async () => {
86107
await fse.remove(path.join(__dirname, 'dist'));
87108
runCliSync('build --root custom-root', {

tests/scripts/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export * from './helper';
2-
export * from './shared';
1+
export * from './helper.ts';
2+
export * from './shared.ts';

tests/scripts/rstest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// This file can only be imported by @rstest/core test files
22
import { expect } from '@rstest/core';
3-
import { getFileBySuffix } from './shared';
3+
import { getFileBySuffix } from './shared.ts';
44

55
export function expectFileContainContent(
66
files: Record<string, string>,

tests/scripts/shared.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
} from '@rsbuild/core';
1616
import type { Format, LibConfig, RslibConfig } from '@rslib/core';
1717
import { build, loadConfig } from '@rslib/core';
18-
import { globContentJSON } from './helper';
18+
import { globContentJSON } from './helper.ts';
1919

2020
const __filename = fileURLToPath(import.meta.url);
2121
const __dirname = dirname(__filename);

tests/scripts/tsconfig.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "../tsconfig.json",
3+
"compilerOptions": {
4+
"module": "nodenext",
5+
"moduleResolution": "nodenext"
6+
},
7+
"include": ["./*.ts"],
8+
"exclude": ["**/node_modules", "**/.*/"]
9+
}

tests/tsconfig.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
"compilerOptions": {
44
"noEmit": true,
55
"composite": true,
6-
"allowJs": true
6+
"allowJs": true,
7+
"allowImportingTsExtensions": true
78
},
89
"include": [
910
"e2e/**/*.ts",
1011
"integration/**/**.test.ts",
1112
"integration/**/rslib.config.ts",
1213
"integration/**/rslib.config.js",
1314
"benchmark/**/*.ts",
14-
"playwright.config.ts",
15-
"scripts"
15+
"playwright.config.ts"
1616
],
1717
"exclude": ["**/node_modules", "**/.*/"],
1818
"references": [

website/docs/en/guide/basic/cli.mdx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ Commands:
2626

2727
Rslib CLI provides several common flags that can be used with all commands:
2828

29-
| Flag | Description |
30-
| ----------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- |
31-
| `-c, --config <config>` | Specify the configuration file, can be a relative or absolute path, see [Specify config file](/guide/basic/configure-rslib#specify-config-file) |
32-
| `--env-dir <dir>` | Specify the directory to load `.env` files, see [Rsbuild - Env directory](https://rsbuild.rs/guide/advanced/env-vars#env-directory) |
33-
| `--env-mode <mode>` | Specify the env mode to load the `.env.[mode]` file, see [Rsbuild - Env mode](https://rsbuild.rs/guide/advanced/env-vars#env-mode) |
34-
| `-h, --help` | Display help for command |
35-
| `--lib <id>` | Specify the library to run commands (repeatable, e.g. `--lib esm --lib cjs`), see [lib.id](/config/lib/id) to learn how to get or set the ID of the library |
36-
| `-r, --root <root>` | Specify the project root directory, can be an absolute path or a path relative to cwd |
29+
| Flag | Description |
30+
| -------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- |
31+
| `-c, --config <config>` | Specify the configuration file, can be a relative or absolute path, see [Specify config file](/guide/basic/configure-rslib#specify-config-file) |
32+
| `--config-loader <loader>` | Set the config file loader (`jiti` \| `native`), see [Specify config loader](/guide/basic/configure-rslib#specify-config-loader) |
33+
| `--env-dir <dir>` | Specify the directory to load `.env` files, see [Rsbuild - Env directory](https://rsbuild.rs/guide/advanced/env-vars#env-directory) |
34+
| `--env-mode <mode>` | Specify the env mode to load the `.env.[mode]` file, see [Rsbuild - Env mode](https://rsbuild.rs/guide/advanced/env-vars#env-mode) |
35+
| `-h, --help` | Display help for command |
36+
| `--lib <id>` | Specify the library to run commands (repeatable, e.g. `--lib esm --lib cjs`), see [lib.id](/config/lib/id) to learn how to get or set the ID of the library |
37+
| `-r, --root <root>` | Specify the project root directory, can be an absolute path or a path relative to cwd |
3738

3839
## rslib build
3940

0 commit comments

Comments
 (0)