onResolve

Bun

Symbol

PluginBuilder.onResolve

onResolve(constraints: PluginConstraints, callback: OnResolveCallback): this

Register a callback to resolve imports matching a filter and/or namespace

@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.onResolve({ filter: /^wat$/ }, (args) => {
      return { path: "/tmp/woah.js" };
    });
  },
});

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 OnResolveCallback = (args: OnResolveArgs) => OnResolveResult | Promise<OnResolveResult | undefined | null> | undefined | null