Skip to content

Support Rollup's this.load() method in the plugin context #6810

@dhaig

Description

@dhaig

Clear and concise description of the problem

When a Rollup plugin needs to inspect the contents of another module, the load() method in the plugin context resolves and transforms the dependency on demand. Vite's plugin container doesn't provide it, so calling this.load() from a plugin hook throws an error. It would be nice to have it available in Vite as I've run into situations where it's necessary and as I understand it there's no workaround for the same functionality.

Suggested solution

In the plugin context, Vite can implement a version of load() that is similar to Rollup's but with a few changes:

  • the final parsing step would be skipped as Vite only parses on demand
  • the returned module information might not always have the AST available
  • it wouldn't trigger moduleParsed

In Rollup, load() returns a promise that finishes after the module is resolved, loaded, and transformed but before its dependencies have been resolved. It supports a flag resolveDependencies that makes the promise finish afterward instead. I think this option would still make sense for Vite, but I'm not 100% clear on when Vite resolves a module's dependencies.

Alternative

No response

Additional context

  • Documentation for this.load() including a few example use cases
  • I checked WMR's plugin container to see if it supports load() and it doesn't – I unfortunately couldn't find any discussion about it, so I'm not sure why they decided against it.

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions