Vite8 怎么配置生产环境移除 log

5 阅读1分钟

转载请注明出处: 原文链接:Vite8 怎么配置生产环境移除log

配置 build 时移除 log

vite.config.ts 中添加以下配置,就可以实现了。

build: {
  rolldownOptions: {
    output: {
      minify: {
        compress: {
          dropConsole: true,
        },
      },
    },
  },
},

怎么样在不同的环境分别控制移除log呢?

答案就是:使用环境变量

  1. 创建.evn文件,添加一个环境变量
# true: 移除log
VITE_DROP_CONSOLE=true

上面的环境变量要分别配置到不同的环境上,.env文件中仅仅是一个默认值。

  1. vite.config.ts 中添加以下配置
import { defineConfig, loadEnv } from "vite";

export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd());

  return {
    build: {
      rolldownOptions: {
        output: {
          minify: {
            compress: {
              dropConsole: env.VITE_DROP_CONSOLE === "true",
            },
          },
        },
      },
    },
  };
});