In IE, console is only defined when in F12 debugging mode. So I'm looking for a convenient way to manage the compilation of Vue
I'd like to be able to write console.log inside the code
alert('a');
console.log('only in development mode');
alert('b');
If i compile in production mode, the command console must disappear
alert('a');
alert('b');
If i work in develope mode, the command console must appear
alert('a');
console.log('only in development mode');
alert('b');
In VueJS, I have two webpack configurations: one for development and one for production - could this be the way?
I can not configure the webpack file correctly, but I think it's something like this:
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
module.exports.plugins = (module.exports.plugins || []).concat([
new UglifyJsPlugin({
sourceMap: true,
compress: {
drop_console: true,
warnings: false
},
comments: false
}),
])
Vue. set is a tool that allows us to add a new property to an already reactive object, and makes sure that this new property is ALSO reactive.
Can you use console log in Vue? Console log can be used in Vue js application, however, it is disabled. The reason for this is that it is not a good practice to use methods on the console. But, if you want to test something, for example, if a method gets the data, by default, you can't use a console log to check this.
vue-cli-service build produces a production-ready bundle in the dist/ directory, with minification for JS/CSS/HTML and auto vendor chunk splitting for better caching. The chunk manifest is inlined into the HTML.
camilos solution didn't work for me but this did (vue cli 3.0):
npm i babel-plugin-transform-remove-console --save-dev
babel.config.js file:
module.exports = {
"presets": [...],
"plugins": [...],
"env": {
"production": {
"plugins": ["transform-remove-console"]
}
}
}
If you are using vue-cli 3 you can install a babel plugin for that with npm install babel-plugin-transform-remove-console --save-dev
and add the following configuration to your babel.config.js
file:
const removeConsolePlugin = []
if (process.env.NODE_ENV === 'production') {
removeConsolePlugin.push('transform-remove-console')
}
module.exports = {
presets: [
'@vue/app'
],
plugins: removeConsolePlugin
}
There are other answers for older versions of vue-cli in the source link
Source: https://forum.vuejs.org/t/remove-console-logs-from-production-buils/39327
Open build > webpack.prod.conf.js under "plugins" array for UglifyJsPlugin you need to add drop_console: true
under compress option.
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false,
drop_console: true <----- ADD THIS LINE
},
sourceMap: true
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With