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).

Community
  • 1
  • 1
Marco Rinck
  • 738
  • 2
  • 6
  • 17
  • 2
    you can consider use https://date-fns.org/ as it allows you to bring only the code you need. – José Quinto Zamora Mar 01 '17 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. – Marco Rinck Mar 01 '17 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](https://github.com/angular/angular-cli/issues/5166) as i don't like the idea of ejecting myself to achieve this – gaa Mar 02 '17 at 09: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. – Marco Rinck Mar 02 '17 at 12:06
  • @MarcoRinck Filipe Silva pointed to yet another workaround which does not require altering webpack config [moment/moment#1435 (comment)](https://github.com/moment/moment/issues/1435#issuecomment-240983788). I have not tried it yet, but hopefully will find a bit of time this week – gaa Mar 16 '17 at 10:12
  • I just tried that and after deleting node_modules/moment/src and importing moment like described in the comment my bundle still contains 212kb of momentjs. So, it changes nothing. But even if it did, it would still be a hack since you need to ensure to delete this folder before doing any build. – Marco Rinck Mar 19 '17 at 10:07
  • @MarcoRinck Did you solve this issue? Same problem with me. – Amit Chigadani Sep 12 '17 at 08:32
  • @AmitChigadani unfortunately not – Marco Rinck Sep 12 '17 at 09:04

0 Answers0