Skip to content

Commit 6d69295

Browse files
authored
feat!: yargs is now ESM first (#503)
1 parent 3aba24c commit 6d69295

24 files changed

+3866
-4229
lines changed

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
build/
2+
test/tscc/

.eslintrc

Lines changed: 0 additions & 21 deletions
This file was deleted.

.eslintrc.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"parser": "@typescript-eslint/parser",
3+
"extends": "./node_modules/gts/",
4+
"parserOptions": {
5+
"ecmaVersion": 2020,
6+
"sourceType": "module"
7+
},
8+
"overrides": [
9+
{
10+
"files": ["**/*.ts", "**/*.tsx"],
11+
"rules": {
12+
"@typescript-eslint/no-var-requires": "off",
13+
"@typescript-eslint/no-unused-vars": "off",
14+
"@typescript-eslint/no-explicit-any": "off",
15+
"@typescript-eslint/no-unsafe-function-type": "off",
16+
"@typescript-eslint/no-empty-object-type": "off"
17+
}
18+
}
19+
]
20+
}

.github/workflows/ci.yaml

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
runs-on: ubuntu-latest
1111
strategy:
1212
matrix:
13-
node: [12, 14, 16]
13+
node: [20, 22, 24]
1414
steps:
1515
- uses: actions/checkout@v1
1616
- uses: actions/setup-node@v1
@@ -28,18 +28,18 @@ jobs:
2828
- uses: actions/checkout@v2
2929
- uses: actions/setup-node@v1
3030
with:
31-
node-version: 12
31+
node-version: 22
3232
- run: npm install
3333
env:
34-
PUPPETEER_SKIP_CHROMIUM_DOWNLOAD: true
34+
PUPPETEER_SKIP_DOWNLOAD: true
3535
- run: npm test
3636
coverage:
3737
runs-on: ubuntu-latest
3838
steps:
3939
- uses: actions/checkout@v2
4040
- uses: actions/setup-node@v1
4141
with:
42-
node-version: 14
42+
node-version: 22
4343
- run: npm install
4444
env:
4545
PUPPETEER_SKIP_CHROMIUM_DOWNLOAD: true
@@ -51,7 +51,7 @@ jobs:
5151
- uses: actions/checkout@v2
5252
- uses: actions/setup-node@v1
5353
with:
54-
node-version: 14
54+
node-version: 22
5555
- run: npm install
5656
env:
5757
PUPPETEER_SKIP_CHROMIUM_DOWNLOAD: true
@@ -70,7 +70,7 @@ jobs:
7070
- uses: actions/checkout@v2
7171
- uses: actions/setup-node@v1
7272
with:
73-
node-version: 14
73+
node-version: 22
7474
- run: npm install
7575
- run: npm run test:browser
7676
typescript:
@@ -79,15 +79,6 @@ jobs:
7979
- uses: actions/checkout@v2
8080
- uses: actions/setup-node@v1
8181
with:
82-
node-version: 14
82+
node-version: 22
8383
- run: npm install
8484
- run: npm run test:typescript
85-
optimize:
86-
runs-on: ubuntu-latest
87-
steps:
88-
- uses: actions/checkout@v2
89-
- uses: actions/setup-node@v1
90-
with:
91-
node-version: 16
92-
- run: cd test/tscc && npm install && npx @tscc/tscc
93-
- run: cd test/tscc && node out.js

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*

.prettierrc.cjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
...require('gts/.prettierrc.json')
3+
}

lib/index.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
/**
22
* @fileoverview Main entrypoint for libraries using yargs-parser in Node.js
3-
* CJS and ESM environments.
43
*
54
* @license
65
* Copyright (c) 2016, Contributors
76
* SPDX-License-Identifier: ISC
87
*/
98

9+
/* eslint-disable n/no-unpublished-import */
10+
1011
import { format } from 'util'
1112
import { normalize, resolve } from 'path'
1213
import { ArgsInput, Arguments, Parser, Options, DetailedArguments } from './yargs-parser-types.js'
1314
import { camelCase, decamelize, looksLikeNumber } from './string-utils.js'
1415
import { YargsParser } from './yargs-parser.js'
1516
import { readFileSync } from 'fs'
17+
import { createRequire } from 'node:module';
1618

1719
// See https://github.com/yargs/yargs-parser#supported-nodejs-versions for our
1820
// version support policy. The YARGS_MIN_NODE_VERSION is used for testing only.
1921
const minNodeVersion = (process && process.env && process.env.YARGS_MIN_NODE_VERSION)
2022
? Number(process.env.YARGS_MIN_NODE_VERSION)
21-
: 12
23+
: 20
2224
const nodeVersion = process?.versions?.node ?? process?.version?.slice(1)
2325
if (nodeVersion) {
2426
const major = Number(nodeVersion.match(/^([^.]+)/)![1])
@@ -29,6 +31,8 @@ if (nodeVersion) {
2931

3032
// Creates a yargs-parser instance using Node.js standard libraries:
3133
const env = process ? process.env as { [key: string]: string } : {}
34+
const require = createRequire ? createRequire(import.meta.url) : undefined;
35+
3236
const parser = new YargsParser({
3337
cwd: process.cwd,
3438
env: () => {
@@ -37,8 +41,6 @@ const parser = new YargsParser({
3741
format,
3842
normalize,
3943
resolve,
40-
// TODO: figure out a way to combine ESM and CJS coverage, such that
41-
// we can exercise all the lines below:
4244
require: (path: string) => {
4345
if (typeof require !== 'undefined') {
4446
return require(path)
@@ -61,3 +63,6 @@ yargsParser.camelCase = camelCase
6163
yargsParser.decamelize = decamelize
6264
yargsParser.looksLikeNumber = looksLikeNumber
6365
export default yargsParser
66+
67+
// special syntax to allow unqualified default export from CommonJS
68+
export {yargsParser as 'module.exports'};

lib/yargs-parser.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright (c) 2016, Contributors
44
* SPDX-License-Identifier: ISC
55
*/
6+
/* eslint-disable prefer-arrow-callback */
67

78
import { tokenizeArgString } from './tokenize-arg-string.js'
89
import type {

package.json

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
"name": "yargs-parser",
33
"version": "21.1.1",
44
"description": "the mighty option parser used by yargs",
5-
"main": "build/index.cjs",
5+
"main": "build/lib/index.js",
66
"exports": {
77
".": [
88
{
9-
"import": "./build/lib/index.js",
10-
"require": "./build/index.cjs"
9+
"import": "./build/lib/index.js"
1110
},
12-
"./build/index.cjs"
11+
"./build/lib/index.js"
1312
],
1413
"./browser": [
1514
"./browser.js"
@@ -18,19 +17,16 @@
1817
"type": "module",
1918
"module": "./build/lib/index.js",
2019
"scripts": {
21-
"check": "standardx '**/*.ts' && standardx '**/*.js' && standardx '**/*.cjs'",
22-
"fix": "standardx --fix '**/*.ts' && standardx --fix '**/*.js' && standardx --fix '**/*.cjs'",
23-
"pretest": "rimraf build && tsc -p tsconfig.test.json && cross-env NODE_ENV=test npm run build:cjs",
24-
"test": "c8 --reporter=text --reporter=html mocha test/*.cjs",
25-
"test:esm": "c8 --reporter=text --reporter=html mocha test/*.mjs",
20+
"check": "gts lint",
21+
"fix": "gts fix",
22+
"pretest": "rimraf build && tsc -p tsconfig.test.json",
23+
"test": "c8 --reporter=text --reporter=html mocha test/*.mjs",
2624
"test:browser": "start-server-and-test 'serve ./ -p 8080' http://127.0.0.1:8080/package.json 'node ./test/browser/yargs-test.cjs'",
2725
"pretest:typescript": "npm run pretest",
2826
"test:typescript": "c8 mocha ./build/test/typescript/*.js",
2927
"coverage": "c8 report --check-coverage",
3028
"precompile": "rimraf build",
3129
"compile": "tsc",
32-
"postcompile": "npm run build:cjs",
33-
"build:cjs": "rollup -c",
3430
"prepare": "npm run compile"
3531
},
3632
"repository": {
@@ -51,29 +47,24 @@
5147
"author": "Ben Coe <[email protected]>",
5248
"license": "ISC",
5349
"devDependencies": {
54-
"@types/chai": "^4.2.11",
55-
"@types/mocha": "^9.0.0",
56-
"@types/node": "^16.11.4",
57-
"@typescript-eslint/eslint-plugin": "^3.10.1",
58-
"@typescript-eslint/parser": "^3.10.1",
59-
"c8": "^7.3.0",
60-
"chai": "^4.2.0",
50+
"@babel/eslint-parser": "^7.27.1",
51+
"@babel/preset-typescript": "^7.27.1",
52+
"@types/chai": "^5.2.1",
53+
"@types/mocha": "^10.0.10",
54+
"@types/node": "^22.15.3",
55+
"@typescript-eslint/eslint-plugin": "^8.29.1",
56+
"@typescript-eslint/parser": "^8.31.1",
57+
"c8": "^10.1.3",
58+
"chai": "^5.2.0",
6159
"cross-env": "^7.0.2",
62-
"eslint": "^7.0.0",
63-
"eslint-plugin-import": "^2.20.1",
64-
"eslint-plugin-node": "^11.0.0",
65-
"gts": "^3.0.0",
66-
"mocha": "^10.0.0",
67-
"puppeteer": "^16.0.0",
68-
"rimraf": "^3.0.2",
69-
"rollup": "^2.22.1",
70-
"rollup-plugin-cleanup": "^3.1.1",
71-
"rollup-plugin-ts": "^3.0.2",
60+
"eslint": "^8.57.1",
61+
"gts": "^5.3.1",
62+
"mocha": "^11.1.0",
63+
"puppeteer": "^24.6.1",
64+
"rimraf": "^6.0.1",
7265
"serve": "^14.0.0",
73-
"standardx": "^7.0.0",
74-
"start-server-and-test": "^1.11.2",
75-
"ts-transform-default-export": "^1.0.2",
76-
"typescript": "^4.0.0"
66+
"start-server-and-test": "^2.0.11",
67+
"typescript": "^5.8.3"
7768
},
7869
"files": [
7970
"browser.js",
@@ -82,11 +73,6 @@
8273
"!*.d.cts"
8374
],
8475
"engines": {
85-
"node": ">=12"
86-
},
87-
"standardx": {
88-
"ignore": [
89-
"build"
90-
]
76+
"node": "^20.19.0 || ^22.12.0 || >=23"
9177
}
9278
}

rollup.config.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)