import utilityRemove from 'lodash/array/remove';
import utilityAssign from 'lodash/object/assign';
import utilityRandom from 'lodash/number/random';
import utilityFind from 'lodash/collection/find';
import utilityWhere from 'lodash/collection/where';
let util;
util = {};
util.remove = utilityRemove;
util.assign = utilityAssign;
util.random = utilityRandom;
util.find = utilityFind;
util.where = utilityWhere;
Is there a better way to do the above using ES6 module system?
import utilityRemove from 'lodash/array/remove';
import utilityAssign from 'lodash/object/assign';
import utilityRandom from 'lodash/number/random';
import utilityFind from 'lodash/collection/find';
import utilityWhere from 'lodash/collection/where';
let util;
util = {};
util.remove = utilityRemove;
util.assign = utilityAssign;
util.random = utilityRandom;
util.find = utilityFind;
util.where = utilityWhere;
Is there a better way to do the above using ES6 module system?
Share Improve this question asked Aug 20, 2015 at 17:30 GajusGajus 74k80 gold badges297 silver badges472 bronze badges 6-
I really wonder why you are not doing
import util from 'lodash'
orimport {remove, assign, random, find, where} from 'lodash'
? – Bergi Commented Aug 20, 2015 at 17:55 - Both approaches would import the entire library (600KB). Importing individual modules adds up to only about 80KB. – Gajus Commented Aug 20, 2015 at 18:00
-
Are you re-exporting
util
or using it inside this same file. If you are re-exporting it, then you should use ES6 syntax to do that and skip making an object entirely. – loganfsmyth Commented Aug 20, 2015 at 18:01 - Just use a next-generation bundler then :-) – Bergi Commented Aug 20, 2015 at 18:02
- @Bergi Thats very nice. You should reach out to people at Babel, if you haven't already. – Gajus Commented Aug 20, 2015 at 18:08
2 Answers
Reset to default 5If these are the only symbols in your module, I would shorten the names and use the new object shorthand to do:
import remove from 'lodash/array/remove';
import assign from 'lodash/object/assign';
import random from 'lodash/number/random';
import find from 'lodash/collection/find';
import where from 'lodash/collection/where';
let util = {
remove,
assign,
random,
find,
where
};
If that could cause conflicts, you might consider moving this section to its own module. Being able to replace the lodash methods while testing could potentially be useful.
Since each symbol es from a different module, you can't bine the imports, unless lodash provides a bined import module for that purpose.
If you're simply exporting a symbol without using it, you can also consider this syntax:
export remove from 'lodash/array/remove';
export assign from 'lodash/object/assign';
Which, to anyone importing and using your module, will appear as:
import {remove, assign} from 'your-module';
You can do this in a utils module:
//utils.js
export remove from 'lodash/array/remove';
export assign from 'lodash/object/assign';
export random from 'lodash/number/random';
export find from 'lodash/collection/find';
export where from 'lodash/collection/where';
and use it like this:
import * as util from './utils';
...
util.random();