pimterry / loglevel

:ledger: Minimal lightweight logging for JavaScript, adding reliable log level methods to wrap any available console.log methods
MIT License
2.6k stars 158 forks source link

ESM - Angular 10 Tree Shaking Support? #155

Open anh-rivergon opened 3 years ago

anh-rivergon commented 3 years ago

WARNING in MyLogging.js depends on 'loglevel'. CommonJS or AMD dependencies can cause optimization bailouts. For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies

How would I get rid of the warning? This happens during tree shaking and I'd like ascertain that optimization does take place?

Thank-you

pimterry commented 3 years ago

I don't know anything about Angular's build process I'm afraid. However, I imagine the optimization is an analysis of how loglevel's exports are used, and whether that means that some of loglevel's own dependencies or code can be omitted from your bundle. Since loglevel is tiny and doesn't have any dependencies at all, you can safely skip that anyway, it shouldn't make any significant difference. I don't imagine that this disables all optimizations in your build, just the optimization of your loglevel usage specifically.

You can test this easily enough - try commenting our your import(s) of loglevel, and compare the bundle size to your normal build. If it makes no difference, the bundle size should differ only by the approximate size of loglevel itself, about 3KB. If it differs by a lot more that that, there's a larger problem where this is affecting your build optimizations. Assuming it indeed doesn't cause any problems, you can disable the warning using allowedCommonJsDependencies as in your link there.

This could also be fixed by migrating loglevel to ESM - if you're really interested, I'd be open to a PR that added that as an output from the build process, but it's a bit complicated to do whilst preserving the existing setup.

anh-rivergon commented 3 years ago

"This could also be fixed by migrating loglevel to ESM - if you're really interested, I'd be open to a PR that added that as an output from the build process, but it's a bit complicated to do whilst preserving the existing setup."

Absolutely :) I was looking for an ESM version of loglevel. That should solve the issue.

pimterry commented 3 years ago

Ok, great! Feel free to open a PR for that, let me know if you have any questions en route. I do want to continue to support the existing UMD output, but I would be very happy to support more formats too if there's interest.