Skip to content

Commit

Permalink
fix!: remove function usage of source.entry
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan committed Jun 25, 2024
1 parent b13d0aa commit 9e9840a
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 67 deletions.
24 changes: 10 additions & 14 deletions e2e/cases/source/multiple-entry/rsbuild.config.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
import { defineConfig } from '@rsbuild/core';

export default defineConfig({
source: {
entry({ target }) {
if (target === 'web') {
return {
index: './src/index.client.js',
};
}
if (target === 'node') {
return {
index: './src/index.server.js',
};
}
},
},
output: {
filenameHash: false,
},
environments: {
web: {
source: {
entry: {
index: './src/index.client.js',
},
},
output: {
target: 'web',
},
},
node: {
source: {
entry: {
index: './src/index.server.js',
},
},
output: {
target: 'node',
distPath: {
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/createContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { getCommonParentPath } from './helpers/path';
import { initHooks } from './initHooks';
import { getHTMLPathByEntry } from './initPlugins';
import { logger } from './logger';
import { getEntryObject } from './plugins/entry';
import type {
CreateRsbuildOptions,
InternalContext,
Expand Down Expand Up @@ -139,19 +138,19 @@ export async function updateEnvironmentContext(
const tsconfigPath = config.source.tsconfigPath
? getAbsolutePath(context.rootPath, config.source.tsconfigPath)
: undefined;
const entry = getEntryObject(config, config.output.target);

const browserslist = await getBrowserslistByEnvironment(
context.rootPath,
config,
);

const entry = config.source.entry ?? {};
const htmlPaths = getEnvironmentHTMLPaths(entry, config);

context.environments[name] = {
target: config.output.target,
distPath: getAbsoluteDistPath(context.rootPath, config),
entry: getEntryObject(config, config.output.target),
entry,
browserslist,
htmlPaths,
tsconfigPath,
Expand Down
26 changes: 2 additions & 24 deletions packages/core/src/plugins/entry.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,7 @@
import {
type NormalizedEnvironmentConfig,
type RsbuildEntry,
type RsbuildTarget,
castArray,
color,
} from '@rsbuild/shared';
import { castArray, color } from '@rsbuild/shared';
import type { EntryDescription } from '@rspack/core';
import { createVirtualModule } from '../helpers';
import { reduceConfigsMergeContext } from '../reduceConfigs';
import type { NormalizedConfig, RsbuildConfig, RsbuildPlugin } from '../types';

export function getEntryObject(
config: RsbuildConfig | NormalizedConfig | NormalizedEnvironmentConfig,
target: RsbuildTarget,
): RsbuildEntry {
if (!config.source?.entry) {
return {};
}

return reduceConfigsMergeContext({
initial: {},
config: config.source?.entry,
ctx: { target },
});
}
import type { RsbuildPlugin } from '../types';

export const pluginEntry = (): RsbuildPlugin => ({
name: 'rsbuild:entry',
Expand Down
6 changes: 3 additions & 3 deletions packages/shared/src/types/config/source.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { RuleSetCondition } from '@rspack/core';
import type { RsbuildEntry, RsbuildTarget } from '../rsbuild';
import type { ConfigChain, ConfigChainMergeContext } from '../utils';
import type { RsbuildEntry } from '../rsbuild';
import type { ConfigChain } from '../utils';

export type Alias = Record<string, string | false | (string | false)[]>;

Expand Down Expand Up @@ -39,7 +39,7 @@ export interface SourceConfig {
/**
* Set the entry modules.
*/
entry?: ConfigChainMergeContext<RsbuildEntry, { target: RsbuildTarget }>;
entry?: RsbuildEntry;
/**
* Specifies that certain files that will be excluded from compilation.
*/
Expand Down
4 changes: 3 additions & 1 deletion website/docs/en/config/output/override-browserslist.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Check out the [browserslist documentation](https://github.com/browserslist/brows

## Set by environment

When you build for multiple environments, you can set different browserslist for each environment:
When you build for multiple [environments](/config/environments), you can set different browserslist for each environment:

For example, set different browserslist for `web` and `node` environments:

Expand All @@ -54,11 +54,13 @@ export default {
environments: {
web: {
output: {
target: 'web',
overrideBrowserslist: ['iOS >= 9', 'Android >= 4.4'],
},
},
node: {
output: {
target: 'node',
overrideBrowserslist: ['node >= 20'],
},
},
Expand Down
2 changes: 1 addition & 1 deletion website/docs/en/config/source/alias.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default {

## Set by environment

When you build for multiple environments, you can set different alias for each environment:
When you build for multiple [environments](/config/environments), you can set different alias for each environment:

For example, set different alias for `web` and `node` environments:

Expand Down
41 changes: 23 additions & 18 deletions website/docs/en/config/source/entry.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
- **Type:**

```ts
type Entry = Record<string, string | string[] | EntryDescription> | Function;
type Entry = Record<string, string | string[] | EntryDescription>;
```

- **Default:**
Expand Down Expand Up @@ -72,30 +72,35 @@ export default {

For the complete usage of the description object, please refer to [Rspack - Entry Description Object](https://rspack.dev/config/entry#entry-description-object).

## Function Usage
## Set by environment

`source.entry` supports setting as a function, which is commonly used to set different entry objects for different build target.
When you build for multiple [environments](/config/environments), you can set different entry for each environment:

For example, set entry for web target and node target separately:
For example, set different entry for `web` and `node` environments:

```ts title="rsbuild.config.ts"
```js
export default {
source: {
entry({ target }) {
if (target === 'web') {
return {
environments: {
web: {
source: {
entry: {
index: './src/index.client.js',
};
}
if (target === 'node') {
return {
},
},
output: {
target: 'web',
},
},
node: {
source: {
entry: {
index: './src/index.server.js',
};
}
},
},
output: {
target: 'node',
},
},
},
output: {
targets: ['web', 'node'],
},
};
```
4 changes: 3 additions & 1 deletion website/docs/zh/config/output/override-browserslist.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default {

## 基于 environment 设置

当你面向多个 environment 构建时,可以为每个 environment 设置不同的 browserslist:
当你面向多个 [environments](/config/environments) 构建时,可以为每个 environment 设置不同的 browserslist:

比如为 `web``node` 环境设置不同的 browserslist:

Expand All @@ -54,11 +54,13 @@ export default {
environments: {
web: {
output: {
target: 'web',
overrideBrowserslist: ['iOS >= 9', 'Android >= 4.4'],
},
},
node: {
output: {
target: 'node',
overrideBrowserslist: ['node >= 20'],
},
},
Expand Down
2 changes: 1 addition & 1 deletion website/docs/zh/config/source/alias.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default {

## 基于 environment 设置

当你面向多个 environment 构建时,可以为每个 environment 设置不同的 alias:
当你面向多个 [environments](/config/environments) 构建时,可以为每个 environment 设置不同的 alias:

比如为 `web``node` 环境设置不同的 alias:

Expand Down
35 changes: 34 additions & 1 deletion website/docs/zh/config/source/entry.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
- **类型:**

```ts
type Entry = Record<string, string | string[] | EntryDescription> | Function;
type Entry = Record<string, string | string[] | EntryDescription>;
```

- **默认值:**
Expand Down Expand Up @@ -99,3 +99,36 @@ export default {
},
};
```

## 基于 environment 设置

当你面向多个 [environments](/config/environments) 构建时,可以为每个 environment 设置不同的 entry:

比如为 `web``node` 环境设置不同的 entry:

```js
export default {
environments: {
web: {
source: {
entry: {
index: './src/index.client.js',
},
},
output: {
target: 'web',
},
},
node: {
source: {
entry: {
index: './src/index.server.js',
},
},
output: {
target: 'node',
},
},
},
};
```

0 comments on commit 9e9840a

Please sign in to comment.