user91579631 user91579631 - 26 days ago 8
Node.js Question

Require small section of lodash to minimize size

I am using browserify on lodash and discovered that lodash made up a significant chunk of the browserified

bundled.js
.

I imported in the entire lodash but I mainly use the
cloneDeep
method so far.

var lodash = require('lodash');


How can I import only the necessary part of lodash to keep
bundled.js
small? I am using node.js v6

Answer

Take note that there are differences when you call lodash functions depending on how you import lodash.

var lodash = require('lodash');
var clone = lodash.cloneDeep(obj);

If you do cherry-picking as John-David Dalton suggests, you need to call the function this way.

var lodash_clonedeep = require('lodash/cloneDeep');
var clone = lodash_clonedeep(obj);

The experts may overlook the possibility that newbies may not know the correct way to call the lodash functions.

Comments