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-modularize

v1.3.3

Published

Generate modular lodash builds for exactly what you use

Downloads

53

Readme

lodash-modularize Dependency Status

Lodash is starting to get pretty heafty; this is a tool to generate modular lodash builds so lodash only includes what you use. This can lead to faster startup and smaller builds (when using compile, browserify, r.js, etc).

Whats in the Box

Features

  • Detect lodash methods/modules being used in source code
  • Compile a perfect custom lodash build using the cli
  • Compile a custom modular lodash.js which imports the exact modules you use
  • Update references (e.g.) require('lodash') to require('./src/custom-lodash'
  • Supports AMD, CJS, ES6 and UMD
  • Natural recompilation: if you decide to output a build (updating references), the tool recognizes lodash features coming from the output path (e.g. lib/lodash.js) in addition to regular lodash sources.
  • Supports using lodash npm modules
  • Other sweetness (see below and try lodash-modularize --help)

Example Usage

All examples are taken from this project

# List all the method's being used in src
lodash-modularize src/** --list
# => assign,chain,flatten,includes,isArray,reject,result,template,uniq,zipObject

lodash-modularize src/**
# <formated module>

lodash-modularize src/** -o src/depends/lodash.js

lodash-modularize src/** -o src/depends/lodash.js --format es6

# Set the global variable to search for to `lodash`
lodash-modularize src/** -o src/depends/lodash.js --global lodash --exports umd

# Compile the code using lodash-cli!
lodash-modularize src/** -o src/depends/lodash.js --amd --compile

# Update the projects using lodash to use the built depends/lodash instead
lodash-modularize src/** -o depends/lodash.js --update
Fancy package.json script

Use this tool to easily use lodash npm modules and avoid installing the entire lodash package (set lodash as a dev dep)!

{
    "devDependencies": {"lodash": "^3.0", "lodash-modularize": "^1.0"},
    "scripts": {
        "prepublish": "lodash-modularize src/**.js -o src/depends/lodash.js -u --use-npm-modules --install-npm-modles"
    }
}

So what can it detect

app.js

import _, {sortBy, uniq} from 'lodash';
let log = require('logger'),
    lodash = require('lodash');

let result = sortBy(_.flatten(uniq([{a: 1}, {a: 2}, {a: 1}, {a: 0}])), 'a');
lodash.each(result, log);
$ lodash-modularize app.js --list
# => each, flatten, sortBy, uniq

$ lodash-modularize ./test/sample.js --cjs -o lodash.js

lodash.js

var each = require('lodash/collection/each');
var flatten = require('lodash/array/flatten');
var sortBy = require('lodash/collection/sortBy');
var uniq = require('lodash/array/uniq');

function lodash() {
  throw 'lodash chaining is not included in this build... Try rebuilding.';
}
module.exports = lodash;

lodash.each = each;
lodash.flatten = flatten;
lodash.sortBy = sortBy;
lodash.uniq = uniq;

And many other patterns including globals (opt-in), chaining, and mixins.

Notes

Lazy chaining is not fully supported (it works but its not lazy).

You should use in conjunction with linters (jshint/eslint/etc) as this won't detect unused variables.

All though we go out of our way to be robust and support various ways to detect lodash imports of lodash there are things we don't bother to handle. For example if you do any of these things, we'll probably miss it (same goes for global variables)

const _ = require('lodash');
const lodash = _;

function reassignment() {
    let _ = 'reassigned'.trim();
    return _;
}