node-plugin-require-context

1.0.0 • Public • Published

node-plugin-require-context

NPM version Downloads License

nodejs plugin like webpack require-context.

Inspiration from webpack's require.context,You can batch import files in NodeJs.

Usage

install

npm install --save-dev node-plugin-require-context

Example

const requireContext = require('node-plugin-require-context');
const context = requireContext('./', true, /\.js$/, __dirname);

context.keys().forEach(modulePath => {
    /* get module */
    context(modulePath);
});

Options

requireContext ( directory, useSubdirectories, regExp, rootPath, fn )

  • directory

search dir, you can input like './' or '../' and so on, Required parameter.

  • useSubdirectories

true will search all child file; false only search current dir, Required parameter.

  • regExp

RegExp match; match the files you need,Required parameter.

  • rootPath

this is indispensable,The fixed value is __dirname, Required parameter.

  • fn

A callback function; when matching to the correct file will call it,Optional parameters

API

The context function has 2 properties: resolve and keys.

  • resolve is a function and when passed in the module relative path and returns absolute path.
  • keys is a function that returns an array of all possible requests that the context module can handle.

Package Sidebar

Install

npm i node-plugin-require-context

Weekly Downloads

3

Version

1.0.0

License

MIT

Unpacked Size

7.62 kB

Total Files

9

Last publish

Collaborators

  • condorhero