Skip to content

Conversation

@nicolo-ribaudo
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo commented Dec 12, 2023

Q                       A
Fixed Issues?
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link babel/website#2866
Any Dependency Changes?
License MIT

https://github.com/tc39/proposal-json-modules has been in Stage 3 for three years and we do not support it yet :)

Now that we already have some logic to converts imports to fetch/fs.readFile for import source, we can just reuse it. The first commit extracts that logic to a shared helper; the second commit implements transform for import ... with { type: "json" }.

@nicolo-ribaudo nicolo-ribaudo added PR: New Feature 🚀 A type of pull request used for our changelog categories area: modules Spec: Import Attributes labels Dec 12, 2023
@babel-bot
Copy link
Collaborator

babel-bot commented Dec 12, 2023

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/56344

@@ -0,0 +1,6 @@
// TODO: Not supported yet
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure how to support this, given that:

  • if it's not a JSON import, we need to fallback to an import with two arguments
  • that is not known statically, so we need to always emit code for that fallback
  • the browser might only support import with a single argument

I'd not block this feature on solving that problem, but rather document it as a known limitation.

Copy link
Contributor

@JLHwung JLHwung left a comment

Choose a reason for hiding this comment

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

The transform looks good to me.

Can you add a test case of integration between this plugin and polyfill providers? For example the web output depends on the fetch API, if users specify chrome: 41 as target, the Fetch API should be polyfilled then.

As for docs, can you also add docs for the proposal-import-wasm-source plugin?

require("fs").readFileSync(require.resolve(${specifier}))
`);
buildFetchAsync = specifier => template.expression.ast`
require("fs").promises.readFile(require.resolve(${specifier}))
Copy link
Contributor

Choose a reason for hiding this comment

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

The fs Promises API is available on Node.js 10. If we would like to support older versions, we should manually promisify the callback-style fs.readFile call. Ideally we can output the most compatible form based on the input target.

@nicolo-ribaudo
Copy link
Member Author

As for docs, can you also add docs for the proposal-import-wasm-source plugin?

https://babeljs.io/docs/babel-plugin-proposal-import-wasm-source 😛

@nicolo-ribaudo
Copy link
Member Author

Can you add a test case of integration between this plugin and polyfill providers? For example the web output depends on the fetch API, if users specify chrome: 41 as target, the Fetch API should be polyfilled then.

core-js does not polyfill fetch, but I added a test showing that it polyfills URL and Promise.

@nicolo-ribaudo nicolo-ribaudo added the PR: Ready to be Merged A pull request with already two approvals, but waiting for the next minor release label Feb 6, 2024
@nicolo-ribaudo nicolo-ribaudo merged commit d8dc333 into babel:main Feb 27, 2024
@nicolo-ribaudo nicolo-ribaudo deleted the json-modules branch February 27, 2024 15:46
liuxingbaoyu pushed a commit to liuxingbaoyu/babel that referenced this pull request Mar 5, 2024
@what1s1ove
Copy link

Hey @nicolo-ribaudo ! Thank you for all your hard work! ❤️

Quick question (didn't find it anywhere). Are there any plans for with { type: "css" }? According to the specification, it should become a CSSStyleSheet object.

@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Jun 7, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 7, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area: modules outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: New Feature 🚀 A type of pull request used for our changelog categories PR: Ready to be Merged A pull request with already two approvals, but waiting for the next minor release Spec: Import Attributes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants