rollup vs tsup
JavaScript 打包工具
rolluptsup类似的npm包:

JavaScript 打包工具

JavaScript 打包工具是将多个 JavaScript 文件及其依赖项合并为一个或多个文件的工具,以优化网页加载时间和性能。这些工具通过分析代码依赖关系,消除未使用的代码(树摇),压缩文件大小,并支持模块化,使得最终生成的文件更高效,适合在浏览器或 Node.js 环境中使用。rollup 是一个专注于 ES 模块的打包工具,提供高效的树摇功能,生成小巧的输出文件,适合库和组件的打包。tsup 是一个快速且简单的打包工具,内置 TypeScript 支持,自动进行树摇和代码压缩,适合快速构建现代 JavaScript 和 TypeScript 项目。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
rollup026,2692.83 MB60111 天前MIT
tsup011,199390 kB3955 个月前MIT

功能对比: rollup vs tsup

树摇(Tree Shaking)

  • rollup:

    rollup 提供了出色的树摇功能,能够分析 ES 模块的依赖关系,自动删除未使用的代码,从而生成更小的输出文件。这对于库和组件的打包尤为重要,可以显著减少最终文件的大小。

  • tsup:

    tsup 也支持树摇,但依赖于底层的打包工具(如 esbuild)来实现。虽然它的树摇功能也很有效,但在可配置性和优化方面可能不如 rollup 那么细致。

TypeScript 支持

  • rollup:

    rollup 本身不提供内置的 TypeScript 支持,但可以通过插件(如 rollup-plugin-typescript2)来实现。这需要额外的配置,但提供了很大的灵活性。

  • tsup:

    tsup 原生支持 TypeScript,无需额外配置。这使得它在处理 TypeScript 项目时更加高效和方便,特别是对于需要快速构建的项目。

配置复杂性

  • rollup:

    rollup 提供了高度可配置的 API,允许开发者根据需要定制打包过程。然而,这也意味着它可能需要更多的配置时间和精力,特别是对于复杂的项目。

  • tsup:

    tsup 旨在提供简单的配置体验,默认设置就能满足大多数需求。对于需要快速打包的项目,tsup 的简单性是一个很大的优势。

输出格式

  • rollup:

    rollup 支持多种输出格式,包括 ES 模块、CommonJS 和 UMD。开发者可以根据需要灵活配置输出格式,适应不同的使用场景。

  • tsup:

    tsup 也支持多种输出格式,默认情况下会生成 ES 模块和 CommonJS 格式的文件。它的输出格式配置简单,适合快速设置。

社区和生态系统

  • rollup:

    rollup 拥有一个活跃的社区和丰富的插件生态系统,开发者可以找到许多现成的插件来扩展其功能。

  • tsup:

    tsup 虽然是一个较新的工具,但已经吸引了很多开发者,特别是在 TypeScript 社区。随着时间的推移,它的生态系统也在不断增长。

示例代码

  • rollup:

    使用 rollup 打包 TypeScript 项目

    # 安装 rollup 和相关插件
    npm install rollup rollup-plugin-typescript2 --save-dev
    
    # 创建 rollup.config.js
    import typescript from 'rollup-plugin-typescript2';
    
    export default {
      input: 'src/index.ts',
      output: {
        file: 'dist/bundle.js',
        format: 'esm',
      },
      plugins: [typescript()],
    };
    
  • tsup:

    使用 tsup 打包 TypeScript 项目

    # 安装 tsup
    npm install tsup --save-dev
    
    # 创建 tsup.config.ts
    import { defineConfig } from 'tsup';
    
    export default defineConfig({
      entry: ['src/index.ts'],
      format: ['esm', 'cjs'],
      dts: true,
    });
    
    # 运行打包
    npx tsup
    

如何选择: rollup vs tsup

  • rollup:

    如果你需要一个高度可配置的打包工具,特别是针对库和组件的打包,rollup 是一个很好的选择。它提供了优秀的树摇功能,可以生成非常小的输出文件,适合对文件大小敏感的项目。

  • tsup:

    如果你想要一个快速、开箱即用的打包工具,tsup 是一个理想的选择。它内置了 TypeScript 支持,配置简单,适合快速构建项目,特别是当你不想花太多时间在配置上时。

rollup的README

npm version node compatibility install size code coverage backers sponsors license Join the chat at https://is.gd/rollup_chat

Rollup

Overview

Rollup is a module bundler for JavaScript which compiles small pieces of code into something larger and more complex, such as a library or application. It uses the standardized ES module format for code, instead of previous idiosyncratic solutions such as CommonJS and AMD. ES modules let you freely and seamlessly combine the most useful individual functions from your favorite libraries. Rollup can optimize ES modules for faster native loading in modern browsers, or output a legacy module format allowing ES module workflows today.

Quick Start Guide

Install with npm install --global rollup. Rollup can be used either through a command line interface with an optional configuration file or else through its JavaScript API. Run rollup --help to see the available options and parameters. The starter project templates, rollup-starter-lib and rollup-starter-app, demonstrate common configuration options, and more detailed instructions are available throughout the user guide.

Commands

These commands assume the entry point to your application is named main.js, and that you'd like all imports compiled into a single file named bundle.js.

For browsers:

# compile to a <script> containing a self-executing function
rollup main.js --format iife --name "myBundle" --file bundle.js

For Node.js:

# compile to a CommonJS module
rollup main.js --format cjs --file bundle.js

For both browsers and Node.js:

# UMD format requires a bundle name
rollup main.js --format umd --name "myBundle" --file bundle.js

Why

Developing software is usually easier if you break your project into smaller separate pieces, since that often removes unexpected interactions and dramatically reduces the complexity of the problems you'll need to solve, and simply writing smaller projects in the first place isn't necessarily the answer. Unfortunately, JavaScript has not historically included this capability as a core feature in the language.

This finally changed with ES modules support in JavaScript, which provides a syntax for importing and exporting functions and data so they can be shared between separate scripts. Most browsers and Node.js support ES modules. However, Node.js releases before 12.17 support ES modules only behind the --experimental-modules flag, and older browsers like Internet Explorer do not support ES modules at all. Rollup allows you to write your code using ES modules, and run your application even in environments that do not support ES modules natively. For environments that support them, Rollup can output optimized ES modules; for environments that don't, Rollup can compile your code to other formats such as CommonJS modules, AMD modules, and IIFE-style scripts. This means that you get to write future-proof code, and you also get the tremendous benefits of...

Tree Shaking

In addition to enabling the use of ES modules, Rollup also statically analyzes and optimizes the code you are importing, and will exclude anything that isn't actually used. This allows you to build on top of existing tools and modules without adding extra dependencies or bloating the size of your project.

For example, with CommonJS, the entire tool or library must be imported.

// import the entire utils object with CommonJS
var utils = require('node:utils');
var query = 'Rollup';
// use the ajax method of the utils object
utils.ajax('https://api.example.com?search=' + query).then(handleResponse);

But with ES modules, instead of importing the whole utils object, we can just import the one ajax function we need:

// import the ajax function with an ES import statement
import { ajax } from 'node:utils';

var query = 'Rollup';
// call the ajax function
ajax('https://api.example.com?search=' + query).then(handleResponse);

Because Rollup includes the bare minimum, it results in lighter, faster, and less complicated libraries and applications. Since this approach is based on explicit import and export statements, it is vastly more effective than simply running an automated minifier to detect unused variables in the compiled output code.

Compatibility

Importing CommonJS

Rollup can import existing CommonJS modules through a plugin.

Publishing ES Modules

To make sure your ES modules are immediately usable by tools that work with CommonJS such as Node.js and webpack, you can use Rollup to compile to UMD or CommonJS format, and then point to that compiled version with the main property in your package.json file. If your package.json file also has a module field, ES-module-aware tools like Rollup and webpack will import the ES module version directly.

Contributors

This project exists thanks to all the people who contribute. [Contribute]. . If you want to contribute yourself, head over to the contribution guidelines.

Backers

Thank you to all our backers! 🙏 [Become a backer]

Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]

Special Sponsor

TNG Logo

TNG has been supporting the work of Lukas Taegert-Atkinson on Rollup since 2017.

License

MIT