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

eslint-plugin-tree-shaking

v1.12.2

Published

Marks all side-effects in module initialization that will interfere with tree-shaking

Downloads

103,782

Readme

eslint-plugin-tree-shaking

Marks all side-effects in module initialization that will interfere with tree-shaking

npm JavaScript Style Guide Greenkeeper badge semantic-release

Usage

This plugin is intended as a means for library developers to identify patterns that will interfere with the tree-shaking algorithm of their module bundler (i.e. rollup or webpack).

JavaScript:

myGlobal = 17;
const x = { [globalFunction()]: "myString" };

export default 42;

Rollup output:

myGlobal = 17;
const x = { [globalFunction()]: "myString" };

var index = 42;

export default index;

ESLint output:

1:1   error  Cannot determine side-effects of assignment to global variable
2:13  error  Cannot determine side-effects of calling global function

This plugin is most useful when you integrate ESLint with your editor.

Installation and Setup

You'll first need to install ESLint:

$ npm i eslint --save-dev

Next, install eslint-plugin-tree-shaking:

$ npm install eslint-plugin-tree-shaking --save-dev

Note: If you installed ESLint globally (using the -g flag) then you must also install eslint-plugin-tree-shaking globally.

Add tree-shaking to the plugins section of your .eslintrc configuration file. You can omit the eslint-plugin- prefix:

{
  "plugins": ["tree-shaking"]
}

Then add the rule no-side-effects-in-initialization to the rules section:

{
  "rules": {
    "tree-shaking/no-side-effects-in-initialization": 2
  }
}

To prevent false positives, configure like this:

{
  "rules": {
    "tree-shaking/no-side-effects-in-initialization": [
      2,
      {
        "noSideEffectsWhenCalled": [
          { "function": "Object.freeze" },
          {
            "module": "react",
            "functions": ["createContext", "createRef"]
          },
          {
            "module": "zod",
            "functions": ["array", "string", "nativeEnum", "number", "object", "optional"]
          },
          {
            "module": "my/local/module",
            "functions": ["foo", "bar", "baz"]
          }
        ]
      }
    ]
  }
}

Magic Comments

ESLint only ever analyzes one file at a time and by default, this plugin assumes that all imported functions have side-effects. If this is not the case, this plugin supports magic comments you can add before identifiers in imports and exports to specify that you assume an import or export to be a pure function. Examples:

  • By default, imported functions are assumed to have side-effects:

    JavaScript:

    import { x } from "./some-file";
    x();

    ESLint output:

    1:9  error  Cannot determine side-effects of calling imported function
  • You can mark a side-effect free import with a magic comment:

    JavaScript:

    import { /* tree-shaking no-side-effects-when-called */ x } from "./some-file";
    x();

    No ESLint errors

  • By default, exported functions are not checked for side-effects:

    JavaScript:

    export const x = globalFunction;

    No ESLint errors

  • You can check exports for side-effects with a magic comment:

    JavaScript:

    export const /* tree-shaking no-side-effects-when-called */ x = globalFunction;

    ESLint output:

    1:65  error  Cannot determine side-effects of calling global function

Background and Planned Development

This plugin is in development. If you want to contribute, please read CONTRIBUTING.md.

This plugin implements a side-effect detection algorithm similar to what rollup uses to determine if code can be removed safely. However, there is no one-to-one correspondence. If you find that you have code that

  • is not removed by rollup (even though tree-shaking is enabled) but
  • has no ESLint issues

please--if no-one else has done so yet--check the guidelines and file an issue!