Tools Core
Shared configuration loader, file discovery, and package metadata for the vitus-labs toolchain.
@vitus-labs/tools-core is the foundation of every @vitus-labs/tools-* package. It provides cascading configuration loading from vl-tools.config.mjs files, file discovery utilities, and pre-resolved package metadata constants.
Installation
npm install @vitus-labs/tools-coreRequirements
- Node.js
>= 22(the package is ESM-only and uses top-levelawait)
How It Works
Every tool in the ecosystem (tools-rolldown, tools-storybook, tools-nextjs, etc.) calls VL_CONFIG(key) to load its configuration. The system:
- Searches upward from
process.cwd()forvl-tools.config.mjsfiles - Deep-merges all found configs (root-level first, closest wins)
- Returns a config object scoped to the requested key
- Each tool merges its own base defaults underneath the result (your values win)
monorepo/
├── vl-tools.config.mjs ← root config (loaded first)
├── packages/
│ ├── my-lib/
│ │ ├── vl-tools.config.mjs ← package config (overrides root)
│ │ └── src/
│ └── my-app/
│ └── src/Configuration File
Create a vl-tools.config.mjs in your project or monorepo root:
// vl-tools.config.mjs
export default {
build: {
// tools-rolldown / tools-rollup config
outputDir: 'lib',
sourceDir: 'src',
replaceGlobals: true,
},
next: {
// tools-nextjs config
headers: true,
transpilePackages: ['@my-org/ui'],
},
stories: {
// tools-storybook config
port: 6006,
},
atlas: {
// tools-atlas config
workspaces: ['packages/*'],
},
favicon: {
// tools-favicon config
icons: [{ input: 'logo.png', output: 'public/favicons', path: '/favicons' }],
},
}Each key corresponds to a tool's CONFIG_KEY:
| Key | Tool Package |
|---|---|
build | tools-rolldown, tools-rollup (also read by tools-mcp) |
next | tools-nextjs |
stories | tools-storybook |
favicon | tools-favicon |
atlas | tools-atlas |
Pre-Resolved Constants
These constants are computed once at import time and reused by all tools:
PKG
Parsed package.json from process.cwd() with computed properties:
import { PKG } from '@vitus-labs/tools-core'
PKG.name // '@vitus-labs/core'
PKG.version // '2.0.0-alpha.25'
PKG.type // 'module' | 'commonjs'
PKG.main // 'lib/index.cjs'
PKG.module // 'lib/index.js'
PKG.exports // { import, require, types }
PKG.browser // browser field (string | object)
PKG.bundleName // UMD global name, e.g. '@my-org/my-lib' → 'myOrgMyLib'
PKG.externalDependencies // RegExp[] built from dependency namesexternalDependencies covers every key of dependencies, peerDependencies and optionalDependencies. Each name is converted with expandExternal() into a RegExp that matches the package and its deep imports (echarts also matches echarts/core) — used by bundlers to mark packages as external.
bundleName is always a valid JS identifier: any run of non-identifier characters (@, /, -, .) acts as a separator, a leading digit is prefixed with _, and a missing name falls back to 'bundle'.
TS_CONFIG
Parsed tsconfig.json from process.cwd():
import { TS_CONFIG } from '@vitus-labs/tools-core'
TS_CONFIG.compilerOptions.target // 'ES2024'
TS_CONFIG.compilerOptions.module // 'Preserve'
TS_CONFIG.include // ['src']
TS_CONFIG.exclude // ['node_modules', ...]VL_CONFIG
Factory function that loads and merges cascading vl-tools.config.mjs files:
import { VL_CONFIG } from '@vitus-labs/tools-core'
const buildConfig = VL_CONFIG('build')
// Returns: { config, get, merge } for the 'build' key
buildConfig.get('sourceDir', 'src') // read a dot-path with a default
const merged = buildConfig.merge(baseDefaults)
// Returns: { config, get, merge } — user values override baseDefaultsUtility Functions
defineConfig()
Type-safe helper for authoring vl-tools.config.mjs:
import { defineConfig } from '@vitus-labs/tools-core'
export default defineConfig({
build: { outputDir: 'dist' },
next: { headers: true },
})findFile()
Searches upward from process.cwd() for a file by name:
import { findFile } from '@vitus-labs/tools-core'
const tsconfigPath = findFile('tsconfig.json')
// '/Users/me/project/tsconfig.json'loadFileToJSON()
Finds a JSON file by walking up from process.cwd() and parses it. Returns {} when the file is missing or invalid:
import { loadFileToJSON } from '@vitus-labs/tools-core'
const pkg = loadFileToJSON('package.json')loadConfigParam()
Curried reader for dot-path keys in a JSON config file. The parsed file is cached per working directory:
import { loadConfigParam } from '@vitus-labs/tools-core'
const readTsConfig = loadConfigParam('tsconfig.json')
const target = readTsConfig('compilerOptions.target', 'ES2022')loadVLToolsConfig()
Loads and merges all vl-tools.config.mjs files from process.cwd() upward and resolves to a fresh accessor with the same shape as VL_CONFIG (useful when the cwd changed after import):
import { loadVLToolsConfig } from '@vitus-labs/tools-core'
const getConfig = await loadVLToolsConfig()
const build = getConfig('build').configswapGlobals()
Inverts a key-value mapping (used for UMD globals):
import { swapGlobals } from '@vitus-labs/tools-core'
swapGlobals({ react: 'React', 'react-dom': 'ReactDOM' })
// { React: 'react', ReactDOM: 'react-dom' }expandExternal()
Turns a bare package name into a RegExp that matches the package and its subpaths. RegExp inputs pass through unchanged:
import { expandExternal } from '@vitus-labs/tools-core'
expandExternal('echarts') // /^echarts(\/|$)/ — matches 'echarts' and 'echarts/core'
expandExternal(/^lodash/) // returned as-isfilesize()
Dependency-free Rollup/Rolldown plugin that logs the raw and gzip size of every emitted chunk (used by tools-rolldown and tools-rollup when filesize: true). Sizes are measured on the bytes actually emitted:
import { filesize } from '@vitus-labs/tools-core'
plugins: [filesize({ name: chalk.bold, value: chalk.dim })]
// logs: index.js 12.34 kB · 4.21 kB gzipCascading Config Resolution
The config system walks from process.cwd() toward the filesystem root:
/Users/me/monorepo/packages/my-lib/ ← cwd
/Users/me/monorepo/packages/ ← checked
/Users/me/monorepo/ ← checked (root config found)
/Users/me/ ← keeps walking up to the filesystem rootAll found vl-tools.config.mjs files are loaded and deep-merged. Root-level values are applied first, then closer configs override (an explicit undefined never overrides an inherited value):
// monorepo/vl-tools.config.mjs (root)
export default {
build: { outputDir: 'lib', replaceGlobals: true },
}
// monorepo/packages/special/vl-tools.config.mjs (package)
export default {
build: { outputDir: 'dist' }, // overrides root
// replaceGlobals: true still inherited from root
}Exports
import {
defineConfig,
expandExternal,
filesize,
findFile,
loadFileToJSON,
loadConfigParam,
loadVLToolsConfig,
swapGlobals,
PKG,
VL_CONFIG,
TS_CONFIG,
} from '@vitus-labs/tools-core'
import type { FilesizeOptions } from '@vitus-labs/tools-core'PKG, VL_CONFIG and TS_CONFIG are computed at module load time using top-level await. Importing this package has side effects: it reads package.json, tsconfig.json and every vl-tools.config.mjs relative to process.cwd().
Last updated on