urish / ngx-moment

moment.js pipes for Angular
MIT License
1.17k stars 154 forks source link

remove locale from moment in angular2/4 #197

Closed jainAdijain closed 6 years ago

jainAdijain commented 6 years ago

I am using angular2-moment in ionic-angular application

In a certain component's module I am using

package.json

"angular2-moment": "^1.7.0",

import {MomentModule} from 'angular2-moment';

@ngModule({
imports: [ MomentModule ] 
})

.html

using `amTimeAgo` pipe

The source maps generated for this module shows locale which increases the size of the overall vendor.

image

How to remove this locale?

urish commented 6 years ago

127 and #168 has some workarounds. Here is what I do in my projects (using the Angular CLI):

  1. Install moment version 2.22.0 (newer versions will work with slight adaptations):

    npm i --save moment@2.22.0
  2. Install patch-package and add it to your package.json inside the scripts section:

    {
    "scripts": {
    "prepare": "patch-package"
    }
    }
  3. Create a file under patches/moment+2.22.0.patch with the following content:

    patch-package
    --- a/node_modules/moment/moment.js
    +++ b/node_modules/moment/moment.js
    @@ -1832,7 +1832,7 @@
                 module && module.exports) {
             try {
                 oldLocale = globalLocale._abbr;
    -                var aliasedRequire = require;
    +                var aliasedRequire = eval('require');
                 aliasedRequire('./locale/' + name);
                 getSetGlobalLocale(oldLocale);
             } catch (e) {}
    --- a/node_modules/moment/src/lib/locale/locales.js
    +++ b/node_modules/moment/src/lib/locale/locales.js
    @@ -52,7 +52,7 @@ function loadLocale(name) {
             module && module.exports) {
         try {
             oldLocale = globalLocale._abbr;
    -            var aliasedRequire = require;
    +            var aliasedRequire = eval('require');
             aliasedRequire('./locale/' + name);
             getSetGlobalLocale(oldLocale);
         } catch (e) {}
  4. Run npm run prepare to apply to patch. It will also be applied automatically by patch-package whenever you npm install.

This workaround makes sure moment does not implicitly bundle any locales - unless you import specific locales that you are interested in.

alexcoroza commented 5 years ago

Then where should I create the "patches" folder? Sorry the solution is not really clear for me. I dont even understand where did you get the codes for the patch file. And what if I have a newer version of moment.js? So I have to change the patch file name? And also I cant understand the codes for the patch file, its missing some brackets so Im thinking its not complete? Because its still not working for me