Skip to content
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

feat: support for esm named imports #986

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Expand Up @@ -59,23 +59,23 @@ const fse = require('fs-extra')

### ESM

There is also an `fs-extra/esm` import, that supports both default and named exports. However, note that `fs` methods are not included in `fs-extra/esm`; you still need to import `fs` and/or `fs/promises` seperately:
There is also an `fs-extra` import, that supports both default and named exports. However, note that `fs` methods are not included in `fs-extra`; you still need to import `fs` and/or `fs/promises` seperately:

```js
import { readFileSync } from 'fs'
import { readFile } from 'fs/promises'
import { outputFile, outputFileSync } from 'fs-extra/esm'
import { outputFile, outputFileSync } from 'fs-extra'
```

Default exports are supported:

```js
import fs from 'fs'
import fse from 'fs-extra/esm'
import fse from 'fs-extra'
// fse.readFileSync is not a function; must use fs.readFileSync
```

but you probably want to just use regular `fs-extra` instead of `fs-extra/esm` for default exports:
but you probably want to just use regular `fs-extra` instead of `fs-extra` for default exports:

```js
import fs from 'fs-extra'
Expand Down Expand Up @@ -224,7 +224,7 @@ fs-extra contains hundreds of tests.

- `npm run lint`: runs the linter ([standard](http://standardjs.com/))
- `npm run unit`: runs the unit tests
- `npm run unit-esm`: runs tests for `fs-extra/esm` exports
- `npm run unit-esm`: runs tests for `fs-extra` exports
- `npm test`: runs the linter and all tests


Expand Down
35 changes: 35 additions & 0 deletions generate.js
@@ -0,0 +1,35 @@
const fsExtra = require('./lib/index')
const path = require('path')
const { difference } = require('lodash')

function scan () {
const excludes = [
'FileReadStream',
'FileWriteStream',
'_toUnixTimestamp',
'F_OK',
'R_OK',
'W_OK',
'X_OK',
'gracefulify'
]
return difference(Object.keys(fsExtra), excludes)
}

function generate (list) {
return (
'import fsExtra from \'./index\'\n' +
list.map((item) => `export const ${item} = fsExtra.${item}\n`).join('') +
`export default {${list
.map((item) => `${item}: fsExtra.${item},`)
.join('')}}`
)
}

async function build () {
const list = scan()
const code = generate(list)
await fsExtra.writeFile(path.resolve(__dirname, 'lib/esm.mjs'), code)
}

build()