unconfig
用于加载配置的通用解决方案。
为什么?
配置很难,尤其是当您想构建工具的生态系统时。
您希望自己的工具成为一般易于使用,允许在package.json的自定义字段中定义您的配置。
您希望您的工具易于集成,允许在其他工具的配置中定义配置,例如vite.config.js或webpack.config.js 。
您希望配置是不可知论的,并且可能需要由IDE加载,您可以创建新的配置文件,例如.myconfigrc 。
您希望配置也具有灵活性和动态性,使您的配置文件成为JavaScript文件,例如my.config.js 。
然后,您希望用户能够使用ESM和Typescript,还可以使您的配置接受.ts或.mjs 。
So users' project root end up with a lot of config files like .npmrc , rollup.config.js , .eslintrc , tsconfig.json , jest.config.js , postcss.config.js , nuxt.config.js , vite.config.cjs , windi.config.ts , etc. And each of them use different syntax, in JSON, in CJS,在ESM中,在打字稿中,INI,toml ...
unconfig无法完全解决这种碎片,但它试图使工具作者更容易加载它们。
用法
npm i unconfig例如,为my.config加载配置:
unconfig'
const { config, sources } = await loadConfig({
sources: [
// load from `my.config.xx`
{
files: 'my.config',
// default extensions
extensions: ['ts', 'mts', 'cts', 'js', 'mjs', 'cjs', 'json', ''],
},
// load `my` field in `package.json` if no above config files found
{
files: 'package.json',
extensions: [],
rewrite(config) {
return config?.my
},
},
// load inline config from `vite.config`
{
files: 'vite.config',
async rewrite(config) {
const resolved = await (typeof config === 'function' ? config() : config)
return resolved?.my
},
},
// ...
],
// if false, the only the first matched will be loaded
// if true, all matched will be loaded and deep merged
merge: false,
})">
import { loadConfig } from ' unconfig ' const { config , sources } = await loadConfig ( { sources : [ // load from `my.config.xx` { files : 'my.config' , // default extensions extensions : [ 'ts' , 'mts' , 'cts' , 'js' , 'mjs' , 'cjs' , 'json' , '' ] , } , // load `my` field in `package.json` if no above config files found { files : 'package.json' , extensions : [ ] , rewrite ( config ) { return config ?. my } , } , // load inline config from `vite.config` { files : 'vite.config' , async rewrite ( config ) { const resolved = await ( typeof config === 'function' ? config ( ) : config ) return resolved ?. my } , } , // ... ] , // if false, the only the first matched will be loaded // if true, all matched will be loaded and deep merged merge : false , } )
默认情况下, unconfig支持加载ts , mjs , js , json 。
赞助商
执照
麻省理工学院许可©2021-Tresent Anthony Fu
下载源码
通过命令行克隆项目:
git clone https://github.com/antfu-collective/unconfig.git