Vite之根据不同的打包配置文件对打包文件夹名进行配置

1.在package.json配置打包命令

"scripts": {
  "dev": "vite  --port 3010 --mode development",
  "build:formGD": "vite build --mode formGD",
  "build:processGD": "vite build --mode processGD",
  "build:formSX": "vite build --mode formSX",
  "build:processSX": "vite build --mode processSX",
  "build:pj": "vite build --mode panji",
  "build:coreuat": "vite build --mode panjiTest",
  "preview": "vite preview"
},

2.在.env.formGD文件定义VITE_NODE_ENV变量

NODE_ENV = formGD
VITE_NODE_ENV = formGD

OPEN_GZIP = no

# just a flag
ENV = 'formGD'

VITE_APP_BASE_API = '/xx/yy/sub/collect_guangzhou/api'
VITE_APP_BASE_URI = '/xx/yy/sub/collect_guangzhou/'

3.在vite.config.js根据VITE_NODE_ENV变量确定文件名

import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import path from "path";

export default ({ mode }) => {
  // Load app-level env vars to node-level env vars.
  process.env = { ...process.env, ...loadEnv(mode, process.cwd()) };

  let dist = "dist-collect";
  const env = process.env.VITE_NODE_ENV;
  if(env){
    if (env.startsWith("form")) {
      dist = "dist-collect";
    } else if (env.startsWith("process")) {
      dist = "dist-smart-process";
    }
  }
  return defineConfig({
    plugins: [vue()],
    build: {
      outDir: dist,
    },
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "./src"),
      },
    },
    base: process.env.VITE_APP_BASE_URI,
  });
};

 

posted @ 2023-06-30 15:49  罗毅豪  阅读(1822)  评论(0编辑  收藏  举报