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";'
      }
    }
  }
});

测试结果:

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

相关推荐
憧憬成为web高手5 小时前
ACTF 12307复现
前端·bootstrap·html
wordbaby6 小时前
Axios 上传大文件崩溃:鸿蒙 RNOH 下 XHR 返回空响应头引发的"假失败"
前端·react native
wordbaby6 小时前
React Native 列表分页实战:下拉刷新与上拉加载的工程化方案
前端·react native
wordbaby7 小时前
脱离 Tab 栏的艺术:React Native 全屏子页面的导航架构实践
前端·react native·harmonyos
陈随易7 小时前
Redis 8.8发布,一定要更新
前端·后端·程序员
wordbaby7 小时前
React Native 新架构落地鸿蒙:跨三端政务级应用的工程实践与深度复盘
前端·react native·harmonyos
excel9 小时前
为什么我推荐使用 Termius:现代 SSH 工具的完整体验
前端·后端
ZC跨境爬虫9 小时前
模块化烹饪小程序开发日记 Day7:(菜谱详情接口开发与JSON数据读取全流程)
前端·javascript·css·ui·微信小程序·json
এ慕ོ冬℘゜9 小时前
JS 前端基础面试题
开发语言·前端·javascript
LaughingZhu9 小时前
Product Hunt 每日热榜 | 2026-05-25
前端·人工智能·经验分享·chatgpt·html