-
Notifications
You must be signed in to change notification settings - Fork 67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
importValue: How to import a module with no exports? #364
Comments
I think this is related to #350 (comment)
|
Does importing it like this work? myShadowRealm.evaluate(`await import("./module-with-no-exports.js")`); |
You can't do "top-level-await" in an eval. You could however manually wrap the promise: await new Promise((resolve, reject) => {
myShadowRealm.evaluate(`(callback) => {
import("./module-with-no-exports.js").then(
() => { callback(null); },
(err) => { callback(String(err)); },
);
})((err) => {
if (err != null) {
reject(err);
} else {
resolve();
}
});
}); |
The restriction on having an export caught me by surprise. I would have assumed this to be similar to APIs like Worker's The current spec editors note for
This restriction is counter to that note. The name of |
I agree; this seems like a strange restriction. |
@jdalton I believe that goes back to @mhofman's comment. When importValue was initially designed, the intention was to not assume how to import or reproduce the module namespace object. With that in mind, the idea was requiring the second argument to always refer to a given name. By the time, an explicit We could say the explicit I want to eventually expand Keep in mind I'd be happy if we can also consider a |
Technically without requiring eval you could have a trampoline: loader.js: export const load = (fullSpecifier, callback) => {
import(fullSpecifier).then(
() => { callback(null); },
(err) => { callback(String(err)); },
);
});
}; main.js: const load = await myShadowRealm.importValue('loader.js', 'load')
.then((rawLoad) => (specifier) => new Promise((resolve, reject) => {
const fullSpecifier = new URL(specifier, import.meta.url).href;
rawLoad(fullSpecifier, (err) => {
if (err != null) {
reject(err);
} else {
resolve();
}
});
});
await load('./module-with-no-exports.js'); I understand this is not ergonomic, but the only limitation is to be able to load the trampoline, which wouldn't be evaluated. |
Maybe we should pull the trigger for "Wrapped Module Namespace Exotic Objects" instead. That will allow you to just do |
I am confused by this thread. importValue is a specialized API which only works for modules which were written with it in mind--there is more that you need to get right besides having an export. As others have mentioned, it is possible to achieve this behavior with either a wrapping module or wrapping code in eval. I would rather hold off on adding this sort of convenience until we have a broader story for how modules which weren't written with ShadowRealm in mind should be usable (e.g. a built-in membrane framework). |
If that's the case, then it seems like a subpar API - modules shouldn't have to know about ShadowRealms to be usable inside one. |
They don't need to know. However the user of the |
@mhofman good clarification, but that still seems problematic to me - i should be able to use any module with a shadowrealm, just like i can use any module with |
I think we'll just need to disagree here. |
We had a pretty extensive discussion about the |
I'm confused at your phrasing - I don't think anyone proposed a requirement, nor am I suggesting changes. New information can be recognized at any stage, and I hadn't thought about it in these terms prior. All normative changes are "supposed" to be made before stage 3, but this proposal has made some and is considering others, and other proposals have made many. The process isn't perfect, as we all know. |
What's the new information here? |
I newly realized that this means that the module consumer has to know details about the module's authoring to be able to consume it inside a ShadowRealm. Philosophically, I don't think module consumers should ever have to know anything about a module except what kind of values it exports, under which bindings. |
I plan to discuss this in July so we can dedicate time for this topic along the other things we are already discussing this week at TC39. I'd be very happy to have an We are aware of expressed objections for adopting Structured Serialization across ShadowRealms. Those are still current. I'm also predicting any sort of cloning for the module ns object will emerge the requirement from other delegates to adopt Structured Serialization, so we should expect blockers on both ways. The current Perhaps we can understand that |
According to ShadowRealm.prototype.importValue, missing
exportName
is aTypeError
, and inside ShadowRealmImportValue,ExportGetter
step 6 says,6. If hasOwn is false, throw a TypeError exception.
This means I cannot import a module into the ShadowRealm with no exports. (Maybe I just want to let that script run in the ShadowRealm, I don't want any value returned.)
The text was updated successfully, but these errors were encountered: