Browse Source
feat: add archive plug-in to output dist.zip after build (#4272)
feat: add archive plug-in to output dist.zip after build (#4272)
* feat: add the archiver plug-in to output dist.zip after build * chore: update envpull/4273/head
Vben
3 weeks ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
21 changed files with 200 additions and 44 deletions
-
6.lintstagedrc.mjs
-
3apps/web-antd/.env.production
-
3apps/web-ele/.env.production
-
3apps/web-naive/.env.production
-
3docs/.vitepress/config/shared.mts
-
1docs/package.json
-
36docs/src/components/common-ui/vben-drawer.md
-
44docs/src/components/common-ui/vben-modal.md
-
3docs/src/en/guide/essentials/settings.md
-
5docs/src/guide/essentials/settings.md
-
2internal/lint-configs/eslint-config/src/configs/perfectionist.ts
-
4internal/tailwind-config/src/index.ts
-
2internal/vite-config/package.json
-
2internal/vite-config/src/config/application.ts
-
67internal/vite-config/src/plugins/archiver.ts
-
12internal/vite-config/src/plugins/index.ts
-
18internal/vite-config/src/typing.ts
-
2internal/vite-config/src/utils/env.ts
-
3playground/.env.production
-
23pnpm-lock.yaml
-
2turbo.json
@ -0,0 +1,67 @@ |
|||
import type { PluginOption } from 'vite'; |
|||
|
|||
import type { ArchiverPluginOptions } from '../typing'; |
|||
|
|||
import fs from 'node:fs'; |
|||
import { join } from 'node:path'; |
|||
|
|||
import archiver from 'archiver'; |
|||
|
|||
export const viteArchiverPlugin = ( |
|||
options: ArchiverPluginOptions = {}, |
|||
): PluginOption => { |
|||
return { |
|||
apply: 'build', |
|||
closeBundle: { |
|||
handler() { |
|||
const { name = 'dist', outputDir = '.' } = options; |
|||
|
|||
setTimeout(async () => { |
|||
const folderToZip = 'dist'; |
|||
const zipOutputPath = join(process.cwd(), outputDir, `${name}.zip`); |
|||
|
|||
try { |
|||
await zipFolder(folderToZip, zipOutputPath); |
|||
console.log(`Folder has been zipped to: ${zipOutputPath}`); |
|||
} catch (error) { |
|||
console.error('Error zipping folder:', error); |
|||
} |
|||
}, 0); |
|||
}, |
|||
order: 'post', |
|||
}, |
|||
enforce: 'post', |
|||
name: 'vite:archiver', |
|||
}; |
|||
}; |
|||
|
|||
async function zipFolder( |
|||
folderPath: string, |
|||
outputPath: string, |
|||
): Promise<void> { |
|||
return new Promise((resolve, reject) => { |
|||
const output = fs.createWriteStream(outputPath); |
|||
const archive = archiver('zip', { |
|||
zlib: { level: 9 }, // 设置压缩级别为 9 以实现最高压缩率
|
|||
}); |
|||
|
|||
output.on('close', () => { |
|||
console.log( |
|||
`ZIP file created: ${outputPath} (${archive.pointer()} total bytes)`, |
|||
); |
|||
resolve(); |
|||
}); |
|||
|
|||
archive.on('error', (err) => { |
|||
reject(err); |
|||
}); |
|||
|
|||
archive.pipe(output); |
|||
|
|||
// 使用 directory 方法以流的方式压缩文件夹,减少内存消耗
|
|||
archive.directory(folderPath, false); |
|||
|
|||
// 流式处理完成
|
|||
archive.finalize(); |
|||
}); |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue