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

lodash-imports-updater

v1.0.0

Published

A script for JSCodeShift that updates JS files to use lighter lodash imports and reduce the bundle's size

Downloads

3

Readme

LodashImportUpdater

A script that updates JS files to use lighter lodash imports and reduce the bundle's size

What it does

It changes default lodash imports like:

import _ from 'lodash';

_.map(array, mapper);

_(array)
  .filter(predicate)
  .map(transformer)
  .value();

To method imports like:

import filter from 'lodash/filter';
import flow from 'lodash/flow';
import map from 'lodash/map';

map(array, mapper);

flow([
  (_) => filter(_, predicate),
  (_) => map(_, predicate),
])(array);

This allows tree-shaking to work efficiently and to shave off a good part of lodash from your app bundle, read this article for more details.

A more complete example of what is supported is available in the examples.

:information_source: If you spot a code pattern where the lodash import is not updated, please either submit a PR or open an issue with a base snippet of code that shows the problematic code pattern.

How to use it

npm install --global lodash-import-updater
update-lodash-imports "$GLOB_FOR_YOUR_SOURCE_CODE"

The glob is interpreted by fast-glob if quoted, by your shell if not. This has impacts on how ** is interpreted.

In your shell, ** probably strictly means one folder of any name in the current location, this means that update-lodash-imports folder/**/*.js matches folder/whatever/file.js but not folder/whatever.js or folder/subfolder/subsubfolder/whatever.js.

With fast-glob, ** means any directory recursively. This means that update-lodash-imports 'folder/**/*/js' matches all the following files folder/whatever/file.js, folder/whatever.js, and folder/subfolder/subsubfolder/whatever.js.

For more information on how the globs work, search for documentation on your shell's globbing system. For fast-glob, the documentation is here.

:warning: Beware the transformed code probably won't follow your formatter/linter's rules. You should re-apply them afterwards with your usual tools (ex: npx eslint --fix .).

Example:

update-lodash-imports 'src/**/*.js'

How it does its thing

It uses JSCodeshift to parse your code to an AST, then applies a transformation and re-generates the source code from the new AST.

Bundle size improvement

| | Legacy imports | Method imports | | -------------------- | ------------------------------------------------------------ | ------------------------------------------------------------- | | Without tree shaking | 94.1 Kb | 61.8 Kb | | With tree shaking | 92.4 Kb | 24.0 Kb |

Support

This tool has been written with minimal effort, this means that no effort went into building a comprehensive test suite or checking compatibility for use in other OSes than the author's.

This tool therefore should work seamlessly on UNIX systems but there is no guarantee that it works on Windows.

PRs are welcome if someone intends to fix this.