esbuild icon indicating copy to clipboard operation
esbuild copied to clipboard

Allow `build.resolve` call that bypasses all plugins

Open aleclarson opened this issue 2 years ago • 1 comments

Some kind of skipPlugins option that could be true or an array of plugin names.

Context

I'm adding a build.onResolved hook (note the past tense) to esbuild-extra. Its purpose is to be able to adjust onResolve results (i.e. edit the suffix based on import kind).

To achieve this, I have to mimic esbuild's fallback resolution logic, or else the onResolved hooks wouldn't be called when all onResolve hooks return null.

Currently, I do this with enhanced-resolve, but it would be nice to simply call build.resolve with skipPlugins: true instead.

aleclarson avatar Jun 28 '23 18:06 aleclarson

I think the standard way to do this today is to just put extra data on the otherOptions that allows it checks to prevent cycles:

export function wrapResolvePlugin() {
  return {
    name: 'wrap-resolve',
    setup(build) {
      build.onResolve({ filter: /./ }, async ({ path: importPath, ...otherOptions }) => {
        // These lines are to prevent infinite recursion when we call `build.resolve`.
        if (otherOptions.pluginData) {
          return;
        }
        const result = await build.resolve(importPath, {
          pluginData: true,
          ...otherOptions
        });
        return {
          // Futz with result
          ...result,
        };
      });
    },
  };
}

You could do anything with pluginData, even use it as the array you mention with names to skip. But this way you prevent the recursion.

I dunno if that helps you, and I still wouldn't mind a general "just do the raw resolve" option myself.

vpanta avatar Apr 22 '25 16:04 vpanta