vite plugin react swc
v3.7.1
使用SWC加速您的Vite Dev服务器
npm i -D @vitejs/plugin-react-swc
import { defineConfig } from "vite" ;
import react from "@vitejs/plugin-react-swc" ;
export default defineConfig ( {
plugins : [ react ( ) ] ,
} ) ;
该插件的选项有限,可以实现良好的性能并成为不可知论者。这是影响运行时行为的非配置选项的列表:
jsx runtime
始终是automatic
target
并默认为es2020
(请参阅devTarget
)控制JSX工厂的进口位置。
@default
“ React”
react ( { jsxImportSource : "@emotion/react" } ) ;
启用打字稿装饰器。需要在TSCONFIG中experimentalDecorators
。
@default
false
react ( { tsDecorators : true } ) ;
使用SWC插件。在构建时间启用SWC。
react ( { plugins : [ [ "@swc/plugin-styled-components" , { } ] ] } ) ;
在开发人员中设置SWC的目标。例如,这可以避免减少转移的私人类方法。
有关生产目标,请参见https://vitejs.dev/config/build-options.html#build-target。
@default
“ ES2020”
react ( { devTarget : "es2022" } ) ;
覆盖默认值包括列表(.ts,.tsx,.mts,.jsx,.mdx)。
这需要重新定义要包含的任何文件的配置(TS,MDX,...)。
如果您想在编译JS上触发快速刷新,请使用jsx: true
。如果需要,应由该功能处理Node_Modules的排除。使用此选项在.js
文件中使用JSX是高度灰心的,并且可以在任何以后的版本中删除。
react ( {
parserConfig ( id ) {
if ( id . endsWith ( ".res" ) ) return { syntax : "ecmascript" , jsx : true } ;
if ( id . endsWith ( ".ts" ) ) return { syntax : "typescript" , tsx : false } ;
} ,
} ) ;
对于React刷新正确工作,您的文件应仅导出React组件。我读过的最好的解释是盖茨比文档中的解释。
如果发现出口不兼容的变化,则该模块将无效,HMR将传播。为了使与组件并肩导出简单常数变得更容易,仅当模块变化时,该模块才会无效。
您可以捕获错误,并使用此ESLINT规则获得更详细的警告。
vite-plugin-swc-react-refresh
迁移插件的先前版本的文档可在V2分支中获得
要迁移,请参阅此更改