Skip to content
Myr

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

5 files+67 added−19 removed

added:tests/esm-test.mjs+32 added
modified:.eslintrc.js+8 added-2 removed
added:esm.mjs+4 added
modified:package.json+6 added-3 removed
modified:Readme.md+17 added-14 removed
37 unmodified lines
38 - [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 lines
72const program = new Command();73program.version('0.0.1');74```7576For named imports in ECMAScript modules, import from `commander/esm.mjs`.7778```js79// index.mjs80import { Command } from 'commander/esm.mjs';81const program = new Command();82```8384And in TypeScript:8586```ts87// index.ts88import { Command } from 'commander';89const program = new Command();90```919293## Options9495Options 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 lines
755```756757### TypeScript758743The Commander package includes its TypeScript Definition file.744759If 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.760761```bash762node -r ts-node/register pm.ts
11 unmodified lines
774`createCommand` is also a method of the Command object, and creates a new command rather than a subcommand. This gets used internally775when creating subcommands using `.command()`, and you may override it to776customise the new subcommand (example file [custom-command-class.js](./examples/custom-command-class.js)).777764### Import into ECMAScript Module765766Commander is currently a CommonJS package, and the default export can be imported into an ES Module:767768```js769// index.mjs770import commander from 'commander';771const program = commander.program;772const newCommand = new commander.Command();773```774778### Node options such as `--harmony`779780You can enable `--harmony` option in two ways:781
136 unmodified lines