diff --git a/.changeset/fast-oranges-collect.md b/.changeset/fast-oranges-collect.md new file mode 100644 index 000000000..a24e8f54a --- /dev/null +++ b/.changeset/fast-oranges-collect.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Include styles imported by hoisted scripts diff --git a/packages/astro/src/core/build/graph.ts b/packages/astro/src/core/build/graph.ts index beef79563..b911253d5 100644 --- a/packages/astro/src/core/build/graph.ts +++ b/packages/astro/src/core/build/graph.ts @@ -1,4 +1,5 @@ import type { GetModuleInfo, ModuleInfo } from 'rollup'; + import { resolvedPagesVirtualModuleId } from '../app/index.js'; // This walks up the dependency graph and yields out each ModuleInfo object. @@ -22,14 +23,20 @@ export function* walkParentInfos( } } +// Returns true if a module is a top-level page. We determine this based on whether +// it is imported by the top-level virtual module. +export function moduleIsTopLevelPage(info: ModuleInfo): boolean { + return info.importers[0] === resolvedPagesVirtualModuleId; +} + // This function walks the dependency graph, going up until it finds a page component. // This could be a .astro page or a .md page. export function* getTopLevelPages( id: string, - ctx: { getModuleInfo: GetModuleInfo } + ctx: { getModuleInfo: GetModuleInfo }, ): Generator<[ModuleInfo, number], void, unknown> { for (const res of walkParentInfos(id, ctx)) { - if (res[0]?.importers[0] === resolvedPagesVirtualModuleId) { + if (moduleIsTopLevelPage(res[0])) { yield res; } } diff --git a/packages/astro/src/core/build/internal.ts b/packages/astro/src/core/build/internal.ts index e8704a282..dfebc3052 100644 --- a/packages/astro/src/core/build/internal.ts +++ b/packages/astro/src/core/build/internal.ts @@ -1,4 +1,4 @@ -import type { OutputChunk, RenderedChunk } from 'rollup'; +import type { OutputChunk, RenderedChunk, ModuleInfo, GetModuleInfo } from 'rollup'; import type { PageBuildData, ViteID } from './types'; import { prependForwardSlash } from '../path.js'; @@ -62,13 +62,6 @@ export function createBuildInternals(): BuildInternals { // Pure CSS chunks are chunks that only contain CSS. // This is all of them, and chunkToReferenceIdMap maps them to a hash id used to find the final file. const pureCSSChunks = new Set(); - const chunkToReferenceIdMap = new Map(); - - // This is a mapping of pathname to the string source of all collected - // inline