diff --git a/.changeset/dull-bobcats-clean.md b/.changeset/dull-bobcats-clean.md new file mode 100644 index 000000000..28ca881a8 --- /dev/null +++ b/.changeset/dull-bobcats-clean.md @@ -0,0 +1,8 @@ +--- +'astro': patch +--- + +Add support for advanced CSS imports with `?raw` and `?url` + +> ⚠️WARNING⚠️: +> Be careful when bypassing Astro's built-in CSS bundling! Styles won't be included in the built output - this is best used in combination with `set:html` to inline styles directly into the built HTML page. \ No newline at end of file diff --git a/packages/astro/src/vite-plugin-build-css/index.ts b/packages/astro/src/vite-plugin-build-css/index.ts index 686dc3e52..dfbfeaa28 100644 --- a/packages/astro/src/vite-plugin-build-css/index.ts +++ b/packages/astro/src/vite-plugin-build-css/index.ts @@ -47,6 +47,10 @@ function isPageStyleVirtualModule(id: string) { return id.startsWith(ASTRO_PAGE_STYLE_PREFIX); } +function isRawOrUrlModule(id: string) { + return id.match(/(\?|\&)([^=]+)(raw|url)/gm) +} + interface PluginOptions { internals: BuildInternals; legacy: boolean; @@ -69,7 +73,7 @@ export function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin { const info = ctx.getModuleInfo(id); if (info) { for (const importedId of info.importedIds) { - if (!seen.has(importedId)) { + if (!seen.has(importedId) && !isRawOrUrlModule(importedId)) { yield* walkStyles(ctx, importedId, seen); } } diff --git a/packages/astro/test/astro-css-bundling-import.test.js b/packages/astro/test/astro-css-bundling-import.test.js index 62e996dbd..5bb37dfd4 100644 --- a/packages/astro/test/astro-css-bundling-import.test.js +++ b/packages/astro/test/astro-css-bundling-import.test.js @@ -48,4 +48,32 @@ describe('CSS Bundling (ESM import)', () => { } } }); + + it('?raw and ?url CSS imports are ignored', async () => { + // note: this test is a little confusing as well, but the main idea is that + // page-3.astro should have site.css imported as an ESM in InlineLayout.astro + // as well as the styles from page-3.css as an inline + +
This text should be purple, because we want the inlined page-3.css
to override site.css