t-comm
Version:
专业、稳定、纯粹的工具库
54 lines (53 loc) • 1.95 kB
TypeScript
/**
* `getZipSize` 的返回结果
*/
export interface IZipSizeResult {
/** zip 压缩后字节数。无法估算时为 null */
zipSize: number | null;
/**
* 体积来源:
* - `zip` : 系统 zip 命令打成真实 zip 包,最准
* - `gzip-sum`: zlib.gzipSync 逐文件累加,估算值,比真实 zip 略大(无共享字典)
* - `none` : 两种方式都失败
*/
zipMethod: 'zip' | 'gzip-sum' | 'none';
}
/**
* `getZipSize` 入参
*/
export interface IGetZipSizeOptions {
/** 产物目录绝对路径(必填) */
outputDir: string;
/**
* 已采集到的产物文件相对路径列表(来自 `outputDir` 的相对路径)。
* 仅在 `gzip-sum` 降级分支使用;不传时降级分支会自行递归 `outputDir`。
*/
files?: Array<{
name: string;
}>;
/** zip 模式下要排除的 glob 模式,默认 `['*.map']`(sourcemap) */
excludeGlobs?: string[];
/** gzip 兜底分支是否同样跳过 sourcemap,默认 `true` */
excludeMapInGzip?: boolean;
/** zip 命令所在路径,默认 `'zip'`(依赖 PATH) */
zipBin?: string;
}
/**
* 获取构建产物目录的 zip 压缩后体积,跨平台兜底。
*
* 估算策略:
* 1) 优先调用系统 `zip` 命令打成真实 zip 包,删除后返回字节数(最准)
* 2) 不可用时,降级为 `zlib.gzipSync` 逐文件累加(估算值,比真实 zip 略大,因为没有共享字典)
* 3) 仍失败则返回 `{ zipSize: null, zipMethod: 'none' }`
*
* Windows 默认没有 `zip` 命令,会自动降级到 gzip-sum,无需额外配置。
*
* @example
* ```ts
* import { getZipSize } from 't-comm';
*
* const { zipSize, zipMethod } = getZipSize({ outputDir: '/path/to/dist' });
* console.log(zipSize, zipMethod); // 1234567 'zip'
* ```
*/
export declare function getZipSize(options: IGetZipSizeOptions): IZipSizeResult;