diff --git a/package.json b/package.json index 89409395..21ef8f03 100644 --- a/package.json +++ b/package.json @@ -55,8 +55,8 @@ "eslint-plugin-vue": "^9.9.0", "pinia-plugin-persist": "^1.0.0", "prettier": "3.3.2", - "rollup-plugin-terser": "^7.0.2", "sass": "^1.58.3", + "terser": "^5.31.1", "typescript": "^5.0.0", "unplugin-auto-import": "^0.17.0", "vite": "^4.3.3", diff --git a/vite.config.ts b/vite.config.ts index 172e129c..0780255c 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,7 +1,6 @@ import vue from '@vitejs/plugin-vue'; import { resolve } from 'path'; import { defineConfig, loadEnv, ConfigEnv } from 'vite'; -import { terser } from 'rollup-plugin-terser'; import vueSetupExtend from 'vite-plugin-vue-setup-extend'; import AutoImport from 'unplugin-auto-import/vite'; import topLevelAwait from 'vite-plugin-top-level-await'; @@ -21,6 +20,8 @@ const alias: Record = { const viteConfig = defineConfig((mode: ConfigEnv) => { const env = loadEnv(mode.mode, process.cwd()); + // 判断是否开发环境 + const isDev = env.ENV === 'development' return { plugins: [ vue(), // Vue 插件 @@ -39,16 +40,6 @@ const viteConfig = defineConfig((mode: ConfigEnv) => { }), viteCompression({ deleteOriginFile: false, // 压缩后删除原来的文件 - }), - // 生产环境移除 console、debugger、注释 - terser({ - format: { - comments: false, // 移除所有注释 - }, - compress: { - drop_console: true, // 删除 console - drop_debugger: true, // 删除 debugger - } }) ], root: process.cwd(), // 项目根目录 @@ -85,6 +76,17 @@ const viteConfig = defineConfig((mode: ConfigEnv) => { build: { outDir: 'dist', // 打包输出目录 chunkSizeWarningLimit: 1500, // 代码分包阈值 + // 开发使用 esbuild 更快,生产环境打包使用 terser 可以删除更多注释 + minify: isDev ? 'esbuild' : 'terser', + terserOptions: { + compress: { + drop_console: true, // 删除 console + drop_debugger: true, // 删除 debugger + }, + format: { + comments: false // 删除所有注释 + } + }, rollupOptions: { output: { entryFileNames: `assets/[name].[hash].js`,