-
Notifications
You must be signed in to change notification settings - Fork 16
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
Import assertions with loaders #39
Comments
Is this table for the current nodejs/node#40250 PR, or a potential future where the assertions are optional? I don’t think we should be doing much, if anything, between I’m going to take a stab at a thinking-out-load proposal; feel free to point out its flaws or suggest a better alternative. Take as an example a YAML loader, to make possible code like this: import data from './data.yaml' assert { type: 'yaml' } For this case, the YAML loader’s So anyway, in this model nothing happens before I think loader authors need the assertions info in both hooks, because The module couldn’t be added to the cache until after Also @aduh95 you’re always welcome to join the loaders meetings, you don’t need an invitation. Subscribe to this repo and you should see an issue created a few days before each meeting with the meeting details. |
The language spec actively prohibits a different module to be loaded based on the assertion - in other words, |
It could be either, considering that no one is supporting implementation nodejs/node#40250, I was thinking about re-using it.
Maybe we are talking about a different cache: if a user imports twice the same specifier, and
To be precise, the spec proposal says |
What about adding a new loader hook, which would only return whether the assertion passes or not? |
About: import data from './data.yaml' assert { type: 'yaml' } From my point of view, this example is invalid. If the code is run with a YAML loader that translates YAML to JSON, then the assertion in the code should be |
That's definitely worth considering. Also maybe something that would correspond to the
I think the idea is that if a TC39 proposal for YAML modules comes up, someone could use a loader hook to emulate support for it in Node.js. |
We can’t really add more loader hooks because of chaining. We just went through a monthslong redesign to collapse four hooks to two to make them chainable. However, making utility functions that split out these smaller pieces that are within the larger hooks would be doable, and should provide many of the same benefits. So if you’re writing a loader that only wants to override the assertion validation, for example to make it always pass, you could create a |
Yeah. For example, according to https://web.dev/css-module-scripts/, Chrome already supports this: import sheet from './styles.css' assert { type: 'css' } I don’t know what a loader would convert this to; ESM JavaScript that is just |
Importing YAML directly into a node.js program seems like it could be pretty useful. How far away are we from such a reality? |
It’s unlikely to be ever supported natively, unless spec committees add support for it like they have for JSON; but you can write a loader to add custom support for it today. |
What is the best resource to get me started if I wanted to write something like that? I found this article online but I'm wondering if there is a more proper document for me to refer to when trying to write a loader? I'm mostly curious about this for my own edification. I doubt I'd be able to write anything official, but it could be helpful internally or for a side project. Thanks for your quick feedback and your time! UPDATE: official documentation is probably where I should start... |
Yes, the official docs should be all you need. The “transpiler loader” example is the pattern you’d want to follow; you’re essentially transpiling YAML into JSON. |
Here's a draft of what solution I can think of regarding relations between loaders and import assertions.
Relation of
ESMLoader
class with import assertions:resolve()
resolve()
resolve()
andload()
load()
load()
importAssertions.type
value is supportedimportAssertions.type
to check for race conditionsfinalFormat
is compatible withimportAssertions.type
importAssertions
toresolve()
importAssertions.type
value is supported, and use it to check for race conditionsfinalFormat
is compatible withimportAssertions.type
importAssertions
toresolve()
finalFormat
is compatible withimportAssertions.type
importAssertions
toresolve()
importAssertions
toload()
importAssertions.type
value is supported and compatible withfinalFormat
importAssertions
toresolve()
importAssertions
toload()
and letload()
do the checksimportAssertions.type
to check for race conditionsimportAssertions
toload()
and letload()
do the checksimportAssertions
toload()
and letload()
do the checksUnflag import assertions
PR, drawbacks of this approach have been discussed.resolve
hook. Not sure this is actually doable and/or desirable.importAssertions
ofresolve()
toload()
context
. This adds complexity, not sure it's worth it.resolve()
.My (new) favourite is
2.
: it seems to me like the perfect balance between simplicity and letting users do powerful things (e.g.: it would be quite easy to implement a loader that supports assertionless imports). I have a PR almost ready implementing this.Anyway, we can discuss that on next week meeting since Goeffrey invited me, but happy to hear thoughts before that if people want to share them.
Footnotes
if the resolver returns an
url
that's already in the cache and the returnedimportAssertion.type
has a value different from the one in the cache, throw aTypeError
. ↩The text was updated successfully, but these errors were encountered: