vue3+ vite 项目取消生产环境下的console.log打印信息

方式一, 在vite.confing.mts中增加:
注意: 开发环境也会被清除
esbuild: {
  drop: ['console', 'debugger'],
},

详细代码: 

import { defineConfig } from '@vben/vite-config';

import ElementPlus from 'unplugin-element-plus/vite';

export default defineConfig(async () => {
  return {
    application: {},
    vite: {
      esbuild: {
        drop: ['console', 'debugger'],
      },
      plugins: [
        ElementPlus({
          format: 'esm',
        }),
      ],
      server: {
        proxy: {
          '/api': {
            changeOrigin: true,
            rewrite: (path) => path.replace(/^\/api/, ''),
            // mock代理目标地址
            target: 'http://localhost:5320/api',
            ws: true,
          },
        },
      },
    },
  };
});

 方式二: 
.env.production文件为生产环境配置
.env.development 文件为开发环境配置
使用 import.meta.env可访问 .env文件内容

// 在app.vue 文件中添加代码 ,关闭生产环境的conosle
if(!import.meta.env?.DEV){
  console.log = function() {};
}

 

posted @ 2025-02-27 10:48  龙卷风吹毁停车场  阅读(508)  评论(0)    收藏  举报