[ci] format
This commit is contained in:
parent
f5d4ebf0e2
commit
6809a0d289
3 changed files with 32 additions and 17 deletions
|
@ -1,8 +1,8 @@
|
||||||
import type { PluginMetadata } from '../vite-plugin-astro/types';
|
import type { PluginObj } from '@babel/core';
|
||||||
import type { PluginObj, NodePath } from '@babel/core';
|
|
||||||
import * as t from '@babel/types';
|
import * as t from '@babel/types';
|
||||||
import { pathToFileURL } from 'node:url'
|
import { pathToFileURL } from 'node:url';
|
||||||
import { ClientOnlyPlaceholder } from '../runtime/server/index.js';
|
import { ClientOnlyPlaceholder } from '../runtime/server/index.js';
|
||||||
|
import type { PluginMetadata } from '../vite-plugin-astro/types';
|
||||||
|
|
||||||
function isComponent(tagName: string) {
|
function isComponent(tagName: string) {
|
||||||
return (
|
return (
|
||||||
|
@ -24,7 +24,7 @@ function hasClientDirective(node: t.JSXElement) {
|
||||||
function isClientOnlyComponent(node: t.JSXElement) {
|
function isClientOnlyComponent(node: t.JSXElement) {
|
||||||
for (const attr of node.openingElement.attributes) {
|
for (const attr of node.openingElement.attributes) {
|
||||||
if (attr.type === 'JSXAttribute' && attr.name.type === 'JSXNamespacedName') {
|
if (attr.type === 'JSXAttribute' && attr.name.type === 'JSXNamespacedName') {
|
||||||
return jsxAttributeToString(attr) === 'client:only';
|
return jsxAttributeToString(attr) === 'client:only';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
@ -52,7 +52,10 @@ function jsxAttributeToString(attr: t.JSXAttribute): string {
|
||||||
return `${attr.name.name}`;
|
return `${attr.name.name}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function addClientMetadata(node: t.JSXElement, meta: { resolvedPath: string; path: string; name: string }) {
|
function addClientMetadata(
|
||||||
|
node: t.JSXElement,
|
||||||
|
meta: { resolvedPath: string; path: string; name: string }
|
||||||
|
) {
|
||||||
const existingAttributes = node.openingElement.attributes.map((attr) =>
|
const existingAttributes = node.openingElement.attributes.map((attr) =>
|
||||||
t.isJSXAttribute(attr) ? jsxAttributeToString(attr) : null
|
t.isJSXAttribute(attr) ? jsxAttributeToString(attr) : null
|
||||||
);
|
);
|
||||||
|
@ -81,11 +84,17 @@ function addClientMetadata(node: t.JSXElement, meta: { resolvedPath: string; pat
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function addClientOnlyMetadata(node: t.JSXElement, meta: { resolvedPath: string; path: string; name: string }) {
|
function addClientOnlyMetadata(
|
||||||
|
node: t.JSXElement,
|
||||||
|
meta: { resolvedPath: string; path: string; name: string }
|
||||||
|
) {
|
||||||
const tagName = getTagName(node);
|
const tagName = getTagName(node);
|
||||||
node.openingElement = t.jsxOpeningElement(t.jsxIdentifier(ClientOnlyPlaceholder), node.openingElement.attributes)
|
node.openingElement = t.jsxOpeningElement(
|
||||||
|
t.jsxIdentifier(ClientOnlyPlaceholder),
|
||||||
|
node.openingElement.attributes
|
||||||
|
);
|
||||||
if (node.closingElement) {
|
if (node.closingElement) {
|
||||||
node.closingElement = t.jsxClosingElement(t.jsxIdentifier(ClientOnlyPlaceholder))
|
node.closingElement = t.jsxClosingElement(t.jsxIdentifier(ClientOnlyPlaceholder));
|
||||||
}
|
}
|
||||||
const existingAttributes = node.openingElement.attributes.map((attr) =>
|
const existingAttributes = node.openingElement.attributes.map((attr) =>
|
||||||
t.isJSXAttribute(attr) ? jsxAttributeToString(attr) : null
|
t.isJSXAttribute(attr) ? jsxAttributeToString(attr) : null
|
||||||
|
@ -132,7 +141,7 @@ export default function astroJSX(): PluginObj {
|
||||||
clientOnlyComponents: [],
|
clientOnlyComponents: [],
|
||||||
hydratedComponents: [],
|
hydratedComponents: [],
|
||||||
scripts: [],
|
scripts: [],
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
path.node.body.splice(
|
path.node.body.splice(
|
||||||
0,
|
0,
|
||||||
|
@ -142,7 +151,7 @@ export default function astroJSX(): PluginObj {
|
||||||
t.stringLiteral('astro/jsx-runtime')
|
t.stringLiteral('astro/jsx-runtime')
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
},
|
||||||
},
|
},
|
||||||
ImportDeclaration(path, state) {
|
ImportDeclaration(path, state) {
|
||||||
const source = path.node.source.value;
|
const source = path.node.source.value;
|
||||||
|
@ -210,8 +219,8 @@ export default function astroJSX(): PluginObj {
|
||||||
(state.file.metadata as PluginMetadata).astro.clientOnlyComponents.push({
|
(state.file.metadata as PluginMetadata).astro.clientOnlyComponents.push({
|
||||||
exportName: meta.name,
|
exportName: meta.name,
|
||||||
specifier: meta.name,
|
specifier: meta.name,
|
||||||
resolvedPath
|
resolvedPath,
|
||||||
})
|
});
|
||||||
|
|
||||||
meta.resolvedPath = resolvedPath;
|
meta.resolvedPath = resolvedPath;
|
||||||
addClientOnlyMetadata(parentNode, meta);
|
addClientOnlyMetadata(parentNode, meta);
|
||||||
|
@ -219,8 +228,8 @@ export default function astroJSX(): PluginObj {
|
||||||
(state.file.metadata as PluginMetadata).astro.hydratedComponents.push({
|
(state.file.metadata as PluginMetadata).astro.hydratedComponents.push({
|
||||||
exportName: meta.name,
|
exportName: meta.name,
|
||||||
specifier: meta.name,
|
specifier: meta.name,
|
||||||
resolvedPath
|
resolvedPath,
|
||||||
})
|
});
|
||||||
|
|
||||||
meta.resolvedPath = resolvedPath;
|
meta.resolvedPath = resolvedPath;
|
||||||
addClientMetadata(parentNode, meta);
|
addClientMetadata(parentNode, meta);
|
||||||
|
|
|
@ -2,8 +2,8 @@
|
||||||
import { SSRResult } from '../../@types/astro.js';
|
import { SSRResult } from '../../@types/astro.js';
|
||||||
import { AstroJSX, isVNode } from '../../jsx-runtime/index.js';
|
import { AstroJSX, isVNode } from '../../jsx-runtime/index.js';
|
||||||
import {
|
import {
|
||||||
escapeHTML,
|
|
||||||
ClientOnlyPlaceholder,
|
ClientOnlyPlaceholder,
|
||||||
|
escapeHTML,
|
||||||
HTMLString,
|
HTMLString,
|
||||||
markHTMLString,
|
markHTMLString,
|
||||||
renderComponent,
|
renderComponent,
|
||||||
|
@ -108,7 +108,13 @@ export async function renderJSX(result: SSRResult, vnode: any): Promise<any> {
|
||||||
slots
|
slots
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
output = await renderComponent(result, typeof vnode.type === 'function' ? vnode.type.name : vnode.type, vnode.type, props, slots);
|
output = await renderComponent(
|
||||||
|
result,
|
||||||
|
typeof vnode.type === 'function' ? vnode.type.name : vnode.type,
|
||||||
|
vnode.type,
|
||||||
|
props,
|
||||||
|
slots
|
||||||
|
);
|
||||||
}
|
}
|
||||||
return markHTMLString(output);
|
return markHTMLString(output);
|
||||||
}
|
}
|
||||||
|
|
|
@ -87,7 +87,7 @@ async function transformJSX({
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
code: result.code || '',
|
code: result.code || '',
|
||||||
map: result.map,
|
map: result.map,
|
||||||
|
|
Loading…
Reference in a new issue