npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rollup-prepublish

v1.3.0

Published

Rollup tool to bundle ES6 modules into trim CJS files

Downloads

17

Readme

rollup-prepublish

A Rollup-based CLI with some handy defaults, useful as a prepublish script for library authors.

What it does:

The goal is to take your first-party and third-party ES6 modules and bundle them into a very trim CommonJS file, suitable to be exported to CommonJS users.

Install

npm install -g rollup-prepublish

Usage

rollup-prepublish index.js > bundle.js
rollup-prepublish --browser index.js > bundle-browser.js

In --browser mode, Rollup will use the "browser" field if it's available to serve up the "browser" version of the module. You may need to do this if any of your dependencies are using "browser".

You can pass in the input file and output file as the first and second arguments, or else it will assume CWD for the first (locating the main index.js as necessary) and stdout for the second.

Example prepublish script:

// package.json
{
  /* ... */
  "scripts": {
    "prepublish": "rollup-prepublish > lib/index.js && rollup-prepublish --browser > lib/browser.js"
  },
  "main": {
    "lib/index.js"
  },
  "browser": {
    "lib/index.js": "lib/browser.js"
  }
  /* ... */
}

With the above configuration, CommonJS consumers will get:

  • lib/index.js if they are using Node, and
  • lib/browser.js if they are using Webpack, Browserify, etc.

Also, Rollup consumers will get src/index.js (assuming that's your ES6 source).

--help output

Usage: rollup-prepublish [options] [inputFile] [outputFile]

Options:
  -b, --browser           Bundle using browser-resolve instead of node-resolve
                                                                       [boolean]
  -f, --fix-dependencies  Update dependencies in package.json to add external
                          deps                                         [boolean]
  -q, --quiet             Don't print warnings about excluded/included deps
                                                                       [boolean]
  -h, --help              Show help                                    [boolean]

Examples:
  rollup-prepublish            Bundle current project and output to stdout
  rollup-prepublish --browser  Same thing, but emit a browser bundle
  rollup-prepublish --quiet    Silence warnings

Tips

Since third-party ES6 modules are bundled by default, you can include them in your devDependencies instead of regular dependencies. (No reason to ship them to consumers, which unnecessarily increases npm install time!) On the other hand, note that this will prevent de-duplication of shared modules.

If you do so, though, then any transitive dependencies should now be treated as regular dependencies. You can automatically add them to your package.json using --fix-dependencies.

Also, if you have any "browser" code in your ES6 source, you should add those to "browser" as well:

{
  "browser": {
    "lib/index.js": "lib/browser.js",
    "src/somefile.js": "src/somefile-browser.js"
  }
}

You may also want to add a "jsnext:main" or "module" to your own package.json, so that consumers of your library can use rollup-prepublish script and get the same benefits.

{
  "module": {
    "src/index.js"
  }
}

JavaScript API

There's also a straight-up JavaScript API:

var rollupPrepublish = require('rollup-prepublish');
rollupPrepublish({
  entry: 'index.js',
  dest: 'bundle.js',
  browser: true, // false by default
  fixDependencies: true, // false by default
  quiet: true // false by default
}).then(function (code) {
  // if you don't specify a `dest`, then the code will be returned here
  // as a string
  console.log('code: ', code)
}).catch(function (err) {
  console.error(err)
})