Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow third-party plugins to access css and asset dependency info #6627

Closed
4 tasks done
ElMassimo opened this issue Jan 25, 2022 · 3 comments
Closed
4 tasks done

Allow third-party plugins to access css and asset dependency info #6627

ElMassimo opened this issue Jan 25, 2022 · 3 comments

Comments

@ElMassimo
Copy link
Contributor

ElMassimo commented Jan 25, 2022

Clear and concise description of the problem

The vite:manifest, vite:build-html, and vite:build-import-analysis plugins are accessing internal state from other plugins such as:

  • chunkToEmittedCssFileMap: managed by the vite:css-post plugin, maps output chunks to emitted css files
  • chunkToEmittedAssetsMap: managed by the vite:asset plugin, maps output chunks to emitted asset files

Also, vite:css-post is using registerAssetToChunk internally shared by vite:asset.

This state is necessary to infer the non-JS dependencies of individual chunks, and would be useful for third-party plugins such as those in Astro, îles, Vite Ruby, Laravel Vite, etc.

Suggested solution

Opening this issue to keep the conversation moving.

It would be nice to encapsulate the internals to simplify maintenance and future refactoring, but any change that exposes the data mentioned above would enable third-party plugins to achieve their goals.

Possible Implementations

Covering a few options we've talked about, some of these are not desirable:

Extend OutputChunk data

To include css and assets dependencies.

This way, we are making this data an explicit part of the dependency graph.

See implementation in #6629.

Extend the PluginContext API

Add additional methods to the PluginContext API that allow accessing the data (akin to getFileName):

this.cssFilesForChunk(chunk)
this.assetFilesForChunk(chunk)

ResolvedConfig functions

Have each plugin inject methods in ResolvedConfig that can provide the necessary functionality without exposing the internal data structures:

resolvedConfig.cssFilesForChunk(chunk)
Downsides

Too much responsibility for ResolvedConfig, poor maintainability.

Per-Plugin API

Expose an api in the vite:css-post and vite:asset plugins with respective functions to map chunks to files, and to register new dependencies.

Downsides

Requires third-party plugins to obtain the ResolvedConfig, filter plugins by name, and obtain the relevant one.

The name of the plugin becomes an external-facing API, which is fragile.

Single API Plugin

Similar to the previous, but instead centralize all internal APIs in a single plugin:

{
  name: 'vite:internal-api',
  get api () { return internalApi },
}
Downsides

Each internal plugin must extend this API, the exposed methods would depend on which plugins are active for the current mode (development vs build) or configuration.

Additional context

Validations

@ElMassimo ElMassimo changed the title Create an internal API to obtain emitted assets Create an internal API to access css and asset dependency info Jan 25, 2022
@ElMassimo ElMassimo changed the title Create an internal API to access css and asset dependency info Allow third-party plugins to access css and asset dependency info Jan 25, 2022
@aleclarson
Copy link
Member

See #6629

@bluwy
Copy link
Member

bluwy commented Apr 4, 2022

@ElMassimo Is this fixed with #6629 merged?

@ElMassimo
Copy link
Contributor Author

Hi @bluwy! Yes, thought this issue was linked to the PR, thanks for noticing!

Closed by #6629.

@github-actions github-actions bot locked and limited conversation to collaborators Apr 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants