1. 搭建项目
使用 NPM:
使用 Yarn:
使用 PNPM:
构建一个 Vite + Vue 项目
| |
| npm create vite@latest my-vue-app --template vue |
| |
| |
| npm create vite@latest my-vue-app -- --template vue |
| |
| |
| yarn create vite my-vue-app --template vue |
| |
| |
| pnpm create vite my-vue-app --template vue |
2. 基础配置
官方文档地址
| import { defineConfig } from "vite"; |
| export default defineConfig({ |
| |
| }); |
3. 共享配置
| import { defineConfig } from "vite"; |
| import { resolve } from "path"; |
| export default defineConfig({ |
| resolve: { |
| alias: [ |
| { |
| find: "@", |
| replacement: resolve(__dirname, "src"), |
| }, |
| ], |
| }, |
| }); |
| import { defineConfig } from "vite"; |
| export default defineConfig({ |
| resolve: { |
| alias: [ |
| { |
| find: "/img", |
| replacement: "src/assets/images/", |
| }, |
| ], |
| }, |
| }); |
| |
| |
| <img src="/img/xx.png" alt="xx" /> |
4. 开发服务器配置
| export default defineConfig({ |
| server: { |
| host: true, |
| port: 8080, |
| hmr: true, |
| open: true, |
| |
| proxy: { |
| |
| '/foo': 'http://localhost:4567', |
| |
| '/api': { |
| target: 'http://jsonplaceholder.typicode.com', |
| changeOrigin: true, |
| rewrite: (path) => path.replace(/^\/api/, '') |
| }, |
| |
| '^/fallback/.*': { |
| target: 'http://jsonplaceholder.typicode.com', |
| changeOrigin: true, |
| rewrite: (path) => path.replace(/^\/fallback/, '') |
| }, |
| |
| '/api': { |
| target: 'http://jsonplaceholder.typicode.com', |
| changeOrigin: true, |
| configure: (proxy, options) => { |
| |
| } |
| }, |
| |
| '/socket.io': { |
| target: 'ws://localhost:3000', |
| ws: true |
| } |
| } |
| }, |
| }); |
5. 构建配置
| import { defineConfig } from "vite"; |
| import {resolve} from "path"; |
| export default defineConfig({ |
| build:{ |
| assetsDir:"static", |
| rollupOptions:{ |
| input:{ |
| index:resolve(__dirname,"index.html") |
| }, |
| output:{ |
| chunkFileNames:'static/js/[name]-[hash].js', |
| entryFileNames:"static/js/[name]-[hash].js", |
| assetFileNames:"static/[ext]/name-[hash].[ext]" |
| } |
| }, |
| }, |
| }); |
6. 配置多环境
根目录下分别新建.env.development
,.env.staging
,.env.production
文件
| //.env.development |
| NODE_ENV="development" |
| VITE_APP_BASEAPI="https://www.dev.com" |
| |
| //.env.staging |
| NODE_ENV="staging" |
| VITE_APP_BASEAPI="https://www.staging.com" |
| |
| //.env.production |
| NODE_ENV="production" |
| VITE_APP_BASEAPI="https://www.production.com" |
package.json中修改
| "scripts": { |
| "dev": "vite --mode development", |
| "build": "vite build --mode production", |
| "staging": "vite build --mode staging", |
| "preview": "vite preview" |
| }, |
项目中获取
| <script setup> |
| console.log("env", import.meta.env.VITE_APP_BASEAPI) |
| </script> |
7. 配置多页面,多入口
| import { defineConfig } from "vite"; |
| import {resolve} from "path"; |
| export default defineConfig({ |
| build:{ |
| assetsDir:"static", |
| rollupOptions:{ |
| input:{ |
| index:resolve(__dirname,"index.html"), |
| project:resolve(__dirname,"project.html") |
| }, |
| }, |
| }, |
| }); |
8. 打包文件分析
安装 rollup-plugin-visualizer
插件
| npm i rollup-plugin-visualizer |
| import { defineConfig } from "vite"; |
| import {visualizer} from "rollup-plugin-visualizer" |
| export default defineConfig({ |
| plugins: [ |
| visualizer({ |
| open:true, |
| gzipSize:true, |
| brotliSize:true |
| }) |
| ] |
| }); |
9. 问题
require is not defined
| |
| require('./images/xx.png') |
| |
| new URL('./images/xx.png',import.meta.url).href |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程