diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index 1c661343a458..5c398566c8f8 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -772,9 +772,7 @@ export interface MarkdownInstance> { }>; } -export type GetHydrateCallback = () => Promise< - () => void | Promise ->; +export type GetHydrateCallback = () => Promise<() => void | Promise>; /** * getStaticPaths() options diff --git a/packages/astro/src/runtime/server/astro-island.prebuilt.ts b/packages/astro/src/runtime/server/astro-island.prebuilt.ts index 0ac881edf74a..d7204bbe8689 100644 --- a/packages/astro/src/runtime/server/astro-island.prebuilt.ts +++ b/packages/astro/src/runtime/server/astro-island.prebuilt.ts @@ -4,4 +4,4 @@ * to generate this file. */ -export default `var a;{const o={0:t=>t,1:t=>JSON.parse(t,n),2:t=>new RegExp(t),3:t=>new Date(t),4:t=>new Map(JSON.parse(t,n)),5:t=>new Set(JSON.parse(t,n)),6:t=>BigInt(t),7:t=>new URL(t)},n=(t,e)=>{if(t===""||!Array.isArray(e))return e;const[r,s]=e;return r in o?o[r](s):void 0};customElements.get("astro-island")||customElements.define("astro-island",(a=class extends HTMLElement{constructor(){super(...arguments);this.hydrate=()=>{if(!this.hydrator||this.parentElement?.closest("astro-island[ssr]"))return;let e=null,r=this.querySelector("astro-fragment");if(r==null&&this.hasAttribute("tmpl")){let i=this.querySelector("template[data-astro-template]");i&&(e=i.innerHTML,i.remove())}else r&&(e=r.innerHTML);const s=this.hasAttribute("props")?JSON.parse(this.getAttribute("props"),n):{};this.hydrator(this)(this.Component,s,e,{client:this.getAttribute("client")}),this.removeAttribute("ssr"),window.removeEventListener("astro:hydrate",this.hydrate),window.dispatchEvent(new CustomEvent("astro:hydrate"))}}async connectedCallback(){const[{default:e}]=await Promise.all([import(this.getAttribute("directive-url")),import(this.getAttribute("before-hydration-url"))]);window.addEventListener("astro:hydrate",this.hydrate);const r=JSON.parse(this.getAttribute("opts"));e(this,r,async()=>{const s=this.getAttribute("renderer-url"),[i,{default:l}]=await Promise.all([import(this.getAttribute("component-url")),s?import(s):()=>()=>{}]);return this.Component=i[this.getAttribute("component-export")||"default"],this.hydrator=l,this.hydrate})}attributeChangedCallback(){this.hydrator&&this.hydrate()}},a.observedAttributes=["props"],a))}`; \ No newline at end of file +export default `var a;{const o={0:t=>t,1:t=>JSON.parse(t,n),2:t=>new RegExp(t),3:t=>new Date(t),4:t=>new Map(JSON.parse(t,n)),5:t=>new Set(JSON.parse(t,n)),6:t=>BigInt(t),7:t=>new URL(t)},n=(t,e)=>{if(t===""||!Array.isArray(e))return e;const[r,s]=e;return r in o?o[r](s):void 0};customElements.get("astro-island")||customElements.define("astro-island",(a=class extends HTMLElement{constructor(){super(...arguments);this.hydrate=()=>{if(!this.hydrator||this.parentElement?.closest("astro-island[ssr]"))return;let e=null,r=this.querySelector("astro-fragment");if(r==null&&this.hasAttribute("tmpl")){let i=this.querySelector("template[data-astro-template]");i&&(e=i.innerHTML,i.remove())}else r&&(e=r.innerHTML);const s=this.hasAttribute("props")?JSON.parse(this.getAttribute("props"),n):{};this.hydrator(this)(this.Component,s,e,{client:this.getAttribute("client")}),this.removeAttribute("ssr"),window.removeEventListener("astro:hydrate",this.hydrate),window.dispatchEvent(new CustomEvent("astro:hydrate"))}}async connectedCallback(){const[{default:e}]=await Promise.all([import(this.getAttribute("directive-url")),import(this.getAttribute("before-hydration-url"))]);window.addEventListener("astro:hydrate",this.hydrate);const r=JSON.parse(this.getAttribute("opts"));e(this,r,async()=>{const s=this.getAttribute("renderer-url"),[i,{default:l}]=await Promise.all([import(this.getAttribute("component-url")),s?import(s):()=>()=>{}]);return this.Component=i[this.getAttribute("component-export")||"default"],this.hydrator=l,this.hydrate})}attributeChangedCallback(){this.hydrator&&this.hydrate()}},a.observedAttributes=["props"],a))}`; diff --git a/packages/astro/src/runtime/server/astro-island.ts b/packages/astro/src/runtime/server/astro-island.ts index cd93854dcda7..365da6ae360e 100644 --- a/packages/astro/src/runtime/server/astro-island.ts +++ b/packages/astro/src/runtime/server/astro-island.ts @@ -8,77 +8,79 @@ } const propTypes: PropTypeSelector = { - 0: value => value, - 1: value => JSON.parse(value, reviver), - 2: value => new RegExp(value), - 3: value => new Date(value), - 4: value => new Map(JSON.parse(value, reviver)), - 5: value => new Set(JSON.parse(value, reviver)), - 6: value => BigInt(value), - 7: value => new URL(value), + 0: (value) => value, + 1: (value) => JSON.parse(value, reviver), + 2: (value) => new RegExp(value), + 3: (value) => new Date(value), + 4: (value) => new Map(JSON.parse(value, reviver)), + 5: (value) => new Set(JSON.parse(value, reviver)), + 6: (value) => BigInt(value), + 7: (value) => new URL(value), }; const reviver = (propKey: string, raw: string): any => { - if(propKey === '' || !Array.isArray(raw)) return raw; + if (propKey === '' || !Array.isArray(raw)) return raw; const [type, value] = raw; - return (type in propTypes) ? propTypes[type](value) : undefined; + return type in propTypes ? propTypes[type](value) : undefined; }; - if(!customElements.get('astro-island')) { - customElements.define('astro-island', class extends HTMLElement { - public Component: any; - public hydrator: any; - static observedAttributes = ['props']; - async connectedCallback(){ - const [ { default: setup } ] = await Promise.all([ - import(this.getAttribute('directive-url')!), - import(this.getAttribute('before-hydration-url')!) - ]); - window.addEventListener('astro:hydrate', this.hydrate); - - const opts = JSON.parse(this.getAttribute('opts')!); - setup(this, opts, async () => { - const rendererUrl = this.getAttribute('renderer-url'); - const [ - componentModule, - { default: hydrator } - ] = await Promise.all([ - import(this.getAttribute('component-url')!), - rendererUrl ? import(rendererUrl) : () => () => {} + if (!customElements.get('astro-island')) { + customElements.define( + 'astro-island', + class extends HTMLElement { + public Component: any; + public hydrator: any; + static observedAttributes = ['props']; + async connectedCallback() { + const [{ default: setup }] = await Promise.all([ + import(this.getAttribute('directive-url')!), + import(this.getAttribute('before-hydration-url')!), ]); - this.Component = componentModule[this.getAttribute('component-export') || 'default']; - this.hydrator = hydrator; - return this.hydrate; - }); - } - hydrate = () => { - if(!this.hydrator || this.parentElement?.closest('astro-island[ssr]')) { - return; + window.addEventListener('astro:hydrate', this.hydrate); + + const opts = JSON.parse(this.getAttribute('opts')!); + setup(this, opts, async () => { + const rendererUrl = this.getAttribute('renderer-url'); + const [componentModule, { default: hydrator }] = await Promise.all([ + import(this.getAttribute('component-url')!), + rendererUrl ? import(rendererUrl) : () => () => {}, + ]); + this.Component = componentModule[this.getAttribute('component-export') || 'default']; + this.hydrator = hydrator; + return this.hydrate; + }); } - let innerHTML: string | null = null; - let fragment = this.querySelector('astro-fragment'); - if (fragment == null && this.hasAttribute('tmpl')) { - // If there is no child fragment, check to see if there is a template. - // This happens if children were passed but the client component did not render any. - let template = this.querySelector('template[data-astro-template]'); - if (template) { - innerHTML = template.innerHTML; - template.remove(); + hydrate = () => { + if (!this.hydrator || this.parentElement?.closest('astro-island[ssr]')) { + return; } - } else if (fragment) { - innerHTML = fragment.innerHTML; + let innerHTML: string | null = null; + let fragment = this.querySelector('astro-fragment'); + if (fragment == null && this.hasAttribute('tmpl')) { + // If there is no child fragment, check to see if there is a template. + // This happens if children were passed but the client component did not render any. + let template = this.querySelector('template[data-astro-template]'); + if (template) { + innerHTML = template.innerHTML; + template.remove(); + } + } else if (fragment) { + innerHTML = fragment.innerHTML; + } + const props = this.hasAttribute('props') + ? JSON.parse(this.getAttribute('props')!, reviver) + : {}; + this.hydrator(this)(this.Component, props, innerHTML, { + client: this.getAttribute('client'), + }); + this.removeAttribute('ssr'); + window.removeEventListener('astro:hydrate', this.hydrate); + window.dispatchEvent(new CustomEvent('astro:hydrate')); + }; + attributeChangedCallback() { + if (this.hydrator) this.hydrate(); } - const props = this.hasAttribute('props') ? JSON.parse(this.getAttribute('props')!, reviver) : {}; - this.hydrator(this)(this.Component, props, innerHTML, { - client: this.getAttribute('client') - }); - this.removeAttribute('ssr'); - window.removeEventListener('astro:hydrate', this.hydrate); - window.dispatchEvent(new CustomEvent('astro:hydrate')); - } - attributeChangedCallback() { - if(this.hydrator) this.hydrate(); } - }); + ); } } diff --git a/packages/astro/src/runtime/server/hydration.ts b/packages/astro/src/runtime/server/hydration.ts index 90a9d7b7d6ad..9f05152b8e54 100644 --- a/packages/astro/src/runtime/server/hydration.ts +++ b/packages/astro/src/runtime/server/hydration.ts @@ -5,8 +5,8 @@ import type { SSRResult, } from '../../@types/astro'; import { escapeHTML } from './escape.js'; -import { hydrationSpecifier, serializeListValue } from './util.js'; import { serializeProps } from './serialize.js'; +import { hydrationSpecifier, serializeListValue } from './util.js'; const HydrationDirectives = ['load', 'idle', 'media', 'visible', 'only']; @@ -95,11 +95,6 @@ interface HydrateScriptOptions { props: Record; } - - - - - /** For hydrated components, generate a `; } - return markHTMLString( - script + - renderElement('astro-island', island, false) - ); + return markHTMLString(script + renderElement('astro-island', island, false)); } /** Create the Astro.fetchContent() runtime function. */ diff --git a/packages/astro/src/runtime/server/serialize.ts b/packages/astro/src/runtime/server/serialize.ts index d101e2f74b99..d15b22ea4a81 100644 --- a/packages/astro/src/runtime/server/serialize.ts +++ b/packages/astro/src/runtime/server/serialize.ts @@ -16,14 +16,16 @@ function serializeArray(value: any[]): any[] { } function serializeObject(value: Record): Record { - return Object.fromEntries(Object.entries(value).map(([k, v]) => { - return [k, convertToSerializedForm(v)]; - })); + return Object.fromEntries( + Object.entries(value).map(([k, v]) => { + return [k, convertToSerializedForm(v)]; + }) + ); } function convertToSerializedForm(value: any): [ValueOf, any] { const tag = Object.prototype.toString.call(value); - switch(tag) { + switch (tag) { case '[object Date]': { return [PROP_TYPE.Date, (value as Date).toISOString()]; } @@ -46,7 +48,7 @@ function convertToSerializedForm(value: any): [ValueOf, any] { return [PROP_TYPE.JSON, JSON.stringify(serializeArray(value))]; } default: { - if(typeof value === 'object') { + if (typeof value === 'object') { return [PROP_TYPE.Value, serializeObject(value)]; } else { return [PROP_TYPE.Value, value]; diff --git a/scripts/cmd/prebuild.js b/scripts/cmd/prebuild.js index 7f8733cfe7a5..4a67babe294d 100644 --- a/scripts/cmd/prebuild.js +++ b/scripts/cmd/prebuild.js @@ -7,7 +7,7 @@ import { pathToFileURL, fileURLToPath } from 'url'; export default async function prebuild(...args) { let buildToString = args.indexOf('--to-string'); - if(buildToString !== -1) { + if (buildToString !== -1) { args.splice(buildToString, 1); buildToString = true; } @@ -33,10 +33,10 @@ export default async function prebuild(...args) { const tscode = await fs.promises.readFile(filepath, 'utf-8'); const esbuildresult = await esbuild.transform(tscode, { loader: 'ts', - minify: true + minify: true, }); const rootURL = new URL('../../', import.meta.url); - const rel = path.relative(fileURLToPath(rootURL), filepath) + const rel = path.relative(fileURLToPath(rootURL), filepath); const mod = `/** * This file is prebuilt from ${rel} * Do not edit this directly, but instead edit that file and rerun the prebuild