2022-03-31 11:51:29 -05:00
|
|
|
import React from 'react';
|
|
|
|
import ReactDOM from 'react-dom/server.js';
|
|
|
|
import StaticHtml from './static-html.js';
|
|
|
|
|
2022-06-23 10:12:46 -05:00
|
|
|
const slotName = (str) => str.trim().replace(/[-_]([a-z])/g, (_, w) => w.toUpperCase());
|
2022-03-31 11:51:29 -05:00
|
|
|
const reactTypeof = Symbol.for('react.element');
|
|
|
|
|
|
|
|
function errorIsComingFromPreactComponent(err) {
|
|
|
|
return (
|
|
|
|
err.message &&
|
|
|
|
(err.message.startsWith("Cannot read property '__H'") ||
|
|
|
|
err.message.includes("(reading '__H')"))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function check(Component, props, children) {
|
|
|
|
// Note: there are packages that do some unholy things to create "components".
|
|
|
|
// Checking the $$typeof property catches most of these patterns.
|
|
|
|
if (typeof Component === 'object') {
|
|
|
|
const $$typeof = Component['$$typeof'];
|
|
|
|
return $$typeof && $$typeof.toString().slice('Symbol('.length).startsWith('react');
|
|
|
|
}
|
|
|
|
if (typeof Component !== 'function') return false;
|
|
|
|
|
|
|
|
if (Component.prototype != null && typeof Component.prototype.render === 'function') {
|
|
|
|
return React.Component.isPrototypeOf(Component) || React.PureComponent.isPrototypeOf(Component);
|
|
|
|
}
|
|
|
|
|
|
|
|
let error = null;
|
|
|
|
let isReactComponent = false;
|
|
|
|
function Tester(...args) {
|
|
|
|
try {
|
|
|
|
const vnode = Component(...args);
|
|
|
|
if (vnode && vnode['$$typeof'] === reactTypeof) {
|
|
|
|
isReactComponent = true;
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
if (!errorIsComingFromPreactComponent(err)) {
|
|
|
|
error = err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return React.createElement('div');
|
|
|
|
}
|
|
|
|
|
|
|
|
renderToStaticMarkup(Tester, props, children, {});
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
return isReactComponent;
|
|
|
|
}
|
|
|
|
|
2022-06-23 10:10:54 -05:00
|
|
|
function renderToStaticMarkup(Component, props, { default: children, ...slotted }, metadata) {
|
2022-03-31 11:51:29 -05:00
|
|
|
delete props['class'];
|
2022-06-23 10:10:54 -05:00
|
|
|
const slots = {};
|
|
|
|
for (const [key, value] of Object.entries(slotted)) {
|
|
|
|
const name = slotName(key);
|
|
|
|
slots[name] = React.createElement(StaticHtml, { value, name });
|
|
|
|
}
|
|
|
|
// Note: create newProps to avoid mutating `props` before they are serialized
|
2022-06-23 10:12:46 -05:00
|
|
|
const newProps = {
|
2022-03-31 11:51:29 -05:00
|
|
|
...props,
|
2022-06-23 10:10:54 -05:00
|
|
|
...slots,
|
2022-06-23 10:12:46 -05:00
|
|
|
};
|
2023-01-25 17:31:57 -05:00
|
|
|
const newChildren = children ?? props.children;
|
|
|
|
if (newChildren != null) {
|
2023-05-17 09:18:04 -05:00
|
|
|
newProps.children = React.createElement(StaticHtml, {
|
|
|
|
// Adjust how this is hydrated only when the version of Astro supports `astroStaticSlot`
|
|
|
|
hydrate: metadata.astroStaticSlot ? !!metadata.hydrate : true,
|
2023-05-17 09:20:24 -05:00
|
|
|
value: newChildren,
|
2023-05-17 09:18:04 -05:00
|
|
|
});
|
2023-01-25 17:31:57 -05:00
|
|
|
}
|
2022-06-23 10:10:54 -05:00
|
|
|
const vnode = React.createElement(Component, newProps);
|
2022-03-31 11:51:29 -05:00
|
|
|
let html;
|
|
|
|
if (metadata && metadata.hydrate) {
|
|
|
|
html = ReactDOM.renderToString(vnode);
|
|
|
|
} else {
|
|
|
|
html = ReactDOM.renderToStaticMarkup(vnode);
|
|
|
|
}
|
|
|
|
return { html };
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
check,
|
|
|
|
renderToStaticMarkup,
|
2023-05-17 09:18:04 -05:00
|
|
|
supportsAstroStaticSlot: true,
|
2022-03-31 11:51:29 -05:00
|
|
|
};
|