unicode-org / icu4x

Solving i18n for client-side and resource-constrained environments.
https://icu4x.unicode.org
Other
1.34k stars 174 forks source link

JS tree-shaking doesn't work well with class functions #2827

Open sffc opened 1 year ago

sffc commented 1 year ago

When building optimized WASM files, we rely on a good DCE algorithm for the target language. When JavaScript is the target, I've been able to tree-shake at least down to the class level; however, it seems that tree-shaking doesn't work lower than that. This means that all of the functions on ICU4XFixedDecimal, for example, are still present in the bundle and therefore linked into WASM.

Good discussion on this: https://github.com/rollup/rollup/issues/349

It does seem that the Closure Compiler can support this, but when I tried it didn't seem to work for me. Probably spending more time on this could make it work.

A few paths forward:

  1. Continue investigations into a method that tree-shakes class functions using Closure Compiler.
  2. Investigate and/or implement alternatives, perhaps relying on TypeScript.
  3. Change Diplomat codegen so that each function is standalone, rather than in a class.

P.S. In terms of bundlers, I had the best luck with rollup for handling async dependencies. Using webpack resulted in a lot of unused classes being included because it doesn't seem to DCE along async dependencies.

Discuss with:

Optional:

Manishearth commented 1 year ago

How about this:

If you want to tree-shake, just use the free functions

sffc commented 1 year ago

@mosuem found that Dart has better DCE than I was able to get in JS.

@mathiasbynens suggested looping in @domenic about how to enable more effective DCE by putting functions into individual modules.

domenic commented 1 year ago

Hmm I don't think I have any special expertise here... not sure why I was recommended :)

sffc commented 11 months ago

I'll put this on Backlog for now until we have a clear client we can work with to improve the DCE in JS/TS.