Skip to content

v0.0.2

Choose a tag to compare

@Colourlessglow Colourlessglow released this 09 Jun 11:14
· 52 commits to main since this release

esconf

配置文件加载工具

内置配置文件解析器一览

  • .mts .ts 基于 importx 实现
  • .mjs .js 基于 原生 import('id') 实现
  • .json .jsonc .json5 .yaml .yml .toml 基于 confbox 实现

npm version
npm downloads
bundle size
license

注意: 预设 js,ts 解析只包含 es module 的解析

安装

# ✨ Auto-detect
npx nypm install esconf

# npm
npm install esconf

# yarn
yarn add esconf

# pnpm
pnpm install esconf

# bun
bun install esconf

简单使用

import { loadConfig } from 'esconf'
import { packageJsonLayer , tsParser } from 'esconf/presets'

const config = await loadConfig({
    // 数组越靠前配置文件的优先级越高
    layers: [
      {
        // load from vrx.config.{mts,ts}
        files: ['vrx.config'],
        extensions: ['mts', 'ts'],
        parser: 'ts',
      },
      {
        // load from vrx.config.{js,mjs}
        files: ['vrx.config'],
        extensions: ['js', 'mjs'],
        parser: 'js',
      },
      {
        // load from vrx.{yaml,yml}
        files: ['vrx'],
        extensions: ['yaml', 'yml'],
        parser: 'yaml',
      },
      {
         // load from vrx.toml
        files: ['vrx'],
        extensions: ['toml'],
        parser: 'toml',
      },
      // 从 package.json 的 vrx key 获取
      packageJsonLayer({ configKey: 'vrx' }),
      // 自定义 cjs 获取的读取
      {
        file:['vrx'],
        extensions:['cjs'],
        parser:(code,id)=> {
          return require(id)
        }
      },
      {
        file:['vrx'],
        extensions:['cts'],
        parser: tsParser({
          // 可继承内置的 tsParser
          // 配置 importx 的选项以尝试支持 cts 文件的读取
          // ....
        })
      },
    ],
    // 配置默认值
    default: { defaultValue: 'value' },
    cwd: process.cwd(),
  })

贡献者

Published under the MIT license.
Made by @Colourlessglow and community 💛






🤖 auto updated with automd