包详细信息

@comandeer/rollup-lib-bundler

Comandeer13kMIT0.27.0

Simple library bundler

rollup, babel, library-bundler, bundler

自述文件

@comandeer/rollup-lib-bundler

Build Status codecov npm (scoped)

Super opinionated library bundler using Rollup, Babel and terser.

Installation

npm install @comandeer/rollup-lib-bundler --save-dev

Usage

Just make it a npm script:

"scripts": {
    "build": "rlb"
}

Configuration

No configuration. Consider it a feature.

How does it work?

It gets package.json from the current working directory, parses it and fetches the neeeded info:

  • name, author, version and license to create beautiful banner comment,
  • exports.import to determine where to save ESM bundle (see the "Supported exports syntax" section for more info).

Then the bundling happens. The default entry point for Rollup is src/index.js.

[!WARNING] Please note that dist directory is purged before bundling! So if anything should be there alongside the bundle, it should be added there after the bundling.

Assumed file structure

This is very opinionated bundler and it assumes that the project's file structure looks like the one below:

package/
|- package.json
|- src/
|     |- index.js
|     |- some-other-chunk.js
|- dist/
|      |- bundled-index.mjs
|      |- bundled-index.mjs.map
|      |- bundled-some-other-chunk.mjs
|      |- bundled-some-other-chunk.mjs.map
  • package.json is in the root of the package (the only bit we all agree on!),
  • src/ directory contains package's source,
    • index.js is the main entrypoint of the package,
    • some-other-chunk.js is the optional additional entrypoint (see #mutliple-bundles section for more info),
  • dist/ directory contains bundled code.

Bundler search for source files with the following extensions in the following order:

  • .mts,
  • .ts,
  • .mjs,
  • .js,
  • .cts,
  • .cjs.

Multiple bundles

By default, src/index.js is treated as the only entry point. However, using subpath exports you can create several bundled chunks/files. Example:

"exports": {
    ".": {
        "import": "./dist/package.mjs"
    },

    "./chunk": {
        "import": "./dist/chunk.mjs"
    }
}

In this case two source files will be bundled:

  • src/index.js:
    • ESM output: dist/package.mjs,
  • src/chunk.js:
    • ESM output: dist/chunk.mjs.

Each subpath is translated to appropriate file in src directory. Basically, ./ at the beginning is translated to src/ and the name of the subpath is translated to <subpath>.<extension> (e.g. ./chunksrc/chunk.js). The only exception is the . subpath, which is translated to src/index.js.

As of version 0.19.0 the bundler also automatically omits bundling bundles inside other bundles. If there were an import of the src/chunk.js file inside the src/index.js file in the above structure, then the dist/package.(c|m)js file would contain an import from dist/chunk.(c|m)js file instead of the content of the other bundle.

Supported exports syntax

The bundler supports only the subset of the exports syntax allowed by the Node.js:

  • exports as a string:

      {
          "exports": "./dist/package.mjs"
      }
    
  • subpaths as strings:

      {
          "exports": {
              ".": "./dist/package.mjs",
              "./subpath": "./dist/chunk.js"
          }
      }
    
  • exports with conditional exports:

      {
          "exports": {
              "types": "./dist/package.d.mts",
              "import": "./dist/package.mjs"
          }
      }
    
  • subpaths with conditional exports:

      {
          "exports": {
              ".": {
                  "types": "./dist/package.d.mts",
                  "import": "./dist/package.mjs"
              }
          }
      }
    

    Transpilation

The bundler transpiles all the code with Babel. The transpilation target can be specified with the engines.node field in the package.json file:

{
    "engines": {
        "node": "20.1.0"
    }
}

Any valid semver syntax is supported.

If the engines.node field is not specified or it contains invalid version, the bundler fallbacks to the current version – so the version of Node that was used to run the bundler.

TypeScript support

Starting from v0.17.0 the bundler is able also to bundle TypeScript projects. There is no configuration needed, just replace the .js extension with the .ts one! Also ensure that there's a valid tsconfig.json file in the root of your project. If you want to provide different configuration for the bundler, place a tsconfig.rlb.json file instead.

[!WARNING] The outDir config option is overridden by the bundler to point to the same directory as the one in the Rollup's configuration. See #327 for more details.

The bundler also bundles .d.ts files but only if you specified the exports.types field in your package.json.

Sample configuration for a TS project:

"exports": {
    ".": {
        "types": "./dist/index.d.ts",
        "import": "./dist/index.mjs"
    },

    "./chunk": {
        "import": "./dist/chunk.mjs"
    }
}

In this case two source files will be bundled:

  • src/index.ts:
    • ESM output: dist/index.mjs,
    • DTS output: dist/index.d.ts,
  • src/chunk.ts:
    • ESM output: dist/chunk.mjs,
    • DTS output: none (there's no types field).

Bundling executables (aka binaries)

From v0.19.0 rlb can also bundle executables defined in the bin field of the package.json. It supports both the simple format of that field and the complex one. Source files for binaries must be placed in the src/__bin__ directory with the same name as in the bin field. All source file formats supported for exports bundles are also supported for the bin ones.

All bundles created from the bin field are saved in the ESM format. The bundler will also preserve shebang in the produced bundle.

Example with the simple bin format

{
    "name": "some-package",
    "exports": {
        ".": {
            "import": "./dist/index.mjs"
        }
    },
    "bin": "./dist/bin.mjs"
}

In that case bundler excepts the following source file structure:

some-package/
|- package.json
|- src/
|     |- index.js
|     |- __bin__/
|     |         |- some-package.js

Please note that when using the simple bin format (so just the path to the executable, without its name), the bundler will look for the source file with the name of the package (derived from the name field in the package.json files).

Example with the complex bin format

{
    "name": "some-package",
    "exports": {
        ".": {
            "import": "./dist/index.mjs"
        }
    },
    "bin": {
        "whatever": "./dist/bin.mjs",
        "another-one": "./dist/bin2.js"
    }
}

In that case bundler excepts the following source file structure:

some-package/
|- package.json
|- src/
|     |- index.js
|     |- __bin__/
|     |         |- whatever.js
|     |         |- another-one.js

Support for non-standard dist directories

From v0.20.0 the bundler officially supports non-standard dist directories (different than the ./dist one). The dist directory is resolved from the exports field in the package.json, e.g.:

"exports": {
    ".": {
        "import": "./hublabubla/package.mjs"
    }
}

In the above example, the ./hublabubla directory will be used instead of the ./dist one.

The bundler supports also multiple non-standard dist directories, e.g.:

"exports": {
    ".": {
        "import": "./bublahubla/package.mjs"
    },
    "./chunk": "./hublabubla/chunk.mjs"
}

[!WARNING] Non-standard dist directories are purged befored the bundling! So if anything should be there alongside the bundle, it should be added there after the bundling.

Configuring VSC to correctly suggest imports

VSC uses TypeScript rules to suggest imports. However, TS uses CJS rules by default, ignoring the constraints of the exports field and suggesting the whole file paths (e.g. <package>/dist/<file> instead of <package>/<submodule-name>). To fix it, TS must be configured by tsconfig.json or jsconfig.json file to resolve modules according to ESM rules:

{
    "module": "node16",
    // or
    "moduleResolution": "node16"
}

License

See LICENSE file for details.

更新日志

@comandeer/rollup-lib-bundler Changelog

All notable changes to this project will be documented in this file. The format is based on Keep a Changelog and this project adheres to Semantic Versioning.


0.27.0 – 2025-04-05

Changed

0.26.0 – 2025-03-09

Changed

  • #335: BREAKING CHANGE: invalid exports shapes (both subpaths and import) are no longer supported.
  • #337: BREAKING CHANGE: updated dependencies:

    | Dependency | Old version | New version | | ---------------------------------------- | ----------- | ----------- | | @babel/core | ^7.25.7 | ^7.26.9 | | @babel/plugin-syntax-import-assertions | ^7.25.7 | ^7.26.0 | | @babel/preset-env | ^7.25.7 | ^7.26.9 | | ☠️ @rollup/plugin-commonjs | ^28.0.0 | N/A | | @rollup/plugin-typescript | ^12.1.0 | ^12.1.2 | | chalk | ^5.3.0 | ^5.4.1 | | globby | ^14.0.2 | ^14.1.0 | | magic-string | ^0.30.11 | ^0.30.17 | | ⚠️ pathe | ^1.1.2 | ^2.0.3 | | rollup | ^4.24.0 | ^4.35.0 | | semver | ^7.6.3 | ^7.7.1 | | tslib | ^2.7.0 | ^2.8.1 | | typescript | ^5.6.2 | ^5.8.2 |

    Dependencies with major version change are marked with the "⚠️" emoji.

    Removed dependencies are marked with the "☠️" emoji.

  • #339: BREAKING CHANGE: the shape of the public API has changed from the default export to the named rlb one.

0.25.0 – 2024-11-16

Added

  • #275: support for exports as a string.

Changed

  • #234: BREAKING CHANGE: updated dependencies:

    | Dependency | Old version | New version | | ---------- | ----------- | ----------- | | ⭐ semver | N/A | ^7.6.3 |

    New dependencies are marked with the "⭐" emoji.

  • #234: BREAKING CHANGE: the transpilation target is now determined by the engines.node field in the package.json file and default to the current version (the one used to run the bundler).
  • #332: BREAKING CHANGE: the minimal supported version of Node.js was bumped to 20.11.0.

0.24.0 – 2024-10-08

Changed

  • #321: BREAKING CHANGE: updated dependencies:

    | Dependency | Old version | New version | | ---------------------------------------- | ----------- | ----------- | | @babel/core | ^7.24.5 | ^7.25.7 | | @babel/plugin-syntax-import-assertions | ^7.24.1 | ^7.25.7 | | @babel/preset-env | ^7.24.5 | ^7.25.7 | | ⚠️ @rollup/plugin-commonjs | ^25.0.7 | ^28.0.0 | | ⚠️ @rollup/plugin-typescript | ^11.1.6 | ^12.1.0 | | globby | ^14.0.1 | ^14.0.2 | | magic-string | ^0.30.1 | ^0.30.11 | | ⚠️ rimraf | ^5.0.5 | ^6.0.1 | | rollup | ^4.17.2 | ^4.24.0 | | rollup-plugin-dts | ^6.1.0 | ^6.1.1 | | tslib | ^2.6.2 | ^2.7.0 | | typescript | ^5.4.5 | ^5.6.2 |

    Dependencies with major version change are marked with the "⚠️" emoji.

  • #327: BREAKING CHANGE: the value of the outDir from the tsconfig.json file is overridden to point to the same directory as the one in Rollup's configuration.

Fixed

  • #309: the spinner is correctly hidden before displaying any results.

0.23.0 – 2024-05-01

Added

  • #314: support for Node 22.

Changed

  • #311: BREAKING CHANGE: updated dependencies:

    | Dependency | Old version | New version | | ---------------------------------------- | ----------- | ----------- | | @babel/core | ^7.22.0 | ^7.24.5 | | @babel/plugin-syntax-import-assertions | ^7.22.5 | ^7.24.1 | | @babel/preset-env | ^7.22.9 | ^7.24.5 | | ⚠️ @comandeer/cli-spinner | ^0.3.2 | ^1.0.2 | | @rollup/plugin-babel | ^6.0.3 | ^6.0.4 | | @rollup/plugin-commonjs | ^25.0.2 | ^25.0.7 | | @rollup/plugin-json | ^6.0.0 | ^6.1.0 | | @rollup/plugin-terser | ^0.4.3 | ^0.4.4 | | @rollup/plugin-typescript | ^11.1.2 | ^11.1.6 | | @rollup/plugin-virtual | ^3.0.1 | ^3.0.2 | | ⭐ chalk | N/A | ^5.3.0 | | ☠️ console-control-strings | ^1.1.0 | N/A | | ⚠️ globby | ^13.2.2 | ^14.0.1 | | pathe | ^1.1.1 | ^1.1.2 | | rimraf | ^5.0.1 | ^5.0.5 | | ⚠️ rollup | ^3.26.2 | ^4.17.2 | | ⚠️ rollup-plugin-dts | ^5.3.0 | ^6.1.0 | | tslib | ^2.6.0 | ^2.6.2 | | typescript | ^5.1.6 | ^5.4.5 |

    New dependencies are marked with the "⭐" emoji.

    Dependencies with major version change are marked with the "⚠️" emoji.

    Removed dependencies are marked with the "☠️" emoji.

Removed

  • #314: BREAKING CHANGE: support for Node 16 & 18.

0.22.1 – 2023-07-28

Fixed

  • #303: incorrect package published on GitHub.
  • #306: incorrect orderd of exports in the package.json file.

0.22.0 – 2023-07-23

Added

  • #232: type definitions for the library.

Changed

  • #232: BREAKING CHANGE: rewrote the project in TS.

0.21.0 – 2023-07-20

Changed

  • #300: BREAKING CHANGE: updated the logic for linking to other bundles.
  • #279: BREAKING CHANGE: updated dependencies:

    | Dependency | Old version | New version | | ---------------------------------------- | ----------- | ----------- | | @babel/core | ^7.20.12 | ^7.22.9 | | @babel/plugin-syntax-import-assertions | ^7.20.0 | ^7.22.5 | | ☠️ @babel/plugin-transform-typescript | ^7.20.13 | N/A | | @babel/preset-env | ^7.20.2 | ^7.22.9 | | ☠️ @babel/types | ^7.20.7 | N/A | | ⚠️ @rollup/plugin-commonjs | ^24.0.0 | ^25.0.2 | | ⚠️ @rollup/plugin-terser | ^0.3.0 | ^0.4.3 | | @rollup/plugin-typescript | ^11.0.0 | ^11.1.2 | | globby | ^13.1.3 | ^13.2.2 | | ⭐ magic-string | N/A | ^0.30.1 | | pathe | ^1.1.0 | ^1.1.1 | | ⚠️ rimraf | ^4.0.7 | ^5.0.1 | | rollup | ^3.10.0 | ^3.26.2 | | tslib | ^2.4.1 | ^2.6.0 | | ⚠️ typescript | ^5.1.6 | ^4.9.4 |

    New dependencies are marked with the "⭐" emoji.

    Dependencies with major version change are marked with the "⚠️" emoji.

    Removed dependencies are marked with the "☠️" emoji.

Removed

  • #276: BREAKING CHANGE: outputting CJS bundles; now only the ESM one is produced.

Fixed

  • #300: incorrect linking to other bundles in .d.ts files.

0.20.0 – 2023-07-13

Added

  • #264: support for bundling .d.cts and .d.mts type declaration files.
  • #265: official support for non-standard dist directories.
  • #271: support for import assertions syntax to enable importing JSON files in the pure ESM packages.
  • #278: official support for Node.js 20.

Changed

  • #233: BREAKING CHANGE: package is a pure ESM one now.
  • #266: BREAKING CHANGE: remove warning for skipping a CJS build.
  • #264: updated the rollup-plugin-dts dependency from ^5.1.1 to ^5.3.0.
  • #265: added docs around import suggestions in VSC.

Removed

  • #249: BREAKING CHANGE: CJS bundle.

Fixed

  • #277: incorrect file permissions for bundled executables.

0.19.1 – 2023-02-25

Fixed

  • #267: the @babel/preset-env dependency was incorrectly marked as the dev one instead of production one.
  • #268: the package.json file contained the incorrect path to the rlb executable.

0.19.0 – 2023-02-23

Added

  • #116: support for bundling binaries based on the bin field from the package.json file.

Changed

  • #230: BREAKING CHANGE: bundler now omits bundling files that are marked in package.json as bundle entrypoints; such files will be always treated as external and imported.
  • #243: BREAKING CHANGE: bundler of type definitions loads now the tsconfig.json file of the project being bundled.
  • #247: BREAKING CHANGE: the bundler's package.json contains now only the exports-based entrypoints.
  • #248: BREAKING CHANGE: bundler of type definitions uses now virtual filesystem instead of a real temporary directory.
  • BREAKING CHANGE: the list of dependencies changed:

    | Dependency | Added/Removed | Old version | New version | | ------------------------------------ | ------------- | ----------- | ----------- | | @babel/plugin-transform-typescript | Added | N/A | ^7.20.13 | | @babel/types | Added | N/A | ^7.20.7 | | @rollup/plugin-virtual | Added | N/A | ^3.0.1 | | rollup-plugin-preseve-shebang | Added | N/A | ^1.0.1 | | tempy | Removed | ^3.0.0 | N/A |

  • #235: binary now uses the ESM version of the bundler.

Fixed

  • #255: .ts source files were not transpiled by Babel to syntax understandable by specified Node.js version (v16.0.0 at the moment of fixing).

0.18.0 – 2023-02-05

Changed

  • #209: BREAKING CHANGE: parsing the project's package.json is no longer blocking as all file operations were rewritten to be asynchronous.

Removed

  • #228: BREAKING CHANGE: support for non-exports entrypoints.

Fixed

  • #240: bundling types incorrectly removes bundled types definitions right after creating them.
  • #242: bundling of types creates incorrect file structure in dist/ if source files are inside subdirectories.

0.17.0 – 2023-01-22

Added

  • #220: Support for bundling TypeScript projects.

Changed

  • #225: BREAKING CHANGE: Update dependencies, including major versions:
    • @rollup/plugin-babel5.3.1^6.0.3;
    • @rollup/plugin-commonjs22.0.2^24.0.0;
    • @rollup/plugin-json4.1.0^6.0.0;
    • rimraf3.0.2^4.0.7;
    • rollup2.79.1^3.10.0;
    • rollup-plugin-terser@rollup/plugin-terser.

0.16.1 – 2022-11-10

Fixed

  • #222: dynamic external imports aren't preserved.

0.16.0 – 2022-05-08

Added

  • #214: Support for Node 18.

    Changed

  • #216: BREAKING CHANGE: update dependencies including major versions of:
    • @rollup/plugin-commonjs from 21.x to ^22.0.0.
  • #215: make CJS builds optional.

    Removed

  • #214: BREAKING CHANGE: support for Node 12 and 14.

0.15.1 – 2022-03-07

Fixed

  • #212: rimraf is incorrectly marked as a dev dependency instead of a production one.

0.15.0 – 2022-03-07

Added

  • #204: BREAKING CHANGE: clearing the dist/ directory before bundling.
  • #185: Support for subpath exports.

    Changed

  • #199: BREAKING CHANGE: update dependencies including major versions of:
    • @rollup/plugin-commonjs from 19.x to ^21.0.0.
  • #202: spinner provided by gauge is replaced by @comandeer/cli-spinner.

    Fixed

  • #208: warnings are logged to the stdout instead of stderr.

0.14.0 – 2021-07-21

Added

  • #189: Support for Node 16.
  • #156: User-friendly error handling.

    Changed

  • #197: BREAKING CHANGE: public API has changed to be always asynchronous.
  • #191: BREAKING CHANGE: update @rollup/plugin-commonjs from ^18.0.0 to ^19.0.1 and other dependencies.
  • #193: New, better output.

0.13.0 – 2021-05-03

Added

  • #61: Support for native ESM via exports field in a package.json file.

    Removed

  • #179 BREAKING CHANGE: support for Node 10.

0.12.0 – 2020-10-17

Changed

  • #176 Update dependencies, including major versions:
    • @rollup/plugin-commonjs – 11.1.0 → 15.1.0;
    • rollup-plugin-terser – 5.3.0 → 7.0.2;
    • mocha – 7.1.2 → 8.2.0 (dev).

0.11.0 – 2020-05-10

Added

Changed

  • #173 rollup-plugin-babel and rollup-plugin-commonjs dependencies are now @rollup/plugin-babel and @rollup/plugin-commonjs ones.

Fixed

0.10.0 – 2020-04-11

Changed

  • #163 BREAKING CHANGE: switch from babel-minify to terser.

Removed

  • #162 BREAKING CHANGE: support for Node 8.
  • #164 BREAKING CHANGE: named exports from the package.

0.9.0 – 2019-06-30

Added

  • #129 Support for Node 12.
  • #128 Official support for Windows and macOS.

Removed

  • #129 BREAKING CHANGE: support for Node 6.

0.8.0 – 2019-01-20

Changed

  • #110 BREAKING CHANGE: Update rollup to ^1.1.0.
  • #111 Update rollup-plugin-babel-minify to ^7.0.0.

0.7.1 – 2018-10-07

Fixed

  • #105 Incorrect sourcemaps for bundles.

0.7.0 – 2018-08-29

Added

  • #71 Add support for Babel 7.

Remove

  • #71 BREAKING CHANGE: remove support for Babel < 7.

0.6.0 – 2018-05-20

Changed

  • #83 BREAKING CHANGE: update rollup-plugin-babel-minify to version ^5.0.0.

0.5.0 – 2018-05-01

Added

  • #80 Add support for Node 10.

Changed

  • #67 BREAKING CHANGE: CJS bundle is also transpiled.
  • #68 BREAKING CHANGE: update @comandeer/babel-preset-rollup to version ^2.0.0.
  • #74 BREAKING CHANGE: rename keys returned by packageParser:
    • dist.es5dist.cjs,
    • dist.es2015dist.esm.

Removed

  • #62 BREAKING CHANGE: remove support for Node <6, 7, 9.

0.4.2 – 2018-02-11

Fixed

  • #58 Usage of deprecated Rollup config options.

0.4.1 – 2018-02-11

Added

  • #54 Add babel-core as dependency.

0.4.0 – 2018-02-03

Changed

  • #48 BREAKING CHANGE: add new line after the banner comment.

Fixed

  • #49 Fix incorrect version of package in banner comment.

0.3.0 – 2017-09-16

Changed

  • #30 BREAKING CHANGE: force Babel to ignore .babelrc file.
  • #16 Update rollup to ^0.50.0.
  • b36613d Update other dependencies.

0.2.0 – 2017-09-03

Changed

  • #17 Switch from rollup-plugin-babili to rollup-plugin-babel-minify.
  • #21 Update rollup to ^0.49.2.
  • #17 Remove rollup-plugin-uglify in favor of rollup-plugin-babel-minify.
  • #18 Replace babel-preset-es2015-rollup with dedicated evergreen @comandeer/babel-preset-rollup.

0.1.2 – 2017-06-17

Fixed

  • #6 Fix incorrect parsing of author's metadata.

0.1.1 – 2017-03-19

Added

0.1.0 – 2017-03-19

Added

  • First working version, yay!