API & options
Language: English | 中文
rolldown-require exposes three APIs: bundleRequire, bundleFile, and loadFromBundledFile. Prefer the one-stop bundleRequire, which resolves the entry, bundles with rolldown, writes the temp output, loads it, and returns:
mod: the executed module (automatically unwrapsdefaultif present)dependencies: the file paths touched during bundling
Common options
filepath / cwd
filepathis required and accepts relative or absolute paths.cwddefaults toprocess.cwd()for resolving the relative entry andtsconfig.
format
- If omitted, format is inferred from extension and
package.json.type(.mjs/.mts/type:module->esm,.cjs/.cts->cjs). - Pass
cjs/esmto skip inference, e.g. force ESM for.js.
require
Customize how the temp output is loaded: (outfile, { format }) => any.
- ESM:
import(outfile)(temp file or data URL is written during bundling) - CJS: compiles the source via a temporary
_require.extensionshook
Use this to plug in your own loader, add custom import logic for ESM output, or inject mocks in tests.
rolldownOptions
Pass through parts of rolldown options:
input: add plugins,resolverules, transforms, etc. Internallyplatform: 'node'andtreeshake: falseare fixed, anddefineinjects__dirname/__filename/import.meta.url.output: merged with defaults;formatis overridden by theformatoption,inlineDynamicImportsis alwaystrue.
Avoid overriding
platform,input, orinlineDynamicImports, otherwise resolution/dependency collection may break.
external
Forwarded to rolldown. The plugin already externalizes most node_modules deps while keeping JSON inlined; use this option to exclude or force-inline specific deps.
tsconfig
- Auto-searches upward for
tsconfig.jsonand readspathsfor alias resolution during bundling. - Pass a string to set the path explicitly; pass
falseto disable tsconfig handling.
getOutputFile
Customize where the temp output is written (defaults to node_modules/.rolldown-require or the system temp dir with a random suffix). Handy for writing to a debuggable location.
preserveTemporaryFile
Temp files are cleaned after CJS load or ESM import by default. Set to true (or BUNDLE_REQUIRE_PRESERVE) to keep them for inspection.
cache
Disabled when false/unset. Pass true or an object to enable persistent + memory cache; see Loading flow & cache for details.
Example configuration
import { bundleRequire } from 'rolldown-require'
const { mod } = await bundleRequire({
cwd: process.cwd(),
filepath: './tooling/config.ts',
format: 'esm',
tsconfig: './tsconfig.node.json',
external: ['fsevents'],
cache: {
enabled: true,
dir: './node_modules/.rolldown-require-cache',
onEvent: e => console.log('[rolldown-require cache]', e),
},
rolldownOptions: {
input: {
plugins: [
myCustomPlugin(),
],
},
},
})This setup will:
- Bundle
tooling/config.tsas ESM using the specifiedtsconfig. - Mark
fseventsas external while keeping the default externalization behaviour for others. - Cache the temp output in the given directory and emit cache events via
onEvent.