rollup

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Rollup

Rollup

Rollup (v4) is the bundler of choice for libraries. It produces smaller, cleaner code than Webpack and pioneered Tree Shaking.
Rollup(v4版本)是类库开发的首选打包工具。它生成的代码比Webpack更小巧、更简洁,并且是Tree Shaking技术的开创者。

When to Use

适用场景

  • NPM Packages: Building a library for others to use.
  • Flat Bundling: "Scope Hoisting" puts everything in one closure for performance.
  • NPM包开发:为其他开发者构建可复用类库。
  • 扁平化打包:"Scope Hoisting"(作用域提升)将所有代码放入单个闭包中,提升性能。

Core Concepts

核心概念

Tree Shaking

Tree Shaking

Rollup analyzes the import graph and excludes unused code statically.
Rollup会分析导入依赖图,静态排除未使用的代码。

Formats

输出格式

Outputs
esm
,
cjs
,
umd
,
iife
.
支持输出
esm
cjs
umd
iife
格式。

Plugins

插件生态

The ecosystem that Vite adopted.
Vite所采用的插件生态系统就源自Rollup。

Best Practices (2025)

2025年最佳实践

Do:
  • Use
    pkg.exports
    : Configure
    package.json
    exports correctly for ESM/CJS dual publish.
  • Externalize dependencies: Don't bundle
    react
    into your library. Mark it as external.
Don't:
  • Don't use for Apps: Use Vite (which uses Rollup internally) for apps. Use Rollup directly for libs.
推荐做法
  • 使用
    pkg.exports
    :正确配置
    package.json
    中的exports字段,实现ESM/CJS双模式发布。
  • 外部化依赖:不要将
    react
    等依赖打包进你的类库,将其标记为外部依赖。
不推荐做法
  • 不要用于应用开发:应用开发请使用Vite(其内部基于Rollup实现),直接使用Rollup进行类库开发即可。

References

参考资料