Skip to content

Vite 速查表

下一代前端构建工具,开发服务器即时启动。

01

快速上手

创建项目

用模板搭建项目脚手架。

vite
# npm create vite@latest my-app -- --template vue
# npm create vite@latest my-app -- --template react-ts

配置

在 Vite 配置中使用插件。

vite
// vite.config.ts
import { defineConfig } from "vite";

export default defineConfig({
  plugins: [vue()],
  server: { port: 3000 },
});

开发/构建

三条主要命令。

vite
# vite        # dev server
# vite build  # production build
# vite preview # preview build
02

核心特性

插件

插件扩展 Vite 功能。

vite
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";

export default defineConfig({
  plugins: [react()],
});

环境变量

VITE_ 前缀的环境变量会暴露给客户端。

vite
import.meta.env.VITE_API_URL

// .env
VITE_API_URL=https://api.example.com

代理

开发期代理 API 请求。

vite
server: {
  proxy: {
    "/api": "http://localhost:8080",
  },
}

这篇内容对您有帮助吗?