A plugin-based Lottie JSON optimizer with configurable presets
npm install lofter-lottie-opt插件化 Lottie JSON 优化器,灵活组合优化策略,大幅减小动画文件体积。
``bash`
npm install lofter-lottie-opt -g或
pnpm add lofter-lottie-opt -g
`bash基础用法:优化 Lottie 文件(使用所有默认插件)
lottie-opt input.json output.json
$3
`bash
验证 Lottie 文件质量
lottie-opt validate input.json完整验证 + HTML 报告
lottie-opt validate input.json --report --visual --playback
`编程接口
`typescript
import { optimize } from 'lofter-lottie-opt';const result = await optimize(lottieData, {
plugins: ['remove-unused-assets', 'minify-json', 'simplify-paths'],
pluginOptions: {
'simplify-paths': { tolerance: 0.3 },
'minify-json': { compressNames: false }
}
});
console.log(
${result.stats.originalSize} → ${result.stats.optimizedSize} bytes);
console.log(压缩率: ${result.stats.compressionRatio.toFixed(1)}%);
`CLI 参数
| 参数 | 说明 |
|------|------|
|
--plugins | 逗号分隔的插件列表 |
| --disable | 禁用的插件列表 |
| --plugin-config | 插件配置,格式 plugin.option=value |
| -c, --config | 配置文件路径 |
| -v, --verbose | 详细日志 |
| -r, --report | 生成优化报告 |
| --dry-run | 仅分析不保存 |
| --show-config | 显示生效的配置 |
| --list-plugins | 列出可用插件 |可用插件
$3
清理未使用的资源,安全无损。BFS 算法扫描,支持预合成嵌套检测。
`bash
lottie-opt input.json --plugins remove-unused-assets
`$3
移除禁用效果和 AE 专有效果,安全无损。清理设计师遗留的无效效果数据。
| 配置项 | 默认值 | 说明 |
|--------|--------|------|
|
removeAeOnlyEffects | true | 移除 AE 专有效果(如 Deep Glow) |
| aeOnlyEffectPrefixes | ['PEDG', 'ADBE'] | AE 效果 matchName 前缀 |
| preserveEffectNames | [] | 保留的效果名称列表 |`bash
lottie-opt input.json --plugin-config remove-disabled-effects.removeAeOnlyEffects=true
`$3
清理空对象和无效数据,安全无损。移除导出工具产生的冗余数据。
| 配置项 | 默认值 | 说明 |
|--------|--------|------|
|
cleanArrayEmptyObjects | true | 清理数组中的空对象 {} |
| cleanEmptyObjectProperties | true | 清理值为空对象的属性 |
| cleanNullValues | true | 清理 null 值 |
| cleanEmptyArrays | false | 清理空数组(默认关闭) |`bash
lottie-opt input.json --plugin-config clean-empty-objects.cleanEmptyArrays=true
`$3
路径简化,使用 RDP 算法减少贝塞尔曲线控制点。
| 配置项 | 默认值 | 说明 |
|--------|--------|------|
|
tolerance | 1.0 | 误差容忍度(像素) |
| mode | static-only | 模式:static-only / animated-consistent / all |
| minPoints | 6 | 每条路径最少保留点数 |
| preserveCorners | true | 保留尖角 |`bash
lottie-opt input.json --plugin-config simplify-paths.tolerance=0.5,simplify-paths.preserveCorners=true
`$3
图像压缩,支持 WebP/AVIF/JPEG/PNG,自动选择最小体积格式。
| 配置项 | 默认值 | 说明 |
|--------|--------|------|
|
enableWebp | true | 启用 WebP |
| enableAvif | false | 启用 AVIF(更小但处理慢) |
| quality | balanced | 质量档位:smallest / balanced / lossless |
| longEdge | 0 | 限制最长边像素,0=不限制 |
| embed | true | 内嵌为 Data URL |
| useTinyPNG | false | 使用 TinyPNG 压缩 |`bash
lottie-opt input.json --plugin-config compress-images.enableWebp=true,compress-images.quality=smallest
`$3
JSON 结构精简,删除元数据、降低精度、压缩名称。
| 配置项 | 默认值 | 说明 |
|--------|--------|------|
|
removeMetadataKeys | true | 删除 ix/mn/bm/ddd 等元数据 |
| enableGlobalPrecision | true | 全局数值精度控制 |
| removeDefaultValues | true | 删除默认值(bm:0, hd:false 等) |
| keepMatchName | true | 保留 matchName 兼容性 |
| compressNames | true | 压缩名称为单字符 |
| keepIds | false | 保留原始 ID(否则转短码) |`bash
lottie-opt input.json --plugin-config minify-json.compressNames=false,minify-json.keepIds=true
`优化层级
项目遵循三层优化策略,默认全部启用:
| 层级 | 插件 | 说明 |
|------|------|------|
| A. 安全无损 |
remove-unused-assets, remove-disabled-effects, clean-empty-objects, minify-json | 不改变视觉效果 |
| B. 可控有损 | simplify-paths | 路径简化,可能轻微改变效果 |
| C. 高级优化 | compress-images | 图像格式转换(WebP/AVIF) |推荐配置
| 场景 | 命令 |
|------|------|
| 完整优化 | 默认(无需参数,使用全部插件) |
| 仅安全优化 |
--plugins remove-unused-assets,remove-disabled-effects,clean-empty-objects,minify-json |
| 禁用图片压缩 | --disable compress-images` |MIT