Skip to content

Commit 7674ab6

Browse files
feat!: config loader default to 'auto' (#1240)
Co-authored-by: Wei <[email protected]>
1 parent 380fa82 commit 7674ab6

File tree

10 files changed

+107
-32
lines changed

10 files changed

+107
-32
lines changed

packages/core/src/cli/commands.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ const applyCommonOptions = (cli: CAC) => {
4343
)
4444
.option(
4545
'--config-loader <loader>',
46-
'Set the config file loader (jiti | native)',
46+
'Set the config file loader (auto | jiti | native)',
4747
{
48-
default: 'jiti',
48+
default: 'auto',
4949
},
5050
)
5151
.option('--env-dir <dir>', 'specify the directory to load `.env` files')

packages/core/src/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ 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';
122+
export type ConfigLoader = 'auto' | 'jiti' | 'native';
123123

124124
export async function loadConfig({
125125
cwd = process.cwd(),

pnpm-lock.yaml

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ describe('build command', async () => {
8282
`);
8383
});
8484

85-
test('--config-loader', async () => {
85+
test('should use Node.js native loader to load config', async () => {
8686
// Skip Node.js <= 22.18
8787
if (!process.features.typescript) {
8888
return;
@@ -103,6 +103,21 @@ describe('build command', async () => {
103103
`);
104104
});
105105

106+
test('should fallback to jiti when --config-loader set to auto which is default strategy', async () => {
107+
await fse.remove(path.join(__dirname, 'dist-auto'));
108+
runCliSync('build --config rslib.config.auto.mts', {
109+
cwd: __dirname,
110+
});
111+
112+
const files = await globContentJSON(path.join(__dirname, 'dist-auto'));
113+
const fileNames = Object.keys(files).sort();
114+
expect(fileNames).toMatchInlineSnapshot(`
115+
[
116+
"<ROOT>/tests/integration/cli/build/dist-auto/index.js",
117+
]
118+
`);
119+
});
120+
106121
test('--root', async () => {
107122
await fse.remove(path.join(__dirname, 'dist'));
108123
runCliSync('build --root custom-root', {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"distPath": "dist-auto"
3+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { defineConfig } from '@rslib/core';
2+
import { generateBundleEsmConfig } from 'test-helper';
3+
import distPathJson from './path.json';
4+
5+
export default defineConfig({
6+
lib: [
7+
generateBundleEsmConfig({
8+
output: {
9+
distPath: {
10+
root: distPathJson.distPath,
11+
},
12+
},
13+
}),
14+
],
15+
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Rslib CLI provides several common flags that can be used with all commands:
2929
| Flag | Description |
3030
| -------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- |
3131
| `-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) |
32+
| `--config-loader <loader>` | Set the config file loader (`auto` \| `jiti` \| `native`), see [Specify config loader](/guide/basic/configure-rslib#specify-config-loader) |
3333
| `--env-dir <dir>` | Specify the directory to load `.env` files, see [Rsbuild - Env directory](https://rsbuild.rs/guide/advanced/env-vars#env-directory) |
3434
| `--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) |
3535
| `-h, --help` | Display help for command |

website/docs/en/guide/basic/configure-rslib.mdx

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,21 +127,42 @@ rslib build -c rslib.prod.config.mjs
127127

128128
## Specify config loader
129129

130-
When you use a configuration file with the `.ts`, `.mts`, and `.cts` extensions, Rslib will use [jiti](https://github.com/unjs/jiti) to load configuration files, providing interoperability between ESM and CommonJS. The behavior of module resolution differs slightly from the native behavior of Node.js.
130+
Rslib provides three ways to load configuration files:
131131

132-
If your JavaScript runtime already natively supports TypeScript, you can use the `--config-loader native` option to use the Node.js native loader to load the configuration file. This can ensure that the module resolution behavior is consistent with the native behavior of Node.js and has better performance.
132+
- `jiti`: When you use a configuration file with the `.ts`, `.mts`, and `.cts` extensions, Rslib will use [jiti](https://github.com/unjs/jiti) to load configuration files, providing interoperability between ESM and CommonJS. The behavior of module resolution differs slightly from the native behavior of Node.js.
133+
- `native`: Use Node.js native loader to load the configuration file. This can ensure that the module resolution behavior is consistent with the native behavior of Node.js and has better performance. This requires that your JavaScript runtime already natively supports TypeScript.
133134

134-
For example, Node.js v22.6.0+ already natively supports TypeScript, you can use the following command to use the Node.js native loader to load the configuration file:
135+
For example, Node.js v22.6.0+ already natively supports TypeScript, you can use the following command to use the Node.js native loader to load the configuration file:
135136

136-
```bash
137-
# Node.js >= v22.18.0
138-
# No need to set --experimental-strip-types
139-
npx rslib build --config-loader native
137+
```bash
138+
# Node.js >= v22.18.0
139+
# No need to set --experimental-strip-types
140+
npx rslib build --config-loader native
140141

141-
# Node.js v22.6.0 - v22.17.0
142-
# Need to set --experimental-strip-types
143-
NODE_OPTIONS="--experimental-strip-types" npx rslib build --config-loader native
144-
```
142+
# Node.js v22.6.0 - v22.17.1
143+
# Need to set --experimental-strip-types
144+
NODE_OPTIONS="--experimental-strip-types" npx rslib build --config-loader native
145+
```
146+
147+
- `auto`(Default): Use Node.js's native loader to load configuration files first, fallback to using jiti if it fails.
148+
149+
### About Node.js native loader
150+
151+
When using Node.js's native loader, please note the following limitations:
152+
153+
1. When importing JSON files, you need to use import attributes:
154+
155+
```ts
156+
import pkgJson from './package.json' with { type: 'json' }; // ✅ Correct
157+
import pkgJson from './package.json'; // ❌ Incorrect
158+
```
159+
160+
2. When importing TypeScript files, you need to include the `.ts` extension:
161+
162+
```ts
163+
import baseConfig from './rslib.base.config.ts'; // ✅ Correct
164+
import baseConfig from './rslib.base.config'; // ❌ Incorrect
165+
```
145166

146167
> See [Node.js - Running TypeScript Natively](https://nodejs.org/en/learn/typescript/run-natively#running-typescript-natively) for more details.
147168

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Rslib CLI 提供了一些公共选项,可以用于所有命令:
2929
| 选项 | 描述 |
3030
| -------------------------- | ------------------------------------------------------------------------------------------------------------------------- |
3131
| `-c, --config <config>` | 指定配置文件路径,可以为相对路径或绝对路径,详见 [指定配置文件](/guide/basic/configure-rslib#指定配置文件) |
32-
| `--config-loader <loader>` | 指定配置文件加载方式(`jiti` \| `native`),详见 [指定加载方式](/guide/basic/configure-rslib#指定加载方式) |
32+
| `--config-loader <loader>` | 指定配置文件加载方式(`auto` \| `jiti` \| `native`),详见 [指定加载方式](/guide/basic/configure-rslib#指定加载方式) |
3333
| `--env-dir <dir>` | 指定目录来加载 `.env` 文件,详见 [Rsbuild - Env 目录](https://rsbuild.rs/zh/guide/advanced/env-vars#env-目录) |
3434
| `--env-mode <mode>` | 指定 env 模式来加载 `.env.[mode]` 文件,详见 [Rsbuild - Env 模式](https://rsbuild.rs/zh/guide/advanced/env-vars#env-模式) |
3535
| `-h, --help` | 显示命令帮助 |

website/docs/zh/guide/basic/configure-rslib.mdx

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,21 +127,42 @@ rslib build -c rslib.prod.config.mjs
127127

128128
## 指定加载方式
129129

130-
当你使用 `.ts`, `.mts``.cts` 后缀的配置文件时,Rslib 会使用 [jiti](https://github.com/unjs/jiti) 来加载配置文件,提供 ESM 与 CommonJS 的互操作性,模块解析的行为与 Node.js 原生行为存在一定差异。
130+
Rslib 提供了三种配置文件加载方式:
131131

132-
如果你使用的 JavaScript 运行时已经原生支持 TypeScript,可以使用 `--config-loader native` 选项来使用 Node.js 原生 loader 来加载配置文件。这可以保证模块解析的行为与 Node.js 原生行为一致,并且性能更好。
132+
- `jiti`:当你使用 `.ts`, `.mts``.cts` 后缀的配置文件时,Rslib 会使用 [jiti](https://github.com/unjs/jiti) 来加载配置文件,提供 ESM 与 CommonJS 的互操作性,模块解析的行为与 Node.js 原生行为存在一定差异。
133+
- `native`:使用 Node.js 原生 loader 来加载配置文件,这可以保证模块解析的行为与 Node.js 原生行为一致,并且性能更好。这要求你使用的 JavaScript 运行时已经原生支持 TypeScript。
133134

134-
例如,Node.js 从 v22.6.0 开始已经原生支持 TypeScript,你可以如下命令来使用 Node.js 原生 loader 来加载配置文件:
135+
例如,Node.js 从 v22.6.0 开始已经原生支持 TypeScript,你可以运行如下命令来使用 Node.js 原生 loader 来加载配置文件:
135136

136-
```bash
137-
# Node.js >= v22.18.0
138-
# 不需要设置 --experimental-strip-types
139-
npx rslib build --config-loader native
137+
```bash
138+
# Node.js >= v22.18.0
139+
# 不需要设置 --experimental-strip-types
140+
npx rslib build --config-loader native
140141

141-
# Node.js v22.6.0 - v22.17.0
142-
# 需要设置 --experimental-strip-types
143-
NODE_OPTIONS="--experimental-strip-types" npx rslib build --config-loader native
144-
```
142+
# Node.js v22.6.0 - v22.17.1
143+
# 需要设置 --experimental-strip-types
144+
NODE_OPTIONS="--experimental-strip-types" npx rslib build --config-loader native
145+
```
146+
147+
- `auto`(默认):优先使用 Node.js 原生 loader 来加载配置文件,失败时回退到使用 jiti 加载。
148+
149+
### 关于 Node.js 原生 loader
150+
151+
使用 Node.js 原生 loader 时,请注意以下限制:
152+
153+
1. 导入 JSON 文件时,需要使用 import attributes:
154+
155+
```ts
156+
import pkgJson from './package.json' with { type: 'json' }; // ✅ 正确
157+
import pkgJson from './package.json'; // ❌ 错误
158+
```
159+
160+
2. 导入 TypeScript 文件时,需要包含 `.ts` 扩展名:
161+
162+
```ts
163+
import baseConfig from './rslib.base.config.ts'; // ✅ 正确
164+
import baseConfig from './rslib.base.config'; // ❌ 错误
165+
```
145166

146167
> 详见 [Node.js - Running TypeScript Natively](https://nodejs.org/en/learn/typescript/run-natively#running-typescript-natively)
147168

0 commit comments

Comments
 (0)