这里是我想要实现一个简单的例子:导出多个文件作为单个模块中节点JS
foo.js:
module.exports.one = function(params) { */ stuff */ }
bar.js:
module.exports.two = function(params) { */ stuff */ }
stuff.js:
const foo = require('Path/foo');
const bar = require('Path/bar');
我想做的事:
otherFile.js:
stuff = require('Path/stuff');
stuff.one(params);
stuff.two(params);
我不想做[在stuff.js]
module.exports = {
one : foo.one,
two: bar.two
}
的我附带的解决方案是:
const files = ['path/foo', 'path/bar']
module.exports = files
.map(f => require(f))
.map(f => Object.keys(f).map(e => ({ [e]: f[e] })))
.reduce((a, b) => a.concat(b), [])
.reduce((a, b) => Object.assign(a, b), {})
或丑陋/短:
module.exports = files
.map(f => require(f))
.reduce((a, b) => Object.assign(a, ...Object.keys(b).map(e => ({ [e]: b[e] }))));
感觉 “的hackish”。
有没有更干净的方法来做到这一点?
该OP没有使用ES6转换器。 – 2017-07-31 02:09:25