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

@optimize-lodash/rollup-plugin

v5.0.0

Published

Rewrite lodash imports with Rollup for improved tree-shaking.

Downloads

189,028

Readme

Optimize lodash imports with Rollup.js

npm node-current npm peer dependency version compatible with Vite 3.x GitHub Workflow Status license Codecov GitHub last commit

There are multiple issues surrounding tree-shaking of lodash. Minifiers, even with dead-code elimination, cannot currently solve this problem. Check out the test showing that even with terser as a minifier, this plugin can still reduce bundle size by 70% for an example input. With this plugin, bundled code output will only include the specific lodash methods your code requires.

There is also an option to use lodash-es for projects which ship CommonJS and ES builds: the ES build will be transformed to import from lodash-es.

Note: versions of this plugin prior to 5.x supported NodeJS 12 and Rollup 2.x - 3.x. If you need support for these older versions, please use the 4.x release. Rollup 4.x contains significant performance improvements over previous versions and is highly recommended.

This input

import { isNil, isString } from "lodash";
import { padStart as padStartFp } from "lodash/fp";

Becomes this output

import isNil from "lodash/isNil.js";
import isString from "lodash/isString.js";
import padStartFp from "lodash/fp/padStart.js";

useLodashEs for ES Module Output

While lodash-es is not usable from CommonJS modules, some projects use Rollup to create two outputs: one for ES and one for CommonJS.

In this case, you can offer your users the best of both:

Your source input

import { isNil } from "lodash";

CommonJS output

import isNil from "lodash/isNil.js";

ES output (with useLodashEs: true)

import { isNil } from "lodash-es";

Usage

import { optimizeLodashImports } from "@optimize-lodash/rollup-plugin";

export default {
  input: "src/index.js",
  output: {
    dir: "dist",
    format: "cjs",
  },
  plugins: [optimizeLodashImports()],
};

Options

Configuration can be passed to the plugin as an object with the following keys:

exclude

Type: String | Array[...String] Default: null

A minimatch pattern, or array of patterns, which specifies the files in the build the plugin should ignore. By default no files are ignored.

include

Type: String | Array[...String] Default: null

A minimatch pattern, or array of patterns, which specifies the files in the build the plugin should operate on. By default all files are targeted.

useLodashEs

Type: boolean Default: false

If true, the plugin will rewrite lodash imports to use lodash-es.

Note: the build will fail if your Rollup output format is not also set to es, esm, or module!

appendDotJs

Type: boolean Default: true

If true, the plugin will append .js to the end of CommonJS lodash imports.

Set to false if you don't want the .js suffix added (prior to v3.x, this was the default).

Vite Compatibility

This plugin "just works" as a Vite 3.x plugin. Simply add it to plugins in your Vite config:

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import { optimizeLodashImports } from "@optimize-lodash/rollup-plugin";

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react(), optimizeLodashImports()],
});

Example Vite output for a use of kebabCase:

| No plugin | With plugin | | ------------------------------------------------------------ | ------------------------------------------------------------ | | dist/assets/index.497fb95b.js 212.88 KiB / gzip: 71.58 KiB | dist/assets/index.54b72c40.js 146.31 KiB / gzip: 47.81 KiB |

A ~23 KiB reduction in compressed size!

Limitations

Default imports are not optimized

Unlike babel-plugin-lodash, there is no support for optimizing the lodash default import, such as in this case:

// this import can't be optimized
import _ from "lodash";

export function testX(x) {
  return _.isNil(x);
}

The above code will not be optimized, and the plugin will print a warning. (Note: Vite supresses these warnings at build time unless --debug is added to the build command.)

To avoid this, always import the specific method(s) you need:

// this import will be optimized
import { isNil } from "lodash";

export function testX(x) {
  return isNil(x);
}

Alternatives

babel-plugin-lodash solves the issue for CommonJS outputs and modifies default imports as well. However, it doesn't enable transparent lodash-es use and may not make sense for projects using @rollup/plugin-typescript which don't wish to add a Babel step.

Other alternatives include eslint-plugin-lodash with the import-scope rule enabled. This works for CommonJS outputs, but may require manual effort to stay on top of imports.