Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(html): Export HTML components as default export so their type is valid JSX #7296

Merged
merged 3 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/khaki-onions-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix HTML component type causing an error when imported in the editor
4 changes: 2 additions & 2 deletions packages/astro/env.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// <reference path="./client.d.ts" />
/// <reference path="./client-base.d.ts" />

// Caution! The types here are only available inside Astro files (injected automatically by our language server)
// As such, if the typings you're trying to add should be available inside ex: React components, they should instead
Expand All @@ -18,6 +18,6 @@ declare const Astro: Readonly<Astro>;
declare const Fragment: any;

declare module '*.html' {
const Component: { render(opts: { slots: Record<string, string> }): string };
const Component: (opts?: { slots?: Record<string, string> }) => string;
export default Component;
}
6 changes: 3 additions & 3 deletions packages/astro/src/runtime/server/render/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from './astro/index.js';
import { Fragment, Renderer, stringifyChunk } from './common.js';
import { componentIsHTMLElement, renderHTMLElement } from './dom.js';
import { renderSlots, renderSlotToString, type ComponentSlots } from './slot.js';
import { renderSlotToString, renderSlots, type ComponentSlots } from './slot.js';
import { formatList, internalSpreadAttributes, renderElement, voidElementNames } from './util.js';

const rendererAliases = new Map([['solid', 'solid-js']]);
Expand Down Expand Up @@ -51,7 +51,7 @@ function isFragmentComponent(Component: unknown) {
}

function isHTMLComponent(Component: unknown) {
return Component && typeof Component === 'object' && (Component as any)['astro:html'];
return Component && (Component as any)['astro:html'] === true;
}

const ASTRO_SLOT_EXP = /\<\/?astro-slot\b[^>]*>/g;
Expand Down Expand Up @@ -364,7 +364,7 @@ async function renderHTMLComponent(
slots: any = {}
) {
const { slotInstructions, children } = await renderSlots(result, slots);
const html = (Component as any).render({ slots: children });
const html = (Component as any)({ slots: children });
const hydrationHtml = slotInstructions
? slotInstructions.map((instr) => stringifyChunk(result, instr)).join('')
: '';
Expand Down
6 changes: 2 additions & 4 deletions packages/astro/src/vite-plugin-html/transform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ export async function transform(code: string, id: string) {

const vfile = new VFile({ value: code, path: id });
await parser.process(vfile);
s.prepend(
`export default {\n\t"astro:html": true,\n\trender({ slots: ${SLOT_PREFIX} }) {\n\t\treturn \``
);
s.append('`\n\t}\n}');
s.prepend(`function render({ slots: ${SLOT_PREFIX} }) {\n\t\treturn \``);
s.append('`\n\t}\nrender["astro:html"] = true;\nexport default render;');

return {
code: s.toString(),
Expand Down