Skip to content

Commit

Permalink
🔒 生产环境移除 console、debugger、注释
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunMengLu committed Jul 3, 2024
1 parent ee34ef6 commit 4a25d98
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
24 changes: 13 additions & 11 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -21,6 +20,8 @@ const alias: Record<string, string> = {

const viteConfig = defineConfig((mode: ConfigEnv) => {
const env = loadEnv(mode.mode, process.cwd());
// 判断是否开发环境
const isDev = env.ENV === 'development'
return {
plugins: [
vue(), // Vue 插件
Expand All @@ -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(), // 项目根目录
Expand Down Expand Up @@ -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`,
Expand Down

0 comments on commit 4a25d98

Please sign in to comment.