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

[internal] Use the Node.js behavior for default imports #12795

Merged

Conversation

nicolo-ribaudo
Copy link
Member

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

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

This is another step for #11701.

Node.s doesn't check if __esModule is defined when importing a CJS module from ESM, and the default import is always mapped to module.exports. This is different from how Babel handles the interop, and this PR aligns the compilation behavior to Node.js to make it easier to migrate to native ESM.

We might also consider adding this as an option to transform-modules-commonjs, but for now I just added a small internal plugin (not 100% compliant, because it doesn't hoist compiled imports) to our config.

When this is merged, I will open a new PR to avoid using __dirname/__filename/require in our sources so that we'll only be using Node.js-compliant modules.

@nicolo-ribaudo nicolo-ribaudo added the PR: Internal 🏠 A type of pull request used for our changelog categories label Feb 12, 2021
@babel-bot
Copy link
Collaborator

babel-bot commented Feb 12, 2021

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

@codesandbox-ci
Copy link

codesandbox-ci bot commented Feb 12, 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 a9e4366:

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

babel.config.js Outdated
@@ -382,3 +383,41 @@ function pluginPackageJsonMacro({ types: t }) {
},
};
}

// Match the Node.js behavior (the default import is module.exports)
function pluginDefaultImportNode({ types: t }) {
Copy link
Member Author

Choose a reason for hiding this comment

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

This plugin transforms import foo from "mod" into const foo = require("mod"), which is what Node.js does.

@nicolo-ribaudo nicolo-ribaudo force-pushed the internal-import-default-match-node branch from 8d8fd33 to a9e4366 Compare March 2, 2021 11:37
@@ -158,6 +158,7 @@ module.exports = function (api) {

convertESM ? "@babel/proposal-export-namespace-from" : null,
convertESM ? "@babel/transform-modules-commonjs" : null,
convertESM ? pluginNodeImportInterop : null,
Copy link
Contributor

Choose a reason for hiding this comment

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

Q: This plugin will be no-op for babel parser and some plugins bundled by Rollup, is that intended?

Copy link
Member Author

Choose a reason for hiding this comment

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

It is not a problem because all the bundled packages don't have any third-party dependency. And even if they did, it would only be a problem if these third-party dependencies used __esModule.

However, I asked to reopen rollup/plugins#635 to add an option to @rollup/plugin-commonjs to mirror the Node.js behavior.

@nicolo-ribaudo
Copy link
Member Author

I'm merging this PR with a single review since it's just internal bookkeeping and doesn't affect in any way our users. I will soon open a follow-up for __dirname/__filename/require.

@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 4, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 4, 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
Development

Successfully merging this pull request may close these issues.

None yet

3 participants