1.0.5 • Published 5 years ago
webpack-recursive-loader v1.0.5
Webpack Recursive Loader
Allows directories to be imported recursively as javascript objects. Useful for easily loading entire directories recursively to traverse. For example, can be used to load api endpoints server-side so they can be mounted in an express app. Supports webpack hot-reloading.
Installation
npm install -D webpack-recursive-loaderUsage
Given a directory structure like
|- api
| |- a.js
| |- subdir
| |- b.js
|- index.js//webpack.config.js
module.exports = {
//...
plugins: [
//...
new WebpackRecursiveLoaderPlugin()
]
//...
};//api/a.js
export function myFunc(x, y){
return x * y;
}//api/subdir/b.js
export const myNumber = 7;//index.js
import assert from 'assert';
import * as api from './api?recursive';
import {myFunc} from './api/a';
import {myNumber} from './api/subdir/b';
assert.strictEqual(api.a.myFunc, myFunc);
assert.strictEqual(api.b.subdir.myNumber, myNumber);Options
includeOnly includes files/directories which match the given pattern. Directories are included automatically if they don't match the pattern. Can be one of the following types:stringA glob to match against root-relative file paths (not directories).RegExpA regex to match against root-relative file paths (not directories).function: (file: File) -> booleanA custom function to check whether to include the file. See below forFileproperties.ArrayAn array of the preceding to be or-ed together.
excludeExcludes file/directories which match the given pattern. Accepts the same types asinclude.getName: (file: File) -> stringAllows customization over how names are generated for a given file. By default, the name for a particular file is the basename without extension(s).namespaceChanges the namespace to which this plugin applies. By specifying multiple instances of the plugin with different namespaces, you can use multiple configurations side-by-side. Namespaces can be specified in the query string like this:./api?recursive=myNamespace.keyword(default'recursive') Allows the keyword which triggers the recursive loader to be changed.
File objects have the following properties:
typeThe type of the object. One of'file'or'directory'. (Symlinks are not yet supported, sorry.)basenameThe basename of the file.pathThe root-relative path of the file.absPathThe absolute path of the file.
Todo
- Support symlinks
- Add documentation example for namespaces