Skip to content

feat: entry default to src/** in bundleless mode #647

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 6, 2025
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
14 changes: 12 additions & 2 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions tests/integration/entry/default/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "entry-default-test",
"version": "1.0.0",
"private": true,
"type": "module"
}
37 changes: 37 additions & 0 deletions tests/integration/entry/default/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -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,
}),
],
});
1 change: 1 addition & 0 deletions tests/integration/entry/default/src/foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'foo';
3 changes: 3 additions & 0 deletions tests/integration/entry/default/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { foo } from './foo';

export const text = () => `hello ${foo}`;
24 changes: 24 additions & 0 deletions tests/integration/entry/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
"<ROOT>/tests/integration/entry/default/dist/cjs-bundle/index.cjs",
],
"cjs1": [
"<ROOT>/tests/integration/entry/default/dist/cjs-bundle-false/foo.cjs",
"<ROOT>/tests/integration/entry/default/dist/cjs-bundle-false/index.cjs",
],
"esm0": [
"<ROOT>/tests/integration/entry/default/dist/esm-bundle/index.js",
],
"esm1": [
"<ROOT>/tests/integration/entry/default/dist/esm-bundle-false/foo.js",
"<ROOT>/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 });
Expand Down
10 changes: 7 additions & 3 deletions website/docs/en/config/lib/bundle.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -51,7 +55,7 @@ export default {
],
source: {
entry: {
index: './src/**',
index: './foo/**',
},
},
};
Expand Down
7 changes: 5 additions & 2 deletions website/docs/en/config/rsbuild/source.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ Replaces variables in your code with other values or expressions at compile time

## source.entry <RsbuildDocBadge path="/config/source/entry" text="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 <RsbuildDocBadge path="/config/source/exclude" text="source.exclude" />
Expand Down
12 changes: 8 additions & 4 deletions website/docs/zh/config/lib/bundle.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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) 可能无法正常工作。
:::

## 设置入口
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -47,7 +51,7 @@ export default {
],
source: {
entry: {
index: './src/**',
index: './foo/**',
},
},
};
Expand Down
7 changes: 5 additions & 2 deletions website/docs/zh/config/rsbuild/source.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ import { RsbuildDocBadge } from '@components/RsbuildDocBadge';

## source.entry <RsbuildDocBadge path="/config/source/entry" text="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 <RsbuildDocBadge path="/config/source/exclude" text="source.exclude" />
Expand Down
Loading