synergetic
synergetic

Reputation: 8036

requiring lodash modules with webpack

I used to use lodash.js without any packaging like below:

if (_.isFunction(myFunc)) { ... } 

Recently, I started using webpack. Now, it seems many functions such as isFunction are not part of the core module. So, the following works:

var _ = require("lodash/core");
var _isFunction = require("lodash/isFunction");

However, the above changes the syntax when I used lodash without webpack (no dot). Should I require like below:

_.isFunction = require("lodash/isFunction")

so, that to maintain the same syntax?

Upvotes: 1

Views: 1564

Answers (1)

Chris
Chris

Reputation: 58182

When you do something like the following, you import everything under one big lodash object:

var _ = require('lodash');

_ (or whatever var you choose) will be filled with a bunch of functions:

_ = {
    isX: function... 
    isY: function... 
    // ... etc
}

Ie: it brings everything in.

When you do the following, it will import only the required module directly into the variable:

var anynameX = require('lodash/isX'); 
var anynameY = require('lodash/isY');

anynameX and anynameY can be anything.

In other words:

_.isX === anynameX;    
_.isY === anynameY;

So to answer your last question, you could do:

_.anynameX = require('lodash/isX');

but only if _ already exists (and isn't frozen), for example:

var _ = {};
_.anynameX = require('lodash/isX');

Upvotes: 2

Related Questions