diff --git a/.changeset/four-doors-trade.md b/.changeset/four-doors-trade.md new file mode 100644 index 000000000000..592c3fea9013 --- /dev/null +++ b/.changeset/four-doors-trade.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix CSS traversal boundaries so pages with `export const partial = true` still contribute styles when imported as components by other pages. diff --git a/packages/astro/src/core/build/plugins/plugin-css.ts b/packages/astro/src/core/build/plugins/plugin-css.ts index 6f050eb349db..2a833c1209f4 100644 --- a/packages/astro/src/core/build/plugins/plugin-css.ts +++ b/packages/astro/src/core/build/plugins/plugin-css.ts @@ -3,6 +3,7 @@ import type { BuildOptions, ResolvedConfig, Plugin as VitePlugin } from 'vite'; import { isCSSRequest } from 'vite'; import { ASTRO_VITE_ENVIRONMENT_NAMES } from '../../constants.js'; import { isPropagatedAssetBoundary } from '../../head-propagation/boundary.js'; +import { VIRTUAL_PAGE_RESOLVED_MODULE_ID } from '../../../vite-plugin-pages/const.js'; import { getParentExtendedModuleInfos, getParentModuleInfos, @@ -33,7 +34,14 @@ interface PluginOptions { function isBuildCssBoundary(id: string, ctx: { getModuleInfo: GetModuleInfo }): boolean { if (isPropagatedAssetBoundary(id)) return true; const info = ctx.getModuleInfo(id); - return info ? moduleIsTopLevelPage(info) : false; + if (!info || !moduleIsTopLevelPage(info)) return false; + const allImporters = info.importers.concat(info.dynamicImporters); + const hasNonVirtualPageImporter = allImporters.some( + (importer) => !importer.includes(VIRTUAL_PAGE_RESOLVED_MODULE_ID), + ); + // Pages imported by non-virtual modules (e.g. partials imported by other pages) + // should propagate CSS transitively instead of acting as hard page boundaries. + return !hasNonVirtualPageImporter; } function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[] { diff --git a/packages/astro/test/fixtures/partials-css-boundary/astro.config.mjs b/packages/astro/test/fixtures/partials-css-boundary/astro.config.mjs new file mode 100644 index 000000000000..86dbfb924824 --- /dev/null +++ b/packages/astro/test/fixtures/partials-css-boundary/astro.config.mjs @@ -0,0 +1,3 @@ +import { defineConfig } from 'astro/config'; + +export default defineConfig({}); diff --git a/packages/astro/test/fixtures/partials-css-boundary/package.json b/packages/astro/test/fixtures/partials-css-boundary/package.json new file mode 100644 index 000000000000..5dac595aa12c --- /dev/null +++ b/packages/astro/test/fixtures/partials-css-boundary/package.json @@ -0,0 +1,8 @@ +{ + "name": "@test/partials-css-boundary", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/partials-css-boundary/src/components/ResultsTable.astro b/packages/astro/test/fixtures/partials-css-boundary/src/components/ResultsTable.astro new file mode 100644 index 000000000000..e1196fe74463 --- /dev/null +++ b/packages/astro/test/fixtures/partials-css-boundary/src/components/ResultsTable.astro @@ -0,0 +1,13 @@ +
| Alpha | +