Require-Load
An asynchronous module loader for nodejs that integrates with the existing module system.
Installation
Npm
npm install require-load
API
load(file: String, options: Object) -> {Promise}
Asynchronously require a specific file. File name resolution works exactly the same as the native require
function.
load.resolve(file: String, options: Object) -> {Promise}
Asynchronously resolve a file path. (Like require.resolve but async.).
Options (same for both load and load.resolve)
cache=false (default true)
When the cache option is false the module will be re-evaluated and not cached for the next load call.
directory=__dirname
(default relative to the function calling load)
You can optionally choose which directory the resolve files from. By default this will be relative to where ever this module is required.
file=__filename
(default relative to the function calling load)
You can also optionally specify a file from which the loader should run. (This also defaults the directory option to be the dirname of the file).
resolve.fileSystem=MemoryFileSystem
(default to nodeFileSystem.)
You can choose the file system to use when resolving and compiling files. The default will just be the standard 'fs' module but you can also get fancy and use other files systems like 'memory-fs'.
const memoryFs = // Save a file to memory fs.memoryFs // Load the file from memory.
Clearing the cache
Clearing a file from the cache is the exact same as any other node module (once the file has loaded).
Custom extensions
Just like node's require you can add or overwrite the file extensions, however instead of calling the extension function with a filename it will be called with the file's contents.
// A naive babel loader for babel es6 files.loadextensions'.es6' = { module}
Contributions
- Use
npm test
to run tests.
Please feel free to create a PR!