最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Import moment.js partial or function - Stack Overflow

matteradmin6PV0评论

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
 |  Show 9 more ments

1 Answer 1

Reset to default 2

It's impossible for moment.. You can do some plugin: https://github./moment/moment/issues/2373

Post a comment

comment list (0)

  1. No comments so far