# batch-import

> 批量加载node.js模块、目录，并转换为对应的JS对象

Latest version **3.2.0** (published 2018-09-27) · ISC license · 0 weekly downloads

## Install

```sh
npm install batch-import
pnpm add batch-import
yarn add batch-import
bun add batch-import
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.2.0 |
| Published | 2018-09-27 |
| First published | 2017-11-10 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 6.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | xiangle |

## Links

- npm: https://www.npmjs.com/package/batch-import
- Repository: https://github.com/xiangle/batch-import
- Homepage: https://github.com/xiangle/batch-import#readme
- Issues: https://github.com/xiangle/batch-import/issues
- npm.io page: https://npm.io/package/batch-import

## Recent versions

- 3.2.0 (latest) — 2018-09-27
- 3.1.1 — 2018-09-13
- 3.1.0 — 2018-08-23
- 3.0.0 — 2018-07-05
- 2.0.1 — 2018-06-24
- 2.0.0 — 2018-06-23
- 1.2.7 — 2018-05-10
- 1.2.6 — 2018-05-10
- 1.2.5 — 2018-05-10
- 1.2.4 — 2018-03-10
- 1.2.3 — 2018-03-10
- 1.2.2 — 2018-03-09
- 1.2.0 — 2018-03-09
- 1.1.6 — 2017-11-17
- 1.1.5 — 2017-11-17
- … 6 more at https://npm.io/package/batch-import/versions

## README

# batch-import

批量加载指定目录下node.js模块，并将模块导出结果保存到与模块路径结构一致的对象中

### install

      npm install batch-import

### 使用方法

```js
let batchImport = require('batch-import')

let modules = batchImport(options, container)
```

### batchImport(options, container)

*  `options` *Object*

      *  `path` *String* - 模块所在相对路径（必填）

      *  `contain` *Array* - 加载指定模块或目录，支持深度递归匹配，与exclude互斥（可选）

      *  `exclude` *Array* - 排除指定模块或目录，支持深度递归匹配，与contain互斥（可选）

      *  `import(filename, data)` *Function* - 模块导出数据处理函数，this指向当前层级容器。用于数据检验、预处理等操作（可选）

            *  `filename` *String* - 当前文件名称，不含后缀

            *  `data` * - 模块导出数据

      *  `complete(data)` *Function* - 同一个配置项下的所有模块导出完成后的数据处理函数，this指向根容器。用于数据检验、预处理等操作（可选）

            *  `data` *Object* - 所有子集模块导出数据集合

*  `container` *Object* - 可选，将模块导出结果保存到指定容器中


### 示例

```js
let batchImport = require('batch-import')

let modules = batchImport({
   "middleware": {
      "path": "middleware/",
      "exclude": ["test.js"],
      import(filename, data) {
         if (data instanceof Function) {
            return data(this)
         } else {
            throw new Error(`${filename}模块导出必须为函数类型`)
         }
      },
      complete(data) {
         for (let name in data) {
            this[name] = data[name]
         }
         return data
      }
   },
   "models": {
      "path": "models/",
      import(filename, data) {
         if (data instanceof Function) {
            return data(this)
         } else {
            throw new Error(`${filename}模块导出必须为函数类型`)
         }
      },
   },
   "controllers": {
      "path": "controllers/",
      "contain": ["_route.js"],
   },
})
```

### 注意事项

* 配置项加载顺序与定义顺序一致，配置越靠前优先级越高

* path路径以入口文件所在位置作为根路径

* path路径中不应该出现./、../等相对路径表达式

---
_Source: https://npm.io/package/batch-import · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
