Generate modular lodash builds for exactly what you use
npm install lodash-modularizeLodash 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).
alt="IMAGE ALT TEXT HERE" width="300" height="180" border="10" />
- 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)
All examples are taken from this project
``shList 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/**
lodash-modularize src/** -o src/depends/lodash.js
lodash-modularize src/** -o src/depends/lodash.js --format es6
##### Fancy
package.json scriptUse this tool to easily use lodash npm modules and avoid installing the entire lodash package (set
lodash as a dev dep)!`js
{
"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"
}
}
`$3
app.js
`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);
``sh
$ lodash-modularize app.js --list
=> each, flatten, sortBy, uniq
$ lodash-modularize ./test/sample.js --cjs -o lodash.js
`
lodash.js
`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)
`js
const _ = require('lodash');
const lodash = _;function reassignment() {
let _ = 'reassigned'.trim();
return _;
}
``