New answer
In chain.js
, you see the first line is
import lodash from './wrapperLodash.js';
If we go to that file, we'll find a long explanation about how chaining is implemented using lazy evaluation that can shortcut iteratees until the call to value()
. Below that is an exported helper function defined like this:
function lodash(value) {
if (isObjectLike(value) && !isArray(value) && !(value instanceof LazyWrapper)) {
if (value instanceof LodashWrapper) {
return value;
}
if (hasOwnProperty.call(value, '__wrapped__')) {
return wrapperClone(value);
}
}
return new LodashWrapper(value);
}
Going back to chain.js
, we see how that is used in the chain()
function:
function chain(value) {
var result = lodash(value);
result.__chain__ = true;
return result;
}
Essentially, chain()
checks the input to make sure it's not already a wrapped value, and if it is, it either returns the value if it's an instance of the correct class, or it returns a new wrapped value.
There are no methods attached to any native prototype chains in this implementation, but it does create a new class called LodashWrapper
that wraps the input object with lodash
functionality and lazy evaluation optimizations.
Old answer
I believe the correct import
statement to apply tree-shaking would be
import chain from 'lodash-es/chain'
This imports the same module to the same variable as the import
statement used in the question, but the difference is that running import { chain } from 'lodash-es'
evaluates all of the imports in lodash.js
, whereas my import
method only touches the chain.js
file and whatever its necessary dependencies are in wrapperLodash.js
.