Is there any way to avoid importing the whole library and import only a function or partial of it?
For example with lodash you can do something like this: import has from 'lodash/has';
Example would be:
import moment from 'moment';
moment.months()
I know the latest webpack can handle it automatically, but question is for old webpack (1.0)
Is there any way to avoid importing the whole library and import only a function or partial of it?
For example with lodash you can do something like this: import has from 'lodash/has';
Example would be:
import moment from 'moment';
moment.months()
I know the latest webpack can handle it automatically, but question is for old webpack (1.0)
Share Improve this question edited Apr 1, 2017 at 12:37 Shota asked Apr 1, 2017 at 12:32 ShotaShota 7,3709 gold badges39 silver badges73 bronze badges 14-
2
have you tried
import { months } from 'moment'
– Surya Purohit Commented Apr 1, 2017 at 12:39 - What's the purpose? Do you want to tree-shake the library or what? – Estus Flask Commented Apr 1, 2017 at 12:46
-
3
You can't do that with Moment. Lodash was designed as decoupled library (it is available as
lodash.*
packages). Moment wasn't. You will have the whole library in your bundle any way. – Estus Flask Commented Apr 1, 2017 at 12:55 - 2 Yes, it is working, my point was that it still imports the whole library. However good thing is that, it looks nicer :) – Shota Commented Apr 1, 2017 at 13:04
-
1
@SuryaPurohit We can't access the keys from
default
export like that in ES6 module. In order for them to be accessed , they should be named exports,export months ...
(and they aren't). The code you've shown works just because it falls back to CJS module - and the way it works always depends on how build tool was configured. And also, it is not possible to tree-shake anything from CJS modules. – Estus Flask Commented Apr 1, 2017 at 13:17
1 Answer
Reset to default 2It's impossible for moment.. You can do some plugin: https://github./moment/moment/issues/2373