MonLoad
Bun

method

PluginBuilder.onLoad

constraints: PluginConstraints,
callback: OnLoadCallback
): this;

Register a callback to load imports with a specific import specifier

@param constraints

The constraints to apply the plugin to

@param callback

The callback to handle the import

@returns

this for method chaining

Bun.plugin({
  setup(builder) {
    builder.onLoad({ filter: /^hello:world$/ }, (args) => {
      return { exports: { foo: "bar" }, loader: "object" };
    });
  },
});

Referenced types

interface PluginConstraints

  • filter: RegExp

    Only apply the plugin when the import specifier matches this regular expression

    // Only apply the plugin when the import specifier matches the regex
    Bun.plugin({
     setup(builder) {
        builder.onLoad({ filter: /node_modules/underscore/ }, (args) => {
         return { contents: "throw new Error('Please use lodash instead of underscore.')" };
        });
     }
    })
    
  • namespace?: string

    Only apply the plugin when the import specifier has a namespace matching this string

    Namespaces are prefixes in import specifiers. For example, "bun:ffi" has the namespace "bun".

    The default namespace is "file" and it can be omitted from import specifiers.

type OnLoadCallback = (args: OnLoadArgs) => OnLoadResult | Promise<OnLoadResult>