-
Notifications
You must be signed in to change notification settings - Fork 7.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 env
- Loading branch information
Showing
21 changed files
with
200 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,6 @@ VITE_ROUTER_HISTORY=hash | |
|
||
# 是否注入全局loading | ||
VITE_INJECT_APP_LOADING=true | ||
|
||
# 打包后是否生成dist.zip | ||
VITE_ARCHIVER=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,6 @@ VITE_ROUTER_HISTORY=hash | |
|
||
# 是否注入全局loading | ||
VITE_INJECT_APP_LOADING=true | ||
|
||
# 打包后是否生成dist.zip | ||
VITE_ARCHIVER=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,6 @@ VITE_ROUTER_HISTORY=hash | |
|
||
# 是否注入全局loading | ||
VITE_INJECT_APP_LOADING=true | ||
|
||
# 打包后是否生成dist.zip | ||
VITE_ARCHIVER=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.