Skip to content

Commit

Permalink
Conditional exports to simplify ECMAScript import (#1589)
Browse files Browse the repository at this point in the history
* FIrst try at exports in package.json

* Set required version for conditional export in package.json

* Update README with export support
  • Loading branch information
shadowspawn committed Aug 26, 2021
1 parent dd28741 commit abec6c5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
16 changes: 7 additions & 9 deletions Readme.md
Expand Up @@ -66,28 +66,26 @@ Commander exports a global object which is convenient for quick programs.
This is used in the examples in this README for brevity.

```js
// CommonJS (.cjs)
const { program } = require('commander');
```

For larger programs which may use commander in multiple ways, including unit testing, it is better to create a local Command object to use.

```js
// CommonJS (.cjs)
const { Command } = require('commander');
const program = new Command();
```

For named imports in ECMAScript modules, import from `commander/esm.mjs`.

```js
// index.mjs
import { Command } from 'commander/esm.mjs';
// ECMAScript (.mjs)
import { Command } from 'commander';
const program = new Command();
```

And in TypeScript:

```ts
// index.ts
// TypeScript (.ts)
import { Command } from 'commander';
const program = new Command();
```
Expand Down Expand Up @@ -1014,8 +1012,8 @@ More samples can be found in the [examples](https://github.com/tj/commander.js/t
## Support
The current version of Commander is fully supported on Long Term Support versions of node, and requires at least node v12.
(For older versions of node, use an older version of Commander. Commander version 2.x has the widest support.)
The current version of Commander is fully supported on Long Term Support versions of Node.js, and requires at least v12.20.0.
(For older versions of Node.js, use an older version of Commander. Commander version 2.x has the widest support.)
The main forum for free and community support is the project [Issues](https://github.com/tj/commander.js/issues) on GitHub.
Expand Down
11 changes: 9 additions & 2 deletions package.json
Expand Up @@ -27,7 +27,6 @@
"typescript-checkJS": "tsc --allowJS --checkJS index.js lib/*.js --noEmit",
"test-all": "npm run test && npm run lint && npm run typescript-lint && npm run typescript-checkJS && npm run test-esm"
},
"main": "./index.js",
"files": [
"index.js",
"lib/*.js",
Expand All @@ -36,6 +35,14 @@
"package-support.json"
],
"type": "commonjs",
"main": "./index.js",
"exports": {
".": {
"require": "./index.js",
"import": "./esm.mjs"
},
"./esm.mjs": "./esm.mjs"
},
"dependencies": {},
"devDependencies": {
"@types/jest": "^26.0.23",
Expand Down Expand Up @@ -63,7 +70,7 @@
]
},
"engines": {
"node": ">= 12"
"node": "^12.20.0 || >=14"
},
"support": true
}

0 comments on commit abec6c5

Please sign in to comment.