API Reference
Complete API reference for @vitus-labs/tools-core.
Requires Node.js >= 22. The package is ESM-only; PKG, VL_CONFIG and TS_CONFIG are computed with top-level await when the module is first imported, relative to process.cwd().
defineConfig()
defineConfig<T extends Record<string, any>>(config: T): TIdentity function providing type inference for vl-tools.config.mjs files. Returns the config object unchanged.
// vl-tools.config.mjs
import { defineConfig } from '@vitus-labs/tools-core'
export default defineConfig({
build: { outputDir: 'lib' },
next: { headers: true },
})VL_CONFIG()
type ConfigAccessor = {
readonly config: Record<string, any>
get: (param: string, defaultValue?: any) => any
merge: (defaults: Record<string, any>) => ConfigAccessor
}
VL_CONFIG(key: string): ConfigAccessorReads the value at key (dot-paths allowed) from the merged cascade of vl-tools.config.mjs files and returns an accessor:
config— the config object for that key ({}when missing)get(param, defaultValue?)— dot-path read inside the section. Falsy defaults (false,0,'') are kept; when no default is given a missing value returns{}merge(defaults)— deep-merges the section on top ofdefaults(user values win) and returns a new accessor
const vlConfig = VL_CONFIG('build')
const CONFIG = vlConfig.merge(baseConfig).config
const sourceDir = vlConfig.get('sourceDir', 'src')Resolution order:
- Find all
vl-tools.config.mjsfromprocess.cwd()up to the filesystem root - Load each via dynamic
import()(default export, or the module namespace) - Deep-merge in order (root first, closest last)
- Extract
result[key]
A config file that fails to import throws [tools-core] Failed to load config: <path> with the original error message.
PKG
const PKG: {
// ...every field of the nearest package.json, plus:
bundleName: string
externalDependencies: RegExp[]
}Pre-parsed package.json, found by walking up from process.cwd() ({} plus the computed fields if none is found).
Computed Properties
| Property | Derivation |
|---|---|
bundleName | Package name camel-cased into a valid JS identifier: every run of non-identifier characters is a separator (@my-org/my-lib → myOrgMyLib), a leading digit gets a _ prefix, missing name → 'bundle' |
externalDependencies | Keys of dependencies, peerDependencies and optionalDependencies, each mapped through expandExternal() |
TS_CONFIG
const TS_CONFIG: Record<string, any>Pre-parsed tsconfig.json (nearest one walking up from process.cwd()). Loaded via loadFileToJSON('tsconfig.json'); {} when not found. Note that extends is not resolved.
findFile()
findFile(filename: string): string | undefinedSearches for filename starting from process.cwd(), walking upward through parent directories. Returns the absolute path if found, undefined otherwise.
findFile('tsconfig.json') // '/Users/me/project/tsconfig.json'
findFile('.eslintrc') // undefined (not found)loadFileToJSON()
loadFileToJSON(filename: string): Record<string, any>Finds filename with findFile() and parses it as JSON. Returns {} if the file doesn't exist or contains invalid JSON — it never throws.
loadConfigParam()
loadConfigParam(filename: string): (key: string, defaultValue?: any) => anyCurried reader. The returned function looks up a dot-path key in the parsed JSON file and returns defaultValue (default {}) when it is missing. The parsed file is cached per process.cwd() + filename.
const readTsConfig = loadConfigParam('tsconfig.json')
readTsConfig('compilerOptions.outDir', 'lib')loadVLToolsConfig()
loadVLToolsConfig(): Promise<(key: string) => ConfigAccessor>Discovers and merges all vl-tools.config.mjs files from process.cwd() upward and resolves to an accessor factory — the same shape as VL_CONFIG (which is simply await loadVLToolsConfig() evaluated at import time).
const getConfig = await loadVLToolsConfig()
getConfig('favicon').configswapGlobals()
swapGlobals(globals: Record<string, string>): Record<string, string>Inverts keys and values in a mapping. Used to convert { packageName: globalVar } to { globalVar: packageName } for UMD bundle externals.
swapGlobals({ react: 'React' })
// { React: 'react' }expandExternal()
expandExternal(id: string | RegExp): string | RegExpConverts a bare package name into a RegExp matching the package itself and any deep import (^name(\/|$)), with regex special characters escaped. RegExp inputs are returned unchanged, so you can supply your own patterns.
expandExternal('echarts').test('echarts/core') // true
expandExternal('react').test('react-dom') // falsefilesize()
type FilesizeOptions = {
name?: (text: string) => string // styles the file name (default: unstyled)
value?: (text: string) => string // styles the size column (default: unstyled)
log?: (message: string) => void // sink per line (default: console.log)
}
filesize(options?: FilesizeOptions): { name: 'vl-filesize', generateBundle(...) }Rollup/Rolldown-compatible plugin that prints one line per emitted chunk with its raw size and gzip size (base-1000 units: B, kB, MB, GB). Assets such as sourcemaps are skipped. Sizes are measured on the bytes actually emitted — there is no separate minified column. Replaces rollup-plugin-filesize.
import { filesize } from '@vitus-labs/tools-core'
import chalk from 'chalk'
filesize({ name: chalk.bold, value: chalk.dim })
// " index.js 12.34 kB · 4.21 kB gzip"Last updated on