diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index 9e968d379..ffca44347 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -879,13 +879,23 @@ export const appendEntryQuery = (entries: RsbuildConfigEntry): RsbuildEntry => traverseEntryQuery(entries, (item) => `${item}?${RSLIB_ENTRY_QUERY}`); const composeEntryConfig = async ( - entries: RsbuildConfigEntry, + rawEntry: RsbuildConfigEntry, bundle: LibConfig['bundle'], root: string, cssModulesAuto: CssLoaderOptionsAuto, ): Promise<{ entryConfig: EnvironmentConfig; lcp: string | null }> => { + let entries = rawEntry; + if (!entries) { - return { entryConfig: {}, lcp: null }; + // In bundle mode, return directly to let Rsbuild apply default entry to './src/index.ts' + if (bundle !== false) { + return { entryConfig: {}, lcp: null }; + } + + // In bundleless mode, set default entry to './src/**' + entries = { + index: 'src/**', + }; } if (bundle !== false) { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ef57ed520..2fcecb65e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -660,6 +660,8 @@ importers: tests/integration/dts/composite/process-files: {} + tests/integration/entry/default: {} + tests/integration/entry/glob: {} tests/integration/entry/glob-bundle: {} diff --git a/tests/integration/entry/default/package.json b/tests/integration/entry/default/package.json new file mode 100644 index 000000000..867565910 --- /dev/null +++ b/tests/integration/entry/default/package.json @@ -0,0 +1,6 @@ +{ + "name": "entry-default-test", + "version": "1.0.0", + "private": true, + "type": "module" +} diff --git a/tests/integration/entry/default/rslib.config.ts b/tests/integration/entry/default/rslib.config.ts new file mode 100644 index 000000000..789f486d0 --- /dev/null +++ b/tests/integration/entry/default/rslib.config.ts @@ -0,0 +1,37 @@ +import { defineConfig } from '@rslib/core'; +import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper'; + +export default defineConfig({ + lib: [ + generateBundleEsmConfig({ + output: { + distPath: { + root: 'dist/esm-bundle', + }, + }, + }), + generateBundleCjsConfig({ + output: { + distPath: { + root: 'dist/cjs-bundle', + }, + }, + }), + generateBundleEsmConfig({ + output: { + distPath: { + root: 'dist/esm-bundle-false', + }, + }, + bundle: false, + }), + generateBundleCjsConfig({ + output: { + distPath: { + root: 'dist/cjs-bundle-false', + }, + }, + bundle: false, + }), + ], +}); diff --git a/tests/integration/entry/default/src/foo.ts b/tests/integration/entry/default/src/foo.ts new file mode 100644 index 000000000..3329a7d97 --- /dev/null +++ b/tests/integration/entry/default/src/foo.ts @@ -0,0 +1 @@ +export const foo = 'foo'; diff --git a/tests/integration/entry/default/src/index.ts b/tests/integration/entry/default/src/index.ts new file mode 100644 index 000000000..3c8dc405f --- /dev/null +++ b/tests/integration/entry/default/src/index.ts @@ -0,0 +1,3 @@ +import { foo } from './foo'; + +export const text = () => `hello ${foo}`; diff --git a/tests/integration/entry/index.test.ts b/tests/integration/entry/index.test.ts index 571d8672c..81f78a432 100644 --- a/tests/integration/entry/index.test.ts +++ b/tests/integration/entry/index.test.ts @@ -3,6 +3,30 @@ import stripAnsi from 'strip-ansi'; import { buildAndGetResults, queryContent } from 'test-helper'; import { expect, test } from 'vitest'; +test('default entry', async () => { + const fixturePath = join(__dirname, 'default'); + const { files } = await buildAndGetResults({ fixturePath }); + + expect(files).toMatchInlineSnapshot(` + { + "cjs0": [ + "/tests/integration/entry/default/dist/cjs-bundle/index.cjs", + ], + "cjs1": [ + "/tests/integration/entry/default/dist/cjs-bundle-false/foo.cjs", + "/tests/integration/entry/default/dist/cjs-bundle-false/index.cjs", + ], + "esm0": [ + "/tests/integration/entry/default/dist/esm-bundle/index.js", + ], + "esm1": [ + "/tests/integration/entry/default/dist/esm-bundle-false/foo.js", + "/tests/integration/entry/default/dist/esm-bundle-false/index.js", + ], + } + `); +}); + test('single entry bundle', async () => { const fixturePath = join(__dirname, 'single'); const { files } = await buildAndGetResults({ fixturePath }); diff --git a/website/docs/en/config/lib/bundle.mdx b/website/docs/en/config/lib/bundle.mdx index 847fe7458..013e34e7b 100644 --- a/website/docs/en/config/lib/bundle.mdx +++ b/website/docs/en/config/lib/bundle.mdx @@ -19,7 +19,9 @@ We should specify the entry file for the build. ### bundle: true -When `bundle` is set to `true`, the entry should be set to the entry file. The default entry is `src/index.(ts|js|tsx|jsx|mjs|cjs)`. You should make sure that the entry file exists, or customize entry through the [source.entry](https://rsbuild.dev/config/source/entry) configuration. +When `bundle` is set to `true`, the entry should be set to the entry file. The default entry in bundle mode is `src/index.(ts|js|tsx|jsx|mjs|cjs)`. You should make sure that the entry file exists, or customize entry through the [source.entry](https://rsbuild.dev/config/source/entry) configuration. + +**Example:** ```ts title="rslib.config.ts" export default { @@ -39,7 +41,9 @@ export default { ### bundle: false -When `bundle` is set to `false`, the entry should be set a [glob pattern](https://github.com/micromatch/picomatch#globbing-features) to include all the files. +When `bundle` is set to `false`, the entry should be set to a [glob pattern](https://github.com/micromatch/picomatch#globbing-features) to include all the files. The default entry in bundleless mode is `src/**`. + +**Example:** ```ts title="rslib.config.ts" export default { @@ -51,7 +55,7 @@ export default { ], source: { entry: { - index: './src/**', + index: './foo/**', }, }, }; diff --git a/website/docs/en/config/rsbuild/source.mdx b/website/docs/en/config/rsbuild/source.mdx index 574a4c8e6..fb202fd6b 100644 --- a/website/docs/en/config/rsbuild/source.mdx +++ b/website/docs/en/config/rsbuild/source.mdx @@ -19,10 +19,13 @@ Replaces variables in your code with other values or expressions at compile time ## source.entry -Used to set the entry modules for building. +Used to set the entry modules for building. In Rslib, the default value of `source.entry` is: + +- bundle mode: `src/index.(ts|js|tsx|jsx|mjs|cjs)` +- bundleless mode: `src/**` :::info -Check out the [lib.bundle](/config/lib/bundle#set-entry) to learn how to set entry for bundle and bundleless project. +Check out the [lib.bundle](/config/lib/bundle#set-entry) to learn more about how to set entry for bundle and bundleless project. ::: ## source.exclude diff --git a/website/docs/zh/config/lib/bundle.mdx b/website/docs/zh/config/lib/bundle.mdx index e612571f3..7ad91245a 100644 --- a/website/docs/zh/config/lib/bundle.mdx +++ b/website/docs/zh/config/lib/bundle.mdx @@ -6,7 +6,7 @@ 指定是否打包库,当 `bundle` 设置为 `true` 时称为 bundle 模式,设置为 `false` 时称为 bundleless 模式。更多详情请参见 [bundle / bundleless](/guide/basic/output-structure#bundle--bundleless)。 ::: warning -无打包模式尚未完全支持,某些功能如 [资源](/guide/advanced/assets) 可能无法正常工作。 +Bundleless 模式尚未完全支持,某些功能如 [资源](/guide/advanced/assets) 可能无法正常工作。 ::: ## 设置入口 @@ -15,7 +15,9 @@ ### bundle: true -当 `bundle` 设置为 `true` 时,入口需要设置为入口文件。默认入口为 `src/index.(ts|js|tsx|jsx|mjs|cjs)`。你需要确保入口文件存在,或通过 [source.entry](https://rsbuild.dev/zh/config/source/entry) 配置自定义入口。 +当 `bundle` 设置为 `true` 时,entry 需要设置为入口文件。Bundle 模式下的默认入口为 `src/index.(ts|js|tsx|jsx|mjs|cjs)`。你需要确保入口文件存在,或通过 [source.entry](https://rsbuild.dev/zh/config/source/entry) 配置自定义入口。 + +**例子:** ```ts title="rslib.config.ts" export default { @@ -35,7 +37,9 @@ export default { ### bundle: false -当 `bundle` 设置为 `false` 时,入口需要设置为 [glob 模式](https://github.com/micromatch/picomatch#globbing-features) 以包含所有文件。 +当 `bundle` 设置为 `false` 时,入口需要设置为 [glob 模式](https://github.com/micromatch/picomatch#globbing-features) 以包含所有文件。Bundleless 模式下的默认入口为 `src/**`。 + +**例子:** ```ts title="rslib.config.ts" export default { @@ -47,7 +51,7 @@ export default { ], source: { entry: { - index: './src/**', + index: './foo/**', }, }, }; diff --git a/website/docs/zh/config/rsbuild/source.mdx b/website/docs/zh/config/rsbuild/source.mdx index de59ba7b3..878ad7317 100644 --- a/website/docs/zh/config/rsbuild/source.mdx +++ b/website/docs/zh/config/rsbuild/source.mdx @@ -18,10 +18,13 @@ import { RsbuildDocBadge } from '@components/RsbuildDocBadge'; ## source.entry -用于设置构建的入口模块。 +用于设置构建的入口模块。在 Rslib 中,`source.entry` 的默认值为: + +- bundle 模式:`src/index.(ts|js|tsx|jsx|mjs|cjs)` +- bundleless 模式:`src/**` :::info -参考 [lib.bundle](/config/lib/bundle#set-entry) 了解如何为 bundle 和 bundleless 项目设置入口。 +参考 [lib.bundle](/config/lib/bundle#set-entry) 进一步了解如何为 bundle 和 bundleless 项目设置入口。 ::: ## source.exclude