背景
前段小伙伴在开发过工具库时,往往避免不了要导出所有工具函数到 index.js 中。如果我们手动维护 index.js,会非常的费时间,且容易出错。
使用 create-index
create-index 能够帮我们自动生成 index.js。下面是一个简单的例子。
- 首先全局安装 create-index
npm install -g create-index
- 假设我们有以下目录结构
src
├── bar.js
└── foo.js
- 运行命令
create-index ./src
[13:17:34] Target directories [ './src' ]
[13:17:34] Update index: false
[13:17:34] ./index.js [created index]
[13:17:34] Done
- 生成 index.js
src
├── bar.js
├── foo.js
└── index.js
0 directories, 3 files
- 查看 index.js 内容,可见 create-index 已经帮我们自动导出所有
// @create-index
export { default as bar } from './bar.js';
export { default as foo } from './foo.js';
以上是 create-index 最主要的功能,当然还有不少其他用法,感兴趣的小伙伴可以看看文档。