目录Vite结合Vue删除指定环境的console.log1.可以通过配置build.minify和build.terserOptions2.通过赋值为空函数Vue项目打包并去掉所有
Vite
结合Vue
删除指定环境的console.log
不推荐是因为打包速度会变慢, 并且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
}
}
}
...
}
})
在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
}
...
// 项目在发布时需要用到的 babel 插件数组
const proPlugins = []
// 如果当前是测试环境或者是生产环境,则使用去掉 console 的插件
if (process.env.node_ENV === 'test' || process.env.NODE_ENV === 'production') {
proPlugins.push('transform-remove-console')
}
module.exports = {
presets: [
'@vue/cli-plugin-babel/preset'
],
plugins: [
[
'component',
{
libraryName: 'element-ui',
styleLibraryName: 'theme-chalk'
}
],
...proPlugins
]
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: Vite结合Vue删除指定环境的console.log问题
本文链接: https://www.lsjlt.com/news/199382.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-01-12
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0