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

Avoid importing .json files #12759

Merged
merged 3 commits into from Feb 5, 2021
Merged

Conversation

nicolo-ribaudo
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo commented Feb 4, 2021

Q                       A
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

I'm starting to check what we need to do in order to migrate to native ES modules, and this is one of the problems I found: native ESM cannot import .json files by default.

This PR replaces imports to package.json files with a compile-time macro, which is replaced with the corresponding package.json entry.

  • This doesn't cause problems when releasing a new version, because we compile after bumping the version in package.json.
  • This has the advantage of slightly reducing the @babel/standalone size, since it doesn't need to include the full package.json contents.

@nicolo-ribaudo nicolo-ribaudo added the PR: Internal 🏠 A type of pull request used for our changelog categories label Feb 4, 2021
@nicolo-ribaudo
Copy link
Member Author

@kaicataldo Do you know if there is an eslint rule to say something like "disallow *.json imports"? I tried looking both at the core rules and at eslint-plugin-import, but I couldn't find anything.

@codesandbox-ci
Copy link

codesandbox-ci bot commented Feb 4, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 771d06d:

Sandbox Source
babel-repl-custom-plugin Configuration
babel-plugin-multi-config Configuration

@babel-bot
Copy link
Collaborator

babel-bot commented Feb 4, 2021

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

@existentialism
Copy link
Member

@nicolo-ribaudo
Copy link
Member Author

That doesn't really prevent us from importing json files, it just ensures that we explicitly write the .json extension.

@existentialism
Copy link
Member

existentialism commented Feb 4, 2021

While not foolproof, can't we use it to at least disallow importing something with the json extension ({ "json": "never" })?

@nicolo-ribaudo
Copy link
Member Author

Actually, probably it's better to do the opposite: if we require the .json extension, we'll avoid accidentally importing json files.

@nicolo-ribaudo nicolo-ribaudo marked this pull request as draft February 4, 2021 23:32
@nicolo-ribaudo
Copy link
Member Author

The new .json imports are all removed by #12583

@nicolo-ribaudo nicolo-ribaudo marked this pull request as ready for review February 5, 2021 00:49
}
const field = path.node.property.name;

// TODO: When dropping old Node.js versions, use require.resolve
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we still need to support old versions since 1) we are building on latest node 2) it is a compile-time macro?

Copy link
Member Author

Choose a reason for hiding this comment

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

"compile time" means also "on Node.js 6 inside babel-jest"

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh I thought we are replacing packageJson.field on the build step.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, but we also run Babel in jest (that's the reason why we cannot use babel.config.mjs)

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.

What do you think extract it to a dedicated plugin in yarn workspace. No need to publish now but maybe we can decide it later. Unlike the BABEL_8_BREAKING flag remover, it falls to an optimizer plugin that will stay for a long time.

@nicolo-ribaudo
Copy link
Member Author

nicolo-ribaudo commented Feb 5, 2021

What do you think extract it to a dedicated plugin in yarn workspace. No need to publish now but maybe we can decide it later. Unlike the BABEL_8_BREAKING flag remover, it falls to an optimizer plugin that will stay for a long time.

We would still need to run Babel inside the yarn plugin to modify the code (unless we want to hack it with regexps 😕)

@nicolo-ribaudo nicolo-ribaudo merged commit e735266 into babel:main Feb 5, 2021
@nicolo-ribaudo nicolo-ribaudo deleted the no-json-import branch February 5, 2021 22:34
@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 May 8, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 8, 2021
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: Internal 🏠 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants