Skip to content

Commit 93c175c

Browse files
committed
docs: recommend use mergeRsbuildConfig within rsbuildFinal
1 parent cf04e40 commit 93c175c

File tree

11 files changed

+90
-52
lines changed

11 files changed

+90
-52
lines changed

biome.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"$schema": "https://biomejs.dev/schemas/2.3.8/schema.json",
2+
"$schema": "https://biomejs.dev/schemas/2.3.10/schema.json",
33
"assist": {
44
"includes": ["**", "!**/*.vue"],
55
"actions": { "source": { "organizeImports": "on" } }

pnpm-lock.yaml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sandboxes/react-native-web/.storybook/main.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import path from 'node:path'
22
import { fileURLToPath } from 'node:url'
3+
import { mergeRsbuildConfig } from '@rsbuild/core'
34
import { pluginBabel } from '@rsbuild/plugin-babel'
45
import type { StorybookConfig } from 'storybook-react-native-web-rsbuild'
56

@@ -40,24 +41,24 @@ const config: StorybookConfig = {
4041
// This is needed because react-native-reanimated (in modulesToTranspile)
4142
// gets its JSX transformed to use nativewind/jsx-runtime, but can't resolve
4243
// nativewind from deep within pnpm's .pnpm directory structure
43-
config.resolve ??= {}
44-
config.resolve.alias = {
45-
...config.resolve.alias,
46-
nativewind: nativewindPath,
47-
}
48-
49-
config.plugins?.push(
50-
pluginBabel({
51-
// KEY: Only transform src files to avoid transforming Rsbuild internals
52-
// Use [\\/] to match both Unix (/) and Windows (\) path separators
53-
include: /[\\/]src[\\/]/,
54-
babelLoaderOptions: {
55-
presets: ['@babel/preset-typescript', 'nativewind/babel'],
56-
plugins: ['@babel/plugin-proposal-export-namespace-from'],
44+
return mergeRsbuildConfig(config, {
45+
resolve: {
46+
alias: {
47+
nativewind: nativewindPath,
5748
},
58-
}),
59-
)
60-
return config
49+
},
50+
plugins: [
51+
pluginBabel({
52+
// KEY: Only transform src files to avoid transforming Rsbuild internals
53+
// Use [\\/] to match both Unix (/) and Windows (\) path separators
54+
include: /[\\/]src[\\/]/,
55+
babelLoaderOptions: {
56+
presets: ['@babel/preset-typescript', 'nativewind/babel'],
57+
plugins: ['@babel/plugin-proposal-export-namespace-from'],
58+
},
59+
}),
60+
],
61+
})
6162
},
6263
}
6364

sandboxes/rslib-vue3-component/.storybook/main.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import path from 'node:path'
22
import { fileURLToPath } from 'node:url'
3+
import { mergeRsbuildConfig } from '@rsbuild/core'
34
import { pluginVue } from '@rsbuild/plugin-vue'
45
import type { StorybookConfig } from 'storybook-vue3-rsbuild'
56

@@ -34,14 +35,18 @@ const config: StorybookConfig = {
3435
rsbuildFinal: (config) => {
3536
// Since HMR for Rspack is not supported by unplugin-vue as of now (https://github.com/unplugin/unplugin-vue/issues/162),
3637
// it's better to remove rsbuild-plugin-unplugin-vue and use @rsbuild/plugin-vue to handle all Vue files.
37-
config.plugins = (config.plugins || []).filter((p) => {
38+
const filteredPlugins = (config.plugins || []).filter((p) => {
3839
if (p && 'name' in p) {
3940
return p.name !== 'plugin-unplugin-vue'
4041
}
4142
return true
4243
})
43-
config.plugins.push(pluginVue())
44-
return config
44+
return mergeRsbuildConfig(
45+
{ ...config, plugins: filteredPlugins },
46+
{
47+
plugins: [pluginVue()],
48+
},
49+
)
4550
},
4651
}
4752

website/docs/guide/configuration.mdx

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,14 @@ The `rsbuildFinal` field gives you direct access to the Rsbuild configuration. Y
1212

1313
This function receives the current Rsbuild config and an options object, and should return the modified config.
1414

15+
:::warning Use `mergeRsbuildConfig` for modifications
16+
Always use [`mergeRsbuildConfig`](https://rsbuild.rs/api/javascript-api/core#mergersbuildconfig) from `@rsbuild/core` to modify the config. Directly mutating properties like `config.tools.rspack = {...}` may not work because internal configs can be arrays of functions/objects, and direct assignment may be silently ignored.
17+
:::
18+
1519
```ts twoslash title=".storybook/main.ts"
1620
// @noErrors
1721
import type { StorybookConfig } from 'storybook-react-rsbuild';
22+
import { mergeRsbuildConfig } from '@rsbuild/core';
1823

1924
const config: StorybookConfig = {
2025
framework: 'storybook-react-rsbuild',
@@ -23,16 +28,21 @@ const config: StorybookConfig = {
2328
async rsbuildFinal(config, { configType }) {
2429
// Customize Rsbuild config here
2530
if (configType === 'PRODUCTION') {
26-
config.performance = {
27-
...config.performance,
28-
removeConsole: true,
29-
};
31+
return mergeRsbuildConfig(config, {
32+
performance: {
33+
removeConsole: true,
34+
},
35+
});
3036
}
3137

32-
// You can modify config.source, config.tools, etc.
33-
// config.source = { ...config.source, alias: { ... } };
34-
35-
return config;
38+
// Example: add source alias
39+
return mergeRsbuildConfig(config, {
40+
source: {
41+
alias: {
42+
'@components': './src/components',
43+
},
44+
},
45+
});
3646
},
3747
};
3848

website/docs/guide/faq.mdx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44

55
The default Vite and webpack builders emit relative asset paths, but Rsbuild recommends absolute paths for deployment (see the [asset prefix tips](https://rsbuild.rs/config/output/asset-prefix#path-types:~:text=on%20file%20location.-,TIP,-It%27s%20not%20recommended)). To deploy Storybook at `https://example.com/sb-path`, set [`output.assetPrefix`](https://rsbuild.rs/config/output/asset-prefix).
66

7-
```diff
7+
```js
8+
import { mergeRsbuildConfig } from '@rsbuild/core'
9+
810
const config: StorybookConfig = {
911
// --snip--
1012
rsbuildFinal: (config) => {
11-
+ config.output ??= {}
12-
+ config.output.assetPrefix = '/sb-path/'
13-
return config
13+
return mergeRsbuildConfig(config, {
14+
output: {
15+
assetPrefix: '/sb-path/',
16+
},
17+
})
1418
},
1519
// --snip--
1620
}

website/docs/guide/framework/react-native-web.mdx

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -339,27 +339,31 @@ pnpm add -D @babel/core babel-loader @babel/preset-flow @babel/preset-react
339339
2. Add a custom Rsbuild configuration in `.storybook/main.ts`:
340340

341341
```ts
342+
import { mergeRsbuildConfig } from '@rsbuild/core'
343+
342344
const config: StorybookConfig = {
343345
framework: {
344346
name: 'storybook-react-native-web-rsbuild',
345347
options: {},
346348
},
347349
rsbuildFinal: async (config) => {
348-
config.tools ??= {}
349-
config.tools.bundlerChain = (chain) => {
350-
// Add babel-loader for packages with Flow syntax
351-
chain.module
352-
.rule('flow')
353-
.test(/\.jsx?$/)
354-
.include.add(/node_modules\/(react-native|@react-native)/)
355-
.end()
356-
.use('babel')
357-
.loader('babel-loader')
358-
.options({
359-
presets: ['@babel/preset-flow', '@babel/preset-react'],
360-
})
361-
}
362-
return config
350+
return mergeRsbuildConfig(config, {
351+
tools: {
352+
bundlerChain: (chain) => {
353+
// Add babel-loader for packages with Flow syntax
354+
chain.module
355+
.rule('flow')
356+
.test(/\.jsx?$/)
357+
.include.add(/node_modules\/(react-native|@react-native)/)
358+
.end()
359+
.use('babel')
360+
.loader('babel-loader')
361+
.options({
362+
presets: ['@babel/preset-flow', '@babel/preset-react'],
363+
})
364+
},
365+
},
366+
})
363367
},
364368
}
365369
```

website/docs/guide/framework/react.mdx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,15 @@ Install the framework package to enable React support.
3030

3131
```js
3232
import { StorybookConfig } from 'storybook-react-rsbuild'
33+
import { mergeRsbuildConfig } from '@rsbuild/core'
3334

3435
const config: StorybookConfig = {
3536
framework: 'storybook-react-rsbuild',
3637
rsbuildFinal: (config) => {
3738
// Customize the final Rsbuild config here
38-
return config
39+
return mergeRsbuildConfig(config, {
40+
// your custom config
41+
})
3942
},
4043
}
4144

website/docs/guide/framework/vanilla.mdx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,15 @@ Install the HTML framework package to enable vanilla stories.
2929

3030
```js
3131
import { StorybookConfig } from 'storybook-html-rsbuild'
32+
import { mergeRsbuildConfig } from '@rsbuild/core'
3233

3334
const config: StorybookConfig = {
3435
framework: 'storybook-html-rsbuild',
3536
rsbuildFinal: (config) => {
3637
// Customize the final Rsbuild config here
37-
return config
38+
return mergeRsbuildConfig(config, {
39+
// your custom config
40+
})
3841
},
3942
}
4043

website/docs/guide/framework/vue.mdx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,15 @@ Install the Vue framework package to enable Storybook support.
3030

3131
```js
3232
import { StorybookConfig } from 'storybook-vue3-rsbuild'
33+
import { mergeRsbuildConfig } from '@rsbuild/core'
3334

3435
const config: StorybookConfig = {
3536
framework: 'storybook-vue3-rsbuild',
3637
rsbuildFinal: (config) => {
3738
// Customize the final Rsbuild config here
38-
return config
39+
return mergeRsbuildConfig(config, {
40+
// your custom config
41+
})
3942
},
4043
}
4144

0 commit comments

Comments
 (0)