Receive the module instance in importMetaHook?
Example:
const x = new Compartment({
// ...
importMetaHook(fullSpec, meta, moduleInsance) {
meta.url = fullSpec
meta.self = moduleInstance
}
})
await x.import("/index")
// {url: "/index", self: Module }
See https://github.com/tc39/proposal-realms/issues/244#issuecomment-633180485
The Compartment proposal has an importMetaHook. This hook is expected to stay in the Compartment proposal, as it relates (loosely) to importing.
I have updated the issue to reflect the latest compartment API.
Do you mean ModuleExportsNamespace? What’s a motivating use-case? That is, how would this feature be used?
Example:
const x = new Compartment({ // ... importMetaHook(fullSpec, meta, moduleInsance) { meta.url = fullSpec meta.self = moduleInstance } }) await x.import("/index") // {url: "/index", self: Module }
This is my motivating use case although it is not common.
Do you mean to build a Map of all the compartment’s instances by their full specifier? Do you need a reified instance or do you need the module exports namespace?
The case seems incomplete, since the object resolved by import is the module exports namespace (so presumably the module in question exports url and self, like export const url = import.meta.url). So, I’ll infer that you do in fact need a reified module instance object. To my knowledge, the only use you’d have for that would be to get the namespace or maybe the environment record.
What would you go on to do with these objects?
const x = new Compartment({
// ...
importMetaHook(fullSpec, meta, moduleInsance) {
meta.url = fullSpec
meta.self = moduleInstance
}
})
await x.import("/index")
/index
export const x = 1
import.meta.self.x = 1;
(await import('/index')) === import.meta.self
Yes, I agree this use case does not look so compelling so maybe we can ignore it.