Vite Plugin React SWC 使用教程

Vite Plugin React SWC 使用教程

vite-plugin-react-swcSpeed up your Vite dev server with SWC项目地址:https://gitcode.com/gh_mirrors/vi/vite-plugin-react-swc

1. 项目的目录结构及介绍

vite-plugin-react-swc/
├── scripts/
├── src/
├── .gitignore
├── CHANGELOG.md
├── LICENSE
├── README.md
├── package.json
├── playwright.config.ts
├── pnpm-lock.yaml
├── pnpm-workspace.yaml
├── tsconfig.json
  • scripts/: 包含项目的脚本文件。
  • src/: 包含项目的源代码文件。
  • .gitignore: 指定Git忽略的文件和目录。
  • CHANGELOG.md: 记录项目的更新日志。
  • LICENSE: 项目的许可证文件。
  • README.md: 项目的介绍和使用说明。
  • package.json: 项目的依赖和脚本配置。
  • playwright.config.ts: Playwright 测试配置文件。
  • pnpm-lock.yaml: pnpm 的锁定文件,确保依赖版本一致。
  • pnpm-workspace.yaml: pnpm 的工作区配置文件。
  • tsconfig.json: TypeScript 的配置文件。

2. 项目的启动文件介绍

项目的启动文件通常是 package.json 中定义的脚本。例如:

{
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "serve": "vite preview"
  }
}
  • dev: 启动开发服务器。
  • build: 构建项目。
  • serve: 预览构建后的项目。

3. 项目的配置文件介绍

tsconfig.json

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "moduleResolution": "node",
    "strict": true,
    "jsx": "preserve",
    "sourceMap": true,
    "resolveJsonModule": true,
    "esModuleInterop": true,
    "lib": ["esnext", "dom"]
  },
  "include": ["src"]
}
  • compilerOptions: TypeScript 编译选项。
  • include: 指定包含的文件和目录。

playwright.config.ts

import { defineConfig } from '@playwright/test';

export default defineConfig({
  testDir: './tests',
  timeout: 30 * 1000,
  expect: {
    timeout: 5000
  },
  fullyParallel: true,
  forbidOnly: !!process.env.CI,
  retries: process.env.CI ? 2 : 0,
  workers: process.env.CI ? 1 : undefined,
  reporter: 'html',
  use: {
    actionTimeout: 0,
    trace: 'on-first-retry'
  }
});
  • testDir: 测试文件的目录。
  • timeout: 测试超时时间。
  • expect: 预期配置。
  • fullyParallel: 是否并行运行测试。
  • forbidOnly: 是否禁止仅运行标记的测试。
  • retries: 重试次数。
  • workers: 并发工作线程数。
  • reporter: 报告格式。
  • use: 其他使用配置。

通过以上配置,可以更好地理解和使用 vite-plugin-react-swc 项目。

vite-plugin-react-swcSpeed up your Vite dev server with SWC项目地址:https://gitcode.com/gh_mirrors/vi/vite-plugin-react-swc

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要使用 Vite 创建 React 项目并使用 swc 作为转译工具,你可以按照以下步骤进行操作: 1. 确保你已经安装了 Node.js 和 npm。你可以在终端中运行以下命令来检查是否已经安装: ``` node -v npm -v ``` 2. 安装 Vite 脚手架工具。在终端中运行以下命令: ``` npm install -g create-vite ``` 3. 创建一个新的 Vite 项目。在终端中运行以下命令: ``` create-vite my-react-app --template react ``` 4. 进入新创建的项目目录。在终端中运行以下命令: ``` cd my-react-app ``` 5. 安装 swc 和相关的插件。在终端中运行以下命令: ``` npm install --save-dev @swc/core @swc/cli ``` 6. 配置 Vite 使用 swc 进行转译。在项目根目录下创建一个名为 `vite.config.js` 的文件,并将以下代码添加到文件中: ```javascript const { defineConfig } = require('vite'); module.exports = defineConfig({ esbuild: { jsxFactory: 'React.createElement', jsxFragment: 'React.Fragment', jsxInject: `import React from 'react'` }, plugins: [ { name: 'swc-loader', transform(code, id) { if (/\.tsx?$/.test(id)) { const { transformSync } = require('@swc/core'); const result = transformSync(code, { module: { type: 'commonjs' }, isModule: true, jsx: { factory: 'React.createElement', fragment: 'React.Fragment', pragmaFrag: 'React.Fragment' } }); return { code: result.code, map: result.map }; } } } ] }); ``` 7. 启动开发服务器。在终端中运行以下命令: ``` npm run dev ``` 现在,你就可以在浏览器中访问 `http://localhost:3000` 来查看并开发你的 React 应用了!该项目使用 swc 作为转译工具,以提供更快的构建速度和更小的包大小。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秦贝仁Lincoln

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值