Skip to content

Commit

Permalink
feat: use @vitejs/plugin-react-swc as default
Browse files Browse the repository at this point in the history
  • Loading branch information
geekact committed Dec 14, 2022
1 parent 38e6ff6 commit ebeeea9
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 3 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,17 @@ export default defineConfig({

# react

React 项目基础插件。具体配置请查看官方文档 [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/tree/main/packages/plugin-react)
React 项目基础插件。

默认使用 [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc),其在开发环境使用 swc 替换 babel,速度会快好几倍。如果要使用基于 babel 的 [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/tree/main/packages/plugin-react),则可以这么配置

```typescript
export default defineConfig({
react: {
swc: false,
},
});
```

# legacy

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@vitejs/plugin-basic-ssl": "^1.0.1",
"@vitejs/plugin-legacy": "^3.0.1",
"@vitejs/plugin-react": "^3.0.0",
"@vitejs/plugin-react-swc": "^3.0.0",
"less": "^4.1.3",
"sass": "^1.56.2",
"terser": "^5.16.1",
Expand Down
119 changes: 119 additions & 0 deletions pnpm-lock.yaml

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

18 changes: 16 additions & 2 deletions src/handler/react.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
import react, { Options as ReactOptions } from '@vitejs/plugin-react';
import reactSWC from '@vitejs/plugin-react-swc';
import { Config } from '../vite';

export interface OverrideReact {
react?: ReactOptions;
/**
* 默认使用插件 @vitejs/plugin-react-swc,如果要使用兼容性更强的 @vitejs/plugin-react,则设置 swc=false
*/
react?:
| (ReactOptions & { swc: false })
| {
swc?: true;
};
}

export const handleReact = (config: Config) => {
config.plugins ||= [];
config.plugins.push(react(config.react));
const { swc, ...reactOptions } = config.react || {};

if (swc !== false) {
config.plugins.push(reactSWC());
} else {
config.plugins.push(react(reactOptions));
}
};

0 comments on commit ebeeea9

Please sign in to comment.