New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
如何在vue.config.js中配置vue-cli3.0打包之后不显示console #3088
Comments
不是很懂你的需求。如果是不需要在控制台输出信息的话,执行命令时自行重定向标准输出流就行了。
另外 GitHub issues 是用来处理 Bug 报告和特性请求的,这个 issue 不是 bug,类似的提问可以到论坛 提。 |
哦你说的是打包后去掉 console 语句。 const TerserPlugin = require('terser-webpack-plugin')
module.exports = {
configureWebpack: {
optimization: {
minimizer: [new TerserPlugin({ terserOptions: { compress: { drop_console: true } } })]
}
}
} (如果用 |
好的谢谢 |
可以解决 |
想要只在生产环境设置,只能这样吗? |
你用的是vue-cli是什么版本的@sunlei4076 |
|
那你可以在vue.config.js直接使用这段代码
打包的时候会关闭,本地运行的时候不会影响 |
配置多个属性不是太方便,能用这种json形式吗的设置吗 { drop_console: true } |
其实我感觉不用纠结这个开关的写法,我感觉写成json这种的比较直观 |
这种配置,怎么去设置只在生产环境生效 |
如果你是用的是vue-cli3.0+,你需要安装一个terser-webpack-plugin的npm包,在vue.config.js引用就好了 |
我是说怎么加上这句判断: if (process.env.NODE_ENV === 'production') |
你尝试过我说的方法吗? if (process.env.NODE_ENV === 'production'){}这个判断语句你不会加?你js怎么写的,而且请不要没尝试过解决方案直接问,可以吗 |
看你你的写法跟他的写法的区别,他是直接返回的的一个json,if语句怎么加。。 |
三元运算符不行吗 |
试试这样
|
@sodatea 这种写法会不会覆盖脚手架原先的配置?我的做法是直接定位 drop_console: process.env.NODE_ENV === 'production' ? true : false,
drop_debugger: process.env.NODE_ENV === 'production' ? true : false 不知道有没有不改源码配置的方法。 |
@wQueryLoveMm 会覆盖
我试了会覆盖 |
我试了会报 TypeError: Cannot read property 'options' of undefined |
|
试过,这样会覆盖vue-cli的productionSourceMap设置,minimizer默认sourceMap是false,要让sourceMap为true,必须再配置里加上sourceMap配置 😓 {
minimizer: [new TerserPlugin({ sourceMap: true, terserOptions: { compress: { drop_console: true } } })]
}, |
@zuibunan 不太清楚你的具体环境。我建议你可以在configureWebpack配置方法里面打debugger, 看下你的config对象具体是什么, 看看terserOptions在哪。 |
使用高级链式配置 configureWebpack: config => {
// 打包时删除console和debugger
Object.assign(config.optimization.minimizer[0].options.terserOptions.compress, {
drop_debugger: true,
drop_console: true
})
} |
翻了不少答案,最后找到Vue Cli4可以这样做,会保留Vue CLI原有的terser配置不被覆盖: module.exports = {
chainWebpack: (config) => {
config.optimization.minimizer('terser').tap((args) => {
args[0].terserOptions.compress.drop_console = true
return args
})
}
} |
翻了不少答案,您这个是正解。赞! @duola8789 |
不会报config.optimization.minimizer('terser').tap未定义的错误吗 |
vue-cli v3 const TerserPlugin = require('terser-webpack-plugin')
module.exports = {
chainWebpack: (config) => {
config.optimization.minimizer([
new TerserPlugin({ terserOptions: { compress: { drop_console: true } } })
])
}
} vue-cli v4/v5 module.exports = {
chainWebpack: (config) => {
config.optimization.minimizer('terser').tap((args) => {
args[0].terserOptions.compress.drop_console = true
return args
})
}
} 具体可以查看https://cli.vuejs.org/migrations/migrate-from-v3.html#vue-config-js-options vue-cli v5还有一种简单的配置,这种配置会覆盖默认的配置,不建议使用 module.exports = {
terser: {
terserOptions: {
compress: {
drop_console: true,
drop_debugger: true,
},
},
}
} 具体可以查看:#6752 |
Version
3.0.5
Node and OS info
yarn
Steps to reproduce
如何在vue.config.js中配置vue-cli3.0打包之后不显示console
What is expected?
如何在vue.config.js中配置vue-cli3.0打包之后不显示console
What is actually happening?
暂无
The text was updated successfully, but these errors were encountered: