npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vite-react

v3.0.0

Published

基于 [Vite](https://github.com/vitejs/vite) 工具的 react 专属配置

Downloads

464

Readme

vite-react

基于 Vite 工具的 react 专属配置

npm peer dependency vite version npm License Code Style

安装

pnpm add vite vite-react -D

配置迁移

package.json

{
  "devDependencies": {
-   "@vitejs/plugin-legacy": "*",
-   "@vitejs/plugin-react": "*",
-   "less": "*",
-   "sass": "*",
    "vite": "*",
+   "vite-react": "*"
    ...
  },
  ...
}

tsconfig.json

{
  "compilerOptions": {
    ...
-   "types": ["vite/client"]
    ...
  }
}

vite.config.ts

- import { defineConfig } from 'vite';
+ import { defineConfig } from 'vite-react';

export default defineConfig({
  ...
+ react: {},
+ legacy: true | {},
+ html: false | {},
  server: {
+   watchExtend: {},
+   qrcode: boolean | {}
+   https: true,
  },
  resolve: {
+   aliasFromTsconfig: boolean | {}
  },
  ...
});

特性

  • 自动使用 react/react-swc 插件
  • 内置 sass 和 less 预处理器
  • 自动引入 vite/client.d.ts 类型文件,无需在 tsconfig.json 中指定
  • 自动识别在 tsconfig.json 中设置的路径别名
  • 启动 vite 服务时默认打开浏览器
  • 打包后的资源按照后缀放置到不同的文件夹
  • 配置 server.https=true 时,自动生成信任的SSL证书
  • css-modules 在开发模式下显示具体文件和类名,在打包时则使用哈希值
  • 在指定 host 时显示链接二维码以便在手机上快速扫描访问
  • .html 文件在 build 模式自动压缩

配置

react

React 项目基础插件,自动引入配置。

默认使用@vitejs/plugin-react-swc插件,其在开发环境使用 swc 替换 babel,速度会快好几倍。如果要使用基于 babel 的@vitejs/plugin-react,则可以这么配置:

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

legacy

默认值:false

使用官方 @vitejs/plugin-legacy 插件兼容不支持 <script type="module"> 标签引入 JS 文件的浏览器。

列举主流浏览器的支持情况:

| 浏览器 | IE | Edge | Chrome | Firefox | Safari | Opera | | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- | | 开始版本 | ❌ | 16 | 61 | 60 | 10.1 | 48 | | 发布时间 | ❌ | 2017.10.17 | 2017.09.05 | 2018.05.09 | 2017.05.27 | 2017.09.27 |

更多浏览器以及手机浏览器兼容性可参考 https://caniuse.com/es6-module

开启 legacy 方式:

export default defineConfig({
  legacy: true,
});

当然也可以配置一些参数。具体配置请参考 官方文档

export default defineConfig({
  legacy: {
    targets: ['chrome >= 50', 'not dead'],
  },
});

html

对 html 文件进行打包、压缩、传值、实现多页面等处理。请查看官方文档 vite-plugin-html

export default defineConfig({
  html: {
    // ...
  },
});

server.watchExtend

使用插件 vite-plugin-restart 额外监听文件变化,可重启 vite 服务或者刷新页面。

export default defineConfig({
  server: {
    watchExtend: {
      restart: [], // 重启服务
      reload: [], // 刷新页面
    },
  },
});

server.qrcode

默认值:true

在开发时使用了 host 时,使用插件 vite-plugin-qrcode 在终端生成二维码,方便手机立即扫码访问。

export default defineConfig({
  server: {
    qrcode: true | false | {},
  },
});

server.https

设置成true时,使用插件 vite-plugin-mkcert 自动生成信任证书并使用http2访问资源

resolve.aliasFromTsconfig

默认值:true

使用插件 vite-tsconfig-paths 自动识别tsconfig.json配置中设置的路径别名,省去了重复配置别名的麻烦。

export default defineConfig({
  resolve: {
    aliasFromTsconfig: true | false | {},
  },
});

温馨提示

  • 使用 css-modules 时,建议安装 vscode 插件 clinyong.vscode-css-modules 以获得更多提示
  • 尽量使用 lodash-es 代替 lodash 以获得 tree-shaking 优化