How to exclude moment locales from angular build?
There is another solution in this Angular Issue: https://github.com/angular/angular-cli/issues/6137#issuecomment-387708972
Add a custom path with the name "moment" so it is by default resolved to the JS file you want:
"compilerOptions": {
"paths": {
"moment": [
"./node_modules/moment/min/moment.min.js"
]
}
}
If you don't want to use any third party libraries the simplest way to do this is to add the following in compilerOptions of your tsconfig.json file
"paths": {
"moment": [
"../node_modules/moment/min/moment.min.js"
]
}
This article describe good solution: https://medium.jonasbandi.net/angular-cli-and-moment-js-a-recipe-for-disaster-and-how-to-fix-it-163a79180173
Briefly:
ng add ngx-build-plus
Add a file webpack.extra.js in the root of your project:
const webpack = require('webpack'); module.exports = { plugins: [ new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/), ] }
Run:
npm run build --prod --extra-webpack-config webpack.extra.js
enter code here
Warning moment.js has been deprecated officially https://momentjs.com/docs/#/-project-status/ (try use day.js or luxon)
For anyone on angular 12 or latest
This does not work for me
const webpack = require('webpack');
module.exports = {
plugins: [
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
]
}
However this does
const webpack = require('webpack');
module.exports = {
plugins: [
new webpack.IgnorePlugin({
resourceRegExp: /^\.\/locale$/,
contextRegExp: /moment$/
})
]
};