📦 一个为 Vite 设计的打包插件,用于在构建完成后自动将构建输出目录打包成 ZIP 压缩文件。
- 🎯 类型安全 - 完整的 TypeScript 类型定义
- ⚙️ 灵活配置 - 丰富的配置选项,满足各种使用场景
- 🔒 文件签名 - 支持输出文件的 MD5 和 SHA256 哈希值
- 📝 详细日志 - 清晰的构建日志输出
- 🎨 自定义过滤 - 支持自定义文件过滤规则
- 🚀 零配置 - 开箱即用,默认配置即可满足大部分需求
使用 npm:
npm install -D @adjfut/vite-plugin-zip-pack使用 pnpm:
pnpm add -D @adjfut/vite-plugin-zip-pack使用 yarn:
yarn add -D @adjfut/vite-plugin-zip-pack在 vite.config.js 或 vite.config.ts 中引入并配置插件:
import { defineConfig } from 'vite';
import zipPack from '@adjfut/vite-plugin-zip-pack';
export default defineConfig({
plugins: [zipPack()]
});运行构建命令后,插件会自动将 dist 目录打包成 dist.zip 文件。
import { defineConfig } from 'vite';
import zipPack from '@adjfut/vite-plugin-zip-pack';
export default defineConfig({
plugins: [
zipPack({
inDir: './dist',
outDir: './output',
outFileName: 'my-app.zip',
pathPrefix: 'my-app',
logLevel: ['info', 'fileHash', 'error']
})
]
});| 选项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
inDir |
string |
'./dist' |
需要打包的目录路径 |
outDir |
string |
'./' |
输出压缩包的目录路径 |
outFileName |
string |
'dist.zip' |
输出压缩包的文件名 |
pathPrefix |
string |
'' |
压缩包内的目录前缀 |
filter |
function(fileName: string,filePath: string,isDirectory: boolean) |
undefined |
文件过滤函数 |
done |
function(file: File) |
undefined |
压缩完成回调 |
error |
function(error: Error) |
undefined |
压缩异常回调 |
logLevel |
boolean | ['info','fileHash','error'] |
true |
是否输出日志 |
zipPack({
filter: (fileName, filePath, isDirectory) => {
// 排除 .map 文件
if (fileName.endsWith('.map')) {
return false;
}
// 排除 node_modules 目录
if (isDirectory && fileName === 'node_modules') {
return false;
}
return true;
}
});zipPack({
done: (file) => {
console.log(`压缩完成: ${file.toString()}`);
// 可以在这里执行上传、通知等操作
},
error: (error) => {
console.error('压缩失败:', error);
// 错误处理逻辑
}
});必须是相对路径
zipPack({
pathPrefix: 'my-app-v1.0.0'
// 压缩包内的文件将位于 my-app-v1.0.0/ 目录下
});我们欢迎所有形式的贡献!请查看 贡献指南 了解详细信息。