Skip to Content
vitus·labs
Tools Rolldown

API Reference

Complete API reference for @vitus-labs/tools-rolldown.

CLI

CommandDescription
vl_rolldown_buildProduction build — runs runBuild()
vl_rolldown_build-watchWatch mode — rebuilds the JS bundles on change (no declarations, no copyFiles)

runBuild()

runBuild(): Promise<void>

Orchestrates the complete build pipeline. Reads configuration, generates build variants, and executes them sequentially.

Steps:

  1. Clean outputDir
  2. Create build pipeline from package.json fields (or entries)
  3. Build JS bundles with Rolldown — variants sharing format/env/platform/extension are built together with shared _chunks/
  4. Copy copyFiles
  5. Generate declarations with rolldown-plugin-dts

File sizes and visualization reports are produced by plugins during step 3.

import { runBuild } from '@vitus-labs/tools-rolldown'

await runBuild()

Configuration Shape

interface BuildConfig {
  sourceDir: string              // Source directory (default: 'src')
  outputDir: string              // Directory cleaned before the build (default: 'lib')
  typescript: boolean            // Generate .d.ts files (default: true)
  replaceGlobals: boolean        // Inject platform globals (default: true)
  filesize: boolean              // Show file size stats (default: true)
  sourcemap: boolean | 'inline' | 'hidden'  // JS sourcemaps (default: true)
  visualise: {
    template: string             // rollup-plugin-visualizer template (default: 'network')
    gzipSize: boolean            // Show gzip sizes
    outputDir: string            // Report dir next to each output (default: 'analysis')
  } | false
  extensions: string[]           // File extensions to resolve
  external: (string | RegExp)[]  // Additional externals (replaces the default list)
  globals: Record<string, string>  // UMD/IIFE globals as { GlobalName: 'module-id' }

  // Advanced build options
  entries?: Array<{              // Explicit entries (bypasses package.json)
    input: string
    file: string
    format?: 'es' | 'cjs' | 'umd' | 'iife'
    env?: 'development' | 'production'
    platform?: 'universal' | 'browser' | 'node' | 'native'
  }>
  bundleAll?: boolean            // Bundle all deps, no externals (default: false)
  copyFiles?: Array<{            // Copy static files post-build (recursive)
    from: string
    to: string
  }>
  banner?: string                // Inject at top of output
  footer?: string                // Inject at bottom of output
  alias?: Record<string, string> // Resolve aliases
  plugins?: RolldownPlugin[]     // Custom plugins (default: [])
}

Default Configuration

{
  sourceDir: 'src',
  outputDir: 'lib',
  typescript: true,
  replaceGlobals: true,
  visualise: {
    template: 'network',
    gzipSize: true,
    outputDir: 'analysis',
  },
  filesize: true,
  sourcemap: true,
  extensions: ['.json', '.js', '.jsx', '.ts', '.tsx', '.es6', '.es', '.mjs'],
  external: ['react/jsx-runtime'],
  globals: {
    react: 'React',
    ReactDOM: 'react-dom',
    styled: 'styled-components',
  },
  entries: undefined,
  bundleAll: false,
  copyFiles: undefined,
  banner: undefined,
  footer: undefined,
  alias: undefined,
  plugins: [],
}

Your vl-tools.config.mjs build object is deep-merged over these defaults: nested objects (visualise, globals) merge key by key, arrays (external, extensions, plugins) replace the default.

globals is inverted before it is passed to Rolldown's output.globals, so each entry is written as GlobalName: 'module-id'.

Build Pipeline Generation

The pipeline is generated from package.json fields (unless entries is set):

exports Field Processing

Read only when "type": "module". Supports a string, a conditions object, or a subpath map (".", "./sub").

Condition KeyOutput FormatPlatform
import'es''universal'
require'cjs''universal'
node'es''node'
default'es' (only without import)'universal'

Nested conditions are flattened (default → import → require). Wildcard subpaths are skipped with a warning.

Other Fields

FieldOutput FormatEnvPlatform
main'es' with type: "module", otherwise 'cjs'development'universal'
module'es'development'universal'
react-native'es' (only if different from module)development'native'
umd:main'umd'development—
unpkg'umd'production—
browser (object map)same as the mapped field—'browser'

Variants that resolve to the same file, format, platform and env are de-duplicated.

Build Variant Shape

interface BuildVariant {
  file: string           // Output file path
  format: 'es' | 'cjs' | 'umd' | 'iife'
  env: 'development' | 'production'
  platform: 'universal' | 'browser' | 'node' | 'web' | 'native'
  input?: string         // Per-entry input (otherwise sourceDir)
}

Plugin Pipeline

Rolldown handles TypeScript, module resolution and global replacement natively (resolve.extensions, transform.define). Plugins added per build:

PluginConditionPurpose
rollup-plugin-visualizervisualise not falseBundle composition HTML
filesize (from @vitus-labs/tools-core)filesize: trueConsole file size reporting (gzip of emitted bytes)
your pluginsalwaysAppended after the built-ins

Declarations are built in separate Rolldown calls with rolldown-plugin-dts (emitDtsOnly, no sourcemaps).

Platform-Specific Extensions

For platform "browser", the resolver checks (in order):

.browser.json, .browser.js, .browser.jsx, .browser.ts, .browser.tsx,
.browser.es6, .browser.es, .browser.mjs,
.json, .js, .jsx, .ts, .tsx, .es6, .es, .mjs

Same pattern applies to node, web, and native platforms. universal uses only the base extensions.

Output Configuration

Common to every build:

{
  format,                          // 'es' | 'cjs' | 'umd' | 'iife'
  globals: swapGlobals(CONFIG.globals),
  sourcemap: CONFIG.sourcemap,     // node_modules sources are ignore-listed
  exports: 'named',                // cjs, umd, iife only
  name: PKG.bundleName,            // umd, iife only — camelCase package name
  esModule: true,
  minify: env === 'production',
  banner: CONFIG.banner,
  footer: CONFIG.footer,
}

Input side: platform maps to Rolldown's 'node', 'browser' or 'neutral'; tsconfig: 'tsconfig.json' when typescript is on; treeshake.moduleSideEffects: false.

Global Type Definitions

{
  "compilerOptions": {
    "types": ["@vitus-labs/tools-rolldown/global"]
  }
}

Declares __BROWSER__, __WEB__, __NATIVE__, __NODE__ and __CLIENT__ as globals (and on Window).

Exports

EntryExports
@vitus-labs/tools-rolldownrunBuild
@vitus-labs/tools-rolldown/globalType declarations only
import { runBuild } from '@vitus-labs/tools-rolldown'
Edit on GitHub

Last updated on

On this page