From affe1a74371b8e7b22f1b477b80aed653307a0d2 Mon Sep 17 00:00:00 2001 From: krolebord Date: Fri, 19 Aug 2022 10:20:19 +0300 Subject: [PATCH 1/3] Add proper support functional components exports --- packages/astro/src/vite-plugin-jsx/tag.ts | 42 +++++++++++++++-------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/packages/astro/src/vite-plugin-jsx/tag.ts b/packages/astro/src/vite-plugin-jsx/tag.ts index 12bb3bcdd5b7..961971812c0b 100644 --- a/packages/astro/src/vite-plugin-jsx/tag.ts +++ b/packages/astro/src/vite-plugin-jsx/tag.ts @@ -55,21 +55,35 @@ export default function tagExportsWithRenderer({ if (node.exportKind === 'type') return; if (node.type === 'ExportAllDeclaration') return; - if (node.type === 'ExportNamedDeclaration') { - if (t.isFunctionDeclaration(node.declaration)) { - if (node.declaration.id?.name) { - const id = node.declaration.id.name; - const tags = state.get('astro:tags') ?? []; - state.set('astro:tags', [...tags, id]); - } + const addTag = (id: string) => { + const tags = state.get('astro:tags') ?? []; + state.set('astro:tags', [...tags, id]); + } + + if (node.type === 'ExportNamedDeclaration' || node.type === 'ExportDefaultDeclaration') { + if (t.isFunctionDeclaration(node.declaration) && node.declaration.id?.name) { + addTag(node.declaration.id.name); } - } else if (node.type === 'ExportDefaultDeclaration') { - if (t.isFunctionDeclaration(node.declaration)) { - if (node.declaration.id?.name) { - const id = node.declaration.id.name; - const tags = state.get('astro:tags') ?? []; - state.set('astro:tags', [...tags, id]); - } + else if (t.isVariableDeclaration(node.declaration)) { + node.declaration.declarations?.forEach(declaration => { + if (t.isArrowFunctionExpression(declaration.init) && t.isIdentifier(declaration.id)) { + addTag(declaration.id.name); + } + }); + } + else if (t.isObjectExpression(node.declaration)) { + node.declaration.properties?.forEach(property => { + if (t.isProperty(property) && t.isIdentifier(property.key)) { + addTag(property.key.name); + } + }); + } + else if (t.isExportNamedDeclaration(node)) { + node.specifiers.forEach(specifier => { + if (t.isExportSpecifier(specifier) && t.isIdentifier(specifier.exported)) { + addTag(specifier.local.name); + } + }); } } }, From 166fde3015648b689f6d84fa902045c4f02f12c6 Mon Sep 17 00:00:00 2001 From: krolebord Date: Fri, 19 Aug 2022 10:39:45 +0300 Subject: [PATCH 2/3] Add changeset --- .changeset/flat-ads-lay.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/flat-ads-lay.md diff --git a/.changeset/flat-ads-lay.md b/.changeset/flat-ads-lay.md new file mode 100644 index 000000000000..a5d2a7e69b7d --- /dev/null +++ b/.changeset/flat-ads-lay.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix warning when using hooks inside the react components not exported as a function declaration From d3f0fcc6bba8b5978dfa3279d28d707eac90a1dd Mon Sep 17 00:00:00 2001 From: krolebord Date: Mon, 22 Aug 2022 22:43:52 +0300 Subject: [PATCH 3/3] Add support for `export default X` --- packages/astro/src/vite-plugin-jsx/tag.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/astro/src/vite-plugin-jsx/tag.ts b/packages/astro/src/vite-plugin-jsx/tag.ts index 961971812c0b..9b3491361c01 100644 --- a/packages/astro/src/vite-plugin-jsx/tag.ts +++ b/packages/astro/src/vite-plugin-jsx/tag.ts @@ -61,7 +61,10 @@ export default function tagExportsWithRenderer({ } if (node.type === 'ExportNamedDeclaration' || node.type === 'ExportDefaultDeclaration') { - if (t.isFunctionDeclaration(node.declaration) && node.declaration.id?.name) { + if (t.isIdentifier(node.declaration)) { + addTag(node.declaration.name); + } + else if (t.isFunctionDeclaration(node.declaration) && node.declaration.id?.name) { addTag(node.declaration.id.name); } else if (t.isVariableDeclaration(node.declaration)) {