Vue Cli 3 打包配置--自动忽略 console.log 语句

下载插件vue

npm i -D uglifyjs-webpack-plugin

在 vue.config.js 引入使用webpack

const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
module.exports = {
    configureWebpack: {
        plugins: [
            new UglifyJsPlugin({
                uglifyOptions: {
                    compress: {
                        drop_console: true,
                    },
                },
            }),
        ],
    },
    devServer: {
        proxy: {
            '/xxx': {
                target: 'http://192.168.150.17:8080/',
                changeOrigin: true,
                ws: true,
                pathRewrite: {
                    '^/xxx': 'xxx',
                },
            },
        },
    },
    publicPath: './',
}

这时执行 npm run build 打包后的文件就没有 console.log 语句了。web

不过这时会有一个问题,就是在开发环境的时候编译会很是慢。例如修改了一个变量的值,个人电脑要编译 10 秒才能从新刷出来页面,一直卡在 92% chunk asset optimizationnpm

因为去掉 console.log 语句这个功能只有在打包时才须要,因此咱们能够加一个判断,只在生产环境时才把上述配置代码加上。ui

因此正确的配置以下:插件

const UglifyJsPlugin = require('uglifyjs-webpack-plugin')

const config = {
    devServer: {
        proxy: {
            '/xxx': {
                target: 'http://192.168.150.17:8080/',
                changeOrigin: true,
                ws: true,
                pathRewrite: {
                    '^/xxx': 'xxx',
                },
            },
        },
    },
    publicPath: './',
}

if (process.env.NODE_ENV === 'production') {
    config.configureWebpack = {
        plugins: [
            new UglifyJsPlugin({
                uglifyOptions: {
                    compress: {
                        drop_console: true,
                    },
                },
            }),
        ],
    }
}

module.exports = config
相关文章
相关标签/搜索