Skip to content

Support plugin hook filters based on importer #7529

@pi0

Description

@pi0

Currently, it is possible to use resolveId: { filter: { id } } to only call plugin hooks for specific ids, but it is not possible to filter based on importer.

export function customResolve(): Plugin {
  const idInclude = /foo|bar/
  const importerInclude = /^virtual:my-module$/

  return {
    name: "custom-resolve",
    resolveId: {
      order: "pre",
      filter: {
        id: idInclude,
        importer: importerInclude, // <-- proposal: native filter
      },
      handler(id, importer) {
        // current solution:
        if (!importerInclude.test(importer || "")) {
          return null;
        }
        // ...
      },
    },
  };
}

Context

Nitro uses virtual modules with the #nitro/virtual/* prefix. These modules import runtime dependencies that are nested dependencies (for example, node_modules/nitro/node_modules/{h3, …}).

This requires a custom plugin with resolveId logic to correctly resolve imports from virtual module IDs within the Nitro package path.

To reduce performance overhead, resolution should be limited to a known set of virtual IDs. However, the importer cannot be natively filtered to only the required patterns. As a result, the plugin is invoked for every importer, even when resolution is not needed, which forces additional JavaScript-side filtering and adds unnecessary overhead.

Metadata

Metadata

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions