Mmodule
Bun

method

PluginBuilder.module

specifier: string,
callback: () => OnLoadResult | Promise<OnLoadResult>
): this;

Create a lazy-loaded virtual module that can be imported or required from other modules

@param specifier

The module specifier to register the callback for

@param callback

The function to run when the module is imported or required

@returns

this for method chaining

Bun.plugin({
  setup(builder) {
    builder.module("hello:world", () => {
      return { exports: { foo: "bar" }, loader: "object" };
    });
  },
});

// sometime later
const { foo } = await import("hello:world");
console.log(foo); // "bar"

// or
const { foo } = require("hello:world");
console.log(foo); // "bar"