Add support for named imports in ESM (#1440)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Add esm wrapper and conditional exports * createCommand is an implict export, make explicit * Use deep link instead of experimental conditional * Add mjs to lint * Add sanity checks for esm imports * Create class instances in test * Add option so test-esm works for node 10 through 15 * Remove stale comment * Condense wrapper * Move esm to declaring program section of README * Add esm and TypeScript to program section * fix typo
authored by John Gee · committed by GitHub · parent 7ab9d3d · Browse files · 5 files·+67 added−19 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+67 added−19 removed
- tests:+32 added−0 removed
- other:+35 added−19 removed
Provenance
- Commit
37825b3- Tree
7d4dde1- Author
John Gee <john@ruru.gen.nz>- Committer
GitHub <noreply@github.com>- Parent
7ab9d3d
5 files changed+67 added−19 removed
added:tests/esm-test.mjs+32 added
1234567891011121314151617181920212223242526272829303132import { 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');modified:.eslintrc.js+8 added-2 removed
122345651 unmodified lines5859606162626364656667686970const javascriptSettings = { files: ['*.js'], files: ['*.js', '*.mjs'], extends: [ 'standard', 'plugin:jest/recommended' ],51 unmodified lines ecmaVersion: 8 }, overrides: [ javascriptSettings, typescriptSettings typescriptSettings, { files: ['*.mjs'], parserOptions: { sourceType: 'module' } } ]};added:esm.mjs+4 added
1234import commander from './index.js';
// wrapper to provide named exports for ESM.export const { program, Option, Command, CommanderError, InvalidOptionArgumentError, Help, createCommand } = commander;modified:package.json+6 added-3 removed
17 unmodified lines181920212222232425262727282929303132333435363738394017 unmodified lines17 unmodified lines "type": "git", "url": "https://github.com/tj/commander.js.git" }, "scripts": { "lint": "eslint index.js \"tests/**/*.js\"", "lint": "eslint index.js esm.mjs \"tests/**/*.js\"", "typescript-lint": "eslint typings/*.ts", "test": "jest && npm run test-typings", "test-esm": "node --experimental-modules ./tests/esm-test.mjs", "test-typings": "tsc -p tsconfig.json", "typescript-checkJS": "tsc --allowJS --checkJS index.js --noEmit", "test-all": "npm run test && npm run lint && npm run typescript-lint && npm run typescript-checkJS" "test-all": "npm run test && npm run lint && npm run typescript-lint && npm run typescript-checkJS && npm run test-esm" }, "main": "index", "main": "./index.js", "files": [ "index.js", "wrapper.mjs", "typings/index.d.ts" ], "type": "commonjs", "dependencies": {}, "devDependencies": { "@types/jest": "^26.0.20", "@types/node": "^14.14.20",17 unmodified linesmodified:Readme.md+17 added-14 removed
37 unmodified lines38 - [Parsing Configuration](#parsing-configuration)39 - [Legacy options as properties](#legacy-options-as-properties)40 - [TypeScript](#typescript)41 - [createCommand()](#createcommand)42- - [Import into ECMAScript Module](#import-into-ecmascript-module)42 - [Node options such as `--harmony`](#node-options-such-as---harmony)43 - [Debugging stand-alone executable subcommands](#debugging-stand-alone-executable-subcommands)44 - [Override exit and output handling](#override-exit-and-output-handling)45 - [Additional documentation](#additional-documentation)26 unmodified lines72 const program = new Command();73 program.version('0.0.1');74 ```75 76+For named imports in ECMAScript modules, import from `commander/esm.mjs`.77+78+```js79+// index.mjs80+import { Command } from 'commander/esm.mjs';81+const program = new Command();82+```83+84+And in TypeScript:85+86+```ts87+// index.ts88+import { Command } from 'commander';89+const program = new Command();90+```91+92+93 ## Options94 95 Options are defined with the `.option()` method, also serving as documentation for the options. Each option can have a short flag (single character) and a long name, separated by a comma or space or vertical bar ('|').96 658 unmodified lines755 ```756 757 ### TypeScript758 743-The Commander package includes its TypeScript Definition file.744-759 If you use `ts-node` and stand-alone executable subcommands written as `.ts` files, you need to call your program through node to get the subcommands called correctly. e.g.760 761 ```bash762 node -r ts-node/register pm.ts11 unmodified lines774 `createCommand` is also a method of the Command object, and creates a new command rather than a subcommand. This gets used internally775 when creating subcommands using `.command()`, and you may override it to776 customise the new subcommand (example file [custom-command-class.js](./examples/custom-command-class.js)).777 764-### Import into ECMAScript Module765-766-Commander is currently a CommonJS package, and the default export can be imported into an ES Module:767-768-```js769-// index.mjs770-import commander from 'commander';771-const program = commander.program;772-const newCommand = new commander.Command();773-```774-778 ### Node options such as `--harmony`779 780 You can enable `--harmony` option in two ways:781 136 unmodified lines