astro 1.6.13 → 1.6.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/dist/@types/astro.d.ts +18 -2
  2. package/dist/core/app/index.js +4 -3
  3. package/dist/core/build/graph.d.ts +1 -1
  4. package/dist/core/build/graph.js +5 -3
  5. package/dist/core/build/vite-plugin-ssr.js +5 -1
  6. package/dist/core/compile/compile.d.ts +2 -1
  7. package/dist/core/compile/compile.js +2 -0
  8. package/dist/core/constants.js +1 -1
  9. package/dist/core/create-vite.js +4 -2
  10. package/dist/core/dev/dev.js +1 -1
  11. package/dist/core/errors/dev/utils.js +1 -1
  12. package/dist/core/messages.js +2 -2
  13. package/dist/core/render/context.d.ts +2 -1
  14. package/dist/core/render/core.js +1 -0
  15. package/dist/core/render/dev/head.d.ts +3 -0
  16. package/dist/core/render/dev/head.js +26 -0
  17. package/dist/core/render/dev/index.d.ts +1 -25
  18. package/dist/core/render/dev/index.js +5 -2
  19. package/dist/core/render/result.d.ts +1 -0
  20. package/dist/core/render/result.js +23 -33
  21. package/dist/jsx/babel.js +2 -1
  22. package/dist/runtime/server/astro-component.d.ts +2 -0
  23. package/dist/runtime/server/astro-component.js +20 -0
  24. package/dist/runtime/server/index.d.ts +3 -4
  25. package/dist/runtime/server/index.js +10 -8
  26. package/dist/runtime/server/jsx.js +3 -3
  27. package/dist/runtime/server/render/any.js +9 -3
  28. package/dist/runtime/server/render/astro/factory.d.ts +13 -0
  29. package/dist/runtime/server/render/astro/factory.js +31 -0
  30. package/dist/runtime/server/render/astro/head-and-content.d.ts +10 -0
  31. package/dist/runtime/server/render/astro/head-and-content.js +15 -0
  32. package/dist/runtime/server/render/astro/index.d.ts +6 -0
  33. package/dist/runtime/server/render/astro/index.js +19 -0
  34. package/dist/runtime/server/render/astro/instance.d.ts +18 -0
  35. package/dist/runtime/server/render/astro/instance.js +62 -0
  36. package/dist/runtime/server/render/astro/render-template.d.ts +16 -0
  37. package/dist/runtime/server/render/astro/render-template.js +65 -0
  38. package/dist/runtime/server/render/component.d.ts +4 -2
  39. package/dist/runtime/server/render/component.js +52 -41
  40. package/dist/runtime/server/render/head.d.ts +3 -2
  41. package/dist/runtime/server/render/head.js +21 -4
  42. package/dist/runtime/server/render/index.d.ts +4 -7
  43. package/dist/runtime/server/render/index.js +13 -4
  44. package/dist/runtime/server/render/page.js +32 -6
  45. package/dist/runtime/server/render/stylesheet.d.ts +7 -0
  46. package/dist/runtime/server/render/stylesheet.js +30 -0
  47. package/dist/vite-plugin-astro/index.d.ts +3 -1
  48. package/dist/vite-plugin-astro/index.js +8 -2
  49. package/dist/vite-plugin-astro/metadata.d.ts +3 -0
  50. package/dist/vite-plugin-astro/metadata.js +10 -0
  51. package/dist/vite-plugin-astro/types.d.ts +2 -0
  52. package/dist/vite-plugin-head-propagation/index.d.ts +11 -0
  53. package/dist/vite-plugin-head-propagation/index.js +42 -0
  54. package/dist/vite-plugin-load-fallback/index.js +1 -1
  55. package/dist/vite-plugin-markdown/index.js +2 -1
  56. package/dist/vite-plugin-markdown-legacy/index.js +4 -2
  57. package/package.json +5 -5
  58. package/dist/runtime/server/render/astro.d.ts +0 -18
  59. package/dist/runtime/server/render/astro.js +0 -105
@@ -0,0 +1,31 @@
1
+ import { HTMLParts } from "../common.js";
2
+ import { isHeadAndContent } from "./head-and-content.js";
3
+ import { renderAstroTemplateResult } from "./render-template.js";
4
+ function isAstroComponentFactory(obj) {
5
+ return obj == null ? false : obj.isAstroComponentFactory === true;
6
+ }
7
+ async function renderToString(result, componentFactory, props, children) {
8
+ const factoryResult = await componentFactory(result, props, children);
9
+ if (factoryResult instanceof Response) {
10
+ const response = factoryResult;
11
+ throw response;
12
+ }
13
+ let parts = new HTMLParts();
14
+ const templateResult = isHeadAndContent(factoryResult) ? factoryResult.content : factoryResult;
15
+ for await (const chunk of renderAstroTemplateResult(templateResult)) {
16
+ parts.append(chunk, result);
17
+ }
18
+ return parts.toString();
19
+ }
20
+ function isAPropagatingComponent(result, factory) {
21
+ let hint = factory.propagation || "none";
22
+ if (factory.moduleId && result.propagation.has(factory.moduleId) && hint === "none") {
23
+ hint = result.propagation.get(factory.moduleId);
24
+ }
25
+ return hint === "in-tree" || hint === "self";
26
+ }
27
+ export {
28
+ isAPropagatingComponent,
29
+ isAstroComponentFactory,
30
+ renderToString
31
+ };
@@ -0,0 +1,10 @@
1
+ import type { RenderTemplateResult } from './render-template';
2
+ declare const headAndContentSym: unique symbol;
3
+ export declare type HeadAndContent = {
4
+ [headAndContentSym]: true;
5
+ head: string | RenderTemplateResult;
6
+ content: RenderTemplateResult;
7
+ };
8
+ export declare function isHeadAndContent(obj: unknown): obj is HeadAndContent;
9
+ export declare function createHeadAndContent(head: string | RenderTemplateResult, content: RenderTemplateResult): HeadAndContent;
10
+ export {};
@@ -0,0 +1,15 @@
1
+ const headAndContentSym = Symbol.for("astro.headAndContent");
2
+ function isHeadAndContent(obj) {
3
+ return typeof obj === "object" && !!obj[headAndContentSym];
4
+ }
5
+ function createHeadAndContent(head, content) {
6
+ return {
7
+ [headAndContentSym]: true,
8
+ head,
9
+ content
10
+ };
11
+ }
12
+ export {
13
+ createHeadAndContent,
14
+ isHeadAndContent
15
+ };
@@ -0,0 +1,6 @@
1
+ export type { AstroComponentFactory } from './factory';
2
+ export { isAstroComponentFactory, renderToString } from './factory.js';
3
+ export { createHeadAndContent, isHeadAndContent } from './head-and-content.js';
4
+ export type { AstroComponentInstance } from './instance';
5
+ export { createAstroComponentInstance, isAstroComponentInstance } from './instance.js';
6
+ export { isRenderTemplateResult, renderAstroTemplateResult, renderTemplate, } from './render-template.js';
@@ -0,0 +1,19 @@
1
+ import { isAstroComponentFactory, renderToString } from "./factory.js";
2
+ import { createHeadAndContent, isHeadAndContent } from "./head-and-content.js";
3
+ import { createAstroComponentInstance, isAstroComponentInstance } from "./instance.js";
4
+ import {
5
+ isRenderTemplateResult,
6
+ renderAstroTemplateResult,
7
+ renderTemplate
8
+ } from "./render-template.js";
9
+ export {
10
+ createAstroComponentInstance,
11
+ createHeadAndContent,
12
+ isAstroComponentFactory,
13
+ isAstroComponentInstance,
14
+ isHeadAndContent,
15
+ isRenderTemplateResult,
16
+ renderAstroTemplateResult,
17
+ renderTemplate,
18
+ renderToString
19
+ };
@@ -0,0 +1,18 @@
1
+ import type { SSRResult } from '../../../../@types/astro';
2
+ import type { AstroComponentFactory, AstroFactoryReturnValue } from './factory.js';
3
+ declare type ComponentProps = Record<string | number, any>;
4
+ declare const astroComponentInstanceSym: unique symbol;
5
+ export declare class AstroComponentInstance {
6
+ [astroComponentInstanceSym]: boolean;
7
+ private readonly result;
8
+ private readonly props;
9
+ private readonly slots;
10
+ private readonly factory;
11
+ private returnValue;
12
+ constructor(result: SSRResult, props: ComponentProps, slots: any, factory: AstroComponentFactory);
13
+ init(): Promise<AstroFactoryReturnValue>;
14
+ render(): AsyncGenerator<any, void, undefined>;
15
+ }
16
+ export declare function createAstroComponentInstance(result: SSRResult, displayName: string, factory: AstroComponentFactory, props: ComponentProps, slots?: any): AstroComponentInstance;
17
+ export declare function isAstroComponentInstance(obj: unknown): obj is AstroComponentInstance;
18
+ export {};
@@ -0,0 +1,62 @@
1
+ var _a;
2
+ import { HydrationDirectiveProps } from "../../hydration.js";
3
+ import { isPromise } from "../../util.js";
4
+ import { renderChild } from "../any.js";
5
+ import { isAPropagatingComponent } from "./factory.js";
6
+ import { isHeadAndContent } from "./head-and-content.js";
7
+ const astroComponentInstanceSym = Symbol.for("astro.componentInstance");
8
+ class AstroComponentInstance {
9
+ constructor(result, props, slots, factory) {
10
+ this[_a] = true;
11
+ this.result = result;
12
+ this.props = props;
13
+ this.slots = slots;
14
+ this.factory = factory;
15
+ }
16
+ async init() {
17
+ this.returnValue = this.factory(this.result, this.props, this.slots);
18
+ return this.returnValue;
19
+ }
20
+ async *render() {
21
+ if (this.returnValue === void 0) {
22
+ await this.init();
23
+ }
24
+ let value = this.returnValue;
25
+ if (isPromise(value)) {
26
+ value = await value;
27
+ }
28
+ if (isHeadAndContent(value)) {
29
+ yield* value.content;
30
+ } else {
31
+ yield* renderChild(value);
32
+ }
33
+ }
34
+ }
35
+ _a = astroComponentInstanceSym;
36
+ function validateComponentProps(props, displayName) {
37
+ if (props != null) {
38
+ for (const prop of Object.keys(props)) {
39
+ if (HydrationDirectiveProps.has(prop)) {
40
+ console.warn(
41
+ `You are attempting to render <${displayName} ${prop} />, but ${displayName} is an Astro component. Astro components do not render in the client and should not have a hydration directive. Please use a framework component for client rendering.`
42
+ );
43
+ }
44
+ }
45
+ }
46
+ }
47
+ function createAstroComponentInstance(result, displayName, factory, props, slots = {}) {
48
+ validateComponentProps(props, displayName);
49
+ const instance = new AstroComponentInstance(result, props, slots, factory);
50
+ if (isAPropagatingComponent(result, factory) && !result.propagators.has(factory)) {
51
+ result.propagators.set(factory, instance);
52
+ }
53
+ return instance;
54
+ }
55
+ function isAstroComponentInstance(obj) {
56
+ return typeof obj === "object" && !!obj[astroComponentInstanceSym];
57
+ }
58
+ export {
59
+ AstroComponentInstance,
60
+ createAstroComponentInstance,
61
+ isAstroComponentInstance
62
+ };
@@ -0,0 +1,16 @@
1
+ import type { RenderInstruction } from '../types';
2
+ import { HTMLBytes } from '../../escape.js';
3
+ declare const renderTemplateResultSym: unique symbol;
4
+ export declare class RenderTemplateResult {
5
+ [renderTemplateResultSym]: boolean;
6
+ private htmlParts;
7
+ private expressions;
8
+ private error;
9
+ constructor(htmlParts: TemplateStringsArray, expressions: unknown[]);
10
+ get [Symbol.toStringTag](): string;
11
+ [Symbol.asyncIterator](): AsyncGenerator<any, void, undefined>;
12
+ }
13
+ export declare function isRenderTemplateResult(obj: unknown): obj is RenderTemplateResult;
14
+ export declare function renderAstroTemplateResult(component: RenderTemplateResult): AsyncIterable<string | HTMLBytes | RenderInstruction>;
15
+ export declare function renderTemplate(htmlParts: TemplateStringsArray, ...expressions: any[]): RenderTemplateResult;
16
+ export {};
@@ -0,0 +1,65 @@
1
+ var _a;
2
+ import { markHTMLString } from "../../escape.js";
3
+ import { isPromise } from "../../util.js";
4
+ import { renderChild } from "../any.js";
5
+ const renderTemplateResultSym = Symbol.for("astro.renderTemplateResult");
6
+ class RenderTemplateResult {
7
+ constructor(htmlParts, expressions) {
8
+ this[_a] = true;
9
+ this.htmlParts = htmlParts;
10
+ this.error = void 0;
11
+ this.expressions = expressions.map((expression) => {
12
+ if (isPromise(expression)) {
13
+ return Promise.resolve(expression).catch((err) => {
14
+ if (!this.error) {
15
+ this.error = err;
16
+ throw err;
17
+ }
18
+ });
19
+ }
20
+ return expression;
21
+ });
22
+ }
23
+ get [(_a = renderTemplateResultSym, Symbol.toStringTag)]() {
24
+ return "AstroComponent";
25
+ }
26
+ async *[Symbol.asyncIterator]() {
27
+ const { htmlParts, expressions } = this;
28
+ for (let i = 0; i < htmlParts.length; i++) {
29
+ const html = htmlParts[i];
30
+ const expression = expressions[i];
31
+ yield markHTMLString(html);
32
+ yield* renderChild(expression);
33
+ }
34
+ }
35
+ }
36
+ function isRenderTemplateResult(obj) {
37
+ return typeof obj === "object" && !!obj[renderTemplateResultSym];
38
+ }
39
+ async function* renderAstroTemplateResult(component) {
40
+ for await (const value of component) {
41
+ if (value || value === 0) {
42
+ for await (const chunk of renderChild(value)) {
43
+ switch (chunk.type) {
44
+ case "directive": {
45
+ yield chunk;
46
+ break;
47
+ }
48
+ default: {
49
+ yield markHTMLString(chunk);
50
+ break;
51
+ }
52
+ }
53
+ }
54
+ }
55
+ }
56
+ }
57
+ function renderTemplate(htmlParts, ...expressions) {
58
+ return new RenderTemplateResult(htmlParts, expressions);
59
+ }
60
+ export {
61
+ RenderTemplateResult,
62
+ isRenderTemplateResult,
63
+ renderAstroTemplateResult,
64
+ renderTemplate
65
+ };
@@ -1,5 +1,7 @@
1
- import type { RouteData, SSRResult } from '../../../@types/astro';
1
+ import type { SSRResult } from '../../../@types/astro';
2
2
  import type { RenderInstruction } from './types.js';
3
3
  import { HTMLBytes } from '../escape.js';
4
+ import { type AstroComponentInstance } from './astro/index.js';
4
5
  export declare type ComponentIterable = AsyncIterable<string | HTMLBytes | RenderInstruction>;
5
- export declare function renderComponent(result: SSRResult, displayName: string, Component: unknown, _props: Record<string | number, any>, slots?: any, route?: RouteData | undefined): Promise<ComponentIterable>;
6
+ export declare function renderComponent(result: SSRResult, displayName: string, Component: unknown, props: Record<string | number, any>, slots?: any): Promise<ComponentIterable> | ComponentIterable | AstroComponentInstance;
7
+ export declare function renderComponentToIterable(result: SSRResult, displayName: string, Component: unknown, props: Record<string | number, any>, slots?: any): Promise<ComponentIterable> | ComponentIterable;
@@ -3,12 +3,14 @@ import { markHTMLString } from "../escape.js";
3
3
  import { extractDirectives, generateHydrateScript } from "../hydration.js";
4
4
  import { serializeProps } from "../serialize.js";
5
5
  import { shorthash } from "../shorthash.js";
6
+ import { isPromise } from "../util.js";
6
7
  import {
8
+ createAstroComponentInstance,
7
9
  isAstroComponentFactory,
8
- renderAstroComponent,
9
- renderTemplate,
10
- renderToIterable
11
- } from "./astro.js";
10
+ isAstroComponentInstance,
11
+ renderAstroTemplateResult,
12
+ renderTemplate
13
+ } from "./astro/index.js";
12
14
  import { Fragment, Renderer, stringifyChunk } from "./common.js";
13
15
  import { componentIsHTMLElement, renderHTMLElement } from "./dom.js";
14
16
  import { renderSlot, renderSlots } from "./slot.js";
@@ -34,43 +36,14 @@ function guessRenderers(componentUrl) {
34
36
  ];
35
37
  }
36
38
  }
37
- function getComponentType(Component) {
38
- if (Component === Fragment) {
39
- return "fragment";
40
- }
41
- if (Component && typeof Component === "object" && Component["astro:html"]) {
42
- return "html";
43
- }
44
- if (isAstroComponentFactory(Component)) {
45
- return "astro-factory";
46
- }
47
- return "unknown";
39
+ function isFragmentComponent(Component) {
40
+ return Component === Fragment;
41
+ }
42
+ function isHTMLComponent(Component) {
43
+ return Component && typeof Component === "object" && Component["astro:html"];
48
44
  }
49
- async function renderComponent(result, displayName, Component, _props, slots = {}, route) {
45
+ async function renderFrameworkComponent(result, displayName, Component, _props, slots = {}) {
50
46
  var _a, _b;
51
- Component = await Component ?? Component;
52
- switch (getComponentType(Component)) {
53
- case "fragment": {
54
- const children2 = await renderSlot(result, slots == null ? void 0 : slots.default);
55
- if (children2 == null) {
56
- return children2;
57
- }
58
- return markHTMLString(children2);
59
- }
60
- case "html": {
61
- const { slotInstructions: slotInstructions2, children: children2 } = await renderSlots(result, slots);
62
- const html2 = Component.render({ slots: children2 });
63
- const hydrationHtml = slotInstructions2 ? slotInstructions2.map((instr) => stringifyChunk(result, instr)).join("") : "";
64
- return markHTMLString(hydrationHtml + html2);
65
- }
66
- case "astro-factory": {
67
- async function* renderAstroComponentInline() {
68
- let iterable = await renderToIterable(result, Component, displayName, _props, slots);
69
- yield* iterable;
70
- }
71
- return renderAstroComponentInline();
72
- }
73
- }
74
47
  if (!Component && !_props["client:only"]) {
75
48
  throw new Error(
76
49
  `Unable to render ${displayName} because it is ${Component}!
@@ -215,7 +188,7 @@ If you're still stuck, please open an issue on GitHub or join us at https://astr
215
188
  }
216
189
  if (!html && typeof Component === "string") {
217
190
  const childSlots = Object.values(children).join("");
218
- const iterable = renderAstroComponent(
191
+ const iterable = renderAstroTemplateResult(
219
192
  await renderTemplate`<${Component}${internalSpreadAttributes(props)}${markHTMLString(
220
193
  childSlots === "" && voidElementNames.test(Component) ? `/>` : `>${childSlots}</${Component}>`
221
194
  )}`
@@ -277,6 +250,44 @@ ${serializeProps(
277
250
  }
278
251
  return renderAll();
279
252
  }
253
+ async function renderFragmentComponent(result, slots = {}) {
254
+ const children = await renderSlot(result, slots == null ? void 0 : slots.default);
255
+ if (children == null) {
256
+ return children;
257
+ }
258
+ return markHTMLString(children);
259
+ }
260
+ async function renderHTMLComponent(result, Component, _props, slots = {}) {
261
+ const { slotInstructions, children } = await renderSlots(result, slots);
262
+ const html = Component.render({ slots: children });
263
+ const hydrationHtml = slotInstructions ? slotInstructions.map((instr) => stringifyChunk(result, instr)).join("") : "";
264
+ return markHTMLString(hydrationHtml + html);
265
+ }
266
+ function renderComponent(result, displayName, Component, props, slots = {}) {
267
+ if (isPromise(Component)) {
268
+ return Promise.resolve(Component).then((Unwrapped) => {
269
+ return renderComponent(result, displayName, Unwrapped, props, slots);
270
+ });
271
+ }
272
+ if (isFragmentComponent(Component)) {
273
+ return renderFragmentComponent(result, slots);
274
+ }
275
+ if (isHTMLComponent(Component)) {
276
+ return renderHTMLComponent(result, Component, props, slots);
277
+ }
278
+ if (isAstroComponentFactory(Component)) {
279
+ return createAstroComponentInstance(result, displayName, Component, props, slots);
280
+ }
281
+ return renderFrameworkComponent(result, displayName, Component, props, slots);
282
+ }
283
+ function renderComponentToIterable(result, displayName, Component, props, slots = {}) {
284
+ const renderResult = renderComponent(result, displayName, Component, props, slots);
285
+ if (isAstroComponentInstance(renderResult)) {
286
+ return renderResult.render();
287
+ }
288
+ return renderResult;
289
+ }
280
290
  export {
281
- renderComponent
291
+ renderComponent,
292
+ renderComponentToIterable
282
293
  };
@@ -1,3 +1,4 @@
1
1
  import type { SSRResult } from '../../../@types/astro';
2
- export declare function renderHead(result: SSRResult): Promise<string>;
3
- export declare function maybeRenderHead(result: SSRResult): AsyncIterable<string>;
2
+ export declare function createRenderHead(result: SSRResult): () => any;
3
+ export declare const renderHead: typeof createRenderHead;
4
+ export declare function maybeRenderHead(result: SSRResult): AsyncGenerator<any, void, unknown>;
@@ -1,27 +1,44 @@
1
1
  import { markHTMLString } from "../escape.js";
2
+ import { renderChild } from "./any.js";
2
3
  import { renderElement } from "./util.js";
3
4
  const uniqueElements = (item, index, all) => {
4
5
  const props = JSON.stringify(item.props);
5
6
  const children = item.children;
6
7
  return index === all.findIndex((i) => JSON.stringify(i.props) === props && i.children == children);
7
8
  };
8
- function renderHead(result) {
9
- result._metadata.hasRenderedHead = true;
9
+ async function* renderExtraHead(result, base) {
10
+ yield base;
11
+ for (const part of result.extraHead) {
12
+ yield* renderChild(part);
13
+ }
14
+ }
15
+ function renderAllHeadContent(result) {
10
16
  const styles = Array.from(result.styles).filter(uniqueElements).map((style) => renderElement("style", style));
11
17
  result.styles.clear();
12
18
  const scripts = Array.from(result.scripts).filter(uniqueElements).map((script, i) => {
13
19
  return renderElement("script", script, false);
14
20
  });
15
21
  const links = Array.from(result.links).filter(uniqueElements).map((link) => renderElement("link", link, false));
16
- return markHTMLString(links.join("\n") + styles.join("\n") + scripts.join("\n"));
22
+ const baseHeadContent = markHTMLString(links.join("\n") + styles.join("\n") + scripts.join("\n"));
23
+ if (result.extraHead.length > 0) {
24
+ return renderExtraHead(result, baseHeadContent);
25
+ } else {
26
+ return baseHeadContent;
27
+ }
28
+ }
29
+ function createRenderHead(result) {
30
+ result._metadata.hasRenderedHead = true;
31
+ return renderAllHeadContent.bind(null, result);
17
32
  }
33
+ const renderHead = createRenderHead;
18
34
  async function* maybeRenderHead(result) {
19
35
  if (result._metadata.hasRenderedHead) {
20
36
  return;
21
37
  }
22
- yield renderHead(result);
38
+ yield createRenderHead(result)();
23
39
  }
24
40
  export {
41
+ createRenderHead,
25
42
  maybeRenderHead,
26
43
  renderHead
27
44
  };
@@ -1,14 +1,11 @@
1
- import { renderTemplate } from './astro.js';
2
- export { renderAstroComponent, renderTemplate, renderToString } from './astro.js';
1
+ export type { AstroComponentFactory, AstroComponentInstance } from './astro/index';
2
+ export { createHeadAndContent, renderAstroTemplateResult, renderTemplate, renderToString, } from './astro/index.js';
3
3
  export { Fragment, Renderer, stringifyChunk } from './common.js';
4
- export { renderComponent } from './component.js';
4
+ export { renderComponent, renderComponentToIterable } from './component.js';
5
5
  export { renderHTMLElement } from './dom.js';
6
6
  export { maybeRenderHead, renderHead } from './head.js';
7
7
  export { renderPage } from './page.js';
8
8
  export { renderSlot } from './slot.js';
9
+ export { renderUniqueStylesheet } from './stylesheet.js';
9
10
  export type { RenderInstruction } from './types';
10
11
  export { addAttribute, defineScriptVars, voidElementNames } from './util.js';
11
- export interface AstroComponentFactory {
12
- (result: any, props: any, slots: any): ReturnType<typeof renderTemplate> | Response;
13
- isAstroComponentFactory?: boolean;
14
- }
@@ -1,25 +1,34 @@
1
- import { renderAstroComponent, renderTemplate as renderTemplate2, renderToString } from "./astro.js";
1
+ import {
2
+ createHeadAndContent,
3
+ renderAstroTemplateResult,
4
+ renderTemplate,
5
+ renderToString
6
+ } from "./astro/index.js";
2
7
  import { Fragment, Renderer, stringifyChunk } from "./common.js";
3
- import { renderComponent } from "./component.js";
8
+ import { renderComponent, renderComponentToIterable } from "./component.js";
4
9
  import { renderHTMLElement } from "./dom.js";
5
10
  import { maybeRenderHead, renderHead } from "./head.js";
6
11
  import { renderPage } from "./page.js";
7
12
  import { renderSlot } from "./slot.js";
13
+ import { renderUniqueStylesheet } from "./stylesheet.js";
8
14
  import { addAttribute, defineScriptVars, voidElementNames } from "./util.js";
9
15
  export {
10
16
  Fragment,
11
17
  Renderer,
12
18
  addAttribute,
19
+ createHeadAndContent,
13
20
  defineScriptVars,
14
21
  maybeRenderHead,
15
- renderAstroComponent,
22
+ renderAstroTemplateResult,
16
23
  renderComponent,
24
+ renderComponentToIterable,
17
25
  renderHTMLElement,
18
26
  renderHead,
19
27
  renderPage,
20
28
  renderSlot,
21
- renderTemplate2 as renderTemplate,
29
+ renderTemplate,
22
30
  renderToString,
31
+ renderUniqueStylesheet,
23
32
  stringifyChunk,
24
33
  voidElementNames
25
34
  };
@@ -1,7 +1,13 @@
1
1
  import { AstroError, AstroErrorData } from "../../../core/errors/index.js";
2
2
  import { isHTMLString } from "../escape.js";
3
3
  import { createResponse } from "../response.js";
4
- import { isAstroComponent, isAstroComponentFactory, renderAstroComponent } from "./astro.js";
4
+ import {
5
+ isAstroComponentFactory,
6
+ isAstroComponentInstance,
7
+ isHeadAndContent,
8
+ isRenderTemplateResult,
9
+ renderAstroTemplateResult
10
+ } from "./astro/index.js";
5
11
  import { chunkToByteArray, encoder, HTMLParts } from "./common.js";
6
12
  import { renderComponent } from "./component.js";
7
13
  import { maybeRenderHead } from "./head.js";
@@ -28,19 +34,36 @@ async function iterableToHTMLBytes(result, iterable, onDocTypeInjection) {
28
34
  }
29
35
  return parts.toArrayBuffer();
30
36
  }
37
+ async function bufferHeadContent(result) {
38
+ const iterator = result.propagators.values();
39
+ while (true) {
40
+ const { value, done } = iterator.next();
41
+ if (done) {
42
+ break;
43
+ }
44
+ const returnValue = await value.init();
45
+ if (isHeadAndContent(returnValue)) {
46
+ result.extraHead.push(returnValue.head);
47
+ }
48
+ }
49
+ }
31
50
  async function renderPage(result, componentFactory, props, children, streaming, route) {
32
51
  if (!isAstroComponentFactory(componentFactory)) {
33
52
  const pageProps = { ...props ?? {}, "server:root": true };
34
53
  let output;
35
54
  try {
36
- output = await renderComponent(
55
+ const renderResult = await renderComponent(
37
56
  result,
38
57
  componentFactory.name,
39
58
  componentFactory,
40
59
  pageProps,
41
- null,
42
- route
60
+ null
43
61
  );
62
+ if (isAstroComponentInstance(renderResult)) {
63
+ output = renderResult.render();
64
+ } else {
65
+ output = renderResult;
66
+ }
44
67
  } catch (e) {
45
68
  if (AstroError.is(e) && !e.loc) {
46
69
  e.setLocation({
@@ -64,8 +87,11 @@ async function renderPage(result, componentFactory, props, children, streaming,
64
87
  });
65
88
  }
66
89
  const factoryReturnValue = await componentFactory(result, props, children);
67
- if (isAstroComponent(factoryReturnValue)) {
68
- let iterable = renderAstroComponent(factoryReturnValue);
90
+ const factoryIsHeadAndContent = isHeadAndContent(factoryReturnValue);
91
+ if (isRenderTemplateResult(factoryReturnValue) || factoryIsHeadAndContent) {
92
+ await bufferHeadContent(result);
93
+ const templateResult = factoryIsHeadAndContent ? factoryReturnValue.content : factoryReturnValue;
94
+ let iterable = renderAstroTemplateResult(templateResult);
69
95
  let init = result.response;
70
96
  let headers = new Headers(init.headers);
71
97
  let body;
@@ -0,0 +1,7 @@
1
+ import { SSRResult } from '../../../@types/astro';
2
+ export declare function renderStylesheet({ href }: {
3
+ href: string;
4
+ }): any;
5
+ export declare function renderUniqueStylesheet(result: SSRResult, link: {
6
+ href: string;
7
+ }): any;
@@ -0,0 +1,30 @@
1
+ import { markHTMLString } from "../escape.js";
2
+ import { renderElement } from "./util.js";
3
+ const stylesheetRel = "stylesheet";
4
+ function renderStylesheet({ href }) {
5
+ return markHTMLString(
6
+ renderElement(
7
+ "link",
8
+ {
9
+ props: {
10
+ rel: stylesheetRel,
11
+ href
12
+ },
13
+ children: ""
14
+ },
15
+ false
16
+ )
17
+ );
18
+ }
19
+ function renderUniqueStylesheet(result, link) {
20
+ for (const existingLink of result.links) {
21
+ if (existingLink.props.rel === stylesheetRel && existingLink.props.href === link.href) {
22
+ return "";
23
+ }
24
+ }
25
+ return renderStylesheet(link);
26
+ }
27
+ export {
28
+ renderStylesheet,
29
+ renderUniqueStylesheet
30
+ };
@@ -1,10 +1,12 @@
1
1
  import type * as vite from 'vite';
2
2
  import type { AstroSettings } from '../@types/astro';
3
3
  import type { LogOptions } from '../core/logger/core.js';
4
+ import type { PluginMetadata as AstroPluginMetadata } from './types';
5
+ export { getAstroMetadata } from './metadata.js';
6
+ export type { AstroPluginMetadata };
4
7
  interface AstroPluginOptions {
5
8
  settings: AstroSettings;
6
9
  logging: LogOptions;
7
10
  }
8
11
  /** Transform .astro files for Vite */
9
12
  export default function astro({ settings, logging }: AstroPluginOptions): vite.Plugin;
10
- export {};