Skip to content

Add uncheckedRequire option for JSON imports to CJS#16579

Merged
nicolo-ribaudo merged 2 commits intobabel:mainfrom
nicolo-ribaudo:json-modules-browser-commonjs
Jul 26, 2024
Merged

Add uncheckedRequire option for JSON imports to CJS#16579
nicolo-ribaudo merged 2 commits intobabel:mainfrom
nicolo-ribaudo:json-modules-browser-commonjs

Conversation

@nicolo-ribaudo
Copy link
Copy Markdown
Member

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

As #16534 shows, sometimes it is impossible to properly compile with { type: "json" } (specifically, when targeting CJS + browsers).

This PR makes that case an error, providing a workaround to compile import "x.json" with { type: "json" } to simply require("x.json"). Technically this is unsound, because there could be a x.json.js file what gets executed without asserting that it's actually JSON, but if you are using CJS you are not loading files from the network anyway.

@nicolo-ribaudo nicolo-ribaudo added PR: New Feature 🚀 A type of pull request used for our changelog categories Spec: Import Attributes labels Jun 20, 2024
@nicolo-ribaudo nicolo-ribaudo added this to the v7.25.0 milestone Jun 20, 2024
@babel-bot
Copy link
Copy Markdown
Collaborator

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

@nicolo-ribaudo
Copy link
Copy Markdown
Member Author

We'll need to enable this flag in our config to fix #16491.

@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 Jun 28, 2024
@nicolo-ribaudo nicolo-ribaudo changed the title Add uncheckedRequire option to transform JSON imports to require() Add uncheckedRequire option for JSON imports to CJS Jul 26, 2024
@nicolo-ribaudo nicolo-ribaudo merged commit 475d9db into babel:main Jul 26, 2024
@nicolo-ribaudo nicolo-ribaudo deleted the json-modules-browser-commonjs branch July 26, 2024 09:24
@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 Nov 2, 2024
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Nov 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

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.

[Bug]: @babel/plugin-proposal-json-module may output some ESM code (or even call undefined as if it's a function) to CJS outputs

4 participants