Vite
结合Vue
删除指定环境的console.log
1. 可以通过配置build.minify
和build.terserOptions
build.minify
build.terserOptions
minify-options
不推荐是因为打包速度会变慢, 并且Vite3.x
的版本配置了minify:'terser'
,需要下载yarn add terser -D
vite.config.ts
文件
export default defineConfig(({ command, mode }) => {
return {
...
build: {
minify: "terser",
terserOptions: {
compress: {
drop_console : true
}
}
}
...
}
})
2. 通过赋值为空函数
在main.ts
中配置
...
// 这里的 VITE_ENV 是自定义的环境变量 参考
// https://cn.vitejs.dev/guide/env-and-mode.html#env-variables
if (import.meta.env.VITE_ENV !== 'development') {
console.log = function () {}
// 还可以添加 .info .warn .error
}
...