Skip to content

Commit 242fb70

Browse files
committed
update
1 parent 487a4aa commit 242fb70

File tree

6 files changed

+36
-16
lines changed

6 files changed

+36
-16
lines changed

src/getBabelCommonConfig.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@ import fs from 'fs-extra';
22
import type { PluginItem, TransformOptions } from '@babel/core';
33
import { resolve, isThereHaveBrowserslistConfig } from './utils/projectHelper';
44

5+
interface BabelConfigOptions {
6+
enabledReactCompiler?: boolean;
7+
}
8+
59
interface BabelConfig extends TransformOptions {
610
cacheDirectory?: boolean;
711
}
812

9-
export default function getBabelCommonConfig(modules?: boolean): BabelConfig {
13+
function getBabelCommonConfig(modules?: boolean, options: BabelConfigOptions = {}): BabelConfig {
1014
const plugins: PluginItem[] = [
11-
[
12-
resolve('babel-plugin-react-compiler'),
13-
{
14-
target: '18', // 最低支持的版本是 React 18
15-
},
16-
],
1715
[
1816
resolve('@babel/plugin-transform-typescript'),
1917
{
@@ -35,6 +33,14 @@ export default function getBabelCommonConfig(modules?: boolean): BabelConfig {
3533
resolve('babel-plugin-transform-dev-warning'),
3634
resolve('@babel/plugin-transform-private-methods'),
3735
];
36+
if (options.enabledReactCompiler === true) {
37+
plugins.unshift([
38+
resolve('babel-plugin-react-compiler'),
39+
{
40+
target: '18', // 最低支持的版本是 React 18
41+
},
42+
]);
43+
}
3844
return {
3945
presets: [
4046
resolve('@babel/preset-react'),
@@ -53,3 +59,5 @@ export default function getBabelCommonConfig(modules?: boolean): BabelConfig {
5359
plugins,
5460
};
5561
}
62+
63+
export default getBabelCommonConfig;

src/getWebpackConfig.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,14 @@ interface GetWebpackConfigFunction {
3535
imageOptions: typeof imageOptions;
3636
}
3737

38+
const libDir = process.env.LIB_DIR || 'components';
39+
3840
const getWebpackConfig: GetWebpackConfigFunction = (modules?: boolean): Configuration[] => {
3941
const pkg: PackageJson = readJsonSync(getProjectPath('package.json'));
40-
const babelConfig = getBabelCommonConfig(modules || false);
42+
43+
const babelConfig = getBabelCommonConfig(modules || false, {
44+
enabledReactCompiler: libDir === 'dist',
45+
});
4146

4247
babelConfig.plugins.push([
4348
resolve('babel-plugin-import'),

src/gulpfile.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import replaceLib from './replaceLib';
1818
import checkDiff from './lint/checkDiff';
1919
import apiCollection from './apiCollection';
2020
import sortApiTable from './sortApiTable';
21+
import type { ICompileStream } from 'gulp-typescript/release/project';
2122

2223
const argv = minimist(process.argv.slice(2));
2324

@@ -143,8 +144,10 @@ gulp.task(
143144
})
144145
);
145146

146-
function babelify(js, modules) {
147-
const babelConfig = getBabelCommonConfig(modules);
147+
function babelify(js: ICompileStream['js'], modules: boolean) {
148+
const babelConfig = getBabelCommonConfig(modules, {
149+
enabledReactCompiler: libDir === 'dist',
150+
});
148151
delete babelConfig.cacheDirectory;
149152
if (modules === false) {
150153
babelConfig.plugins.push(replaceLib);

src/jest/codePreprocessor.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ const preprocessor: Preprocessor = {
3636
canInstrument: true,
3737
process(src, filePath, config, transformOptions) {
3838
global.__clearBabelAntdPlugin && global.__clearBabelAntdPlugin(); // eslint-disable-line
39-
const babelConfig = getBabelCommonConfig();
39+
const babelConfig = getBabelCommonConfig(undefined, {
40+
enabledReactCompiler: libDir === 'dist',
41+
});
4042
babelConfig.plugins = [...(babelConfig.plugins || [])];
4143

4244
if (/\/demo\//.test(filePath)) {

src/jest/demoPreprocessor.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,10 @@ function transform(src: string, pathFilename: string): ProcessResult {
120120

121121
global.__clearBabelAntdPlugin && global.__clearBabelAntdPlugin(); // eslint-disable-line
122122

123-
const babelConfig = getBabelCommonConfig();
123+
const babelConfig = getBabelCommonConfig(undefined, {
124+
enabledReactCompiler: libDir === 'dist',
125+
});
126+
124127
babelConfig.plugins = [...babelConfig.plugins];
125128
babelConfig.plugins.push(createDemo);
126129

src/jest/rewriteSource.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import type { NodePath } from '@babel/traverse';
2-
import type { ImportDeclaration } from '@babel/types';
1+
import * as babel from '@babel/core';
32

43
function rewriteSource(
5-
t: typeof import('@babel/types'),
6-
path: NodePath<ImportDeclaration>,
4+
t: typeof babel.types,
5+
path: babel.NodePath<babel.types.ImportDeclaration>,
76
libDir: string
87
): void {
98
if (libDir === 'dist') {

0 commit comments

Comments
 (0)