Skip to content

Commit 0264e5a

Browse files
committed
fix(builder-rsbuild): pass through lazyCompilation options
- align lazyCompilation handling with Rsbuild dev config typing - default lazyCompilation to entries:false when option is unset - pass through lazyCompilation booleans/objects to virtual module mapping - expand iframe config tests for lazyCompilation scenarios
1 parent 652f3bf commit 0264e5a

File tree

6 files changed

+71
-15
lines changed

6 files changed

+71
-15
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.10/schema.json",
2+
"$schema": "https://biomejs.dev/schemas/2.3.11/schema.json",
33
"assist": {
44
"includes": ["**", "!**/*.vue"],
55
"actions": { "source": { "organizeImports": "on" } }

packages/builder-rsbuild/src/preview/iframe-rsbuild.config.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,13 @@ export default async (
137137
const builderOptions = await getBuilderOptions<BuilderOptions>(options)
138138
const cacheConfig = builderOptions.fsCache ? true : undefined
139139

140-
const lazyCompilationConfig: Rspack.LazyCompilationOptions | undefined =
141-
builderOptions.lazyCompilation && !isProd
140+
const lazyCompilationConfig: Rspack.Configuration['lazyCompilation'] = !isProd
141+
? builderOptions.lazyCompilation === undefined
142142
? {
143143
entries: false,
144144
}
145-
: undefined
145+
: builderOptions.lazyCompilation
146+
: undefined
146147

147148
if (!template) {
148149
throw new Error(dedent`

packages/builder-rsbuild/src/preview/virtual-module-mapping.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ export const getVirtualModules = async (options: Options) => {
4646
const storiesFilename = 'storybook-stories.js'
4747
const storiesPath = resolve(join(workingDir, storiesFilename))
4848

49-
const needPipelinedImport = !!builderOptions.lazyCompilation && !isProd
49+
const needPipelinedImport =
50+
builderOptions.lazyCompilation !== false && !isProd
5051
virtualModules[storiesPath] = toImportFn(stories, {
5152
needPipelinedImport,
5253
})

packages/builder-rsbuild/src/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { RsbuildConfig } from '@rsbuild/core'
1+
import type { DevConfig, RsbuildConfig } from '@rsbuild/core'
22
import type { PluginTypeCheckerOptions } from '@rsbuild/plugin-type-check'
33
import type {
44
Builder,
@@ -42,9 +42,9 @@ export type BuilderOptions = {
4242
*/
4343
rsbuildConfigPath?: string
4444
/**
45-
* Enable Rspack's lazy compilation (experimental).
45+
* Pass-through Rsbuild `dev.lazyCompilation` options.
4646
*/
47-
lazyCompilation?: NonNullable<RsbuildConfig['performance']>['buildCache']
47+
lazyCompilation?: DevConfig['lazyCompilation']
4848
/**
4949
* Enable Rspack's [persistent cache(experimental)](https://rspack.dev/config/experiments#experimentscache).
5050
* We continue to use the name `fsCache` here to maintain better compatibility with the webpack builder.

packages/builder-rsbuild/tests/preview/iframe-rsbuild.config.test.ts

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { resolve } from 'node:path'
22
import { fileURLToPath } from 'node:url'
3+
import type { Rspack } from '@rsbuild/core'
34
import { describe, expect, it, vi } from 'vitest'
45
import type { RsbuildBuilderOptions } from '../../src/preview/iframe-rsbuild.config'
56
import createIframeRsbuildConfig from '../../src/preview/iframe-rsbuild.config'
@@ -16,19 +17,25 @@ const storiesConfig = [
1617
},
1718
]
1819

19-
const createOptions = () => {
20+
type LazyCompilationOption = Rspack.Configuration['lazyCompilation']
21+
22+
const createOptions = (
23+
lazyCompilation: LazyCompilationOption | 'unset' = false,
24+
) => {
25+
const builderCoreOptions: Record<string, unknown> = {
26+
rsbuildConfigPath: fixtureRsbuildConfig,
27+
addonDocs: {},
28+
fsCache: false,
29+
...(lazyCompilation === 'unset' ? {} : { lazyCompilation }),
30+
}
31+
2032
const presetValues = new Map<string, unknown>([
2133
[
2234
'core',
2335
{
2436
builder: {
2537
name: 'storybook-builder-rsbuild',
26-
options: {
27-
rsbuildConfigPath: fixtureRsbuildConfig,
28-
addonDocs: {},
29-
fsCache: false,
30-
lazyCompilation: false,
31-
},
38+
options: builderCoreOptions,
3239
},
3340
},
3441
],
@@ -108,4 +115,49 @@ describe('iframe-rsbuild.config', () => {
108115
main: [storybookEntries[0], expectedDynamicEntry],
109116
})
110117
})
118+
119+
const runRspackTool = async (
120+
lazyCompilation: LazyCompilationOption | 'unset',
121+
) => {
122+
const { options } = createOptions(lazyCompilation)
123+
const config = await createIframeRsbuildConfig(
124+
options as RsbuildBuilderOptions,
125+
)
126+
127+
const rspackTool = config.tools?.rspack
128+
expect(typeof rspackTool).toBe('function')
129+
130+
const baseConfig = {} as any
131+
132+
return (rspackTool as any)(baseConfig, {
133+
addRules: vi.fn(),
134+
rspack: {
135+
experiments: {
136+
VirtualModulesPlugin: class VirtualModulesPlugin {},
137+
},
138+
ProvidePlugin: class ProvidePlugin {},
139+
},
140+
mergeConfig: (c: any) => c,
141+
}) as any
142+
}
143+
144+
it('uses entries:false when lazyCompilation is unset', async () => {
145+
const rspackConfig = await runRspackTool('unset')
146+
expect(rspackConfig.lazyCompilation).toEqual({ entries: false })
147+
})
148+
149+
it('disables lazyCompilation when set to false', async () => {
150+
const rspackConfig = await runRspackTool(false)
151+
expect(rspackConfig.lazyCompilation).toBe(false)
152+
})
153+
154+
it('passes through lazyCompilation when set to true', async () => {
155+
const rspackConfig = await runRspackTool(true)
156+
expect(rspackConfig.lazyCompilation).toBe(true)
157+
})
158+
159+
it('passes through lazyCompilation options object', async () => {
160+
const rspackConfig = await runRspackTool({ entries: true })
161+
expect(rspackConfig.lazyCompilation).toEqual({ entries: true })
162+
})
111163
})

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.

0 commit comments

Comments
 (0)