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

测试结果:

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

相关推荐
李重楼几秒前
前端性能优化之 HTTP/2 多路复用
前端·面试
yanessa_yu3 分钟前
全屏滚动网站PC端自适应方案
前端
RoyLin10 分钟前
TypeScript设计模式:桥接模式
前端·后端·typescript
火星开发者13 分钟前
Vue中实现Word、Excel、PDF预览的详细步骤
前端
brzhang19 分钟前
干翻 Docker?WebAssembly 3.0 的野心,远不止浏览器,来一起看看吧
前端·后端·架构
lecepin1 小时前
AI Coding 资讯 2025-09-17
前端·javascript·面试
IT_陈寒1 小时前
React 18实战:7个被低估的Hooks技巧让你的开发效率提升50%
前端·人工智能·后端
树上有只程序猿2 小时前
终于有人把数据库讲明白了
前端
猩兵哥哥2 小时前
前端面向对象设计原则运用 - 策略模式
前端·javascript·vue.js
司宸2 小时前
Prompt设计实战指南:三大模板与进阶技巧
前端