From 1cfbd5923f7de6d56e83d2d989a71e97c82b1fa4 Mon Sep 17 00:00:00 2001 From: matthewp Date: Tue, 6 Dec 2022 21:28:24 +0000 Subject: [PATCH] [ci] format --- packages/astro/src/@types/astro.ts | 4 +- packages/astro/src/core/create-vite.ts | 2 +- packages/astro/src/core/render/dev/head.ts | 10 +-- packages/astro/src/core/render/dev/index.ts | 2 +- .../src/runtime/server/astro-component.ts | 2 +- packages/astro/src/runtime/server/index.ts | 13 ++-- .../astro/src/runtime/server/render/any.ts | 12 +-- .../runtime/server/render/astro/factory.ts | 14 ++-- .../server/render/astro/head-and-content.ts | 10 +-- .../src/runtime/server/render/astro/index.ts | 27 ++----- .../runtime/server/render/astro/instance.ts | 27 ++++--- .../server/render/astro/render-template.ts | 4 +- .../src/runtime/server/render/component.ts | 29 +++---- .../astro/src/runtime/server/render/head.ts | 12 +-- .../astro/src/runtime/server/render/index.ts | 12 ++- .../astro/src/runtime/server/render/page.ts | 18 +++-- .../src/runtime/server/render/stylesheet.ts | 24 +++--- packages/astro/src/vite-plugin-astro/index.ts | 2 +- .../astro/src/vite-plugin-astro/metadata.ts | 4 +- .../src/vite-plugin-head-propagation/index.ts | 25 +++--- .../src/vite-plugin-markdown-legacy/index.ts | 2 +- .../test/units/dev/head-injection.test.js | 76 ++++++++++--------- 22 files changed, 172 insertions(+), 159 deletions(-) diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index ce117f3ac..4bbbe996f 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -1401,11 +1401,11 @@ export interface SSRMetadata { /** * A hint on whether the Astro runtime needs to wait on a component to render head * content. The meanings: - * + * * - __none__ (default) The component does not propagation head content. * - __self__ The component appends head content. * - __in-tree__ Another component within this component's dependency tree appends head content. - * + * * These are used within the runtime to know whether or not a component should be waited on. */ export type PropagationHint = 'none' | 'self' | 'in-tree'; diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index 576ef0469..5b8991f02 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -10,6 +10,7 @@ import { vitePluginAstroServer } from '../vite-plugin-astro-server/index.js'; import astroVitePlugin from '../vite-plugin-astro/index.js'; import configAliasVitePlugin from '../vite-plugin-config-alias/index.js'; import envVitePlugin from '../vite-plugin-env/index.js'; +import astroHeadPropagationPlugin from '../vite-plugin-head-propagation/index.js'; import htmlVitePlugin from '../vite-plugin-html/index.js'; import astroIntegrationsContainerPlugin from '../vite-plugin-integrations-container/index.js'; import jsxVitePlugin from '../vite-plugin-jsx/index.js'; @@ -18,7 +19,6 @@ import legacyMarkdownVitePlugin from '../vite-plugin-markdown-legacy/index.js'; import markdownVitePlugin from '../vite-plugin-markdown/index.js'; import astroScriptsPlugin from '../vite-plugin-scripts/index.js'; import astroScriptsPageSSRPlugin from '../vite-plugin-scripts/page-ssr.js'; -import astroHeadPropagationPlugin from '../vite-plugin-head-propagation/index.js'; import { createCustomViteLogger } from './errors/dev/index.js'; import { resolveDependency } from './util.js'; diff --git a/packages/astro/src/core/render/dev/head.ts b/packages/astro/src/core/render/dev/head.ts index 9294192b3..56f110a20 100644 --- a/packages/astro/src/core/render/dev/head.ts +++ b/packages/astro/src/core/render/dev/head.ts @@ -2,8 +2,8 @@ import type { SSRResult } from '../../../@types/astro'; import type { ModuleInfo, ModuleLoader } from '../../module-loader/index'; -import { viteID } from '../../util.js'; import { getAstroMetadata } from '../../../vite-plugin-astro/index.js'; +import { viteID } from '../../util.js'; import { crawlGraph } from './vite.js'; export async function getPropagationMap( @@ -13,7 +13,7 @@ export async function getPropagationMap( const map: SSRResult['propagation'] = new Map(); const rootID = viteID(filePath); - addInjection(map, loader.getModuleInfo(rootID)) + addInjection(map, loader.getModuleInfo(rootID)); for await (const moduleNode of crawlGraph(loader, rootID, true)) { const id = moduleNode.id; if (id) { @@ -25,10 +25,10 @@ export async function getPropagationMap( } function addInjection(map: SSRResult['propagation'], modInfo: ModuleInfo | null) { - if(modInfo) { + if (modInfo) { const astro = getAstroMetadata(modInfo); - if(astro && astro.propagation) { - map.set(modInfo.id, astro.propagation) + if (astro && astro.propagation) { + map.set(modInfo.id, astro.propagation); } } } diff --git a/packages/astro/src/core/render/dev/index.ts b/packages/astro/src/core/render/dev/index.ts index e35e152e8..16fb13fd3 100644 --- a/packages/astro/src/core/render/dev/index.ts +++ b/packages/astro/src/core/render/dev/index.ts @@ -15,8 +15,8 @@ import { createRenderContext, renderPage as coreRenderPage } from '../index.js'; import { filterFoundRenderers, loadRenderer } from '../renderer.js'; import { getStylesForURL } from './css.js'; import type { DevelopmentEnvironment } from './environment'; -import { getScriptsForURL } from './scripts.js'; import { getPropagationMap } from './head.js'; +import { getScriptsForURL } from './scripts.js'; export { createDevelopmentEnvironment } from './environment.js'; export type { DevelopmentEnvironment }; diff --git a/packages/astro/src/runtime/server/astro-component.ts b/packages/astro/src/runtime/server/astro-component.ts index 52b993f62..f3df3a78f 100644 --- a/packages/astro/src/runtime/server/astro-component.ts +++ b/packages/astro/src/runtime/server/astro-component.ts @@ -21,7 +21,7 @@ function createComponentWithOptions(opts: CreateComponentOptions) { } // Used in creating the component. aka the main export. export function createComponent(arg1: AstroComponentFactory, moduleId: string) { - if(typeof arg1 === 'function') { + if (typeof arg1 === 'function') { return baseCreateComponent(arg1, moduleId); } else { return createComponentWithOptions(arg1); diff --git a/packages/astro/src/runtime/server/index.ts b/packages/astro/src/runtime/server/index.ts index 519703b95..7348ea2ed 100644 --- a/packages/astro/src/runtime/server/index.ts +++ b/packages/astro/src/runtime/server/index.ts @@ -1,3 +1,4 @@ +export { createComponent } from './astro-component.js'; export { createAstro } from './astro-global.js'; export { renderEndpoint } from './endpoint.js'; export { escapeHTML, HTMLBytes, HTMLString, markHTMLString, unescapeHTML } from './escape.js'; @@ -18,17 +19,19 @@ export { renderSlot, renderTemplate as render, renderTemplate, - renderUniqueStylesheet, renderToString, + renderUniqueStylesheet, stringifyChunk, voidElementNames, } from './render/index.js'; -export { createComponent } from './astro-component.js'; -export type { AstroComponentFactory, AstroComponentInstance, RenderInstruction } from './render/index.js'; +export type { + AstroComponentFactory, + AstroComponentInstance, + RenderInstruction, +} from './render/index.js'; import { markHTMLString } from './escape.js'; -import { Renderer } from './render/index.js'; -import { addAttribute } from './render/index.js'; +import { addAttribute, Renderer } from './render/index.js'; export function mergeSlots(...slotted: unknown[]) { const slots: Record any> = {}; diff --git a/packages/astro/src/runtime/server/render/any.ts b/packages/astro/src/runtime/server/render/any.ts index 119dbc105..cded2c496 100644 --- a/packages/astro/src/runtime/server/render/any.ts +++ b/packages/astro/src/runtime/server/render/any.ts @@ -1,6 +1,9 @@ import { escapeHTML, isHTMLString, markHTMLString } from '../escape.js'; -import { isRenderTemplateResult, renderAstroTemplateResult } from './astro/index.js'; -import { isAstroComponentInstance } from './astro/index.js'; +import { + isAstroComponentInstance, + isRenderTemplateResult, + renderAstroTemplateResult, +} from './astro/index.js'; import { SlotString } from './slot.js'; export async function* renderChild(child: any): AsyncIterable { @@ -25,10 +28,9 @@ export async function* renderChild(child: any): AsyncIterable { yield markHTMLString(escapeHTML(child)); } else if (!child && child !== 0) { // do nothing, safe to ignore falsey values. - } - else if(isRenderTemplateResult(child)) { + } else if (isRenderTemplateResult(child)) { yield* renderAstroTemplateResult(child); - } else if(isAstroComponentInstance(child)) { + } else if (isAstroComponentInstance(child)) { yield* child.render(); } else if (ArrayBuffer.isView(child)) { yield child; diff --git a/packages/astro/src/runtime/server/render/astro/factory.ts b/packages/astro/src/runtime/server/render/astro/factory.ts index 50cda589d..ec66edd55 100644 --- a/packages/astro/src/runtime/server/render/astro/factory.ts +++ b/packages/astro/src/runtime/server/render/astro/factory.ts @@ -1,10 +1,10 @@ -import type { SSRResult, PropagationHint } from '../../../../@types/astro'; +import type { PropagationHint, SSRResult } from '../../../../@types/astro'; import type { HeadAndContent } from './head-and-content'; import type { RenderTemplateResult } from './render-template'; -import { renderAstroTemplateResult } from './render-template.js'; -import { isHeadAndContent } from './head-and-content.js'; import { HTMLParts } from '../common.js'; +import { isHeadAndContent } from './head-and-content.js'; +import { renderAstroTemplateResult } from './render-template.js'; export type AstroFactoryReturnValue = RenderTemplateResult | Response | HeadAndContent; @@ -40,13 +40,15 @@ export async function renderToString( parts.append(chunk, result); } - return parts.toString(); } -export function isAPropagatingComponent(result: SSRResult, factory: AstroComponentFactory): boolean { +export function isAPropagatingComponent( + result: SSRResult, + factory: AstroComponentFactory +): boolean { let hint: PropagationHint = factory.propagation || 'none'; - if(factory.moduleId && result.propagation.has(factory.moduleId) && hint === 'none') { + if (factory.moduleId && result.propagation.has(factory.moduleId) && hint === 'none') { hint = result.propagation.get(factory.moduleId)!; } return hint === 'in-tree' || hint === 'self'; diff --git a/packages/astro/src/runtime/server/render/astro/head-and-content.ts b/packages/astro/src/runtime/server/render/astro/head-and-content.ts index e44911424..57f05425d 100644 --- a/packages/astro/src/runtime/server/render/astro/head-and-content.ts +++ b/packages/astro/src/runtime/server/render/astro/head-and-content.ts @@ -5,11 +5,11 @@ const headAndContentSym = Symbol.for('astro.headAndContent'); export type HeadAndContent = { [headAndContentSym]: true; head: string | RenderTemplateResult; - content: RenderTemplateResult; -} + content: RenderTemplateResult; +}; export function isHeadAndContent(obj: unknown): obj is HeadAndContent { - return typeof obj === 'object' && !!((obj as any)[headAndContentSym]); + return typeof obj === 'object' && !!(obj as any)[headAndContentSym]; } export function createHeadAndContent( @@ -19,6 +19,6 @@ export function createHeadAndContent( return { [headAndContentSym]: true, head, - content - } + content, + }; } diff --git a/packages/astro/src/runtime/server/render/astro/index.ts b/packages/astro/src/runtime/server/render/astro/index.ts index 0dc39805d..cbddf7876 100644 --- a/packages/astro/src/runtime/server/render/astro/index.ts +++ b/packages/astro/src/runtime/server/render/astro/index.ts @@ -1,25 +1,10 @@ - -export { - createAstroComponentInstance, - isAstroComponentInstance -} from './instance.js'; -export { - isAstroComponentFactory, - renderToString -} from './factory.js'; +export type { AstroComponentFactory } from './factory'; +export { isAstroComponentFactory, renderToString } from './factory.js'; +export { createHeadAndContent, isHeadAndContent } from './head-and-content.js'; +export type { AstroComponentInstance } from './instance'; +export { createAstroComponentInstance, isAstroComponentInstance } from './instance.js'; export { isRenderTemplateResult, renderAstroTemplateResult, - renderTemplate + renderTemplate, } from './render-template.js'; -export { - isHeadAndContent, - createHeadAndContent -} from './head-and-content.js'; - -export type { - AstroComponentFactory -} from './factory'; -export type { - AstroComponentInstance -} from './instance'; diff --git a/packages/astro/src/runtime/server/render/astro/instance.ts b/packages/astro/src/runtime/server/render/astro/instance.ts index db3916a49..6e1cc465a 100644 --- a/packages/astro/src/runtime/server/render/astro/instance.ts +++ b/packages/astro/src/runtime/server/render/astro/instance.ts @@ -2,10 +2,10 @@ import type { SSRResult } from '../../../../@types/astro'; import type { AstroComponentFactory, AstroFactoryReturnValue } from './factory.js'; import { HydrationDirectiveProps } from '../../hydration.js'; -import { renderChild } from '../any.js'; -import { isHeadAndContent } from './head-and-content.js'; -import { isAPropagatingComponent } from './factory.js'; import { isPromise } from '../../util.js'; +import { renderChild } from '../any.js'; +import { isAPropagatingComponent } from './factory.js'; +import { isHeadAndContent } from './head-and-content.js'; type ComponentProps = Record; @@ -19,7 +19,12 @@ export class AstroComponentInstance { private readonly slots: any; private readonly factory: AstroComponentFactory; private returnValue: ReturnType | undefined; - constructor(result: SSRResult, props: ComponentProps, slots: any, factory: AstroComponentFactory) { + constructor( + result: SSRResult, + props: ComponentProps, + slots: any, + factory: AstroComponentFactory + ) { this.result = result; this.props = props; this.slots = slots; @@ -32,18 +37,18 @@ export class AstroComponentInstance { } async *render() { - if(this.returnValue === undefined) { + if (this.returnValue === undefined) { await this.init(); } let value: AstroFactoryReturnValue | undefined = this.returnValue; - if(isPromise(value)) { + if (isPromise(value)) { value = await value; } - if(isHeadAndContent(value)) { - yield * value.content; + if (isHeadAndContent(value)) { + yield* value.content; } else { - yield * renderChild(value); + yield* renderChild(value); } } } @@ -71,12 +76,12 @@ export function createAstroComponentInstance( ) { validateComponentProps(props, displayName); const instance = new AstroComponentInstance(result, props, slots, factory); - if(isAPropagatingComponent(result, factory) && !result.propagators.has(factory)) { + if (isAPropagatingComponent(result, factory) && !result.propagators.has(factory)) { result.propagators.set(factory, instance); } return instance; } export function isAstroComponentInstance(obj: unknown): obj is AstroComponentInstance { - return typeof obj === 'object' && !!((obj as any)[astroComponentInstanceSym]); + return typeof obj === 'object' && !!(obj as any)[astroComponentInstanceSym]; } diff --git a/packages/astro/src/runtime/server/render/astro/render-template.ts b/packages/astro/src/runtime/server/render/astro/render-template.ts index 2c637f3c8..66930594b 100644 --- a/packages/astro/src/runtime/server/render/astro/render-template.ts +++ b/packages/astro/src/runtime/server/render/astro/render-template.ts @@ -52,9 +52,7 @@ export class RenderTemplateResult { // Determines if a component is an .astro component export function isRenderTemplateResult(obj: unknown): obj is RenderTemplateResult { - return ( - typeof obj === 'object' && !!((obj as any)[renderTemplateResultSym]) - ); + return typeof obj === 'object' && !!(obj as any)[renderTemplateResultSym]; } export async function* renderAstroTemplateResult( diff --git a/packages/astro/src/runtime/server/render/component.ts b/packages/astro/src/runtime/server/render/component.ts index 0e25d7014..d79992a51 100644 --- a/packages/astro/src/runtime/server/render/component.ts +++ b/packages/astro/src/runtime/server/render/component.ts @@ -1,9 +1,4 @@ -import type { - AstroComponentMetadata, - RouteData, - SSRLoadedRenderer, - SSRResult, -} from '../../../@types/astro'; +import type { AstroComponentMetadata, SSRLoadedRenderer, SSRResult } from '../../../@types/astro'; import type { RenderInstruction } from './types.js'; import { AstroError, AstroErrorData } from '../../../core/errors/index.js'; @@ -11,19 +6,19 @@ import { HTMLBytes, markHTMLString } from '../escape.js'; import { extractDirectives, generateHydrateScript } from '../hydration.js'; import { serializeProps } from '../serialize.js'; import { shorthash } from '../shorthash.js'; +import { isPromise } from '../util.js'; import { createAstroComponentInstance, isAstroComponentFactory, isAstroComponentInstance, renderAstroTemplateResult, renderTemplate, - type AstroComponentInstance + type AstroComponentInstance, } from './astro/index.js'; import { Fragment, Renderer, stringifyChunk } from './common.js'; import { componentIsHTMLElement, renderHTMLElement } from './dom.js'; import { renderSlot, renderSlots } from './slot.js'; import { formatList, internalSpreadAttributes, renderElement, voidElementNames } from './util.js'; -import { isPromise } from '../util.js'; const rendererAliases = new Map([['solid', 'solid-js']]); @@ -55,9 +50,7 @@ function isFragmentComponent(Component: unknown) { } function isHTMLComponent(Component: unknown) { - return ( - Component && typeof Component === 'object' && (Component as any)['astro:html'] - ); + return Component && typeof Component === 'object' && (Component as any)['astro:html']; } async function renderFrameworkComponent( @@ -65,7 +58,7 @@ async function renderFrameworkComponent( displayName: string, Component: unknown, _props: Record, - slots: any = {}, + slots: any = {} ): Promise { if (!Component && !_props['client:only']) { throw new Error( @@ -358,22 +351,22 @@ export function renderComponent( props: Record, slots: any = {} ): Promise | ComponentIterable | AstroComponentInstance { - if(isPromise(Component)) { - return Promise.resolve(Component).then(Unwrapped => { + if (isPromise(Component)) { + return Promise.resolve(Component).then((Unwrapped) => { return renderComponent(result, displayName, Unwrapped, props, slots) as any; }); } - if(isFragmentComponent(Component)) { + if (isFragmentComponent(Component)) { return renderFragmentComponent(result, slots); } // .html components - if(isHTMLComponent(Component)) { + if (isHTMLComponent(Component)) { return renderHTMLComponent(result, Component, props, slots); } - if(isAstroComponentFactory(Component)) { + if (isAstroComponentFactory(Component)) { return createAstroComponentInstance(result, displayName, Component, props, slots); } @@ -388,7 +381,7 @@ export function renderComponentToIterable( slots: any = {} ): Promise | ComponentIterable { const renderResult = renderComponent(result, displayName, Component, props, slots); - if(isAstroComponentInstance(renderResult)) { + if (isAstroComponentInstance(renderResult)) { return renderResult.render(); } return renderResult; diff --git a/packages/astro/src/runtime/server/render/head.ts b/packages/astro/src/runtime/server/render/head.ts index 701432c2a..c02e973f1 100644 --- a/packages/astro/src/runtime/server/render/head.ts +++ b/packages/astro/src/runtime/server/render/head.ts @@ -1,8 +1,8 @@ import type { SSRResult } from '../../../@types/astro'; import { markHTMLString } from '../escape.js'; -import { renderElement } from './util.js'; import { renderChild } from './any.js'; +import { renderElement } from './util.js'; // Filter out duplicate elements in our set const uniqueElements = (item: any, index: number, all: any[]) => { @@ -13,10 +13,10 @@ const uniqueElements = (item: any, index: number, all: any[]) => { ); }; -async function * renderExtraHead(result: SSRResult, base: string) { +async function* renderExtraHead(result: SSRResult, base: string) { yield base; - for(const part of result.extraHead) { - yield * renderChild(part); + for (const part of result.extraHead) { + yield* renderChild(part); } } @@ -35,9 +35,9 @@ function renderAllHeadContent(result: SSRResult) { .filter(uniqueElements) .map((link) => renderElement('link', link, false)); - const baseHeadContent = markHTMLString(links.join('\n') + styles.join('\n') + scripts.join('\n')) + const baseHeadContent = markHTMLString(links.join('\n') + styles.join('\n') + scripts.join('\n')); - if(result.extraHead.length > 0) { + if (result.extraHead.length > 0) { return renderExtraHead(result, baseHeadContent); } else { return baseHeadContent; diff --git a/packages/astro/src/runtime/server/render/index.ts b/packages/astro/src/runtime/server/render/index.ts index 15a4d1977..776058d44 100644 --- a/packages/astro/src/runtime/server/render/index.ts +++ b/packages/astro/src/runtime/server/render/index.ts @@ -1,12 +1,16 @@ -export type { RenderInstruction } from './types'; export type { AstroComponentFactory, AstroComponentInstance } from './astro/index'; - -export { createHeadAndContent, renderAstroTemplateResult, renderToString, renderTemplate } from './astro/index.js'; +export { + createHeadAndContent, + renderAstroTemplateResult, + renderTemplate, + renderToString, +} from './astro/index.js'; export { Fragment, Renderer, stringifyChunk } from './common.js'; export { renderComponent, renderComponentToIterable } from './component.js'; export { renderHTMLElement } from './dom.js'; export { maybeRenderHead, renderHead } from './head.js'; export { renderPage } from './page.js'; export { renderSlot } from './slot.js'; -export { addAttribute, defineScriptVars, voidElementNames } from './util.js'; export { renderUniqueStylesheet } from './stylesheet.js'; +export type { RenderInstruction } from './types'; +export { addAttribute, defineScriptVars, voidElementNames } from './util.js'; diff --git a/packages/astro/src/runtime/server/render/page.ts b/packages/astro/src/runtime/server/render/page.ts index 9f9c7ae40..b949019d6 100644 --- a/packages/astro/src/runtime/server/render/page.ts +++ b/packages/astro/src/runtime/server/render/page.ts @@ -8,9 +8,9 @@ import { createResponse } from '../response.js'; import { isAstroComponentFactory, isAstroComponentInstance, - isRenderTemplateResult, isHeadAndContent, - renderAstroTemplateResult + isRenderTemplateResult, + renderAstroTemplateResult, } from './astro/index.js'; import { chunkToByteArray, encoder, HTMLParts } from './common.js'; import { renderComponent } from './component.js'; @@ -55,13 +55,13 @@ async function iterableToHTMLBytes( // to be propagated up. async function bufferHeadContent(result: SSRResult) { const iterator = result.propagators.values(); - while(true) { + while (true) { const { value, done } = iterator.next(); - if(done) { + if (done) { break; } const returnValue = await value.init(); - if(isHeadAndContent(returnValue)) { + if (isHeadAndContent(returnValue)) { result.extraHead.push(returnValue.head); } } @@ -85,9 +85,9 @@ export async function renderPage( componentFactory.name, componentFactory, pageProps, - null, + null ); - if(isAstroComponentInstance(renderResult)) { + if (isAstroComponentInstance(renderResult)) { output = renderResult.render(); } else { output = renderResult; @@ -123,7 +123,9 @@ export async function renderPage( if (isRenderTemplateResult(factoryReturnValue) || factoryIsHeadAndContent) { // Wait for head content to be buffered up await bufferHeadContent(result); - const templateResult = factoryIsHeadAndContent ? factoryReturnValue.content : factoryReturnValue; + const templateResult = factoryIsHeadAndContent + ? factoryReturnValue.content + : factoryReturnValue; let iterable = renderAstroTemplateResult(templateResult); let init = result.response; diff --git a/packages/astro/src/runtime/server/render/stylesheet.ts b/packages/astro/src/runtime/server/render/stylesheet.ts index cc704bc0b..a492e6492 100644 --- a/packages/astro/src/runtime/server/render/stylesheet.ts +++ b/packages/astro/src/runtime/server/render/stylesheet.ts @@ -1,22 +1,28 @@ import { SSRResult } from '../../../@types/astro'; -import { renderElement } from './util.js'; import { markHTMLString } from '../escape.js'; +import { renderElement } from './util.js'; const stylesheetRel = 'stylesheet'; export function renderStylesheet({ href }: { href: string }) { - return markHTMLString(renderElement('link', { - props: { - rel: stylesheetRel, - href - }, - children: '' - }, false)); + return markHTMLString( + renderElement( + 'link', + { + props: { + rel: stylesheetRel, + href, + }, + children: '', + }, + false + ) + ); } export function renderUniqueStylesheet(result: SSRResult, link: { href: string }) { for (const existingLink of result.links) { - if(existingLink.props.rel === stylesheetRel && existingLink.props.href === link.href) { + if (existingLink.props.rel === stylesheetRel && existingLink.props.href === link.href) { return ''; } } diff --git a/packages/astro/src/vite-plugin-astro/index.ts b/packages/astro/src/vite-plugin-astro/index.ts index 4d9accab2..0ecf91268 100644 --- a/packages/astro/src/vite-plugin-astro/index.ts +++ b/packages/astro/src/vite-plugin-astro/index.ts @@ -18,8 +18,8 @@ import { normalizeFilename } from '../vite-plugin-utils/index.js'; import { cachedFullCompilation } from './compile.js'; import { handleHotUpdate } from './hmr.js'; import { parseAstroRequest, ParsedRequestResult } from './query.js'; -export type { AstroPluginMetadata }; export { getAstroMetadata } from './metadata.js'; +export type { AstroPluginMetadata }; interface AstroPluginOptions { settings: AstroSettings; diff --git a/packages/astro/src/vite-plugin-astro/metadata.ts b/packages/astro/src/vite-plugin-astro/metadata.ts index 866d01277..ee66c12d0 100644 --- a/packages/astro/src/vite-plugin-astro/metadata.ts +++ b/packages/astro/src/vite-plugin-astro/metadata.ts @@ -1,8 +1,8 @@ -import type { PluginMetadata } from './types'; import type { ModuleInfo } from '../core/module-loader'; +import type { PluginMetadata } from './types'; export function getAstroMetadata(modInfo: ModuleInfo): PluginMetadata['astro'] | undefined { - if(modInfo.meta?.astro) { + if (modInfo.meta?.astro) { return modInfo.meta.astro as PluginMetadata['astro']; } return undefined; diff --git a/packages/astro/src/vite-plugin-head-propagation/index.ts b/packages/astro/src/vite-plugin-head-propagation/index.ts index dd8355c0f..20cd84637 100644 --- a/packages/astro/src/vite-plugin-head-propagation/index.ts +++ b/packages/astro/src/vite-plugin-head-propagation/index.ts @@ -1,5 +1,5 @@ -import type { AstroSettings } from '../@types/astro'; import type { ModuleInfo } from 'rollup'; +import type { AstroSettings } from '../@types/astro'; import * as vite from 'vite'; import { getAstroMetadata } from '../vite-plugin-astro/index.js'; @@ -16,17 +16,22 @@ export default function configHeadPropagationVitePlugin({ }: { settings: AstroSettings; }): vite.Plugin { - function addHeadInjectionInTree(graph: vite.ModuleGraph, id: string, getInfo: (id: string) => ModuleInfo | null, seen: Set = new Set()) { + function addHeadInjectionInTree( + graph: vite.ModuleGraph, + id: string, + getInfo: (id: string) => ModuleInfo | null, + seen: Set = new Set() + ) { const mod = server.moduleGraph.getModuleById(id); - for(const parent of mod?.importers || []) { - if(parent.id) { - if(seen.has(parent.id)) { + for (const parent of mod?.importers || []) { + if (parent.id) { + if (seen.has(parent.id)) { continue; } const info = getInfo(parent.id); - if(info?.meta.astro) { + if (info?.meta.astro) { const astroMetadata = getAstroMetadata(info); - if(astroMetadata) { + if (astroMetadata) { astroMetadata.propagation = 'in-tree'; } } @@ -42,13 +47,13 @@ export default function configHeadPropagationVitePlugin({ server = _server; }, transform(source, id) { - if(!server) { + if (!server) { return; } - if(injectExp.test(source)) { + if (injectExp.test(source)) { addHeadInjectionInTree(server.moduleGraph, id, (child) => this.getModuleInfo(child)); } - } + }, }; } diff --git a/packages/astro/src/vite-plugin-markdown-legacy/index.ts b/packages/astro/src/vite-plugin-markdown-legacy/index.ts index f55f47248..e0d3f4d62 100644 --- a/packages/astro/src/vite-plugin-markdown-legacy/index.ts +++ b/packages/astro/src/vite-plugin-markdown-legacy/index.ts @@ -233,7 +233,7 @@ ${tsResult}`; clientOnlyComponents: transformResult.clientOnlyComponents, hydratedComponents: transformResult.hydratedComponents, scripts: transformResult.scripts, - propagation: 'none' + propagation: 'none', }; return { diff --git a/packages/astro/test/units/dev/head-injection.test.js b/packages/astro/test/units/dev/head-injection.test.js index 5f57d2400..0426b4c78 100644 --- a/packages/astro/test/units/dev/head-injection.test.js +++ b/packages/astro/test/units/dev/head-injection.test.js @@ -55,24 +55,28 @@ describe('head injection', () => { root ); - await runInContainer({ - fs, root, - userConfig: { - vite: { server: { middlewareMode: true } } - } - }, async (container) => { - const { req, res, done, text } = createRequestAndResponse({ - method: 'GET', - url: '/', - }); - container.handle(req, res); - await done; - const html = await text(); - const $ = cheerio.load(html); + await runInContainer( + { + fs, + root, + userConfig: { + vite: { server: { middlewareMode: true } }, + }, + }, + async (container) => { + const { req, res, done, text } = createRequestAndResponse({ + method: 'GET', + url: '/', + }); + container.handle(req, res); + await done; + const html = await text(); + const $ = cheerio.load(html); - expect($('link[rel=stylesheet][href="/some/fake/styles.css"]')).to.have.a.lengthOf(1); - expect($('#other')).to.have.a.lengthOf(1); - }); + expect($('link[rel=stylesheet][href="/some/fake/styles.css"]')).to.have.a.lengthOf(1); + expect($('#other')).to.have.a.lengthOf(1); + } + ); }); it('Dynamic injection from a layout component', async () => { @@ -138,23 +142,27 @@ describe('head injection', () => { root ); - await runInContainer({ - fs, root, - userConfig: { - vite: { server: { middlewareMode: true } } - } - }, async (container) => { - const { req, res, done, text } = createRequestAndResponse({ - method: 'GET', - url: '/', - }); - container.handle(req, res); - await done; - const html = await text(); - const $ = cheerio.load(html); + await runInContainer( + { + fs, + root, + userConfig: { + vite: { server: { middlewareMode: true } }, + }, + }, + async (container) => { + const { req, res, done, text } = createRequestAndResponse({ + method: 'GET', + url: '/', + }); + container.handle(req, res); + await done; + const html = await text(); + const $ = cheerio.load(html); - expect($('link[rel=stylesheet][href="/some/fake/styles.css"]')).to.have.a.lengthOf(1); - expect($('#other')).to.have.a.lengthOf(1); - }); + expect($('link[rel=stylesheet][href="/some/fake/styles.css"]')).to.have.a.lengthOf(1); + expect($('#other')).to.have.a.lengthOf(1); + } + ); }); });