6

When I import libraries like momentjs in a webpack project webpack automatically includes all their optional dependencies in the bundle too. In the case of momentjs when I do this:

import * as moment from 'momentjs'

I add 200kb into my bundle because this automatically includes ALL possible locales. Locales are 150kb where the library itself is only 50kb. In this question its described how to exclude those dependencies in the webpack configuration with the help of the ContextReplacementPlugin.

How can I do that in an angular-cli project where I don't have direct access to the webpack configuration (unless I eject the config, which has other side effects).

8
  • 2
    you can consider use date-fns.org as it allows you to bring only the code you need. Commented Mar 1, 2017 at 13:39
  • thanks for the link. I will use this in my own code for sure! Problem is, in this case, charts.js is importing momentjs. Its normally an optional dependency when you load it with a script tag, but its always auto-included with webpack. Commented Mar 1, 2017 at 14:23
  • FYI: when I came across this question I already was hopeless and started to write feature request on ng-cli GitHub #5166 as i don't like the idea of ejecting myself to achieve this
    – gaa
    Commented Mar 2, 2017 at 9:44
  • yes, the situation seems to be not so good, even the proposed solution via ContextReplacementPlugin is not a solution but only a workaround. I don't understand why momentjs doesn't change its behaviour. But thanks for your angular-cli issue, I just upvoted it. Commented Mar 2, 2017 at 12:06
  • @MarcoRinck Filipe Silva pointed to yet another workaround which does not require altering webpack config moment/moment#1435 (comment). I have not tried it yet, but hopefully will find a bit of time this week
    – gaa
    Commented Mar 16, 2017 at 10:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.