forked from chao921125/vue-vite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
executable file
·428 lines (426 loc) · 12 KB
/
vite.config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// @see https://vitejs.dev/config/
import path from "node:path";
import fs from "node:fs";
import { fileURLToPath } from "node:url";
import dayjs from "dayjs";
/**
* 核心
*/
import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
/**
* 插件
*/
// 热重载
import fullReload from "vite-plugin-full-reload";
// build 生成打包分析的 html
import { visualizer } from "rollup-plugin-visualizer";
// ejs 支持,在 html 中使用 变量语法
import { ViteEjsPlugin } from "vite-plugin-ejs";
// 生成一个离线的快捷应用 PWA
import { VitePWA } from "vite-plugin-pwa";
// gzip 压缩
import { compression } from "vite-plugin-compression2";
// 浏览器兼容
import browserslist from "browserslist";
import legacy from "@vitejs/plugin-legacy";
// 支持 ES6
import commonjs from "@rollup/plugin-commonjs";
// 自动导入模块
import autoImport from "unplugin-auto-import/vite";
import components from "unplugin-vue-components/vite";
import { VantResolver, ElementPlusResolver } from "unplugin-vue-components/resolvers";
import Vuetify, { transformAssetUrls } from "vite-plugin-vuetify";
// 自动导入模块 图标
import icons from "unplugin-icons/vite";
import IconsResolver from "unplugin-icons/resolver";
import svgLoader from "vite-svg-loader";
// 自定义文件,变量处理
import pkg from "./package.json";
import { getEnvConfig, createProxy } from "./build";
const __APP_INFO__ = {
pkg,
lastBuildTime: dayjs().format("YYYY-MM-DD HH:mm:ss"),
};
export default defineConfig(({ command, mode }) => {
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const envConfig = getEnvConfig(loadEnv(mode, import.meta.dirname, ""));
const browserslistConfig = browserslist.loadConfig({ path: "." });
const isBuild = command.includes("build");
/**
* 动态配置
*/
let dynamicConfig;
if (isBuild) {
// 新建一个build文件,用来告诉用户是否需要刷新页面升级,正常来说不需要告知用户
fs.writeFileSync(path.join(__dirname, "./public/build.json"), JSON.stringify({ version: `${Date.now()}` }));
dynamicConfig = {};
} else {
dynamicConfig = {};
}
/**
* 静态配置
*/
const defaultConfig = {
root: path.resolve(__dirname, ""), // "./public/index.html", // 入口,可以指定到public文件夹
base: isBuild ? envConfig.VITE_PUBLIC_PATH : "./", // 公共基础路径
// mode: "development", // 指令覆盖构建模式 --mode
define: {
// 定义全局常量替换方式
// setting vue-i18-next
// Suppress warning
__APP_INFO__: JSON.stringify(__APP_INFO__),
},
plugins: [
vue({
template: { transformAssetUrls },
}),
fullReload(["config/*", "src/**/*", "types/**/*"], { root: __dirname, delay: 100 }),
// * 是否生成包预览
envConfig.VITE_REPORT &&
visualizer({
emitFile: false,
filename: "stats.html",
}),
ViteEjsPlugin(
(viteConfig) => {
// viteConfig is the current viteResolved config.
return {
root: viteConfig.root,
title: envConfig.VITE_TITLE,
};
},
{
ejs: {
// ejs options goes here.
beautify: true,
},
},
),
VitePWA({
injectRegister: "auto",
registerType: "autoUpdate",
devOptions: {
enabled: false,
},
includeAssets: ["favicon.ico", "apple-touch-icon.png", "mask-icon.svg"],
manifest: {
name: "vue-vite",
short_name: "vv",
description: "vue-vite frame",
id: "vue-vite",
start_url: ".",
theme_color: "#FFFFFF",
icons: [
{
src: "/pwa-640.png",
sizes: "640x640",
type: "image/png",
},
],
},
workbox: {
globPatterns: ["**/*.{js,jsx,mjs,cjs,ts,tsx,mts,cts,css,scss,sass,less,html,ico,icon,png,jpg,jpeg,gif,webp,svg,ttf,otf,woff,woff2}"],
cleanupOutdatedCaches: false,
sourcemap: true,
maximumFileSizeToCacheInBytes: 15 * 1024 ** 2,
runtimeCaching: [
mode === "production"
? {
urlPattern: ({ url }) => url.origin.includes("https"),
handler: "NetworkFirst",
options: {
cacheName: "pwa-api",
cacheableResponse: {
statuses: [200],
},
},
}
: {
urlPattern: ({ url }) => url.origin.includes("test"),
handler: "NetworkFirst",
options: {
cacheName: "pwa-api",
cacheableResponse: {
statuses: [200],
},
},
},
{
urlPattern: /\.(?:png|jpg|jpeg|svg|gif)$/,
handler: "CacheFirst",
options: {
cacheName: "pwa-images",
expiration: {
// 最多30个图
maxEntries: 30,
},
},
},
{
urlPattern: /.*\.js.*/,
handler: "StaleWhileRevalidate",
options: {
cacheName: "pwa-js",
expiration: {
maxEntries: 30, // 最多缓存30个,超过的按照LRU原则删除
maxAgeSeconds: 30 * 24 * 60 * 60,
},
cacheableResponse: {
statuses: [200],
},
},
},
{
urlPattern: /.*\.css.*/,
handler: "StaleWhileRevalidate",
options: {
cacheName: "pwa-css",
expiration: {
maxEntries: 20,
maxAgeSeconds: 30 * 24 * 60 * 60,
},
cacheableResponse: {
statuses: [200],
},
},
},
{
urlPattern: /.*\.html.*/,
handler: "StaleWhileRevalidate",
options: {
cacheName: "pwa-html",
expiration: {
maxEntries: 20,
maxAgeSeconds: 30 * 24 * 60 * 60,
},
cacheableResponse: {
statuses: [200],
},
},
},
],
},
}),
legacy({
renderLegacyChunks: false,
modernPolyfills: ["es.global-this"],
targets: browserslistConfig,
}),
commonjs({
include: "node_modules/**",
defaultIsModuleExports: false,
}),
envConfig.VITE_BUILD_GZIP && compression(),
autoImport({
include: [
/\.[tj]s?$/, // .ts, .tsx, .js, .jsx
/\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
/\.vue$/,
/\.vue\?vue/, // .vue
/\.md$/, // .md
],
imports: ["vue", "vue-router", "pinia", "@vueuse/head", "@vueuse/core", "vue-i18n"],
dirs: ["./hooks", "./hooks/**", "./components", "./components/**"],
dts: true,
resolvers: [VantResolver(), ElementPlusResolver(), IconsResolver()],
}),
components({
dts: true,
resolvers: [VantResolver(), ElementPlusResolver(), IconsResolver()],
directoryAsNamespace: true,
}),
Vuetify({
autoImport: true,
styles: {
configFile: "src/assets/styles/theme.scss",
},
}),
icons({
compiler: "vue3",
autoInstall: true,
}),
svgLoader({ defaultImport: "component" }),
],
// publicDir: "public", // 静态资源根路径,false关闭
// cacheDir: "node_modules/.vite", // 缓存路径
resolve: {
alias: {
// 别名必须以 / 开头、结尾
// "/@/": root, -- vite 内部在用,这里不能用了
// "/root/": __dirname, -- vite 内部在用,这里不能用了
// remove i18n waring
"vue-i18n": "vue-i18n/dist/vue-i18n.cjs.js",
"@": path.resolve(__dirname, "./src"),
"#": path.resolve(__dirname, "./types"),
},
// dedupe: "", // 一般SSR+ESM使用
// conditions: "",
// mainFields: "",
// extensions: "",
// preserveSymlinks: "",
},
css: {
// modules: "",
// 打开此处 postcss.config.js失效
// postcss: {
// plugins: [],
// },
preprocessorOptions: {
css: { charset: false },
styl: {
additionalData: `$injectedColor ?= orange`,
},
less: {
javascriptEnabled: true,
// additionalData: `$injectedColor: orange`
},
scss: {
javascriptEnabled: true,
additionalData: `@use "@/assets/styles/theme.scss" as *;`,
},
},
// devSourcemap: false,
},
// json: {
// namedExports: true, // 按名称导入
// stringify: false, // 序列化后导入,即:JSON.parse(),开启后则namedExports失效
// },
esbuild: {
// false or jsx settings
// jsxFactory: "h",
// jsxFragment: "Fragment",
pure: envConfig.VITE_DROP_CONSOLE ? ["console.log", "debugger"] : [],
},
// assetsInclude: "", // 静态资源处理
logLevel: "info", // 可以根据开发环境动态改变 "info" | "warn" | "error" | "silent"
clearScreen: false, // --clearScreen
// envDir: "", // 配置.env文件相关
// envPrefix: "", // 配置.env变量以VUE_还是默认的VITE_
// appType: "", // 'spa' | 'mpa' | 'custom'
server: {
// host: "localhost",
port: envConfig.VITE_PORT,
strictPort: true, // 存在冲突端口,则继续下找可用端口
// https: true, // boolean | https.ServerOptions
open: envConfig.VITE_OPEN, // boolean | string
proxy: createProxy(envConfig.VITE_PROXY),
// {
// // string shorthand
// // "/foo": "http://localhost:4567/foo",
// "/api": {
// target: "http://jsonplaceholder.typicode.com",
// changeOrigin: true,
// rewrite: path => path.replace(/^\/api/, ""),
// },
// // with RegEx
// // "^/fallback/.*": {
// // target: "http://jsonplaceholder.typicode.com",
// // changeOrigin: true,
// // rewrite: (path) => path.replace(/^\/fallback/, "")
// // },
// // 使用 proxy 实例
// // "api": {
// // target: "http://jsonplaceholder.typicode.com",
// // changeOrigin: true,
// // configure: () => {
// // // proxy http-proxy
// // },
// // },
// // '/socket.io': {
// // target: 'ws://localhost:3000',
// // ws: true
// // }
// },
cors: true, // boolean | CorsOptions
// headers: false, // OutgoingHttpHeaders 指定服务器响应的 header
hmr: true, // boolean | { protocol?: string, host?: string, port?: number, path?: string, timeout?: number, overlay?: boolean }
// watch: "", // object
// middlewareMode: "",
// base: "", // string | undefined
// fs: {
// strict: "",
// allow: "",
// deny: "",
// },
// origin: "",
},
build: {
target: "modules",
// polyfillModulePreload: true,
outDir: path.join(__dirname, "./dist"), // path.join(__dirname, "dist/render"),
assetsDir: path.join(__dirname, "./assets"),
assetsInlineLimit: 5120, // 5KB
// 如果设置为false,整个项目中的所有 CSS 将被提取到一个 CSS 文件中
cssCodeSplit: true,
// cssTarget: true, // 与 build.target 一致
// cssMinify: true, // 与 build.minify 一致
sourcemap: false,
rollupOptions: {
input: {
index: path.resolve(__dirname, "./index.html"),
// color: path.resolve(__dirname, "/public/color.html"),
},
output: {
dir: "dist",
// Static resource classification and packaging
chunkFileNames: "assets/js/[name]-[hash].js",
entryFileNames: "assets/js/[name]-[hash].js",
assetFileNames: "assets/[ext]/[name]-[hash].[ext]",
compact: true,
manualChunks: {
vue: ["vue", "vue-router", "pinia"],
echarts: ["echarts"],
},
},
},
// commonjsOptions: "",
// dynamicImportVarsOptions: "",
// lib: "",
// manifest: false, // manifest.json
// ssrManifest: false,
// ssr: false,
minify: "terser", // boolean | "terser" | "esbuild"
terserOptions: {
compress: {
drop_console: isBuild, // 生产环境去除console
drop_debugger: isBuild, // 生产环境去除debugger
},
},
// write: true,
// emptyOutDir: true, // outDiroutDir--emptyOutDir
reportCompressedSize: true,
chunkSizeWarningLimit: 2048,
// watch: 1024,
},
// preview: {
// host: "",
// port: "",
// strictPort: "",
// https: "",
// open: "",
// proxy: {},
// cors: true,
// },
optimizeDeps: {
// entries: "optimize.js",
exclude: [],
include: [],
// esbuildOptions: "",
force: false,
},
// ssr: {
// external: "",
// noExternal: "",
// target: "",
// format: "esm",
// },
// worker: {
// format: "iife", // 'es' | 'iife'
// plugins: [],
// rollupOptions: {},
// },
};
return Object.assign(defaultConfig, dynamicConfig);
});