Skip to content

Instantly share code, notes, and snippets.

@theharveyz
Last active September 1, 2016 10:18
Show Gist options
  • Save theharveyz/710606bd7da460b4e4b971118ad86b98 to your computer and use it in GitHub Desktop.
Save theharveyz/710606bd7da460b4e4b971118ad86b98 to your computer and use it in GitHub Desktop.
ES6模拟package功能,导入一个文件夹下的所有module
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