vite项目配置vite.config.ts在打包过程中去除日志

在生产环境上,务必要将日志清除干净,其因有二,在webgis系统中,有很多几何数据,体积大、数量多,很容易引起系统卡顿;清除log后,系统看着舒服,协同开发有很多无聊的日志,打出来没必要。

vite中已经内置移除console的设置,只需简单配置即可生效。在配合文件根目录下开启build的配置。

复制代码
 build: {
    minify: 'terser',
    terserOptions: {
      compress: {
        //生产环境时移除console、debugger
        drop_console: true,
        drop_debugger: true,
      },
    },
  },

全局位置如下:

复制代码
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import eslintPlugin from 'vite-plugin-eslint';
import { VantResolve, createStyleImportPlugin } from 'vite-plugin-style-import';
import path from 'path';
import { svgBuilder } from './scripts/svg-builder';
import { viteVConsole } from 'vite-plugin-vconsole';
import vueJsx from '@vitejs/plugin-vue-jsx';
import compression from 'vite-plugin-compression';

function resolve(dir: string) {
  return path.join(__dirname, dir);
}

// https://vitejs.dev/config/
export default defineConfig({
  base: './',
  server: {
    proxy: {
      '/dgp-oms-server-web': 'http://192.168.1.196:8650'
    }
  },
  plugins: [
    vue(),
    vueJsx(),
    eslintPlugin({
      fix: true
    }),
    createStyleImportPlugin({
      libs: [
        {
          libraryName: 'vant',
          esModule: true,
          resolveStyle: (name) => `../es/${name}/style`
        }
      ]
    }),
    compression(),
    svgBuilder('./src/svg/') // 导入全部svg,无需再单独导入
    // viteVConsole({
    //   entry: path.resolve('src/main.ts'), // entry file
    //   localEnabled: false, // dev environment
    //   enabled: true, // build production
    //   config: {
    //     maxLogNumber: 1000,
    //     theme: 'dark'
    //   }
    // })
  ],
  build: {
    minify: 'terser',
    terserOptions: {
      compress: {
        //生产环境时移除console、debugger
        drop_console: true,
        drop_debugger: true,
      },
    },
  },
  resolve: {
    extensions: ['.ts', '.tsx', '.vue', '.json', '.jsx', '.mjs', '.js'],
    alias: {
      '@': resolve('./src')
    }
  },
  css: {
    modules: {
      localsConvention: 'camelCase'
    },
    preprocessorOptions: {
      // scss全局预定义变量
      scss: {
        additionalData: '@import "@/styles/index.scss";'
      }
    }
  }
});

测试结果:

开启前打包后有日志,开启后打包上线无日志,主打一个清爽。

相关推荐
Bigger1 小时前
Tauri (26)——托盘图标总对不上系统主题?一行 Template Image 搞定
前端·rust·app
kyriewen3 小时前
面试官问你:“AI 能写 80% 的代码了,公司为什么还需要你?”
前端·javascript·面试
甲维斯4 小时前
又升级咯!坦克大战2026,科技与复古并存!
前端·人工智能·游戏开发
搬砖的码农6 小时前
(08)为什么我的 Agent 一跑后台服务就卡死
前端·agent·ai编程
飘尘6 小时前
前端转全栈(Java 后端)必须要知道的:开发中的锁机制与分布式并发控制
前端·后端·全栈
亲亲小宝宝鸭6 小时前
前端性能监控:web-vitals
前端·性能优化·监控
陆枫Larry7 小时前
可滚动页面背景填不满:`height: 100vh` vs `min-height: 100vh`
前端
Patrick_Wilson7 小时前
Squash Merge 的血缘陷阱:为什么删掉的代码又活了过来
前端·git·程序员
kyriewen8 小时前
今天的科技圈,全在抢英伟达的饭碗
前端·面试·ai编程