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

测试结果:

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

相关推荐
IT_陈寒31 分钟前
Python搞不定字符串编码?这破玩意坑我两小时!
前端·人工智能·后端
DigitalOcean2 小时前
Laravel 开发者已在 DigitalOcean 上开通超过 10 万台服务器
前端·laravel
星始流年2 小时前
从 Tool 到 Skill——基于 LangChain 的服务端Skill实现
前端·langchain·agent
李惟2 小时前
开源本地通信库,纯客户端 RPC,像聊天一样通信
前端
YAwu112 小时前
深入解析 React 炫彩鼠标跟随标题组件:从坐标定位到动画性能
前端·react.js
GuWenyue2 小时前
排序效率低?5分钟吃透快速排序,性能飙升至O(nlogn)
前端·javascript·面试
OpenTiny社区2 小时前
🎨 看完 GenUI SDK 源码我悟了!
前端·vue.js·github
叁两2 小时前
前端转型AI Agent该如何学习?(前置篇)
前端·人工智能·node.js
何时梦醒2 小时前
深入理解递归与快速排序 —— 从基础入门到手写实现
前端·javascript
爱勇宝2 小时前
淡泊名利之前,先承认我们都很焦虑
前端·后端·程序员