We're using FontAwesome, which injects a custom CSS stylesheet on page load. To prevent paint flashes, we once stored the "rendered" CSS into a simple, custom .css file.
However, they also have a neat function to generate the CSS during build - which i'd love to use via a custom, virtual module. Like so:
import { dom } from '@fortawesome/fontawesome-svg-core';
const VIRTUAL_MODULE_ID = 'virtual:fontawesome-css/dom.css';
const RESOLVED_VIRTUAL_MODULE_ID = '\0' + VIRTUAL_MODULE_ID;
/** @returns {import('vite').Plugin} */
export function fontAwesomeCss() {
return {
name: 'vite-plugin-fontawesome-css',
sharedDuringBuild: true,
resolveId(id) {
if (id === VIRTUAL_MODULE_ID) {
return RESOLVED_VIRTUAL_MODULE_ID;
}
},
load(id) {
if (id === RESOLVED_VIRTUAL_MODULE_ID) {
return {
code: `@layer base { ${dom.css()} }`,
};
}
},
};
}
However, Astro completely ignores it. There's no style sheet being imported at all. Is there some special handling I am missing here?
I even tried a "dumbed down test version":
const VIRTUAL_MODULE_ID = 'virtual:custom-css/red.css';
const RESOLVED_VIRTUAL_MODULE_ID = '\0' + VIRTUAL_MODULE_ID;
/** @returns {import('vite').Plugin} */
export function fontAwesomeCss() {
return {
name: 'vite-plugin-fake-css',
resolveId(id) {
if (id === VIRTUAL_MODULE_ID) {
return RESOLVED_VIRTUAL_MODULE_ID;
}
},
load(id) {
if (id === RESOLVED_VIRTUAL_MODULE_ID) {
return {
code: `* { color: red !important }`,
};
}
},
};
}
Same thing - nothing's happending.