proposal-defer-import-eval icon indicating copy to clipboard operation
proposal-defer-import-eval copied to clipboard

No named exports edge case

Open guybedford opened this issue 2 years ago • 4 comments

Was just wondering about the case for a module without any exports, say only imports, where that module represents some ordered execution that should take place.

Currently the deferral would work, but there would be no way to trigger the execution without a named export.

guybedford avatar May 29 '23 06:05 guybedford

It seems reasonable to me to disallow defer when no bindings are present?

ljharb avatar May 30 '23 01:05 ljharb

I don't think it's a problem. The [[Get]] (or [[GetOwnPropertyDescriptor]]) on the ModuleNamespace exotic object can trigger the module execution, so accessing a non-existed property should be able to trigger the execution too.

Jack-Works avatar May 30 '23 05:05 Jack-Works

@Jack-Works right but in this case there is no module namespace object (for static; and it'd be weird for dynamic for any random .foo to be the encouraged mechanism to evaluate it imo)

ljharb avatar May 30 '23 17:05 ljharb

I like @ljharb's proposal. The only reason modules without exports exist is because they have side effects, such as some initial setup of the environment. On the other hand, deferred imports are for "pure" modules, i.e. modules that don't have significant side effects so you can very safely delay their initialization.

If your module only executes side effects, and these side effects are not for the initial setup of the application, it should just export a function that can be later called at any time.

nicolo-ribaudo avatar May 30 '23 17:05 nicolo-ribaudo