batch-import
Advanced tools
Comparing version 2.0.1 to 3.0.0
@@ -57,4 +57,4 @@ 'use strict'; | ||
// 模块导出数据处理函数,覆盖原始值 | ||
if (option.process) { | ||
container[filename] = option.process.call(this.container, filename, container[filename]) | ||
if (option.import) { | ||
container[filename] = option.import.call(this.container, filename, container[filename]) | ||
} | ||
@@ -97,2 +97,6 @@ | ||
parser.recursion(directoryPath, option, container[name]) | ||
// 导入完毕后的处理函数 | ||
if (option.complete) { | ||
container[name] = option.complete.call(container, container[name]) | ||
} | ||
} | ||
@@ -99,0 +103,0 @@ |
{ | ||
"name": "batch-import", | ||
"version": "2.0.1", | ||
"version": "3.0.0", | ||
"description": "批量加载node.js模块、目录,并转换为对应的JS对象", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -17,23 +17,31 @@ # batch-import | ||
### 参数 | ||
### batchImport(container, container) | ||
* `options.path` *String* - 指定模块加载目录 | ||
* `options.path` *String* - 指定模块加载目录(必填) | ||
* `options.contain` *Array* - 仅加载指定模块,不能与exclude同时使用 | ||
* `options.contain` *Array* - 仅加载指定模块,不能与exclude同时使用(可选) | ||
* `options.exclude` *Array* - 排除指定模块,不能与contain同时使用 | ||
* `options.exclude` *Array* - 排除指定模块,不能与contain同时使用(可选) | ||
* `options.process` *Function* - 模块导出数据处理函数,用于数据检验、预处理等操作。 | ||
* `options.import` *Function* - 模块导出数据处理函数,用于数据检验、预处理等操作(可选) | ||
* `container` *Object* - 指定一个容器用于存放模块导出结果,按照指定顺序加载模块,来解决依赖时序问题(可选) | ||
* `options.complete` *Function* - 同一个配置项下的所有模块导出完成后的数据处理函数,用于数据检验、预处理等操作(可选) | ||
#### process函数参数 | ||
* `container` *Object* - 将模块导出结果保存到已存在的指定容器中(可选) | ||
> 函数中this指向根容器,按照约定,process函数必须返回包含error和data属性的对象,error用于抛出错误信息,data输出正常结果。 | ||
#### options.import([filename],[data]) | ||
> this指向根容器 | ||
* `filename` *String* - 当前文件名称,不含后缀 | ||
* `data` * - 模块返回值 | ||
* `data` * - 模块导出数据 | ||
#### options.import([data]) | ||
> this指向根容器 | ||
* `data` *Object* - 所有子集模块导出数据集合 | ||
### 示例 | ||
@@ -48,10 +56,23 @@ | ||
"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/", | ||
process(filename, func) { | ||
if (typeof func === 'function') { | ||
return func(this) | ||
import(filename, data) { | ||
if (data instanceof Function) { | ||
return data(this) | ||
} else { | ||
return { error: `模块输出数据类型必须为函数` } | ||
throw new Error(`${filename}模块导出必须为函数类型`) | ||
} | ||
@@ -74,8 +95,1 @@ }, | ||
* path路径中不应该出现./、../等相对路径表达式 | ||
### 与1.x版本差异 | ||
* 2.x调整了process函数的入参,参考入参部分 | ||
* 取消process函数必须以{error、data}作为返回值的约定,不再使用模块内代理throw抛出异常,导致不能直接定位到错误 |
46
test.js
"use strict" | ||
let batchImport = require('./index') | ||
let batchImport = require('.') | ||
let container = batchImport({ | ||
"extend": { | ||
"path": "app/extend", | ||
import(name, data) { | ||
if (data instanceof Function) { | ||
return data(this) | ||
} | ||
return data | ||
}, | ||
complete(data) { | ||
// 对extend进行扁平化处理,缩短访问路径 | ||
for (let name in data) { | ||
this[name] = data[name] | ||
} | ||
return data | ||
} | ||
}, | ||
"controllers": { | ||
@@ -10,7 +26,7 @@ "path": "app/controllers", | ||
// "exclude": ['c1/'], | ||
process(name, func) { | ||
if (func instanceof Function) { | ||
return func(this) | ||
import(name, data) { | ||
if (data instanceof Function) { | ||
return data(this) | ||
} else { | ||
throw new Error(`控制器输出数据类型必须为函数`) | ||
throw new Error(`controllers中${name}模块导出必须为函数类型`) | ||
} | ||
@@ -22,22 +38,12 @@ }, | ||
"exclude": ['c1'], | ||
process(name, func) { | ||
if (func instanceof Function) { | ||
return func(this) | ||
import(name, data) { | ||
if (data instanceof Function) { | ||
return data(this) | ||
} else { | ||
throw new Error(`模型输出数据类型必须为函数`) | ||
throw new Error(`models中${name}模块导出必须为函数类型`) | ||
} | ||
}, | ||
}, | ||
"extend": { | ||
"path": "app/extend", | ||
process(name, func) { | ||
if (func instanceof Function) { | ||
return func(this) | ||
} else { | ||
throw new Error(`扩展输出数据类型必须为函数`) | ||
} | ||
}, | ||
}, | ||
} | ||
}) | ||
console.log(container) |
7186
12
142
93