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-plugin-tree-shakeable

v1.0.3

Published

A Rollup plugin that automatically annotates your module as tree shakeable.

Downloads

4,714

Readme

Install

$ npm i rollup-plugin-tree-shakeable

Usage

import treeShakeable from 'rollup-plugin-tree-shakeable'

export default {
  input: `src/index.js`,
  output: {
    dir: `output`,
    format: `esm`,
  },
  plugins: [treeShakeable()],
}

Why?

Imagine you have code similar to this:

src/index.js:

const withLogging =
  fn =>
  (...args) => {
    console.log(`Started call!`)
    try {
      return fn(...args)
    } finally {
      console.log(`Finished call!`)
    }
  }

export const f1 = withLogging(/* ... */)
export const f2 = withLogging(/* ... */)
export const f3 = withLogging(/* ... */)
// ...

You might expect that if a user of your package writes import { f1 } from 'your-package', then bundlers will tree shake and omit f1, f2, etc. from the final bundle. Unfortunately, bundlers will generally include all of your package's code in this case (yes, even if you set "sideEffects": false in your package.json) because withLogging is called at the "top-level" of your package and the bundler cannot easily tell the call is side-effect free. There are many other code patterns that cause the same issue.

This plugin solves the problem by automatically adding @__PURE__ annotations to all top-level expressions in your code that prevent tree-shaking, with the assumption that your package is actually pure, but bundlers need a little convincing of that.

For example, for the code above, running it through this plugin would result in the following code:

const withLogging =
  fn =>
  (...args) => {
    console.log(`Started call!`)
    try {
      return fn(...args)
    } finally {
      console.log(`Finished call!`)
    }
  }

export const f1 = /* @__PURE__*/ withLogging(/* ... */)
export const f2 = /* @__PURE__*/ withLogging(/* ... */)
export const f3 = /* @__PURE__*/ withLogging(/* ... */)
// ...

And if a user of your package writes import { f1 } from 'your-package', then bundlers will tree shake and strip out all functions other than f1 from the final bundle.

[!CAUTION]

Only use this plugin if your package is actually tree-shakeable, meaning that each export would still function correctly if all the other exports were stripped out.

This plugin does not give your package that property. It only convinces bundlers that this is the case.

Contributing

Stars are always welcome!

For bugs and feature requests, please create an issue.

License

MIT © Tomer Aberbach
Apache 2.0 © Google