Skip to content

Commit eb47053

Browse files
committed
feat: use @rsbuild/plugin-babel
1 parent 068a3bf commit eb47053

File tree

17 files changed

+522
-503
lines changed

17 files changed

+522
-503
lines changed

.changeset/plenty-baboons-tan.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

.changeset/sour-weeks-wait.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
11
---
22
"@lynx-js/react-rsbuild-plugin": patch
3-
"@lynx-js/react-alias-rsbuild-plugin": patch
43
---
54

6-
Support [React Compiler](https://react.dev/learn/react-compiler) for ReactLynx, enable it by set `experimental_enableReactCompiler` to `true` in `lynx.config.js`:
5+
Add `react-compiler-runtime` to `resolve.dedupe`.
6+
7+
With this change you can setup [React Compiler](https://react.dev/learn/react-compiler) for ReactLynx by `pluginBabel`:
78

89
```js
10+
import { pluginBabel } from '@rsbuild/plugin-babel';
11+
912
export default defineConfig({
1013
plugins: [
11-
pluginReactLynx({
12-
experimental_enableReactCompiler: true,
14+
pluginBabel({
15+
include: /\.(?:jsx|tsx)$/,
16+
babelLoaderOptions(opts) {
17+
opts.plugins?.unshift([
18+
'babel-plugin-react-compiler',
19+
// See https://react.dev/reference/react-compiler/configuration for config
20+
{
21+
// ReactLynx only supports target to version 17
22+
target: '17',
23+
},
24+
]);
25+
},
1326
}),
1427
],
1528
});

examples/react-compiler/lynx.config.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { pluginBabel } from '@rsbuild/plugin-babel';
2+
13
import { pluginQRCode } from '@lynx-js/qrcode-rsbuild-plugin';
24
import { pluginReactLynx } from '@lynx-js/react-rsbuild-plugin';
35
import { defineConfig } from '@lynx-js/rspeedy';
@@ -6,8 +8,19 @@ const enableBundleAnalysis = !!process.env['RSPEEDY_BUNDLE_ANALYSIS'];
68

79
export default defineConfig({
810
plugins: [
9-
pluginReactLynx({
10-
experimental_enableReactCompiler: true,
11+
pluginReactLynx(),
12+
pluginBabel({
13+
include: /\.(?:jsx|tsx)$/,
14+
babelLoaderOptions(opts) {
15+
opts.plugins?.unshift([
16+
'babel-plugin-react-compiler',
17+
// See https://react.dev/reference/react-compiler/configuration for config
18+
{
19+
// ReactLynx only supports target to version 17
20+
target: '17',
21+
},
22+
]);
23+
},
1124
}),
1225
pluginQRCode({
1326
schema(url) {

examples/react-compiler/package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,11 @@
1212
"react-compiler-runtime": "0.0.0-experimental-0566679-20250709"
1313
},
1414
"devDependencies": {
15-
"@babel/core": "^7.28.0",
16-
"@babel/plugin-syntax-jsx": "^7.27.1",
17-
"@babel/plugin-syntax-typescript": "^7.27.1",
1815
"@lynx-js/qrcode-rsbuild-plugin": "workspace:*",
1916
"@lynx-js/react-rsbuild-plugin": "workspace:*",
2017
"@lynx-js/rspeedy": "workspace:*",
2118
"@lynx-js/types": "3.4.11",
22-
"@swc/react-compiler": "^1.13.1",
19+
"@rsbuild/plugin-babel": "^1.0.6",
2320
"@types/react": "^18.3.25",
2421
"babel-plugin-react-compiler": "0.0.0-experimental-0566679-20250709"
2522
}

packages/react/testing-library/src/pure.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ export function render(
7979
container: lynxTestingEnv.mainThread.elementTree.root,
8080
unmount: cleanup,
8181
rerender: (rerenderUi) => {
82-
act(() => {});
8382
// Intentionally do not return anything to avoid unnecessarily complicating the API.
8483
// folks can use all the same utilities we return in the first place that are bound to
8584
// the container
8685
render(rerenderUi, {
86+
queries,
8787
wrapper: WrapperComponent,
8888
enableMainThread,
8989
enableBackgroundThread,

packages/react/testing-library/src/vitest.config.js

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,16 @@ export const createVitestConfig = async (options) => {
8686
];
8787

8888
function transformReactCompilerPlugin() {
89-
let rootContext, compilerDeps, isReactCompilerRequired, babel;
89+
let rootContext, compilerDeps, babel;
9090

9191
function resolveCompilerDeps(rootContext) {
9292
const missingBabelPackages = [];
9393
const [
94-
swcReactCompilerPath,
9594
babelPath,
9695
babelPluginReactCompilerPath,
9796
babelPluginSyntaxJsxPath,
9897
babelPluginSyntaxTypescriptPath,
9998
] = [
100-
'@swc/react-compiler',
10199
'@babel/core',
102100
'babel-plugin-react-compiler',
103101
'@babel/plugin-syntax-jsx',
@@ -123,7 +121,6 @@ export const createVitestConfig = async (options) => {
123121
}
124122

125123
return {
126-
swcReactCompilerPath,
127124
babelPath,
128125
babelPluginReactCompilerPath,
129126
babelPluginSyntaxJsxPath,
@@ -159,47 +156,43 @@ export const createVitestConfig = async (options) => {
159156
config.test.alias.push(...reactCompilerRuntimeAlias);
160157

161158
compilerDeps = resolveCompilerDeps(rootContext);
162-
const { swcReactCompilerPath, babelPath } = compilerDeps;
163-
isReactCompilerRequired = require(swcReactCompilerPath).isReactCompilerRequired;
159+
const { babelPath } = compilerDeps;
164160
babel = require(babelPath);
165161
},
166162
async transform(sourceText, sourcePath) {
167163
if (/\.(?:jsx|tsx)$/.test(sourcePath)) {
168-
const needReactCompiler = await isReactCompilerRequired(Buffer.from(sourceText));
169-
if (needReactCompiler) {
170-
const { babelPluginReactCompilerPath, babelPluginSyntaxJsxPath, babelPluginSyntaxTypescriptPath } =
171-
compilerDeps;
172-
const isTSX = sourcePath.endsWith('.tsx');
164+
const { babelPluginReactCompilerPath, babelPluginSyntaxJsxPath, babelPluginSyntaxTypescriptPath } =
165+
compilerDeps;
166+
const isTSX = sourcePath.endsWith('.tsx');
173167

174-
try {
175-
const result = babel.transformSync(sourceText, {
176-
plugins: [
177-
// We use '17' to make `babel-plugin-react-compiler` compiles our code
178-
// to use `react-compiler-runtime` instead of `react/compiler-runtime`
179-
// for the `useMemoCache` hook
180-
[babelPluginReactCompilerPath, { target: '17' }],
181-
babelPluginSyntaxJsxPath,
182-
isTSX ? [babelPluginSyntaxTypescriptPath, { isTSX: true }] : null,
183-
].filter(Boolean),
184-
filename: sourcePath,
185-
ast: false,
186-
sourceMaps: true,
187-
});
188-
if (result?.code != null && result?.map != null) {
189-
return {
190-
code: result.code,
191-
map: result.map,
192-
};
193-
} else {
194-
this.error(
195-
`babel-plugin-react-compiler transform failed for ${sourcePath}: ${
196-
result ? 'missing code or map' : 'no result'
197-
}`,
198-
);
199-
}
200-
} catch (e) {
201-
this.error(e);
168+
try {
169+
const result = babel.transformSync(sourceText, {
170+
plugins: [
171+
// We use '17' to make `babel-plugin-react-compiler` compiles our code
172+
// to use `react-compiler-runtime` instead of `react/compiler-runtime`
173+
// for the `useMemoCache` hook
174+
[babelPluginReactCompilerPath, { target: '17' }],
175+
babelPluginSyntaxJsxPath,
176+
isTSX ? [babelPluginSyntaxTypescriptPath, { isTSX: true }] : null,
177+
].filter(Boolean),
178+
filename: sourcePath,
179+
ast: false,
180+
sourceMaps: true,
181+
});
182+
if (result?.code != null && result?.map != null) {
183+
return {
184+
code: result.code,
185+
map: result.map,
186+
};
187+
} else {
188+
this.error(
189+
`babel-plugin-react-compiler transform failed for ${sourcePath}: ${
190+
result ? 'missing code or map' : 'no result'
191+
}`,
192+
);
202193
}
194+
} catch (e) {
195+
this.error(e);
203196
}
204197
}
205198

packages/rspeedy/plugin-react-alias/etc/react-alias-rsbuild-plugin.api.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ export function createLazyResolver(directory: string, conditionNames: string[]):
1515
//
1616
// @public (undocumented)
1717
export interface Options {
18-
// (undocumented)
19-
experimental_enableReactCompiler?: boolean | undefined;
2018
// (undocumented)
2119
LAYERS: {
2220
MAIN_THREAD: string;

packages/rspeedy/plugin-react-alias/src/index.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,12 @@ export interface Options {
1717
}
1818

1919
rootPath?: string | undefined
20-
21-
experimental_enableReactCompiler?: boolean | undefined
2220
}
2321

2422
const S_PLUGIN_REACT_ALIAS = Symbol.for('@lynx-js/plugin-react-alias')
2523

2624
export function pluginReactAlias(options: Options): RsbuildPlugin {
27-
const { LAYERS, lazy, rootPath, experimental_enableReactCompiler = false } =
28-
options ?? {}
25+
const { LAYERS, lazy, rootPath } = options ?? {}
2926

3027
return {
3128
name: 'lynx:react-alias',
@@ -171,14 +168,6 @@ export function pluginReactAlias(options: Options): RsbuildPlugin {
171168
'@lynx-js/react$',
172169
reactLepus.background,
173170
)
174-
if (experimental_enableReactCompiler) {
175-
chain.resolve.alias.set(
176-
'react-compiler-runtime$',
177-
require.resolve('react-compiler-runtime', {
178-
paths: [api.context.rootPath],
179-
}),
180-
)
181-
}
182171

183172
if (reactCompat) {
184173
chain

packages/rspeedy/plugin-react/etc/react-rsbuild-plugin.api.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ export interface PluginReactLynxOptions {
6666
enableRemoveCSSScope?: boolean | undefined;
6767
enableSSR?: boolean;
6868
engineVersion?: string;
69-
experimental_enableReactCompiler?: boolean;
7069
// @alpha
7170
experimental_isLazyBundle?: boolean;
7271
extractStr?: Partial<ExtractStrConfig> | boolean;

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

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import { applyEntry } from './entry.js'
2828
import { applyGenerator } from './generator.js'
2929
import { applyLazy } from './lazy.js'
3030
import { applyLoaders } from './loaders.js'
31-
import { applyReactCompiler } from './reactCompiler.js'
3231
import { applyRefresh } from './refresh.js'
3332
import { applySplitChunksRule } from './splitChunks.js'
3433
import { applySWC } from './swc.js'
@@ -264,13 +263,6 @@ export interface PluginReactLynxOptions {
264263
* @alpha
265264
*/
266265
experimental_isLazyBundle?: boolean
267-
268-
/**
269-
* Enable React Compiler for this build.
270-
*
271-
* {@link https://react.dev/learn/react-compiler}
272-
*/
273-
experimental_enableReactCompiler?: boolean
274266
}
275267

276268
/**
@@ -318,7 +310,6 @@ export function pluginReactLynx(
318310
extractStr: false,
319311

320312
experimental_isLazyBundle: false,
321-
experimental_enableReactCompiler: false,
322313
}
323314
const resolvedOptions = Object.assign(defaultOptions, userOptions, {
324315
// Use `engineVersion` to override the default values
@@ -329,8 +320,6 @@ export function pluginReactLynx(
329320
return [
330321
pluginReactAlias({
331322
lazy: resolvedOptions.experimental_isLazyBundle,
332-
experimental_enableReactCompiler:
333-
resolvedOptions.experimental_enableReactCompiler,
334323
LAYERS,
335324
}),
336325
{
@@ -378,15 +367,18 @@ export function pluginReactLynx(
378367
},
379368
}, config)
380369

370+
config = mergeRsbuildConfig({
371+
resolve: {
372+
dedupe: ['react-compiler-runtime'],
373+
},
374+
}, config)
375+
381376
return config
382377
})
383378

384379
if (resolvedOptions.experimental_isLazyBundle) {
385380
applyLazy(api)
386381
}
387-
if (resolvedOptions.experimental_enableReactCompiler) {
388-
applyReactCompiler(api)
389-
}
390382

391383
const rspeedyAPIs = api.useExposed<ExposedAPI>(
392384
Symbol.for('rspeedy.api'),

0 commit comments

Comments
 (0)