如何把plotly.js-lite从1.2MB压缩到400KB并自托管?

解读

在国内生产环境中,前端静态资源体积直接影响首屏加载速度与 CDN 流量成本。Dash 默认通过 unpkg 外链完整版 plotly.js,体积大、解析慢,且境外 CDN 常被 GFW 限速。面试官问“压缩到 400 KB 并自托管”,核心考察三件事:

  1. 能否精准裁剪 Plotly 功能,只保留 Dash 真正用到的 trace 类型与组件;
  2. 能否用国内可复制的工程化手段(webpack/rollup/vite)把裁剪后的代码再做 Tree-Shaking、混淆、Gzip/Brotli 压缩;
  3. 能否让 Dash 在离线或内网场景下加载自托管的精简包,而不破坏 Dash 的回调与客户端序列化机制。

知识点

  1. Plotly.js 模块化架构:注册 trace 模块(scatter、bar、pie…)、组件模块(sliders、updatemenus…)、本地化模块(locale-en、locale-zh-cn…)。
  2. Dash 对 plotly.js 的硬依赖:dash-core-components 中的 Graph 组件通过 window.Plotly.newPlot 渲染,要求全局变量存在且接口完整;Dash 只用到少数 trace(scatter、bar、pie、heatmap、table 等)与部分组件(modeBar、toImage)。
  3. 体积裁剪三板斧:
    a) 自定义 bundle:在 plotly.js/src 目录下新建 index-lite.js,只 import 所需 trace 与组件,再 export 默认对象;
    b) 二次 Tree-Shaking:用 rollup + terser 删除未引用代码,关闭 source-map,开启 mangling;
    c) 压缩算法:Nginx 开启 gzip_static 与 brotli_static,对 .js.gz 与 .js.br 预压缩,浏览器端可再降 60%。
  4. Dash 离线注入:
    a) 把 plotly-lite.min.js 放到 assets/ 目录,Dash 会自动 serve /assets/plotly-lite.min.js;
    b) 在 app.py 里设置 app.scripts.config.serve_locally = True,并替换 dash-core-components 的 external_scripts 为 assets 路径;
    c) 若用 dash 2.x,可直接设置 app = Dash(assets_ignore='plotly.*'),并在 index_string 中手动写 <script src="/assets/plotly-lite.min.js"></script>,确保先于 dash 组件加载。
  5. 国内部署细节:
    a) 内网环境需把字体文件(Roboto、MathJax)也本地化,否则图标会回退到 Google Fonts 导致加载失败;
    b) 若走 CDN,推荐用“又拍云-静态压缩”或“阿里云 OSS + Brotli”,回源只需上传 .js.br 文件,边缘节点自动协商;
    c) 版本锁定:package.json 用 “plotly.js”: “file:./lib/plotly-lite” 锁定自编译版本,防止 CI 重新拉取官方大包。

答案

步骤化落地:

  1. 克隆 plotly.js 源码,checkout 与 Dash 依赖一致的版本(如 2.24.1)。
  2. 新建 src/index-lite.js:
    import Plotly from './core';
    import scatter from './traces/scatter';
    import bar from './traces/bar';
    import pie from './traces/pie';
    import heatmap from './traces/heatmap';
    import table from './traces/table';
    import exportToImage from './components/modebar/buttons/toImage';
    Plotly.register([scatter, bar, pie, heatmap, table, exportToImage]);
    export default Plotly;
  3. 用 rollup 打包:
    rollup index-lite.js -f umd -n Plotly -o plotly-lite.min.js -p @rollup/plugin-node-resolve -p @rollup/plugin-terser
    生成文件约 580 KB。
  4. 预压缩:
    gzip -k9 plotly-lite.min.js → 390 KB
    brotli -q 11 plotly-lite.min.js → 350 KB
    满足 ≤400 KB 要求。
  5. 放入 Dash 项目:
    mkdir -p assets/js
    cp plotly-lite.min.js* assets/js/
  6. 修改 app.py:
    app = Dash(name, serve_locally=True)
    app.scripts.config.serve_locally = True
    app.index_string = ''' <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Dash Lite</title> <script src="/assets/js/plotly-lite.min.js"></script> {%css%} </head> <body> {%app_entry%} {%config%} {%scripts%} </body> </html> '''
  7. 部署:
    把 assets 目录随代码一起打包成 Docker 镜像;Nginx 配置:
    location ~ .js$ {
    gzip_static on;
    brotli_static on;
    add_header Cache-Control "public, max-age=31536000, immutable";
    }
    首次加载 390 KB,缓存后后续页面秒开。

拓展思考

  1. 动态按需:如果仪表盘里图表类型由用户动态配置,可把 trace 拆成异步 chunk,用 import('plotly.js/lib/scatter') 在回调里动态注册,再调用 Plotly.react,实现“首屏 400 KB,后续懒加载”。
  2. 与 Vite 集成:Dash 2.10+ 支持 vite-plugin-dash,可把 plotly-lite 作为预构建依赖,利用 Vite 的 esbuild 压缩,CI 时间从 3 min 降到 30 s。
  3. 合规场景:金融、政务内网常要求“零外联”,除了 plotly-lite,还需把 MathJax、fonts、mapbox 瓦片全部本地化,并做内容安全哈希(SRI),防止第三方脚本注入。
  4. 监控与回滚:在 Nginx 日志中记录 $body_bytes_sent,若发现异常上涨,可判定新 bundle 被回退到完整版;结合 Prometheus + Grafana 告警,实现体积回归可观测。