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

Add support for named imports in ESM #1440

Merged
merged 12 commits into from Jan 20, 2021
Merged

Conversation

shadowspawn
Copy link
Collaborator

@shadowspawn shadowspawn commented Jan 20, 2021

Pull Request

Problem

Importing commander as CommonJs into an ECMAScript module only allows using the default import, and can not use named imports.

// import { program } from 'commander'; // ERROR
import commander from 'commander';
const program = commander.program;

Related issue: #1284

Solution

Add esm wrapper and use as a deep import:

import { program } from 'commander/esm.mjs';

In the future we will add support so the deep import is not needed by using conditional exports and/or subpaths. Being conservative for now as the functionality is still marked as experimental in node 12. (There have been breaking changes in the support in minor versions of node 12 and 13.)

The --experimental-modules option used in the test-esm run-script will get dropped by node eventually, but currently allows that test to run locally with node 10 through node 15.

ChangeLog

  • added support for named imports from ECMAScript modules

Copy link
Collaborator

@abetomo abetomo left a comment

Choose a reason for hiding this comment

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

LGTM

@shadowspawn shadowspawn mentioned this pull request Jan 20, 2021
@shadowspawn
Copy link
Collaborator Author

So fast! 🚀
Thanks @abetomo

@shadowspawn shadowspawn added the pending release Merged into a branch for a future release, but not released yet label Jan 20, 2021
@shadowspawn shadowspawn merged commit 37825b3 into tj:develop Jan 20, 2021
@shadowspawn shadowspawn deleted the feature/esm branch January 20, 2021 04:49
"files": [
"index.js",
"wrapper.mjs",
Copy link
Contributor

Choose a reason for hiding this comment

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

looks like a typo, must be esm.mjs

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh, good catch, thanks! I changed the name half way through.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

(For interest, I thought wrapper was a suitable name for a wrapper used with conditional exports and not seen by normal client use. But when I changed to deep import and the name became visible, I wanted something more meaningful.)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in #1443

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.

None yet

3 participants