lodash-modularize
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')
torequire('./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
package.json
script
Fancy 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
;let log = lodash = ; let result = ;lodash;
$ lodash-modularize app.js --list# => each, flatten, sortBy, uniq $ lodash-modularize ./test/sample.js --cjs -o lodash.js
lodash.js
var each = ;var flatten = ;var sortBy = ;var uniq = ; { throw 'lodash chaining is not included in this build... Try rebuilding.';}moduleexports = lodash; lodasheach = each;lodashflatten = flatten;lodashsortBy = sortBy;lodashuniq = 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 _ = ;const lodash = _; { let _ = 'reassigned'; return _;}