Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/core/src/cli/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const applyCommonOptions = (cli: CAC) => {
'--config-loader <loader>',
'Set the config file loader (auto | jiti | native)',
{
default: 'jiti',
default: 'auto',
},
)
.option('--env-dir <dir>', 'Set the directory for loading `.env` files')
Expand Down
25 changes: 14 additions & 11 deletions packages/core/src/loadConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,16 @@ export async function loadConfig({
return config;
};

let configExport: RsbuildConfigExport;
let configExport: RsbuildConfigExport | undefined;

// Determine the loading strategy based on the config loader type
const useNative =
const useNative = Boolean(
loader === 'native' ||
(loader === 'auto' &&
(process.features.typescript ||
process.versions.bun ||
process.versions.deno));
(loader === 'auto' &&
(process.features.typescript ||
process.versions.bun ||
process.versions.deno)),
);

if (useNative || /\.(?:js|mjs|cjs)$/.test(configFilePath)) {
try {
Expand All @@ -164,8 +165,8 @@ export async function loadConfig({
}
}

try {
if (configExport! === undefined) {
if (configExport === undefined) {
try {
const { createJiti } = await import('jiti');
const jiti = createJiti(__filename, {
// disable require cache to support restart CLI and read the new config
Expand All @@ -179,10 +180,12 @@ export async function loadConfig({
configExport = await jiti.import<RsbuildConfigExport>(configFilePath, {
default: true,
});
} catch (err) {
logger.error(
`Failed to load file with jiti: ${color.dim(configFilePath)}`,
);
throw err;
}
} catch (err) {
logger.error(`Failed to load file with jiti: ${color.dim(configFilePath)}`);
throw err;
}

if (typeof configExport === 'function') {
Expand Down
5 changes: 2 additions & 3 deletions website/docs/en/guide/configuration/rsbuild.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ rsbuild build -c rsbuild.prod.config.mjs

Rsbuild provides three ways to load configuration files:

- `jiti` (Default): When you use a configuration file with the `.ts`, `.mts`, or `.cts` extensions, Rsbuild uses [jiti](https://github.com/unjs/jiti) to load the configuration file, providing interoperability between ESM and CommonJS. The module resolution behavior differs slightly from Node.js native behavior.
- `auto` (Default): Use Node.js's native loader to load configuration files first, falling back to jiti if it fails.
- `jiti`: Use [jiti](https://github.com/unjs/jiti) to load the configuration file, providing interoperability between ESM and CommonJS. The module resolution behavior differs slightly from Node.js native behavior.
- `native`: Use Node.js native loader to load the configuration file. This ensures that module resolution behavior is consistent with Node.js native behavior and has better performance. This requires your JavaScript runtime to natively support TypeScript.

For example, Node.js v22.6.0+ natively supports TypeScript. You can use the following command with the Node.js native loader to load the configuration file:
Expand All @@ -132,8 +133,6 @@ Rsbuild provides three ways to load configuration files:
NODE_OPTIONS="--experimental-strip-types" npx rsbuild build --config-loader native
```

- `auto`: Use Node.js's native loader to load configuration files first, falling back to jiti if it fails.

### About Node.js native loader

When using Node.js's native loader, please note the following limitations:
Expand Down
5 changes: 2 additions & 3 deletions website/docs/zh/guide/configuration/rsbuild.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ rsbuild build -c rsbuild.prod.config.mjs

Rsbuild 提供了三种配置文件加载方式:

- `jiti`(默认):当你使用 `.ts`, `.mts` 和 `.cts` 后缀的配置文件时,Rsbuild 会使用 [jiti](https://github.com/unjs/jiti) 来加载配置文件,提供 ESM 与 CommonJS 的互操作性,模块解析的行为与 Node.js 原生行为存在一定差异。
- `auto`(默认):优先使用 Node.js 原生 loader 来加载配置文件,失败时回退到使用 jiti 加载。
- `jiti`:使用 [jiti](https://github.com/unjs/jiti) 来加载配置文件,提供 ESM 与 CommonJS 的互操作性,模块解析的行为与 Node.js 原生行为存在一定差异。
- `native`:使用 Node.js 原生 loader 来加载配置文件,这可以保证模块解析的行为与 Node.js 原生行为一致,并且性能更好。这要求你使用的 JavaScript 运行时已经原生支持 TypeScript。

例如,Node.js 从 v22.6.0 开始已经原生支持 TypeScript,你可以运行如下命令来使用 Node.js 原生 loader 来加载配置文件:
Expand All @@ -132,8 +133,6 @@ Rsbuild 提供了三种配置文件加载方式:
NODE_OPTIONS="--experimental-strip-types" npx rsbuild build --config-loader native
```

- `auto`:优先使用 Node.js 原生 loader 来加载配置文件,失败时回退到使用 jiti 加载。

### 关于 Node.js 原生 loader

使用 Node.js 原生 loader 时,请注意以下限制:
Expand Down
Loading