This is an esModule implementation of the following module: https://github.com/aseemk/requireDir
I've tried to port as many of its features that seem useful or usable with esModules.
Currently only .js
files are supported as .json
files are only available with experimental flags.
With esModule an async module loader is used and to allow flexibility you need to await the each key of the object when you want to use it.
Currently esModules are not aware of the parent module they've been imported from. This means that an absolute path needs to be passed. If you try to pass a relative path, this will return an error.
I can add support for relative paths later on but currently this can only be achieved with experimental launch flags, if an issue is opened on GitHub, I'll take a look at adding it.
You can use resolve to get the absolute path of a relative path.
import { resolve } from 'path'
import importDir from '@yimura/import-dir'
const import = importDir(resolve('./dir/'));
Given this directory structure:
dir
+ a.js
+ b.json
+ c.coffee
+ d.txt
+ e.js
importDir(resolve('./dir'))
will return the equivalent of:
{
a: Promise<import('./dir/a.js')>,
e: Promise<import('./dir/e.js')>
}
npm install @yimura/import-dir
Basic usage that examines only directories' immediate files:
import { resolve } from 'path'
import importDir from '@yimura/import-dir'
let dir = importDir(resolve('./path/to/dir'));
You can optionally customize the behavior by passing an extra options object:
let dir = importDir('/path/to/dir', { recurse: true });
recurse
: Whether to recursively import()
subdirectories too.
(node_modules
within subdirectories will be ignored.)
Default is false.
recurseDepth
: Set how deep we have to recurse.
noCache
: Prevent file caching. Could be useful using gulp.watch or other watch requiring refreshed file content Default is false.
let dir = importDir('/path/to/dir', { noCache: true });