• vue项目上线后去除控制台所有console.log打印-配置说明


     方式一

    npm  i   babel-plugin-transform-remove-console --save-dev

    babel.config.js文件中添加

    1. // 然后在babel.config.js中添加判断
    2. const prodPlugin = []
    3. if (process.env.NODE_ENV === 'production') {
    4. // 如果是生产环境,则自动清理掉打印的日志,但保留error 与 warn
    5. prodPlugin.push([
    6. 'transform-remove-console',
    7. {
    8. // 保留 console.error 与 console.warn
    9. exclude: ['error', 'warn']
    10. }
    11. ])
    12. }
    13. module.exports = {
    14. 'presets': [
    15. '@vue/app'
    16. ],
    17. 'plugins': [
    18. [
    19. 'import',
    20. {
    21. 'libraryName': 'ant-design-vue',
    22. 'libraryDirectory': 'es',
    23. 'style': true
    24. },
    25. 'ant-design-vue'
    26. ],
    27. ...prodPlugin
    28. ]
    29. }

    方式二

    npm i uglifyjs-webpack-plugin --save-dev

     2.1 vue-cli3 生成环境去除console.log

    在项目 目录vue.config.js 

    1. const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
    2. configureWebpack: {
    3. //注释console
    4. optimization: {
    5. minimizer: [
    6. new UglifyJsPlugin({
    7. uglifyOptions: {
    8. compress: {
    9. // warnings: false,
    10. drop_console: false, //注释console
    11. drop_debugger: false,
    12. pure_funcs: ['console.log'] //移除console
    13. }
    14. }
    15. })
    16. ]
    17. }
    18. }

     2.2 vue-cli2 生成环境去除console.log 

    项目build 下面webpack.prod.config.js 文件中

    1. plugins: [
    2. new webpack.DefinePlugin({
    3. 'process.env': env
    4. }),
    5. new UglifyJsPlugin({
    6. uglifyOptions: {
    7. compress: {
    8. warnings: false,
    9. //drop_console 传递true以放弃对控制台的调用。*功能
    10. drop_console: true,
    11. // pure_funces 禁用console.log函数
    12. pure_funcs: ['console.log']
    13. }
    14. },
    15. sourceMap: config.build.productionSourceMap,
    16. parallel: true
    17. ]

  • 相关阅读:
    MacOS中不使用XQuartz/X11构建OpenGL程序的方法
    第七天 dfs剪枝&优化
    Java日期时间处理详解
    Nuxt3环境变量配置
    leetcode-105 从前序与中序遍历序列构造二叉树-使用栈代替递归
    laravel生命周期超细腻
    C++之if-else基本应用
    tomcat基础介绍
    vcontact2:病毒聚类(失败)
    设计模式学习(八):桥接模式
  • 原文地址:https://blog.csdn.net/JackieDYH/article/details/133543609