From c773dcde317d46e3f8e68088cda580c30bc8e1da Mon Sep 17 00:00:00 2001 From: Tony Sullivan Date: Thu, 7 Apr 2022 18:26:26 +0000 Subject: [PATCH] Exclude any ?raw or ?url css imports when adding asset links (#3020) * exclude any ?raw css imports when adding css asset links * ?url imports should be ignored as well * chore: adding changeset --- .changeset/dull-bobcats-clean.md | 8 ++++++ .../astro/src/vite-plugin-build-css/index.ts | 6 +++- .../test/astro-css-bundling-import.test.js | 28 +++++++++++++++++++ .../src/layouts/InlineLayout.astro | 28 +++++++++++++++++++ .../src/pages/page-3.astro | 12 ++++++++ .../src/styles/page-three.css | 3 ++ 6 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 .changeset/dull-bobcats-clean.md create mode 100644 packages/astro/test/fixtures/astro-css-bundling-import/src/layouts/InlineLayout.astro create mode 100644 packages/astro/test/fixtures/astro-css-bundling-import/src/pages/page-3.astro create mode 100644 packages/astro/test/fixtures/astro-css-bundling-import/src/styles/page-three.css diff --git a/.changeset/dull-bobcats-clean.md b/.changeset/dull-bobcats-clean.md new file mode 100644 index 0000000000..28ca881a81 --- /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 686dc3e526..dfbfeaa28e 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 62e996dbdb..5bb37dfd4a 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 + +

Page 3

+

This text should be purple, because we want the inlined page-3.css to override site.css

+ diff --git a/packages/astro/test/fixtures/astro-css-bundling-import/src/styles/page-three.css b/packages/astro/test/fixtures/astro-css-bundling-import/src/styles/page-three.css new file mode 100644 index 0000000000..ab17cb5cf4 --- /dev/null +++ b/packages/astro/test/fixtures/astro-css-bundling-import/src/styles/page-three.css @@ -0,0 +1,3 @@ +p { + color: purple; +}