tc39 / proposal-extensions

Extensions proposal for ECMAScript
MIT License
150 stars 6 forks source link

Tree shaking of imports? #5

Open mmis1000 opened 3 years ago

mmis1000 commented 3 years ago

The semantic of x::ext:name( seems incompatible with tree shaking after transpile if the the import and x::ext:name( got trasnpiled individually.

import * as ext from 'someOtherModule'
ExtInvoke(ext, 'name', x, args)

There is no way for any tool chain after it to realize that only ext.name is used instead of the whole ext.

Although the transpiler is still possible to do it if they are transformed at same time (by just assume ext[SymbolExtension] do not exist because it is a module and emit different output)

hax commented 3 years ago

Good question!

Actually tree shaking is one of the important use cases of this proposal.

First, ad-hoc extension methods and accessors are very friendly to tree shaking, it could solve size issue of many libraries (could be splitted into core and some extensions).

There could be a import ::{x, y, z} from 'module' syntax to import extension methods and declare x, y, z as ad-hoc extension methods/accessors in current module. This syntax is not included in the README just because it's not the core part of this proposal and could be separate to a follow-on proposal. I will improve the document to cover that soon.

Second, for x::ext:name, the behavior is delegated by ext. I would expect there will be three types in practice: