Laravel mix webpack - Ignore locales when importing Moment.js

Viewed 1611

I would like to load 2 locales en-gb and fr-ch only from Moment.js , then assign the moment class to the scopewindow to use the library everywhere in my Vuejs components.

Currently, my app.js has this require line:

window.moment = require('moment')

I am suspecting it can be accomplished thanks to the solutions here (adding IgnorePlugin and ContextReplacementPlugin to webpack.config.js):

plugins.push(new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /en-gb|fr-ch/))
module.exports.plugins = plugins;

OR

plugins.push(new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/))
module.exports.plugins = plugins;

Where should you add these pieces of code (webpack.config.js and/or app.js and/or webpack.mix.js) to ignore all other locales when importing momentjs?

2 Answers
mix.webpackConfig( webpack => {
  plugins: [
    new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)
  ]
});

this way you dont have to import webpack on the top of your file, which sometimes can cause errors.

and you also can put other necessary options like this

mix.webpackConfig( webpack => {
  plugins: [
    new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)
  ],
  resolve: {
    extensions: ['.js', '.vue'],
    alias: {
      '@': __dirname + '/resources',
      '@root': __dirname,
      '@pages': __dirname + '/resources/js/pages'
    },
    fallback: { "path": false },
  },
});
Related