引入
ts
import {
IZipSizeResult,
IGetZipSizeOptions,
analyzeIndexBundle,
getZipSize
} from 't-comm';
// 不支持 tree-shaking 的项目
import {
IZipSizeResult,
IGetZipSizeOptions,
analyzeIndexBundle,
getZipSize
} from 't-comm/lib/bundle-analyze/index';
// 只支持 ESM 的项目
import {
IZipSizeResult,
IGetZipSizeOptions,
analyzeIndexBundle,
getZipSize
} from 't-comm/es/bundle-analyze/index';IZipSizeResult
描述:getZipSize 的返回结果
参数:
IZipSizeResult.zipSize:number|null.zipMethod:'zip'|'gzip-sum'|'none'
iZipSizeResult.zipSize : number | null
zip 压缩后字节数。无法估算时为 null
Kind: instance property of IZipSizeResult
iZipSizeResult.zipMethod : 'zip' | 'gzip-sum' | 'none'
体积来源:
zip: 系统 zip 命令打成真实 zip 包,最准gzip-sum: zlib.gzipSync 逐文件累加,估算值,比真实 zip 略大(无共享字典)none: 两种方式都失败
Kind: instance property of IZipSizeResult
IGetZipSizeOptions
描述:getZipSize 入参
参数:
IGetZipSizeOptions.outputDir:string.files:Array<object>.excludeGlobs:Array.excludeMapInGzip:boolean.zipBin:string
iGetZipSizeOptions.outputDir : string
产物目录绝对路径(必填)
Kind: instance property of IGetZipSizeOptions
iGetZipSizeOptions.files : Array<object>
已采集到的产物文件相对路径列表(来自 outputDir 的相对路径)。 仅在 gzip-sum 降级分支使用;不传时降级分支会自行递归 outputDir。
Kind: instance property of IGetZipSizeOptions
Optional:
Properties
| Name | Type |
|---|---|
| name | string |
iGetZipSizeOptions.excludeGlobs : Array
zip 模式下要排除的 glob 模式,默认 ['*.map'](sourcemap)
Kind: instance property of IGetZipSizeOptions
Optional:
iGetZipSizeOptions.excludeMapInGzip : boolean
gzip 兜底分支是否同样跳过 sourcemap,默认 true
Kind: instance property of IGetZipSizeOptions
Optional:
iGetZipSizeOptions.zipBin : string
zip 命令所在路径,默认 'zip'(依赖 PATH)
Kind: instance property of IGetZipSizeOptions
Optional:
analyzeIndexBundle(config)
描述:分析首页Bundle信息
参数:
| 参数名 | 类型 | 描述 |
|---|---|---|
| config | 配置 | |
| config.domain | string | 域名 |
| config.buildPath | string | 打包路径 |
示例
ts
analyzeIndexBundle({
domain: '',
buildPath: '',
})getZipSize()
描述:获取构建产物目录的 zip 压缩后体积,跨平台兜底。 估算策略:
- - 优先调用系统 `zip` 命令打成真实 zip 包,删除后返回字节数(最准) - 不可用时,降级为 `zlib.gzipSync` 逐文件累加(估算值,比真实 zip 略大,因为没有共享字典) - 仍失败则返回 `{ zipSize: null, zipMethod: 'none' }`
参数:
示例
ts
import { getZipSize } from 't-comm';
const { zipSize, zipMethod } = getZipSize({ outputDir: '/path/to/dist' });
console.log(zipSize, zipMethod); // 1234567 'zip'