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

Expose replacing VITE_ASSET markers externally #6625

Closed
4 tasks done
matthewp opened this issue Jan 25, 2022 · 2 comments
Closed
4 tasks done

Expose replacing VITE_ASSET markers externally #6625

matthewp opened this issue Jan 25, 2022 · 2 comments

Comments

@matthewp
Copy link
Contributor

Clear and concise description of the problem

In Astro we have had to write our own CSS plugin, due to the fact that the internal vite:css-post plugin has private state that only the HTML plugin knows about.

As such, we have had to solve most of the same problems that vite:css-post solves. One problem we cannot solve, however, is replacing VITE_ASSET_hash markers that the Asset plugin adds.

Suggested solution

One idea is to expose a function from the Asset plugin that replaces all VITE_ASSET markers, like:

import { replaceAssetMarkers } from 'vite';

css = replaceAssetMarkers(config, css);

Or alternatively, expose getAssetFilename and assetUrlRE so that they can be used in the same way that the CSS plugin does:

css = css.replace(assetUrlRE, (_, fileHash, postfix = '') => {
const filename = getAssetFilename(fileHash, config) + postfix
registerAssetToChunk(chunk, filename)
if (!isRelativeBase || inlined) {
// absolute base or relative base but inlined (injected as style tag into
// index.html) use the base as-is
return config.base + filename
} else {
// relative base + extracted CSS - asset file will be in the same dir
return `./${path.posix.basename(filename)}`
}
})

Alternative

No response

Additional context

The coupling of HTML, CSS, and Asset plugins has made it difficult for us to use any of them, because they each rely on private state of the other. This means that if you try and replace one, it's difficult to keep the others.

Validations

@matthewp
Copy link
Contributor Author

This is no longer needed due to the new metadata added by the asset plugin.

@patak-dev
Copy link
Member

Related, there will be fine-grained control when replacing __VITE_ASSET__ after #8762

@github-actions github-actions bot locked and limited conversation to collaborators Jul 9, 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

2 participants