Variable Loader

Loader: { dependencyKeysIfEvaluated: ((specifier: string) => string[]); registry: Map<string, { dependencies: string[]; module: any; state: number }>; resolve: ((specifier: string) => Promise<string>); resolveSync: ((specifier: string, from: string) => string) }

Low-level JavaScriptCore API for accessing the native ES Module loader (not a Bun API)

Before using this, be aware of a few things:

Using this incorrectly will crash your application.

This API may change any time JavaScriptCore is updated.

Bun may rewrite ESM import specifiers to point to bundled code. This will be confusing when using this API, as it will return a string like "/node_modules.server.bun".

Bun may inject additional imports into your code. This usually has a bun: prefix.

Type declaration

  • dependencyKeysIfEvaluated: ((specifier: string) => string[])
      • (specifier: string): string[]
      • For an already-evaluated module, return the dependencies as module specifiers

        This list is already sorted and uniqued.

        Example

        For this code:

        // /foo.js
        import classNames from 'classnames';
        import React from 'react';
        import {createElement} from 'react';

        This would return:

        Loader.dependencyKeysIfEvaluated("/foo.js")
        ["bun:wrap", "/path/to/node_modules/classnames/index.js", "/path/to/node_modules/react/index.js"]

        Parameters

        • specifier: string

          module specifier as it appears in transpiled source code

        Returns string[]

  • registry: Map<string, { dependencies: string[]; module: any; state: number }>

    ESM module registry

    This lets you implement live reload in Bun. If you delete a module specifier from this map, the next time it's imported, it will be re-transpiled and loaded again.

    The keys are the module specifiers and the values are metadata about the module.

    The keys are an implementation detail for Bun that will change between versions.

    • Userland modules are an absolute file path
    • Virtual modules have a bun: prefix or node: prefix
    • JS polyfills start with "/bun-vfs/". "buffer" is an example of a JS polyfill
    • If you have a node_modules.bun file, many modules will point to that file

    Virtual modules and JS polyfills are embedded in bun's binary. They don't point to anywhere in your local filesystem.

  • resolve: ((specifier: string) => Promise<string>)
      • (specifier: string): Promise<string>
      • The function JavaScriptCore internally calls when you use an import statement.

        This may return a path to node_modules.server.bun, which will be confusing.

        Consider Bun.resolve or ImportMeta.resolve instead.

        Parameters

        • specifier: string

          module specifier as it appears in transpiled source code

        Returns Promise<string>

  • resolveSync: ((specifier: string, from: string) => string)
      • (specifier: string, from: string): string
      • Synchronously resolve a module specifier

        This may return a path to node_modules.server.bun, which will be confusing.

        Consider Bun.resolveSync instead.

        Parameters

        • specifier: string
        • from: string

        Returns string

Generated using TypeDoc