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:
@@ -29,6 +29,7 @@
|
||||
"dependencies": {
|
||||
"@intlify/unplugin-vue-i18n": "^4.0.0",
|
||||
"@jspm/generator": "^2.1.3",
|
||||
"archiver": "^7.0.1",
|
||||
"cheerio": "1.0.0",
|
||||
"get-port": "^7.1.0",
|
||||
"html-minifier-terser": "^7.2.0",
|
||||
@@ -39,6 +40,7 @@
|
||||
"vite-plugin-vue-devtools": "^7.3.9"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/archiver": "^6.0.2",
|
||||
"@types/html-minifier-terser": "^7.0.2",
|
||||
"@vben/node-utils": "workspace:*",
|
||||
"@vitejs/plugin-vue": "^5.1.2",
|
||||
|
@@ -24,6 +24,8 @@ function defineApplicationConfig(userConfigPromise?: DefineApplicationOptions) {
|
||||
const env = loadEnv(mode, root);
|
||||
|
||||
const plugins = await loadApplicationPlugins({
|
||||
archiver: true,
|
||||
archiverPluginOptions: {},
|
||||
compress: false,
|
||||
compressTypes: ['brotli', 'gzip'],
|
||||
devtools: true,
|
||||
|
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,
|
||||
|
@@ -33,6 +33,19 @@ interface NitroMockPluginOptions {
|
||||
verbose?: boolean;
|
||||
}
|
||||
|
||||
interface ArchiverPluginOptions {
|
||||
/**
|
||||
* 输出文件名
|
||||
* @default dist
|
||||
*/
|
||||
name?: string;
|
||||
/**
|
||||
* 输出目录
|
||||
* @default .
|
||||
*/
|
||||
outputDir?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* importmap 插件配置
|
||||
*/
|
||||
@@ -74,6 +87,10 @@ interface CommonPluginOptions {
|
||||
}
|
||||
|
||||
interface ApplicationPluginOptions extends CommonPluginOptions {
|
||||
/** 开启后,会在打包dist同级生成dist.zip */
|
||||
archiver?: boolean;
|
||||
/** 压缩归档插件配置 */
|
||||
archiverPluginOptions?: ArchiverPluginOptions;
|
||||
/** 开启 gzip|brotli 压缩 */
|
||||
compress?: boolean;
|
||||
/** 压缩类型 */
|
||||
@@ -134,6 +151,7 @@ type DefineConfig = DefineApplicationOptions | DefineLibraryOptions;
|
||||
|
||||
export type {
|
||||
ApplicationPluginOptions,
|
||||
ArchiverPluginOptions,
|
||||
CommonPluginOptions,
|
||||
ConditionPlugin,
|
||||
DefineApplicationOptions,
|
||||
|
@@ -74,6 +74,7 @@ async function loadAndConvertEnv(
|
||||
|
||||
const {
|
||||
VITE_APP_TITLE,
|
||||
VITE_ARCHIVER,
|
||||
VITE_BASE,
|
||||
VITE_COMPRESS,
|
||||
VITE_DEVTOOLS,
|
||||
@@ -90,6 +91,7 @@ async function loadAndConvertEnv(
|
||||
|
||||
return {
|
||||
appTitle: getString(VITE_APP_TITLE, 'Vben Admin'),
|
||||
archiver: getBoolean(VITE_ARCHIVER),
|
||||
base: getString(VITE_BASE, '/'),
|
||||
compress: compressTypes.length > 0,
|
||||
compressTypes,
|
||||
|
Reference in New Issue
Block a user