Open nicolo-ribaudo opened 5 months ago
I really like this framing of a new keyword for optional
, I think point (4) in https://github.com/tc39/proposal-defer-import-eval/pull/31 already speaks to why this makes more sense semantically and in how it might compose with the other phases.
The idea of a partial deferred namespace is very interesting and I remember Yulia wanting to see something like this in the past. Would be interesting to explore that design space further as well!
This PR adds a new keyword for exports, π
optional
, to markexport ... from
statements as three-shakeable. It has the following properties:export * from
cannot be optionaloptional
modifier is indipendent from modifier that affect how the imported module is represented, so all the following variations are valid:export optional * as x from "x"
- only loadx
if the consumer is importing it. Eagerly link/execute it and build the namespace object.export defer * as x from "x"
- always loadx
and its dependencies, eagerly execute it's asynchronous subgraphs, and only execute the rest on property access on the namespace objectexport optional defer * as x from "x"
- only loadx
if the consumer is importing it. If it gets loaded, eagerly execute it's asynchronous subgraphs, and only execute the rest on property access on the namespace object.export optional source x from "x"
- only loadx
if the consumer is importing it, and do so as defined in https://github.com/tc39/proposal-source-phase-importsAn optional re-export is considered to be used if the consumer of the module is:
import * as
(π orimport defer * as
)import { ... }
and listing the optional bindingimport(...)
(π orimport.defer(...)
)A follow-up proposal might introduce some syntax to mark which bindings will be used in namespace imports, such as
import { foo, bar } as partialNamespace from "x"
andimport("x", { bindings: ["foo", "bar"] })
.A bundler/transpiler can re-write optional re-exports to these exact semantics by moving them to the consumer module:
Rendered preview: https://nicolo-ribaudo.github.io/proposal-defer-import-eval/branch/optional-exports.html