Skip to content

Commit

Permalink
feat(babel): 添加react代码性能优化转换 (#261)
Browse files Browse the repository at this point in the history
  • Loading branch information
LeuisKen authored Feb 24, 2022
1 parent 904ff8d commit 38252e6
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
. "$(dirname "$0")/_/husky.sh"

node scripts/check-deps.mjs
lerna run --exclude-dependents --since HEAD lint
npx lerna run --exclude-dependents --since HEAD lint
2 changes: 2 additions & 0 deletions packages/config-babel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
"@babel/plugin-proposal-throw-expressions": "^7.16.5",
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
"@babel/plugin-syntax-import-meta": "^7.10.4",
"@babel/plugin-transform-react-constant-elements": "^7.16.7",
"@babel/plugin-transform-react-inline-elements": "^7.16.7",
"@babel/preset-env": "^7.16.5",
"@babel/preset-react": "^7.16.5",
"@babel/preset-typescript": "^7.16.5",
Expand Down
8 changes: 8 additions & 0 deletions packages/config-babel/src/transform.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {PluginItem, TransformOptions} from '@babel/core';
import pluginLodash from 'babel-plugin-lodash';
import pluginReactConstantElement from '@babel/plugin-transform-react-constant-elements';
import pluginReactInlineElement from '@babel/plugin-transform-react-inline-elements';
import {compact} from '@reskript/core';
import addReactDisplayName from '@reskript/babel-plugin-add-react-display-name';
import {shouldEnable} from './utils.js';
Expand Down Expand Up @@ -28,6 +30,12 @@ export default (options: BabelConfigOptionsFilled): TransformOptions => {
id: ['lodash', 'lodash-decorators'],
},
],
// https://babeljs.io/docs/en/babel-plugin-transform-react-constant-elements
// https://github.com/facebook/react/issues/3226
pluginReactConstantElement,
// https://babeljs.io/docs/en/babel-plugin-transform-react-inline-elements
// https://github.com/facebook/react/issues/3228
pluginReactInlineElement,
];

return {
Expand Down
14 changes: 14 additions & 0 deletions packages/config-babel/src/types/babel.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,20 @@ declare module '@babel/plugin-syntax-import-meta' {
export default target;
}

declare module '@babel/plugin-transform-react-constant-elements' {
import {PluginTarget} from '@babel/core';

const target: PluginTarget;
export default target;
}

declare module '@babel/plugin-transform-react-inline-elements' {
import {PluginTarget} from '@babel/core';

const target: PluginTarget;
export default target;
}

declare module 'babel-plugin-styled-components' {
import {PluginTarget} from '@babel/core';

Expand Down
33 changes: 33 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 comment on commit 38252e6

@vercel
Copy link

@vercel vercel bot commented on 38252e6 Feb 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.