cosmiconfig 方便加载配置的npm 包

cosmiconfig 是一个比较方便的工具,提供了灵活的配置加载处理能力,可以用来实现不少框架的约定配置文件能力

参考玩法

  • index.ts
const { cosmiconfig } = require('cosmiconfig');
const explorer = cosmiconfig("rong");
const config = await explorer.search();
console.log(config);

  • rong.config.js
module.exports = {  
   name: "rong",
   version: "1.0.0",
   description: "Configuration for the Rong project",
   callFunctions: async function() {
   },
   plugins: [
     {name: "pluginName"}
   ]
};
  • 运行效果
{
  config: {
    name: "rong",
    version: "1.0.0",
    description: "Configuration for the Rong project",
    callFunctions: [AsyncFunction: callFunctions],
    plugins: [
      [Object ...]
    ],
  },
  filepath: "/Users/dalong/mylearning/myconf/rong.config.js",
}

说明

此模块使用的项目还是挺多的,如果做框架很值得尝试下

参考资料

https://github.com/cosmiconfig/cosmiconfig

posted on 2026-09-27 08:00  荣锋亮  阅读(5)  评论(0)    收藏  举报

导航