Last active
September 1, 2016 10:18
-
-
Save theharveyz/710606bd7da460b4e4b971118ad86b98 to your computer and use it in GitHub Desktop.
ES6模拟package功能,导入一个文件夹下的所有module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fs from 'fs'; | |
import path from 'path'; | |
const modules = {}; | |
fs | |
.readdirSync(__dirname) | |
.filter((file) => { | |
const fileArray = file.split('.'); | |
// 排除[隐藏文件]、[非js文件]、[index.js本身] | |
return [0, -1].indexOf(file.indexOf('.')) === -1 | |
&& ['js', 'es6'].indexOf(fileArray.pop()) !== -1 | |
&& fileArray[0] != 'index'; | |
}) | |
.forEach((file) => { | |
const module = require(path.join(__dirname, file)).default; | |
modules[module.name] = module; | |
}); | |
export default modules; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment