lodash / babel-plugin-lodash

Modular Lodash builds without the hassle.
Other
1.96k stars 91 forks source link

'unionBy' do not accept property name as iteratee #231

Closed guanpu closed 5 years ago

guanpu commented 5 years ago

lodash version: 4.17.11 babel-plugin-lodash version: ^3.3.4

the code below works fine when import complete lodash

import _ from 'lodash';
...
this.currentdata = _.sortBy(_.unionBy(data, this.currentdata, 'id'), i => i.id);

but after using this plugin, and change code to:

import { sortBy, unionBy } from 'lodash';
this.currentdata = sortBy(unionBy(data, this.currentdata, 'id'), i => i.id);

it reports error:

index.jsx:103 TypeError: iteratee is not a function
    at baseUniq (_baseUniq.js:47)
    at unionBy.js:36
    at apply (_apply.js:14)
    at _overRest.js:32
    at index.jsx:95

I have to change the code to

import { sortBy, unionBy } from 'lodash';
this.currentdata = sortBy(unionBy(data, this.currentdata, j=>j.id), i => i.id);

to make it work.

I suppose it is related with the method of 'property' which have been tree-shaked by webpack in the second case. If it is still not clear to you, feel free to ping me to provide a minimal repo code.

jdalton commented 5 years ago

Are you using lodash-webpack-plugin?

guanpu commented 5 years ago

Are you using lodash-webpack-plugin?

Yes, and the version I use is "^0.11.5"

jdalton commented 5 years ago

You need to enable iteratee sorthands feature set in lodash-webpack-plugin. It is expensive in terms of kB though so you might just drop the short hand for an explicit function (o) => o.id.