justnorris's avatar

Laravel Mix: Prevent Moment.js locales

I'm using Chart.js, and webpack is complaining that the js file is too large.

I stumbled upon this topic on SO: https://stackoverflow.com/questions/25384360/how-to-prevent-moment-js-from-loading-locales-with-webpack

Looks like that answer could help me, is I was using Webpack directly, but I have no idea how to apply it to Laravel Mix.

0 likes
6 replies
lindstrom's avatar

I have one page that requires fullcalendar which has moment as a dependency. I ended up copying moment.min.js directly to public/js rather than loading it through webpack. And, to be clear, I did the same with fullcalendar as well.

There's probably a "right way" to do this so you can bundle it, but ain't nobody got time for that.

1 like
justnorris's avatar

Still... I'd like someone who knows the right way to pitch in. Maybe we can eventually learn the "right way".

So far - I'm not liking Webpack too much. Gulp is much, much easier to use. I'm not big on Vue and React either. They seem like good solid tools, but as you said - ain't nobody got time for that.

jsonuk's avatar

I've had trouble in the past with overriding settings with Mix Webpack, but thats because its new and needs to mature a bit more.

What I suggest, as posted from the link you supplied, is to import moment-mini and use that as a replacement for moments.js (to avoid the locale bloat). https://github.com/ksloan/moment-mini

npm install moment-mini --save-dev

Then in your javascript, swap your require for moment-mini (I have mine situated in bootstrap.js):

window.moment = require('moment-mini');

1 like
steveperrito's avatar

Chiming in late, but the below worked for me. I do believe the IgnorePlugin is better suited than the ContextReplacementPlugin. Webpack docs show this exact approach for moment.js:

  mix.webpackConfig({
    plugins: [
      new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)
    ]
  })
1 like

Please or to participate in this conversation.