Skip to content

Commit

Permalink
Improved hybrid module, no .default needed
Browse files Browse the repository at this point in the history
Same trick as in isaacs/node-mkdirp#41
  • Loading branch information
isaacs committed Jan 17, 2023
1 parent b563d8b commit 842c6ae
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 19 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 2 additions & 2 deletions fixup.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/usr/bin/env bash

cat >dist/cjs/src/package.json <<!EOF
cat >dist/cjs/package.json <<!EOF
{
"type": "commonjs"
}
!EOF

cat >dist/mjs/src/package.json <<!EOF
cat >dist/mjs/package.json <<!EOF
{
"type": "module"
}
Expand Down
2 changes: 1 addition & 1 deletion libtap-settings.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// use this module for tap's recursive directory removal, so that
// the windows tests don't fail with EBUSY.
const rimraf = require('./').default
const rimraf = require('./')
module.exports = {
rmdirRecursiveSync: path => rimraf.sync(path),
rmdirRecursive(path, cb) {
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
},
Expand All @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion src/bin.ts
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
3 changes: 3 additions & 0 deletions src/index-cjs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import rimraf from './index.js'

export = Object.assign(rimraf, { default: rimraf })
10 changes: 5 additions & 5 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
},
},
Expand Down Expand Up @@ -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]) {
Expand Down
1 change: 1 addition & 0 deletions tsconfig-esm.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
3 changes: 2 additions & 1 deletion tsconfig-cjs.json → tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": "./tsconfig-base.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "dist/cjs"
"outDir": "dist/cjs",
"moduleResolution": "Node"
}
}

0 comments on commit 842c6ae

Please sign in to comment.