Vite下Vue和React项目build生成zip文件

Vue 和 React 都使用 vite-plugin-zip-pack 插件实现目的

  1. 安装 vite-plugin-zip-pack
npm i -D vite-plugin-zip-pack
  1. 在 vite.config.ts 添加代码
import react from "@vitejs/plugin-react-swc";
import { defineConfig } from "vite";
import svgr from "vite-plugin-svgr";
import zipPack from "vite-plugin-zip-pack";
const apiRoutes = ["^/api/v1/", "/health"];

// Use environment variable to determine the target. 0.0.0.0:9860 192.168.10.55:7860
const target = process.env.VITE_PROXY_TARGET || "http://0.0.0.0:9860";

// Use environment variable to determine the UI server port
const port = process.env.VITE_PORT || 3000;

const proxyTargets = apiRoutes.reduce((proxyObj, route) => {
  proxyObj[route] = {
    target: target,
    changeOrigin: true,
    secure: false,
    ws: true,
  };
  return proxyObj;
}, {});
export default defineConfig(() => {
  return {
    build: {
      outDir: "build",
    },
    plugins: [
      react(),
      svgr(),
      zipPack({
        inDir: "build",
        outDir: "../frontend",
        outFileName: "build.zip",
      }),
    ],
    server: {
      port: Number(port),
      proxy: {
        ...proxyTargets,
      },
    },
  };
});

说明如下:

export interface Options {
    /**
     * Input Directory
     * @default `dist`
     */
    inDir?: string;
    /**
     * Output Directory
     * @default `dist-zip`
     */
    outDir?: string;
    /**
     * Zip Archive Name
     * @default `dist.zip`
     */
    outFileName?: string;
    /**
     * Path prefix for the files included in the zip file
     * @default ``
     */
    pathPrefix?: string;
    /**
     * Callback, which is executed after the zip file was created
     * err is only defined if the save function fails
     */
    done?: (err: Error | undefined) => void;
    /**
    * Filter function equivalent to Array.prototype.filter
    * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
    * is executed for every files and directories
    * files and directories are only included when return ist true.
    * All files are included when function is not defined
    */
    filter?: (fileName: string, filePath: string, isDirectory: boolean) => Boolean;
    /**
     * Enable logging
     * @default true
     */
    enableLogging?: boolean;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

第七感 de秘密

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值