Discussion: Module Reflection - import.reflect

# Randy Buchholz (3 months ago)

Would it be worthwhile to add reflection to modules as a distinct feature?

import.meta.url is a basic form of reflection, but it seems (?) meta is trying to be a fairly loose specification. Being able query a module can be helpful. For example, if I can see the static imports, I can conditionally do dynamic imports. Or (thinking out loud here), can "generic" modules be built where default is the generic?

Some of the possibilities are import.reflect.default - returns default in its native form (function, class, value) import.reflect.imports - provides a list of static imports. Not sure what this should be (value or key-value (e.g., [importName, module]), but should be enumerable. import.reflect.functions - provides a list of exported functions. Can be use, for instance, to validate the shape of a module.

This can be exposed to an importing module.

// myModule has default export of `class xxx`

Import * as m from myModule
const instance = Reflect.construct(m.import.reflect.default, []);

# Jordan Harband (3 months ago)

In import * as m from myModule, m.default is already the default export; Object.keys(m) is already the list of export names, and Object.entries(m).filter(([k, v]) => typeof v === 'function')) is the

list of functions.