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
This commit is contained in:
67
internal/vite-config/src/plugins/archiver.ts
Normal file
67
internal/vite-config/src/plugins/archiver.ts
Normal file
@@ -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();
|
||||
});
|
||||
}
|
@@ -18,6 +18,7 @@ import { libInjectCss as viteLibInjectCss } from 'vite-plugin-lib-inject-css';
|
||||
import { VitePWA } from 'vite-plugin-pwa';
|
||||
import viteVueDevTools from 'vite-plugin-vue-devtools';
|
||||
|
||||
import { viteArchiverPlugin } from './archiver';
|
||||
import { viteExtraAppConfigPlugin } from './extra-app-config';
|
||||
import { viteImportMapPlugin } from './importmap';
|
||||
import { viteInjectAppLoadingPlugin } from './inject-app-loading';
|
||||
@@ -92,6 +93,8 @@ async function loadApplicationPlugins(
|
||||
const env = options.env;
|
||||
|
||||
const {
|
||||
archiver,
|
||||
archiverPluginOptions,
|
||||
compress,
|
||||
compressTypes,
|
||||
extraAppConfig,
|
||||
@@ -138,6 +141,7 @@ async function loadApplicationPlugins(
|
||||
return [await viteNitroMockPlugin(nitroMockOptions)];
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
condition: injectAppLoading,
|
||||
plugins: async () => [await viteInjectAppLoadingPlugin(!!isBuild, env)],
|
||||
@@ -184,7 +188,6 @@ async function loadApplicationPlugins(
|
||||
condition: !!html,
|
||||
plugins: () => [viteHtmlPlugin({ minify: true })],
|
||||
},
|
||||
|
||||
{
|
||||
condition: isBuild && importmap,
|
||||
plugins: () => {
|
||||
@@ -197,6 +200,12 @@ async function loadApplicationPlugins(
|
||||
await viteExtraAppConfigPlugin({ isBuild: true, root: process.cwd() }),
|
||||
],
|
||||
},
|
||||
{
|
||||
condition: archiver,
|
||||
plugins: async () => {
|
||||
return [await viteArchiverPlugin(archiverPluginOptions)];
|
||||
},
|
||||
},
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -226,6 +235,7 @@ async function loadLibraryPlugins(
|
||||
export {
|
||||
loadApplicationPlugins,
|
||||
loadLibraryPlugins,
|
||||
viteArchiverPlugin,
|
||||
viteCompressPlugin,
|
||||
viteDtsPlugin,
|
||||
viteHtmlPlugin,
|
||||
|
Reference in New Issue
Block a user