Skip to content

feat!: yargs is now ESM first #503

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

Merged
merged 13 commits into from
May 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build/
test/tscc/
21 changes: 0 additions & 21 deletions .eslintrc

This file was deleted.

20 changes: 20 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"parser": "@typescript-eslint/parser",
"extends": "./node_modules/gts/",
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module"
},
"overrides": [
{
"files": ["**/*.ts", "**/*.tsx"],
"rules": {
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/no-unused-vars": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-unsafe-function-type": "off",
"@typescript-eslint/no-empty-object-type": "off"
}
}
]
}
23 changes: 7 additions & 16 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node: [12, 14, 16]
node: [20, 22, 24]
steps:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
Expand All @@ -28,18 +28,18 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 12
node-version: 22
- run: npm install
env:
PUPPETEER_SKIP_CHROMIUM_DOWNLOAD: true
PUPPETEER_SKIP_DOWNLOAD: true
- run: npm test
coverage:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 14
node-version: 22
- run: npm install
env:
PUPPETEER_SKIP_CHROMIUM_DOWNLOAD: true
Expand All @@ -51,7 +51,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 14
node-version: 22
- run: npm install
env:
PUPPETEER_SKIP_CHROMIUM_DOWNLOAD: true
Expand All @@ -70,7 +70,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 14
node-version: 22
- run: npm install
- run: npm run test:browser
typescript:
Expand All @@ -79,15 +79,6 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 14
node-version: 22
- run: npm install
- run: npm run test:typescript
optimize:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 16
- run: cd test/tscc && npm install && npx @tscc/tscc
- run: cd test/tscc && node out.js
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*
3 changes: 3 additions & 0 deletions .prettierrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
...require('gts/.prettierrc.json')
}
13 changes: 9 additions & 4 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
/**
* @fileoverview Main entrypoint for libraries using yargs-parser in Node.js
* CJS and ESM environments.
*
* @license
* Copyright (c) 2016, Contributors
* SPDX-License-Identifier: ISC
*/

/* eslint-disable n/no-unpublished-import */

import { format } from 'util'
import { normalize, resolve } from 'path'
import { ArgsInput, Arguments, Parser, Options, DetailedArguments } from './yargs-parser-types.js'
import { camelCase, decamelize, looksLikeNumber } from './string-utils.js'
import { YargsParser } from './yargs-parser.js'
import { readFileSync } from 'fs'
import { createRequire } from 'node:module';

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

// Creates a yargs-parser instance using Node.js standard libraries:
const env = process ? process.env as { [key: string]: string } : {}
const require = createRequire ? createRequire(import.meta.url) : undefined;

const parser = new YargsParser({
cwd: process.cwd,
env: () => {
Expand All @@ -37,8 +41,6 @@ const parser = new YargsParser({
format,
normalize,
resolve,
// TODO: figure out a way to combine ESM and CJS coverage, such that
// we can exercise all the lines below:
require: (path: string) => {
if (typeof require !== 'undefined') {
return require(path)
Expand All @@ -61,3 +63,6 @@ yargsParser.camelCase = camelCase
yargsParser.decamelize = decamelize
yargsParser.looksLikeNumber = looksLikeNumber
export default yargsParser

// special syntax to allow unqualified default export from CommonJS
export {yargsParser as 'module.exports'};
1 change: 1 addition & 0 deletions lib/yargs-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright (c) 2016, Contributors
* SPDX-License-Identifier: ISC
*/
/* eslint-disable prefer-arrow-callback */

import { tokenizeArgString } from './tokenize-arg-string.js'
import type {
Expand Down
62 changes: 24 additions & 38 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
"name": "yargs-parser",
"version": "21.1.1",
"description": "the mighty option parser used by yargs",
"main": "build/index.cjs",
"main": "build/lib/index.js",
"exports": {
".": [
{
"import": "./build/lib/index.js",
"require": "./build/index.cjs"
"import": "./build/lib/index.js"
},
"./build/index.cjs"
"./build/lib/index.js"
],
"./browser": [
"./browser.js"
Expand All @@ -18,19 +17,16 @@
"type": "module",
"module": "./build/lib/index.js",
"scripts": {
"check": "standardx '**/*.ts' && standardx '**/*.js' && standardx '**/*.cjs'",
"fix": "standardx --fix '**/*.ts' && standardx --fix '**/*.js' && standardx --fix '**/*.cjs'",
"pretest": "rimraf build && tsc -p tsconfig.test.json && cross-env NODE_ENV=test npm run build:cjs",
"test": "c8 --reporter=text --reporter=html mocha test/*.cjs",
"test:esm": "c8 --reporter=text --reporter=html mocha test/*.mjs",
"check": "gts lint",
"fix": "gts fix",
"pretest": "rimraf build && tsc -p tsconfig.test.json",
"test": "c8 --reporter=text --reporter=html mocha test/*.mjs",
"test:browser": "start-server-and-test 'serve ./ -p 8080' http://127.0.0.1:8080/package.json 'node ./test/browser/yargs-test.cjs'",
"pretest:typescript": "npm run pretest",
"test:typescript": "c8 mocha ./build/test/typescript/*.js",
"coverage": "c8 report --check-coverage",
"precompile": "rimraf build",
"compile": "tsc",
"postcompile": "npm run build:cjs",
"build:cjs": "rollup -c",
"prepare": "npm run compile"
},
"repository": {
Expand All @@ -51,29 +47,24 @@
"author": "Ben Coe <[email protected]>",
"license": "ISC",
"devDependencies": {
"@types/chai": "^4.2.11",
"@types/mocha": "^9.0.0",
"@types/node": "^16.11.4",
"@typescript-eslint/eslint-plugin": "^3.10.1",
"@typescript-eslint/parser": "^3.10.1",
"c8": "^7.3.0",
"chai": "^4.2.0",
"@babel/eslint-parser": "^7.27.1",
"@babel/preset-typescript": "^7.27.1",
"@types/chai": "^5.2.1",
"@types/mocha": "^10.0.10",
"@types/node": "^22.15.3",
"@typescript-eslint/eslint-plugin": "^8.29.1",
"@typescript-eslint/parser": "^8.31.1",
"c8": "^10.1.3",
"chai": "^5.2.0",
"cross-env": "^7.0.2",
"eslint": "^7.0.0",
"eslint-plugin-import": "^2.20.1",
"eslint-plugin-node": "^11.0.0",
"gts": "^3.0.0",
"mocha": "^10.0.0",
"puppeteer": "^16.0.0",
"rimraf": "^3.0.2",
"rollup": "^2.22.1",
"rollup-plugin-cleanup": "^3.1.1",
"rollup-plugin-ts": "^3.0.2",
"eslint": "^8.57.1",
"gts": "^5.3.1",
"mocha": "^11.1.0",
"puppeteer": "^24.6.1",
"rimraf": "^6.0.1",
"serve": "^14.0.0",
"standardx": "^7.0.0",
"start-server-and-test": "^1.11.2",
"ts-transform-default-export": "^1.0.2",
"typescript": "^4.0.0"
"start-server-and-test": "^2.0.11",
"typescript": "^5.8.3"
},
"files": [
"browser.js",
Expand All @@ -82,11 +73,6 @@
"!*.d.cts"
],
"engines": {
"node": ">=12"
},
"standardx": {
"ignore": [
"build"
]
"node": "^20.19.0 || ^22.12.0 || >=23"
}
}
27 changes: 0 additions & 27 deletions rollup.config.js

This file was deleted.

6 changes: 5 additions & 1 deletion test/browser/yargs-test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ const puppeteer = require('puppeteer')
let browser
async function parse (argv, opts) {
if (!browser) {
browser = await puppeteer.launch()
// The developer install of Chromium is blocked by apparmor changes in Ubuntu 22.04.
// We are only running local tests and so easiest setup is to skip the sandbox.
browser = await puppeteer.launch({
args: ['--no-sandbox', '--disable-setuid-sandbox']
})
}
const page = await browser.newPage()
opts = encodeURIComponent(JSON.stringify(opts))
Expand Down
4 changes: 2 additions & 2 deletions test/string-utils.cjs → test/string-utils.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* global describe, it */

const { strictEqual } = require('assert')
const { camelCase, decamelize, looksLikeNumber } = require('../build/index.cjs')
import { strictEqual } from 'assert'
import { camelCase, decamelize, looksLikeNumber } from '../build/lib/string-utils.js'

describe('string-utils', function () {
describe('camelCase', () => {
Expand Down
1 change: 0 additions & 1 deletion test/tscc/.gitignore

This file was deleted.

1 change: 0 additions & 1 deletion test/tscc/lib

This file was deleted.

47 changes: 0 additions & 47 deletions test/tscc/optimized.ts

This file was deleted.

8 changes: 0 additions & 8 deletions test/tscc/package.json

This file was deleted.

Loading