New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
require moment without locales #2416
Comments
I just experienced the same issue as you with webpack. This SO post covers 2 different ways to avoid packaging the locales: http://stackoverflow.com/a/25426019/239965 The plugins: [
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)
] |
Very cool, thanks @balexand |
The IgnorePlugin does the job, I'm down to 11kb gzipped when adding moment. Still would be nice to have the option to require with locales or without locales. Closing this one, as there is already #2373 |
According to this comment, there should be a moment without locales on npmjs. |
this module exposes moment without the locales https://github.com/ksloan/moment-mini |
Using As I am not using |
By default, the node bundle for moment includes internationalization data and functionality for ~100 locales. We aren't using those, so strip them from the webpack output. See: moment/moment#2416
@balexand Thanks, it works for me |
If someone misses typings, here's my fork: https://github.com/kirillgroshkov/moment-mini-ts
|
How about ignoring all locales with IgnorePlugin except some of my choosing? For example, ignore all except EN, ES, FR. Can that be done with a regular expression? plugins: [
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)
] Edit: I figured It out, in case someone still needs it. Found in https://stackoverflow.com/a/25426019/2477303 plugins: [
new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /en|es|fr/),
// new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)
] |
@kuncevic, did you achieve to exclude |
I know it's kind of hacky, but for our projects we've created a 'use strict';
const fs = require( 'fs' );
console.log( 'Patchin internal Angular CLI configuration ...' );
const webpackProductionConfigPath = './node_modules/@angular/cli/models/webpack-configs/production.js';
fs.readfile( webpackProductionConfigPath, 'utf-8', ( error, fileContent ) => {
const momentFix = 'extraPlugins.push( new webpack.ContextReplacementPlugin( /moment[\\/\\\\]locale$/, /de.js/ ) );';
if ( fileContent.indexOf( momentFix ) === -1 ) {
const uniqueContent = 'return {'; // Line 112
const modifiedFileContent = fileContent.replace( uniqueContent, `${ momentFix }\n ${ uniqueContent }` );
fs.writeFile( webpackProductionConfigPath, modifiedFileContent, 'utf-8', ( error ) => {
console.log( 'Done.' );
} );
} else {
console.log( 'Nothing to do.' );
}
} ); The script above ignores all locales except german, and it's written specifically for "scripts": {
"postinstall": "node ./moment-angular-cli-patch.js"
} Not a great solution but a solution that works ... |
@dominique-mueller that is interesting idea, thanks for sharing |
@dominique-mueller For now is interesting idea, thanks |
@dominique-mueller thanks for the idea! I've ended up patching moment instead of angular cli though.
|
@dominique-mueller interesting idea but I don't think patching the internal webpack configuration of the angular-cli is a good idea in the long run, given that the patch'd be really dependent on the angular-cli versions and you wouldn't be able to update really easily. The same goes for the patch in the moment config. |
In addition to @fergardi 's comment, slightly changing the regex avoids including some more undesired locales. plugins: [
new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /(en|es|fr)$/),
] This way you don't get all the |
With ngx-build-plus you should be able to adjust the Webpack config in Angular CLI without ejecting. |
@SamVerschueren THIS IS AMAZING 😸 |
How can I ignore few directory from my modules? As my project is huge and it is giving while prod build so I thinking to build chunks by chunks so will need to Ignore/exclude few directory ? Please help its bit urgent. Thanks in advance. |
is there an update to this that uses best practice? I see that a recent issue referenced this fix again urish/ngx-moment#212 |
For anyone else looking for a more up to date solution this is from webpacks site:
See: Example of Ignoring Moment Locales |
✅ It works with the new Laravel Mix 6.* |
in tsconfig.app.ts compilerOptions use
It will use only moment without any locals. No need no use extra webpack plugins |
@esaesa perfect! Here's the same configuration for webpack: module.exports = {
resolve: {
alias: {
// Ignore all locale files of moment.js
moment: "moment/min/moment.min.js"
}
}
} |
I am using moment in a webpack build.
It looks like
require('moment')
is the equivalent of usingmoment-with-locales.min.js
from CDN (judging mainly by the size increase in my bundle).Is there a way to require moment with only the default english locale? (i.e. the equivalen of
moment.min.js
from CDN).I guess my question is related to #2373
The text was updated successfully, but these errors were encountered: