Load and resolve oxfmt configuration files and merge supported
.editorconfigsettings for oxfmt.
- 🔍 Auto-discovery - Automatically searches for config files in current and parent directories
- 📦 Multiple formats - Supports
.oxfmtrc.json,.oxfmtrc.jsonc, andoxfmt.config.ts - 🧩 EditorConfig fallback - Merges supported
.editorconfigfields into the returned oxfmt config result - ⚡ Built-in caching - Caches both file resolution and parsed configs for optimal performance
- 🎯 TypeScript support - Fully typed with comprehensive type definitions
- 🛠️ Flexible API - Support explicit config paths or automatic discovery
npm install load-oxfmt-configyarn add load-oxfmt-configpnpm add load-oxfmt-configLoad config from current directory or parent directories:
import { loadOxfmtConfig } from 'load-oxfmt-config'
// Automatically searches for oxfmt config files and the nearest .editorconfig
const config = await loadOxfmtConfig()
console.log(config) // { printWidth: 80, ... }import { loadOxfmtConfig } from 'load-oxfmt-config'
const config = await loadOxfmtConfig({ cwd: '/path/to/project' })
// Returns one merged static config object
console.log(config)
// {
// tabWidth: 2,
// printWidth: 100,
// overrides: [
// { files: ['src/**/*.ts'], options: { printWidth: 120 } }
// ]
// }import { loadOxfmtConfig } from 'load-oxfmtConfig'
const config = await loadOxfmtConfig({
cwd: '/path/to/project',
})import { loadOxfmtConfig } from 'load-oxfmt-config'
// Relative path (resolved relative to cwd)
const config = await loadOxfmtConfig({
configPath: 'configs/.oxfmtrc.json',
cwd: '/path/to/project',
})
// Absolute path
const config = await loadOxfmtConfig({
configPath: '/absolute/path/to/.oxfmtrc.json',
})import { loadOxfmtConfig } from 'load-oxfmt-config'
// Skip .editorconfig reading entirely
const config = await loadOxfmtConfig({
editorconfig: false,
})import { loadOxfmtConfig } from 'load-oxfmt-config'
// Only look in the cwd directory itself, no upward traversal
const config = await loadOxfmtConfig({
editorconfig: { onlyCwd: true },
})import { loadOxfmtConfig } from 'load-oxfmt-config'
// Search .editorconfig from a custom directory instead of the config file's directory
const config = await loadOxfmtConfig({
editorconfig: {
cwd: '/path/to/editorconfig-dir',
},
})import { loadOxfmtConfig } from 'load-oxfmt-config'
// Force reload from disk, bypassing cache
const config = await loadOxfmtConfig({
useCache: false,
})import { resolveOxfmtrcPath } from 'load-oxfmt-config'
// Only resolve the config file path without loading it
const configPath = await resolveOxfmtrcPath(process.cwd())
console.log(configPath) // '/path/to/.oxfmtrc.json' or undefinedLoad and parse oxfmt configuration files, then merge supported .editorconfig fields into the returned result.
Parameters:
options- Optional configuration object
Returns: Promise<FormatOptions> - Parsed and merged oxfmt configuration object. Returns empty object {} if no supported config file is found.
Throws: Error if config file exists but cannot be parsed.
Resolve the absolute path to oxfmt config file.
Parameters:
cwd- Starting directory for resolutionconfigPath- Optional explicit path (absolute or relative to cwd)
Returns: Promise<string | undefined> - Absolute path to config file, or undefined if not found.
All options are optional.
- Type:
string - Default:
process.cwd()
Current working directory to start searching for config files. The loader will walk up from this directory to find a config file.
- Type:
string - Default:
undefined
Explicit path to the config file:
- Relative path: Resolved relative to
cwd - Absolute path: Used as-is
- When provided: Skips auto-discovery and uses this path directly
- Type:
boolean - Default:
true
Enable in-memory caching for both path resolution and parsed config contents. When enabled:
- Config file paths are cached to avoid repeated filesystem lookups
- Parsed config objects are cached to avoid re-parsing
- Subsequent calls with the same parameters return cached results instantly
Set to false to force reload from disk on every call.
- Type:
boolean | EditorconfigOption - Default:
true
Control how .editorconfig files are read and merged:
true— Read and merge the nearest.editorconfig, walking up from the config file's directory (orcwdwhen no config path is given).false— Disable.editorconfigreading entirely.EditorconfigOption— Enable with additional settings:
| Property | Type | Default | Description |
|---|---|---|---|
onlyCwd |
boolean |
false |
When true, only look for .editorconfig in cwd itself — no upward traversal. |
cwd |
string |
undefined |
Override the directory from which .editorconfig resolution starts, instead of the config file's directory or cwd. |
When configPath is not provided, the loader automatically searches for config files:
- Search order: Starts from
cwdand walks up to parent directories - Supported filenames:
.oxfmtrc.json.oxfmtrc.jsonc
oxfmt.config.ts
- Stops when:
- A valid config file is found
- Reaches the filesystem root
- EditorConfig: The nearest
.editorconfigis also resolved and merged into the returned result - Returns: Empty object
{}if no config file is found
{
"printWidth": 100,
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": true
}JSON with comments support:
export default {
printWidth: 100,
tabWidth: 2,
}The loader reads the nearest .editorconfig file and maps the subset of fields that oxfmt supports:
end_of_line→endOfLineindent_style→useTabsindent_size/tab_width→tabWidthmax_line_length→printWidthinsert_final_newline→insertFinalNewlinequote_type→singleQuote
Glob sections such as [*.ts] are converted into returned overrides entries.
The merged result follows this order:
- Root-level values from the nearest
.editorconfig - Root-level values from
.oxfmtrc.json,.oxfmtrc.jsonc, oroxfmt.config.ts - Overrides generated from
.editorconfigsections - Overrides declared directly in the oxfmt config file
This means explicit oxfmt config values always win over .editorconfig fallback values.
loadOxfmtConfig() returns a static OxfmtOptions object. That means .editorconfig support is represented as a merged config shape, not as per-file runtime evaluation. In practice this works well for common root settings and section-based overrides, but it is not a full replacement for oxfmt's own file-by-file config resolution.
import { loadOxfmtConfig } from 'load-oxfmt-config'
try {
const config = await loadOxfmtConfig()
} catch (error) {
// Thrown when config file exists but contains invalid JSON
console.error('Failed to parse oxfmt config:', error.message)
}The caching system maintains two separate caches:
- Path Resolution Cache: Stores resolved config file paths
- Config Content Cache: Stores parsed configuration objects
Cache keys are based on:
cwd+configPathfor path resolution- Resolved oxfmt path and resolved
.editorconfigpath for config content
Cache invalidation:
- Failed operations automatically clear their cache entries
- Use
useCache: falseto bypass cache for specific calls - Cache persists for the lifetime of the Node.js process
{ // Formatting options "printWidth": 100, "tabWidth": 2, /* Code style preferences */ "useTabs": false, "semi": true, "singleQuote": true, }