tc39 / proposal-defer-import-eval

A proposal for introducing a way to defer evaluate of a module
https://tc39.es/proposal-defer-import-eval
MIT License
208 stars 12 forks source link

Make `ns.foo` trigger evaluation also for non-existing properties #33

Closed nicolo-ribaudo closed 4 months ago

nicolo-ribaudo commented 4 months ago

This change solves a few problem:

  1. There was currently no way to trigger evaluation of a deferred module with no exports (see https://github.com/tc39/proposal-defer-import-eval/issues/19 by @guybedford). I was initially leaning towards just banning import defer * as ns, but the solution in this PR also has the benefit in (2).
  2. When implementing import defer * in tool, it's not always easy to know the list of bindings exported by a module at runtime without evaluating it. Examples are Webpack and Babel, that get the list of exports from properties of an object populated by the module evaluation. It would be possible in some cases to hard-code the list of exports, but it introduces additional complexity and duplicated information in the generated bundle.

Symbols still do not trigger evaluation, because: a. it's guaranteed that exports of a module have string names b. module[Symbol.toStringTag] === "Module" can still be used to detect probable namespace objects without forcing their evaluation.

guybedford commented 4 months ago

I must admit I did prefer the pure model framing in https://github.com/tc39/proposal-defer-import-eval/issues/19#issuecomment-1567647857 in making this an error. I understand if detecting the list is tricky in unbound tooling workflows, but I'm not completely sure I agree that justifies omitting the error.

nicolo-ribaudo commented 4 months ago

I will prepare a PR for that — I don't have a super strong preference between these possible behaviours

Jack-Works commented 4 months ago

but the ecosystem also uses module[__esModule] to interop

nicolo-ribaudo commented 4 months ago

Since it's a known string it can be easily worked around. For example, the webpack implementation doesn't trigger evaluation on __esModule.