Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Find and load configuration from a package.json property, rc file, or CommonJS module.

License

NotificationsYou must be signed in to change notification settings

jaywcjlove/auto-config-loader

Repository files navigation

Usingmy app is also a way tosupport me:
KeyzerVidwall HubVidCropVidwallMousio HintMousioMusicerAudioerFileSentinelFocusCursorVideoerKeyClickerDayBarIconedMousioQuick RSSQuick RSSWeb ServeCopybook GeneratorDevTutor for SwiftUIRegexMateTime PassageIconize FolderTextsound SaverCreate Custom SymbolsDevHubResume RevisePalette GeniusSymbol Scribe

Auto Config Loader

Buy me a coffeeCINPM versionCoverage Statusnpm Downloads

Find and load configuration from apackage.json property,rc file, orCommonJS module. It has smart default based on traditional expectations in the JavaScript ecosystem. But it's also flexible enough to search anywhere you want and load whatever you want.

V1 To V2 Migration

Features

Install

$ npm i auto-config-loader

Quick start

constautoConf=require('auto-config-loader');import{autoConf}from'auto-config-loader';// will look for:// process.cwd() + '.namespacerc'// process.cwd() + '.namespacerc.js'// process.cwd() + '.namespacerc.ts'// process.cwd() + '.namespacerc.mjs'// process.cwd() + '.namespacerc.cjs'// process.cwd() + '.namespacerc.json'// process.cwd() + '.namespacerc.json5'// process.cwd() + '.namespacerc.jsonc'// process.cwd() + '.namespacerc.yaml'// process.cwd() + '.namespacerc.yml'// process.cwd() + '.namespacerc.toml'// process.cwd() + 'namespace.config.mjs'// process.cwd() + 'namespace.config.cjs'// process.cwd() + 'namespace.config.js'// ........constdata=awaitautoConf('namespace',{default:{testItem2:'some value'}});

Load JS

Load the JS file and return the result, support.js,.cjs,.mjs,.ts.

// => ./app/app.config.jsexportdefault{name:'app'}
import{loadConf}from'auto-config-loader/load-conf';interfaceConfig{name:string;}constresult=awaitloadConf<Config>('./app/app.config.js');// => { name: 'app' }

Option

import{LoadConfOption}from'auto-config-loader';exporttypeLoaderFunc<T>=(filepath:string,content:string,jsOption?:LoadConfOption)=>T|Promise<T>;exporttypeLoader<T>=Record<string,LoaderFunc<T>>;exportinterfaceAutoConfOption<T>{searchPlaces?:string[];/** An object that maps extensions to the loader functions responsible for loading and parsing files with those extensions. */loaders?:Loader<T>;/** Specify default configuration. It has the lowest priority and is applied after extending config. */default?:T;/** Resolve configuration from this working directory. The default is `process.cwd()` */cwd?:string;/** Default transform js configuration */jsOption?:LoadConfOption;/**@deprecated use `mustExist` instead */ignoreLog?:boolean;mustExist?:boolean;}exportdeclareconstgetConfigPath:()=>string;/** * Find and load configuration from a `package.json` property, `rc` file, or `CommonJS` module. *@param namespace {string} Configuration base name. The default is `autoconf`. *@param option */exportdeclarefunctionautoConf<T>(namespace?:string,option?:AutoConfOption<T>):Promise<{}&T>;exportdefaultautoConf;

Discover configurations in the specified directory order. When configuring a tool, you can use multiple file formats and put these in multiple places. Usually, a tool would mention this in its own README file, but by default, these are the following places, where${moduleName} represents the name of the tool:

DefaultsearchPlaces:

['package.json',`.${moduleName}rc`,`.${moduleName}rc.json`,`.${moduleName}rc.json5`,`.${moduleName}rc.jsonc`,`.${moduleName}rc.yaml`,`.${moduleName}rc.yml`,`.${moduleName}rc.toml`,`.${moduleName}rc.ini`,`.${moduleName}rc.js`,`.${moduleName}rc.ts`,`.${moduleName}rc.cjs`,`.${moduleName}rc.mjs`,`.config/${moduleName}rc`,`.config/${moduleName}rc.json`,`.config/${moduleName}rc.json5`,`.config/${moduleName}rc.jsonc`,`.config/${moduleName}rc.yaml`,`.config/${moduleName}rc.yml`,`.config/${moduleName}rc.toml`,`.config/${moduleName}rc.ini`,`.config/${moduleName}rc.js`,`.config/${moduleName}rc.ts`,`.config/${moduleName}rc.cjs`,`.config/${moduleName}rc.mjs`,`${moduleName}.config.js`,`${moduleName}.config.ts`,`${moduleName}.config.cjs`,`${moduleName}.config.mjs`,]

Configurations are loaded sequentially, and the configuration file search is terminated when a configuration file exists.

The content of these files is defined by the tool. For example, you can add asemi configuration value tofalse using a file called.config/autoconfig.yml:

semi:true

Additionally, you have the option to put a property named after the tool in yourpackage.json file, with the contents of that property being the same as the file contents. To use the same example as above:

{"name":"your-project","autoconfig":{"semi":true}}

This has the advantage that you can put the configuration of all tools (at least the ones that useauto-config-loader) in one file.

Loader

.js,.ts,.cjs,.mjs

importtypejitifrom'jiti';import{Options}from'sucrase';typeJiti=ReturnType<typeofjiti>;typeJITIOptions=Jiti['options'];exportinterfaceLoadConfOption{jiti?:boolean;jitiOptions?:JITIOptions;transformOption?:Options;}exportdeclarefunctionloadConf<T>(path:string,option?:LoadConfOption):Promise<T>;exportdeclarefunctionjsLoader<T>(filepath:string,content:string,option?:LoadConfOption):Promise<T>;

Modify default.js,.ts,.cjs,.mjs loader parameters.

importload,{jsLoader}from'auto-config-loader';functionloadJS(filepath,content){returnjsLoader(filepath,content,{// change option...});}constdata=awaitload('namespace',{loaders:{'.js':loadJS,'.ts':loadJS,'.cjs':loadJS,'.mjs':loadJS,},default:{testItem2:'some value'}});

example:

import{jsLoader}from'auto-config-loader';constdata=jsLoader('/path/to/file/name.js')

.ini

exportdeclarefunctioniniLoader<T>(_:string,content:string):T;

example:

import{iniLoader}from'auto-config-loader';constdata=iniLoader(undefined,`...`)

.json,.jsonc,json5

exportdeclarefunctionjsonLoader<T>(_:string,content:string):T;

example:

import{jsonLoader}from'auto-config-loader';constdata=jsonLoader(undefined,`{ "a": 123 }`)

.toml

exportdeclarefunctiontomlLoader<T>(_:string,content:string):T;

example:

import{tomlLoader}from'auto-config-loader';constdata=tomlLoader(undefined,`...`)

.yaml

exportdeclarefunctionyamlLoader<T>(_:string,content:string):T;

example:

import{yamlLoader}from'auto-config-loader';constdata=yamlLoader(undefined,`...`)

CustomYaml loader

This is an example, the defaultyaml/yml does not require a loader.

importloadfrom'auto-config-loader';importyamlfrom'yaml';functionloadYaml(filepath,content){returnyaml.parse(content);}constdata=awaitload('namespace',{searchPlaces:['.namespacerc.yaml','.namespacerc.yml',],loaders:{'.yaml':loadYaml,'.yml':loadYaml,},default:{testItem2:'some value'}});

Utils

merge

exportdeclareconstmerge:{<TObject,TSource>(object:TObject,source:TSource):TObject&TSource;<TObject,TSource1,TSource2>(object:TObject,source1:TSource1,source2:TSource2):TObject&TSource1&TSource2;<TObject,TSource1,TSource2,TSource3>(object:TObject,source1:TSource1,source2:TSource2,source3:TSource3):TObject&TSource1&TSource2&TSource3;<TObject,TSource1,TSource2,TSource3,TSource4>(object:TObject,source1:TSource1,source2:TSource2,source3:TSource3,source4:TSource4):TObject&TSource1&TSource2&TSource3&TSource4;(object:any, ...otherArgs:any[]):any;};

findConfigFile

exportdeclarefunctionfindConfigFile(moduleName:string,root:string,searchPlaces?:string[]):string;

getConfigPath

exportdeclareconstgetConfigPath:()=>string;

Example:

import{autoConf,getConfigPath}from'auto-config-loader';constdata=autoConf<Config>('idoc');constconfigPath=getConfigPath();// => /.autoconfrc.js

V1 To V2 Migration

This guide provides the steps to migrate to the latest version of the configuration loader API.

Key Changes

  1. Loader Functions Support Async

    • LoaderFunc<T> now supports returningT orPromise<T>.
    • Update custom loaders to handle asynchronous operations if needed.

    Example:

    exporttypeLoaderFunc<T>=(filepath:string,content:string,jsOption?:LoadConfOption)=>T|Promise<T>;
  2. autoConf Returns a Promise

    • TheautoConf function now returns aPromise instead of a synchronous result.
    • Update your code to handle asynchronous calls.

    Example:

    exportdeclarefunctionautoConf<T>(namespace?:string,option?:AutoConfOption<T>):Promise<{}&T>;

Migration Steps

1. Update Custom Loader Functions

If you have custom loaders, update their return types to support asynchronous operations:

Example:

constjsonLoader:LoaderFunc<MyConfig>=async(filepath,content)=>JSON.parse(content);

2. Handle AsynchronousautoConf Calls

Update all calls toautoConf to useawait or.then to handle Promises:

Example Usingawait:

constconfig=awaitautoConf('myNamespace',options);console.log(config);

Example Using.then:

autoConf('myNamespace',options).then(config=>{console.log(config);});

Related

  • cosmiconfig Find and load configuration from a package.json property, rc file, or CommonJS module
  • cjson Comments enabled json loader (Commented JavaScript Object Notation)
  • Config Loader Load user config files for node js projects.
  • Lilconfig Zero-dependency nodejs config seeker.
  • proload Searches for and loads your tool's JavaScript configuration files with full support for CJS, ESM, TypeScript and more.
  • rc The non-configurable configuration loader for lazy people.
LibraryLast commitDownloadloadersconfig ext
auto-config-loaderGitHub last commitNPM Downloads.js,.ts,.cjs,.mjs,.json,.jsonc,json5,.ini,.toml,.yaml ++
cosmiconfigGitHub last commitNPM Downloads.json,.yaml,.yml,.js,.mjs,.cjs
rcGitHub last commitNPM Downloads.json,.yaml,.yml,.js,.mjs,.cjs
@proload/coreGitHub last commitNPM Downloads.js,.ts,.cjs,.mjs,.json,.jsonc,json5,.ini,.toml,.yaml ++
lilconfigGitHub last commitNPM Downloads.js,.cjs,.mjs,.json ++
cjsonGitHub last commitNPM Downloads.json
@web/config-loaderGitHub last commitNPM Downloads.js,.mjs,.cjs

Contributors

As always, thanks to our amazing contributors!

Made withcontributors.

License

This package is licensed under the MIT License.

About

Find and load configuration from a package.json property, rc file, or CommonJS module.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors3

  •  
  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp