2022-03-18 17:35:45 -05:00
|
|
|
import React from 'react';
|
2022-03-31 11:51:29 -05:00
|
|
|
import ReactDOM from 'react-dom/server';
|
2022-03-18 17:35:45 -05:00
|
|
|
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-18 17:35:45 -05:00
|
|
|
const reactTypeof = Symbol.for('react.element');
|
|
|
|
|
|
|
|
function errorIsComingFromPreactComponent(err) {
|
2022-04-02 15:15:41 -05:00
|
|
|
return (
|
|
|
|
err.message &&
|
|
|
|
(err.message.startsWith("Cannot read property '__H'") ||
|
|
|
|
err.message.includes("(reading '__H')"))
|
|
|
|
);
|
2022-03-18 17:35:45 -05:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:10:06 -05:00
|
|
|
async function check(Component, props, children) {
|
2022-03-18 17:35:45 -05:00
|
|
|
// 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');
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:10:06 -05:00
|
|
|
await renderToStaticMarkup(Tester, props, children, {});
|
2022-03-18 17:35:45 -05:00
|
|
|
|
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
return isReactComponent;
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:10:06 -05:00
|
|
|
async function getNodeWritable() {
|
|
|
|
let nodeStreamBuiltinModuleName = 'stream';
|
2022-08-30 02:25:34 -05:00
|
|
|
let { Writable } = await import(/* @vite-ignore */ nodeStreamBuiltinModuleName);
|
2022-04-21 11:10:06 -05:00
|
|
|
return Writable;
|
|
|
|
}
|
|
|
|
|
2022-06-23 10:10:54 -05:00
|
|
|
async function renderToStaticMarkup(Component, props, { default: children, ...slotted }, metadata) {
|
2022-03-18 17:35:45 -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-18 17:35:45 -05:00
|
|
|
...props,
|
2022-06-23 10:10:54 -05:00
|
|
|
...slots,
|
2022-03-18 17:35:45 -05:00
|
|
|
children: children != null ? React.createElement(StaticHtml, { value: children }) : undefined,
|
2022-06-23 10:12:46 -05:00
|
|
|
};
|
2022-06-23 10:10:54 -05:00
|
|
|
const vnode = React.createElement(Component, newProps);
|
2022-03-18 17:35:45 -05:00
|
|
|
let html;
|
|
|
|
if (metadata && metadata.hydrate) {
|
|
|
|
html = ReactDOM.renderToString(vnode);
|
2022-04-21 11:11:09 -05:00
|
|
|
if ('renderToReadableStream' in ReactDOM) {
|
2022-04-21 11:10:06 -05:00
|
|
|
html = await renderToReadableStreamAsync(vnode);
|
|
|
|
} else {
|
|
|
|
html = await renderToPipeableStreamAsync(vnode);
|
|
|
|
}
|
2022-03-18 17:35:45 -05:00
|
|
|
} else {
|
2022-04-21 11:11:09 -05:00
|
|
|
if ('renderToReadableStream' in ReactDOM) {
|
2022-04-21 11:10:06 -05:00
|
|
|
html = await renderToReadableStreamAsync(vnode);
|
|
|
|
} else {
|
|
|
|
html = await renderToStaticNodeStreamAsync(vnode);
|
|
|
|
}
|
2022-03-18 17:35:45 -05:00
|
|
|
}
|
|
|
|
return { html };
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:10:06 -05:00
|
|
|
async function renderToPipeableStreamAsync(vnode) {
|
|
|
|
const Writable = await getNodeWritable();
|
|
|
|
let html = '';
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let error = undefined;
|
|
|
|
let stream = ReactDOM.renderToPipeableStream(vnode, {
|
|
|
|
onError(err) {
|
|
|
|
error = err;
|
|
|
|
reject(error);
|
|
|
|
},
|
|
|
|
onAllReady() {
|
2022-04-21 11:11:09 -05:00
|
|
|
stream.pipe(
|
|
|
|
new Writable({
|
|
|
|
write(chunk, _encoding, callback) {
|
|
|
|
html += chunk.toString('utf-8');
|
|
|
|
callback();
|
|
|
|
},
|
|
|
|
destroy() {
|
|
|
|
resolve(html);
|
|
|
|
},
|
|
|
|
})
|
|
|
|
);
|
|
|
|
},
|
2022-04-21 11:10:06 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function renderToStaticNodeStreamAsync(vnode) {
|
|
|
|
const Writable = await getNodeWritable();
|
|
|
|
let html = '';
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
let stream = ReactDOM.renderToStaticNodeStream(vnode);
|
2022-04-21 11:11:09 -05:00
|
|
|
stream.pipe(
|
|
|
|
new Writable({
|
|
|
|
write(chunk, _encoding, callback) {
|
|
|
|
html += chunk.toString('utf-8');
|
|
|
|
callback();
|
|
|
|
},
|
|
|
|
destroy() {
|
|
|
|
resolve(html);
|
|
|
|
},
|
|
|
|
})
|
|
|
|
);
|
2022-04-21 11:10:06 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function renderToReadableStreamAsync(vnode) {
|
|
|
|
const decoder = new TextDecoder();
|
|
|
|
const stream = await ReactDOM.renderToReadableStream(vnode);
|
|
|
|
let html = '';
|
2022-04-21 11:11:09 -05:00
|
|
|
for await (const chunk of stream) {
|
2022-04-21 11:10:06 -05:00
|
|
|
html += decoder.decode(chunk);
|
|
|
|
}
|
|
|
|
return html;
|
|
|
|
}
|
|
|
|
|
2022-03-18 17:35:45 -05:00
|
|
|
export default {
|
|
|
|
check,
|
|
|
|
renderToStaticMarkup,
|
|
|
|
};
|