Vue3中(vite.config.js)配置打包的时候去除console.log

发布时间 2023-04-15 18:45:20作者: Felix_Openmind

参考:https://www.cnblogs.com/lovewhatIlove/p/16476165.html

安装terser

npm add -D terser

vite中配置

import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import vueJsx from "@vitejs/plugin-vue-jsx";
import visualizer from "rollup-plugin-visualizer";
import viteCompression from "vite-plugin-compression";
import { viteZip } from "vite-plugin-zip-file";
import mpaPlugin from "vite-plugin-mpa-plus";

const getBuildInfo = require("./version/version.js");
const path = require("path");

export default defineConfig({
  base: "/", // 开发或生产环境服务的公共基础路径
  plugins: [
    vue(),
    mpaPlugin({
      pages: {
        index: {
          filename: "index.html",
          template: "public/index.html",
          inject: {
            data: {
              title: "mpa-app1"
            }
          }
        },
        version: {
          filename: "version.html",
          template: "version/index.html",
          inject: {
            data: {
              buildInfo: getBuildInfo()
            }
          }
        }
      }
    }),
    viteZip({
      folderPath: path.resolve(__dirname, "dist"),
      outPath: path.resolve(__dirname),
      zipName: "dist.zip"
    }),
    vueJsx(),
    visualizer({
      open: false,
      gzipSize: true
    }),
    viteCompression({
      deleteOriginFile: false,
      algorithm: "gzip"
    })
  ],
  resolve: {
    // 定义路径别名
    alias: {
      "@": path.resolve(__dirname, "src")
    },
    // 导入时省略扩展名
    extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json", ".vue"]
  },
  server: {
    host: "0.0.0.0",
    port: 9999,
    open: true,
    proxy: {
      "/yyfnzx": {
        target: "http://39.106.114.130:18895/yyfnzx",
        changeOrigin: true,
        rewrite: path => path.replace(/^\/yyfnzx/, "")
      },
      "/empower": {
        target: "http://192.168.1.141:8180/empower",
        changeOrigin: true,
        rewrite: path => path.replace(/^\/empower/, "")
      }
      // 'empower/ucs': {
      // 	// target: 'http://39.106.114.130:18894/',
      // 	target: 'https://dmc.haizhi.com/',
      // 	changeOrigin: true
      // },
    }
  },
  build: {
    outDir: "./dist/",
    brotliSize: false, // 关闭打包过程中计算包的大小
    cssCodeSplit: true, //启用/禁用 CSS 代码拆分
    assetsInlineLimit: 4096, // 图片转base64编码的阈值
    sourcemap: false, //构建后是否生成 source map 文件
    rollupOptions: {
      output: {
        manualChunks(id) {
          if (id.includes("node_modules")) {
            return id
              .toString()
              .split("node_modules/")[1]
              .split("/")[0]
              .toString();
          }
        },
        chunkFileNames: "static/js/[name]-[hash].js",
        entryFileNames: "static/js/[name]-[hash].js",
        assetFileNames: "static/[ext]/[name]-[hash].[ext]"
      }
    },
  //  start = 核心配置
    minify: 'terser',
    terserOptions: {
      compress: {
        drop_console: true,
        drop_debugger: true
      }
    }
  // end = 核心配置
  }
});

// https://vitejs.dev/config/