Skip to content

Commit 9c8ff9a

Browse files
committed
feat(rsbuild-plugin): apply the build engine for rslib
`rslib` was left out of the engine entirely, so a library build read none of the Lynx config and had to mirror the parts it needed by hand. Apply the engine there too, and skip the plugins that shape or serve a bundle: `rslib` assembles its own, and `rstest` has none.
1 parent 395095a commit 9c8ff9a

5 files changed

Lines changed: 52 additions & 22 deletions

File tree

.changeset/rslib-engine.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@lynx-js/react-rsbuild-plugin": patch
3+
"@lynx-js/rsbuild-plugin": patch
4+
---
5+
6+
Apply the Lynx build engine for `rslib` as well, so a library build reads the Lynx config. The plugins that shape or serve a bundle stay off, since `rslib` assembles its own.

packages/rspeedy/plugin-lynx/src/index.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,26 +74,40 @@ export function isPluginLynxRegistered(
7474
export function pluginLynx(
7575
options: LynxPluginOptions = {},
7676
): RsbuildPlugin[] {
77-
return [
78-
{
79-
name: PLUGIN_LYNX_NAME,
80-
setup() {
81-
// A marker, so its presence can be detected. It has no behavior.
82-
},
83-
},
84-
pluginConfig(options),
77+
// `rslib` assembles the bundle itself and `rstest` has none.
78+
const bundlePlugins = [
8579
pluginChunkLoading(),
80+
pluginLynxDebugMetadata(),
81+
pluginOutput(),
82+
pluginResolve(),
83+
pluginSwc(),
8684
pluginCssMinimizer(),
8785
pluginDev(),
88-
pluginLynxDebugMetadata(),
8986
pluginMinify(),
9087
pluginOptimization(),
91-
pluginOutput(),
92-
pluginResolve(),
9388
pluginServer(),
9489
pluginSourcemap(),
95-
pluginSwc(),
9690
pluginTarget(),
91+
].map((plugin): RsbuildPlugin => ({
92+
...plugin,
93+
setup(api) {
94+
const { callerName } = api.context
95+
if (callerName === 'rslib' || callerName === 'rstest') {
96+
return
97+
}
98+
return plugin.setup(api)
99+
},
100+
}))
101+
102+
return [
103+
{
104+
name: PLUGIN_LYNX_NAME,
105+
setup() {
106+
// A marker, so its presence can be detected. It has no behavior.
107+
},
108+
},
109+
pluginConfig(options),
97110
pluginTemplate(),
111+
...bundlePlugins,
98112
]
99113
}

packages/rspeedy/plugin-react/src/autoLynx.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,14 @@ import type { RsbuildContext, RsbuildPlugin } from '@rsbuild/core'
66

77
import { isPluginLynxRegistered, pluginLynx } from '@lynx-js/rsbuild-plugin'
88

9-
// Rspeedy applies `pluginLynx` itself. With plain Rsbuild nothing does, so the
10-
// build engine is applied here.
9+
// Rspeedy applies `pluginLynx` itself. Nothing does with plain Rsbuild or
10+
// `rslib`, so the engine is applied here.
1111
export function pluginAutoLynx(): RsbuildPlugin {
1212
return {
1313
name: 'lynx:react:auto-lynx',
1414
async setup(api) {
15-
// The callers that do not emit a Lynx template do not want the engine
16-
// either. See the same condition in `applyEntry`.
17-
const { callerName } = api.context
18-
if (callerName === 'rslib' || callerName === 'rstest') {
15+
// A test run has no bundle for the engine to configure.
16+
if (api.context.callerName === 'rstest') {
1917
return
2018
}
2119

packages/rspeedy/plugin-react/src/entry.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,6 @@ export function applyEntry(
8787
const emitTemplate = api.context.callerName !== 'rslib'
8888
&& api.context.callerName !== 'rstest'
8989
if (emitTemplate) {
90-
// `pluginAutoLynx` applies the engine for the same callers, so the config
91-
// is there whenever a template is emitted.
9290
if (!lynxConfig) {
9391
throw new Error(
9492
'No Lynx config exposed. `pluginLynx` has to be applied for the Lynx build engine to be configured.',

packages/rspeedy/plugin-react/test/auto-lynx.test.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ describe('pluginAutoLynx', () => {
165165
}
166166
})
167167

168-
test('does not apply the engine for the rslib caller', async () => {
168+
test('applies the engine for the rslib caller, minus the bundle it does not own', async () => {
169+
let lynxConfig: unknown
169170
const rsbuild = await createRsbuild({
170171
callerName: 'rslib',
171172
// eslint-disable-next-line n/no-unsupported-features/node-builtins
@@ -174,12 +175,25 @@ describe('pluginAutoLynx', () => {
174175
mode: 'production',
175176
environments: { lynx: {} },
176177
source: { entry: { main: './fixtures/basic.tsx' } },
177-
plugins: [pluginReactLynx()],
178+
plugins: [
179+
pluginReactLynx(),
180+
{
181+
name: 'test:read-lynx-config',
182+
setup(api) {
183+
api.modifyBundlerChain(() => {
184+
lynxConfig = api.useExposed(
185+
Symbol.for('@lynx-js/rsbuild-plugin:config'),
186+
)
187+
})
188+
},
189+
} satisfies RsbuildPlugin,
190+
],
178191
},
179192
})
180193

181194
const [config] = await rsbuild.initConfigs()
182195

196+
expect(lynxConfig).toBeDefined()
183197
expect(
184198
config?.plugins?.some(plugin =>
185199
plugin?.constructor.name === 'LynxTemplatePlugin'

0 commit comments

Comments
 (0)