Loading flow & cache
Language: English | 中文
This page outlines how bundleRequire bundles, writes, and loads under the hood, plus practical cache settings.
End-to-end flow
- Resolve entry: turn
filepath+cwdinto an absolute path and inferformatfrom the extension/package.json.type(overrideable). - Bundle with rolldown:
- Fix
platform: 'node',inlineDynamicImports: true,treeshake: falseto emit a single entry output and collect dependencies. - Inject
__dirname/__filename/import.meta.urlso runtime code sees the real source path. - Externalize most
node_modulesvia the externalize-deps plugin, keep JSON inlined, and respectmodule-syncconditions.
- Fix
- Execute the temp output:
- ESM: write a temp
.mjs/.cjsor data URL, thenimport()it. - CJS: compile the source via a temporary
_require.extensionshook.
- ESM: write a temp
- Return results: provide
modplusdependencies(excluding the entry) for watching or debugging.
Externalization & resolution
- Resolution aligns with Vite/rolldown: same main-field priority and
tsconfigpath support (when enabled). - Node built-ins and
node:/npm:namespace imports are marked external. - Dependencies inside nested
node_modulesunder the entry directory are inlined to keep the temp output runnable. - JSON resources are always handled by rolldown (never externalized).
Temp output control
- Defaults to the nearest
node_modules/.rolldown-require; falls back to the system temp dir. Filenames include a random hash to avoid contention. - Customize the path via
getOutputFile; setpreserveTemporaryFile: trueto skip cleanup for direct inspection. - If all disk attempts fail for ESM, it falls back to a
data:URL.
Cache strategy
Caching is off by default. Set cache: true or pass an object to enable persistent + in-memory cache:
- Location: nearest
node_modules/.rolldown-require-cache, elseos.tmpdir()/rolldown-require-cache; override withcache.dir. - Cache key: entry path,
mtime/size,format,tsconfigpath, Node version, and a hash ofrolldownOptions. - Validation: after a hit, each entry/dependency
mtime/sizeis compared; any change invalidates the cache. - Memory cache: on by default (
cache.memory !== false); returns the loaded module directly to avoid extra fs I/O. - Reset & events:
cache.reset: trueremoves prior code/meta before writing.cache.onEventreceiveshit/miss/store/skip-invalid;skip-invalid.reasonmay bemissing-entry,format-mismatch,stale-deps, etc.
Example: log cache events and set a custom directory
ts
await bundleRequire({
filepath: './config/vite.config.ts',
cache: {
enabled: true,
dir: './.cache/rolldown-require',
onEvent: (e) => {
console.log(`[cache] ${e.type}`, e)
},
},
})Debugging tips
- Watch
dependenciesto decide when to callbundleRequireagain. - Pair with
preserveTemporaryFile: trueto inspect temp code, or compare*.code.mjs/cjsdirectly inside the cache dir. - To debug suspicious hits, temporarily set
cache.reset: trueor disable cache entirely. - When the entry extension and
package.json.typedisagree, passformatexplicitly to avoid Node/rolldown differences.