-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8ee8635
Add esm wrapper and conditional exports
shadowspawn ff338de
createCommand is an implict export, make explicit
shadowspawn 7677bcc
Use deep link instead of experimental conditional
shadowspawn a07d726
Add mjs to lint
shadowspawn 381194c
Add sanity checks for esm imports
shadowspawn 5b7eedc
Create class instances in test
shadowspawn 210c3f7
Add option so test-esm works for node 10 through 15
shadowspawn ebf107a
Remove stale comment
shadowspawn 0b0d7e6
Condense wrapper
shadowspawn d3d8bfe
Move esm to declaring program section of README
shadowspawn d6b613c
Add esm and TypeScript to program section
shadowspawn 81db72f
fix typo
shadowspawn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import commander from './index.js'; | ||
|
||
// wrapper to provide named exports for ESM. | ||
export const { program, Option, Command, CommanderError, InvalidOptionArgumentError, Help, createCommand } = commander; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { program, Command, Option, CommanderError, InvalidOptionArgumentError, Help, createCommand } from '../esm.mjs'; | ||
|
||
// Do some simple checks that expected imports are available. | ||
// Run using `npm run test-esm`. | ||
|
||
function check(condition, explanation) { | ||
if (!condition) { | ||
console.log(`Failed assertion: ${explanation}`); | ||
process.exit(2); | ||
} | ||
} | ||
|
||
function checkClass(obj, name) { | ||
console.log(`Checking ${name}`); | ||
check(typeof obj === 'object', `new ${name}() produces object`); | ||
check(obj.constructor.name === name, `object constructor is ${name}`); | ||
} | ||
|
||
console.log('Checking program'); | ||
check(typeof program === 'object', 'program is object'); | ||
check(program.constructor.name === 'Command', 'program is class Command'); | ||
|
||
checkClass(new Command(), 'Command'); | ||
checkClass(new Option('-e, --example'), 'Option'); | ||
checkClass(new CommanderError(1, 'code', 'failed'), 'CommanderError'); | ||
checkClass(new InvalidOptionArgumentError('failed'), 'InvalidOptionArgumentError'); | ||
checkClass(new Help(), 'Help'); | ||
|
||
console.log('Checking createCommand'); | ||
check(typeof createCommand === 'function', 'createCommand is function'); | ||
|
||
console.log('No problems'); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in #1443