前端打包自动压缩为zip--archiver

安装依赖

shell 复制代码
pnpm add archiver @types/archiver

/vitePlugins/autoBuildZip.ts

ts 复制代码
import { Plugin } from 'vite';
import archiver from 'archiver';
import fs from 'fs';

const compressFolder = (folderPath: string, outputFilePath: string) => {
  const output = fs.createWriteStream(`${__dirname}/../${outputFilePath}.zip`);
  const archive = archiver('zip', {
    zlib: { level: 9 },
  });

  output.on('close', function () {
    const size = (archive.pointer() / 1024 / 1024).toFixed(2);
    console.log(`
          ----------------------------------------------------------
          ------                  压缩完成                    ------
          ------ 文件路径:项目根目录:${outputFilePath}.zip     ------
          ------               文件大小${size}M                  ------
          ----------------------------------------------------------
      `);
  });

  archive.on('warning', function (err: { code: string }) {
    if (err.code === 'ENOENT') {
    } else {
      throw err;
    }
  });

  archive.on('error', function (err: any) {
    throw err;
  });
  archive.pipe(output);
  archive.directory(folderPath, outputFilePath);
  archive.finalize();
};

export default function autoBuildZip(fileName: string): Plugin {
  return {
    name: 'vite:autoBuildZip',
    apply: 'build',
    enforce: 'post',
    closeBundle() {
      compressFolder(fileName, fileName);
    },
  };
}

使用

vite.config.ts中引入

ts 复制代码
// 该插件用于自动打包zip文件
import autoBuildZip from './vitePlugins/autoBuildZip';

plugins:[
	autoBuildZip('dist'),
]
相关推荐
刘发财4 小时前
弃用html2pdf.js,这个html转pdf方案能力是它的几十倍
前端·javascript·github
牛奶7 小时前
2026年大模型怎么选?前端人实用对比
前端·人工智能·ai编程
牛奶7 小时前
前端人为什么要学AI?
前端·人工智能·ai编程
Kagol9 小时前
🎉OpenTiny NEXT-SDK 重磅发布:四步把你的前端应用变成智能应用!
前端·开源·agent
GIS之路10 小时前
ArcGIS Pro 中的 notebook 初识
前端
JavaGuide10 小时前
7 道 RAG 基础概念知识点/面试题总结
前端·后端
ssshooter11 小时前
看完就懂 useSyncExternalStore
前端·javascript·react.js
格砸12 小时前
从入门到辞职|从ChatGPT到OpenClaw,跟上智能时代的进化
前端·人工智能·后端
Live0000012 小时前
在鸿蒙中使用 Repeat 渲染嵌套列表,修改内层列表的一个元素,页面不会更新
前端·javascript·react native
柳杉12 小时前
使用Ai从零开发智慧水利态势感知大屏(开源)
前端·javascript·数据可视化