From 842c6ae876a42e51b01d578fdb2c051462a74513 Mon Sep 17 00:00:00 2001 From: isaacs Date: Mon, 16 Jan 2023 16:37:24 -0800 Subject: [PATCH] Improved hybrid module, no .default needed Same trick as in https://github.com/isaacs/node-mkdirp/pull/41 --- CHANGELOG.md | 6 ++++++ README.md | 4 ++-- fixup.sh | 4 ++-- libtap-settings.js | 2 +- package.json | 14 +++++++------- src/bin.ts | 3 ++- src/index-cjs.ts | 3 +++ test/index.js | 10 +++++----- tsconfig-esm.json | 1 + tsconfig-cjs.json => tsconfig.json | 3 ++- 10 files changed, 31 insertions(+), 19 deletions(-) create mode 100644 src/index-cjs.ts rename tsconfig-cjs.json => tsconfig.json (62%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 774bdb32..5fe51ce2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# v4.1 + +- Improved hybrid module with no need to look at the `.default` + dangly bit. `.default` preserved as a reference to `rimraf` + for compatibility with anyone who came to rely on it in v4.0. + # v4.0 - Remove `glob` dependency entirely. This library now only diff --git a/README.md b/README.md index 03251006..c51081c3 100644 --- a/README.md +++ b/README.md @@ -21,9 +21,9 @@ Hybrid module, load either with `import` or `require()`. ```js // default export is the main rimraf function, or use named imports -import { rimraf } from 'rimraf' +import rimraf from 'rimraf' // or -const { rimraf } = require('rimraf') +const rimraf = require('rimraf') // other strategies exported as well import { rimraf, rimrafSync, native, nativeSync } from 'rimraf' diff --git a/fixup.sh b/fixup.sh index e1859aec..9fd16d16 100644 --- a/fixup.sh +++ b/fixup.sh @@ -1,12 +1,12 @@ #!/usr/bin/env bash -cat >dist/cjs/src/package.json <dist/cjs/package.json <dist/mjs/src/package.json <dist/mjs/package.json < rimraf.sync(path), rmdirRecursive(path, cb) { diff --git a/package.json b/package.json index 3ab0d405..6205110b 100644 --- a/package.json +++ b/package.json @@ -1,18 +1,18 @@ { "name": "rimraf", "version": "4.0.7", - "main": "./dist/cjs/src/index.js", - "module": "./dist/mjs/src/index.js", + "main": "./dist/cjs/src/index-cjs.js", + "module": "./dist/mjs/index.js", "bin": "./dist/cjs/src/bin.js", "exports": { ".": { "import": { - "default": "./dist/mjs/src/index.js", - "types": "./dist/mjs/src/index.d.ts" + "default": "./dist/mjs/index.js", + "types": "./dist/mjs/index.d.ts" }, "require": { - "default": "./dist/cjs/src/index.js", - "types": "./dist/cjs/src/index.d.ts" + "default": "./dist/cjs/src/index-cjs.js", + "types": "./dist/cjs/src/index-cjs.d.ts" } } }, @@ -28,7 +28,7 @@ "postversion": "npm publish", "prepublishOnly": "git push origin --follow-tags", "preprepare": "rm -rf dist", - "prepare": "tsc -p tsconfig-cjs.json && tsc -p tsconfig-esm.json", + "prepare": "tsc -p tsconfig.json && tsc -p tsconfig-esm.json", "postprepare": "bash fixup.sh", "pretest": "npm run prepare", "presnap": "npm run prepare", diff --git a/src/bin.ts b/src/bin.ts index cbc8a26f..6aaed9b8 100755 --- a/src/bin.ts +++ b/src/bin.ts @@ -1,6 +1,7 @@ #!/usr/bin/env node import { version } from '../package.json' -import rimraf, { RimrafOptions } from './' +import rimraf from './index-cjs.js' +import type { RimrafOptions } from './index.js' const runHelpForUsage = () => console.error('run `rimraf --help` for usage information') diff --git a/src/index-cjs.ts b/src/index-cjs.ts new file mode 100644 index 00000000..7662c671 --- /dev/null +++ b/src/index-cjs.ts @@ -0,0 +1,3 @@ +import rimraf from './index.js' + +export = Object.assign(rimraf, { default: rimraf }) diff --git a/test/index.js b/test/index.js index 74929d0f..43a5a06d 100644 --- a/test/index.js +++ b/test/index.js @@ -5,12 +5,12 @@ t.same( { '.': { import: { - default: './dist/mjs/src/index.js', - types: './dist/mjs/src/index.d.ts', + default: './dist/mjs/index.js', + types: './dist/mjs/index.d.ts', }, require: { - default: './dist/cjs/src/index.js', - types: './dist/cjs/src/index.d.ts', + default: './dist/cjs/src/index-cjs.js', + types: './dist/cjs/src/index-cjs.d.ts', }, }, }, @@ -66,7 +66,7 @@ t.test('mocky unit tests to select the correct function', t => { }, } process.env.__TESTING_RIMRAF_PLATFORM__ = 'posix' - const rimraf = t.mock('../', mocks).default + const rimraf = t.mock('../', mocks) t.afterEach(() => (CALLS.length = 0)) for (const useNative of [true, false]) { diff --git a/tsconfig-esm.json b/tsconfig-esm.json index 9a571575..1384ff21 100644 --- a/tsconfig-esm.json +++ b/tsconfig-esm.json @@ -1,5 +1,6 @@ { "extends": "./tsconfig-base.json", + "exclude": ["./test", "./tap-snapshots", "src/index-cjs.ts", "src/bin.ts"], "compilerOptions": { "module": "esnext", "outDir": "dist/mjs" diff --git a/tsconfig-cjs.json b/tsconfig.json similarity index 62% rename from tsconfig-cjs.json rename to tsconfig.json index 7aae8118..13690021 100644 --- a/tsconfig-cjs.json +++ b/tsconfig.json @@ -2,6 +2,7 @@ "extends": "./tsconfig-base.json", "compilerOptions": { "module": "commonjs", - "outDir": "dist/cjs" + "outDir": "dist/cjs", + "moduleResolution": "Node" } }