diff --git a/packages/core/src/cli/commands.ts b/packages/core/src/cli/commands.ts index 31cf63d55e..b6fb8cb179 100644 --- a/packages/core/src/cli/commands.ts +++ b/packages/core/src/cli/commands.ts @@ -48,7 +48,7 @@ const applyCommonOptions = (cli: CAC) => { '--config-loader ', 'Set the config file loader (auto | jiti | native)', { - default: 'jiti', + default: 'auto', }, ) .option('--env-dir ', 'Set the directory for loading `.env` files') diff --git a/packages/core/src/loadConfig.ts b/packages/core/src/loadConfig.ts index a8b4b02705..8f057595e3 100644 --- a/packages/core/src/loadConfig.ts +++ b/packages/core/src/loadConfig.ts @@ -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 { @@ -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 @@ -179,10 +180,12 @@ export async function loadConfig({ configExport = await jiti.import(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') { diff --git a/website/docs/en/guide/configuration/rsbuild.mdx b/website/docs/en/guide/configuration/rsbuild.mdx index 5e10c924a1..b40539b500 100644 --- a/website/docs/en/guide/configuration/rsbuild.mdx +++ b/website/docs/en/guide/configuration/rsbuild.mdx @@ -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: @@ -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: diff --git a/website/docs/zh/guide/configuration/rsbuild.mdx b/website/docs/zh/guide/configuration/rsbuild.mdx index c39c4da8fc..d12ed6624e 100644 --- a/website/docs/zh/guide/configuration/rsbuild.mdx +++ b/website/docs/zh/guide/configuration/rsbuild.mdx @@ -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 来加载配置文件: @@ -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 时,请注意以下限制: