Skip to content
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

Merged
merged 7 commits into from
Feb 6, 2024
Merged

Import attributes #6599

merged 7 commits into from
Feb 6, 2024

Conversation

zth
Copy link
Collaborator

@zth zth commented Feb 1, 2024

@zth zth changed the base branch from master to 11.0_release February 1, 2024 10:00
@zth zth marked this pull request as draft February 1, 2024 10:00
@@ -0,0 +1,6 @@
@@config({flags: ["-bs-package-output", "es6:jscomp/test:.mjs"]})

@module({from: "./myJson.json", type_: "json"})
Copy link
Member

@cometkim cometkim Feb 1, 2024

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" } })

Copy link
Collaborator Author

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?

Copy link
Member

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"}})

Copy link
Collaborator Author

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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok @cometkim look again.

Copy link
Member

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).

Copy link
Collaborator Author

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?

Copy link
Member

@cometkim cometkim Feb 1, 2024

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

@zth zth marked this pull request as ready for review February 1, 2024 14:03
@zth zth requested review from cristianoc and mununki February 1, 2024 14:04
@zth
Copy link
Collaborator Author

zth commented Feb 1, 2024

@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.

@zth zth force-pushed the import-attributes branch from 63215ab to 845a595 Compare February 1, 2024 15:03
Comment on lines 3 to 5
@module({from: "./myJson.json", with: {type_: "json"}})
external myJson: Js.Json.t = "default"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@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"

Copy link
Collaborator Author

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.

Copy link
Collaborator Author

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.

@zth
Copy link
Collaborator Author

zth commented Feb 5, 2024

@cometkim does this look good from your end in terms of functionality provided?

@cometkim
Copy link
Member

cometkim commented Feb 6, 2024

@zth It looks great for default export! Does it support non-default access too? It would be also common by CSS modules or something

@zth
Copy link
Collaborator Author

zth commented Feb 6, 2024

@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.

@cometkim
Copy link
Member

cometkim commented Feb 6, 2024

gogo 🚀

Copy link
Collaborator

@cristianoc cristianoc left a 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

@zth zth force-pushed the import-attributes branch from 6df8c3e to 17f1ef4 Compare February 6, 2024 19:30
@zth zth enabled auto-merge (squash) February 6, 2024 19:31
@zth zth merged commit f510d13 into 11.0_release Feb 6, 2024
@zth zth deleted the import-attributes branch February 6, 2024 19:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants