2021-06-14 12:35:25 +00:00
|
|
|
import { Component as BaseComponent, createElement as h } from 'react';
|
2021-07-09 13:00:32 +00:00
|
|
|
import { renderToStaticMarkup as reactRenderToStaticMarkup, renderToString } from 'react-dom/server.js';
|
2021-05-26 18:30:22 +00:00
|
|
|
import StaticHtml from './static-html.js';
|
|
|
|
|
2021-06-14 12:35:25 +00:00
|
|
|
const reactTypeof = Symbol.for('react.element');
|
|
|
|
|
2021-05-28 22:19:40 +00:00
|
|
|
function check(Component, props, children) {
|
2021-06-14 12:36:32 +00:00
|
|
|
if (typeof Component !== 'function') return false;
|
2021-06-14 12:35:25 +00:00
|
|
|
|
2021-06-14 17:24:37 +00:00
|
|
|
if (Component.prototype != null && typeof Component.prototype.render === 'function') {
|
2021-06-14 12:35:25 +00:00
|
|
|
return BaseComponent.isPrototypeOf(Component);
|
|
|
|
}
|
|
|
|
|
|
|
|
let error = null;
|
|
|
|
let isReactComponent = false;
|
|
|
|
function Tester(...args) {
|
|
|
|
try {
|
|
|
|
const vnode = Component(...args);
|
2021-06-14 12:36:32 +00:00
|
|
|
if (vnode && vnode['$$typeof'] === reactTypeof) {
|
2021-06-14 12:35:25 +00:00
|
|
|
isReactComponent = true;
|
|
|
|
}
|
2021-06-14 12:36:32 +00:00
|
|
|
} catch (err) {
|
2021-06-14 12:35:25 +00:00
|
|
|
error = err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return h('div');
|
|
|
|
}
|
|
|
|
|
2021-07-09 13:00:32 +00:00
|
|
|
renderToStaticMarkup(Tester, props, children, {});
|
2021-06-14 12:35:25 +00:00
|
|
|
|
2021-06-14 12:36:32 +00:00
|
|
|
if (error) {
|
2021-06-14 12:35:25 +00:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
return isReactComponent;
|
2021-05-26 18:31:27 +00:00
|
|
|
}
|
2021-05-26 18:30:22 +00:00
|
|
|
|
2021-07-09 13:00:32 +00:00
|
|
|
function renderToStaticMarkup(Component, props, children, metadata) {
|
|
|
|
const vnode = h(Component, { ...props, children: h(StaticHtml, { value: children }), innerHTML: children });
|
|
|
|
let html;
|
2021-07-09 13:41:50 +00:00
|
|
|
if (metadata && metadata.hydrate) {
|
2021-07-09 13:00:32 +00:00
|
|
|
html = renderToString(vnode);
|
|
|
|
} else {
|
|
|
|
html = reactRenderToStaticMarkup(vnode);
|
|
|
|
}
|
2021-05-26 18:30:22 +00:00
|
|
|
return { html };
|
|
|
|
}
|
|
|
|
|
2021-05-26 18:31:27 +00:00
|
|
|
export default {
|
|
|
|
check,
|
|
|
|
renderToStaticMarkup,
|
2021-05-26 18:30:22 +00:00
|
|
|
};
|