-
Notifications
You must be signed in to change notification settings - Fork 463
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 attributes #6599
Import attributes #6599
Conversation
jscomp/test/ImportAttributes.res
Outdated
@@ -0,0 +1,6 @@ | |||
@@config({flags: ["-bs-package-output", "es6:jscomp/test:.mjs"]}) | |||
|
|||
@module({from: "./myJson.json", type_: "json"}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer a syntax that explicitly provides the "with" key rather than having the special keys (that is the whole point of the spec over the import assertion) .
This is common even in regular js syntax.
// import attrs can be specified with others e.g. import assertion
import module from "module-id" asserts { type: "json" } with { type: "json" }
// import attrs in dynamic imports
import("module-id", { with: { ... } })
// import attra in workers
new Worker("id", { with: { ... } })
// Notice that the "type" in the top-level and the "type" in the import attributes (with) have different semantic
new Worker("id", { type: "module", with: { type: "something-special" } })
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you write an example of how you'd want it to look?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@module({from: "./myJson.json", with: { type_: "json"}})
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. Curious, can more things appear in that with list than type
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok @cometkim look again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. Curious, can more things appear in that with list than
type
?
Yes, import attributes are freely extensible and their behavior is determined by the host (mostly bundlers).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cometkim is the input/output fine now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep, it will be better if we have other random cases than type_
in the test
@cristianoc @mununki you recently did dynamic imports. Interested in your take on whether what I've done here is reasonable/correct or if there's a better way. |
63215ab
to
845a595
Compare
jscomp/test/ImportAttributes.res
Outdated
@module({from: "./myJson.json", with: {type_: "json"}}) | ||
external myJson: Js.Json.t = "default" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@module({from: "./myJson.json", with: {type_: "json"}}) | |
external myJson: Js.Json.t = "default" | |
@module({from: "./myJson.json", with: {type_: "json"}}) | |
external myJson: Js.Json.t = "default" | |
@module({from: "./myModule", with: {foo: "foo", "bar-baz": "only-string-allowed"}}) | |
external moreAttributes: unit = "default" | |
@module({with: {type_: "css"}}) | |
external styles: Styles.t = "./myModule.css" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah it doesn't handle invalid identifiers yet, but that would be easy to handle by just outputting quotes around the keys. That should be fine, right?
You'd have to escape the identifiers BTW, it needs to be a record in the AST.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cometkim I updated the code to handle this case, and added an example to the test output. Let me know what you think.
@cometkim does this look good from your end in terms of functionality provided? |
@zth It looks great for default export! Does it support non-default access too? It would be also common by CSS modules or something |
@cometkim it should, yes. Should add a test for that too. If you think it all looks good, we can go ahead and get this into 11.1. |
gogo 🚀 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
go ahead as long as it's obvious that this does not affect existing code
6df8c3e
to
17f1ef4
Compare
https://github.com/tc39/proposal-import-attributes
Closes: #6500
cc @cometkim