From ba7aa36d195286b113aad89bfbdb58c1558f88c5 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Wed, 24 Aug 2022 11:25:51 -0400 Subject: [PATCH 1/3] Include styles imported by hoisted scripts --- packages/astro/src/core/build/graph.ts | 12 ++++- packages/astro/src/core/build/internal.ts | 29 ++++++++---- .../astro/src/core/build/vite-plugin-css.ts | 45 ++++++++++++------- packages/astro/test/astro-scripts.test.js | 7 +++ .../astro-scripts/src/pages/with-styles.astro | 15 +++++++ .../fixtures/astro-scripts/src/styles/one.css | 3 ++ 6 files changed, 84 insertions(+), 27 deletions(-) create mode 100644 packages/astro/test/fixtures/astro-scripts/src/pages/with-styles.astro create mode 100644 packages/astro/test/fixtures/astro-scripts/src/styles/one.css diff --git a/packages/astro/src/core/build/graph.ts b/packages/astro/src/core/build/graph.ts index beef7956335d..82c1d6c7a8cb 100644 --- a/packages/astro/src/core/build/graph.ts +++ b/packages/astro/src/core/build/graph.ts @@ -1,4 +1,6 @@ import type { GetModuleInfo, ModuleInfo } from 'rollup'; +import type { BuildInternals } from './internal.js'; + import { resolvedPagesVirtualModuleId } from '../app/index.js'; // This walks up the dependency graph and yields out each ModuleInfo object. @@ -22,14 +24,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 e8704a2828e5..23213f03ac35 100644 --- a/packages/astro/src/core/build/internal.ts +++ b/packages/astro/src/core/build/internal.ts @@ -1,6 +1,7 @@ -import type { OutputChunk, RenderedChunk } from 'rollup'; +import type { OutputChunk, RenderedChunk, ModuleInfo, GetModuleInfo } from 'rollup'; import type { PageBuildData, ViteID } from './types'; +import { walkParentInfos, moduleIsTopLevelPage } from './graph.js'; import { prependForwardSlash } from '../path.js'; import { viteID } from '../util.js'; @@ -62,13 +63,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