joanllenas / ngx-date-fns

⏳ date-fns pipes for Angular
163 stars 14 forks source link

Documentation about tree shaking is not correct #355

Closed MickL closed 3 years ago

MickL commented 3 years ago

The documentation says:

The library itself is optimized to be tree-shakable by just importing DateFnsModule.forRoot()

I did this and checked with ng build --stats-json and then using webpack-bundle-analyzer to see full 512kb ngx-date-fns is imported:

Bildschirmfoto 2021-03-09 um 12 59 55

But when I import just the specific pipe it works and ngx-date-fns is not even visible.

Dont try to reproduce with something newer than 2.16.1 since newer versions do not support tree shaking in the first place: https://github.com/date-fns/date-fns/issues/2207

joanllenas commented 3 years ago

Ok, that makes sense. I will fix the readme and also the demo app to reflect this.

Thanks!

joanllenas commented 3 years ago

@MickL I tested the bundle size with webpack-bundle-analyzer as you suggested but I think it's not working properly. The command used to test tree shaking in this project is npm run analyze:app which uses source-map-explorer, and if you double-check with the actual main.js size, it makes sense.

date-fns v2.19.0

Screenshot 2021-03-14 at 13 17 12 Screenshot 2021-03-14 at 14 01 34

date-fns v2.16.1

Screenshot 2021-03-14 at 14 10 19 Screenshot 2021-03-14 at 13 56 39

Issues with webpack-bundle-analyzer

I don't know the exact details about why the tool is not working properly, but I found a couple of places where they recommend aginst using it for Angular:

(source)

While the Angular CLI uses Webpack for some of its bundling, it also makes additional optimizations on top of Webpack. Because of these optimizations, it's important to use the source-map-explorer to test the final output and not the webpack-bundle-analyzer that is commonly used with Webpack based applications.

(source)

The angular team strongly recommends to only use source-map-explorer to analyze your bundle size instead of webpack-bundle-analyzer. According to them, webpack-bundle-analyzer and a few other similar tools doesn't give the actual info pertaining to angular build process.

Thanks for taking the time to explore this issue and for the v2.16.1 issue finding. I will add a note to the readme. Cheers!