2022-09-21 19:23:58 +00:00
|
|
|
import { Component as BaseComponent, h } from 'preact';
|
2022-03-18 22:35:45 +00:00
|
|
|
import render from 'preact-render-to-string';
|
2022-09-21 19:21:21 +00:00
|
|
|
import { getContext } from './context.js';
|
|
|
|
import { restoreSignalsOnProps, serializeSignals } from './signals.js';
|
2022-09-21 19:23:58 +00:00
|
|
|
import StaticHtml from './static-html.js';
|
|
|
|
import type { AstroPreactAttrs, RendererContext } from './types';
|
2022-03-18 22:35:45 +00:00
|
|
|
|
2022-09-21 19:21:21 +00:00
|
|
|
const slotName = (str: string) => str.trim().replace(/[-_]([a-z])/g, (_, w) => w.toUpperCase());
|
2022-06-23 15:10:54 +00:00
|
|
|
|
2022-09-21 19:21:21 +00:00
|
|
|
let originalConsoleError: typeof console.error;
|
2022-06-30 12:10:56 +00:00
|
|
|
let consoleFilterRefs = 0;
|
|
|
|
|
2022-09-21 19:21:21 +00:00
|
|
|
function check(this: RendererContext, Component: any, props: Record<string, any>, children: any) {
|
2022-03-18 22:35:45 +00:00
|
|
|
if (typeof Component !== 'function') return false;
|
|
|
|
|
|
|
|
if (Component.prototype != null && typeof Component.prototype.render === 'function') {
|
|
|
|
return BaseComponent.isPrototypeOf(Component);
|
|
|
|
}
|
|
|
|
|
2022-06-30 12:10:56 +00:00
|
|
|
useConsoleFilter();
|
|
|
|
|
2022-03-18 22:35:45 +00:00
|
|
|
try {
|
2022-06-30 12:10:56 +00:00
|
|
|
try {
|
2022-09-21 19:21:21 +00:00
|
|
|
const { html } = renderToStaticMarkup.call(this, Component, props, children);
|
2022-06-30 12:10:56 +00:00
|
|
|
if (typeof html !== 'string') {
|
|
|
|
return false;
|
|
|
|
}
|
2022-03-18 22:35:45 +00:00
|
|
|
|
2022-06-30 12:10:56 +00:00
|
|
|
// There are edge cases (SolidJS) where Preact *might* render a string,
|
|
|
|
// but components would be <undefined></undefined>
|
2022-03-18 22:35:45 +00:00
|
|
|
|
2022-06-30 12:10:56 +00:00
|
|
|
return !/\<undefined\>/.test(html);
|
|
|
|
} catch (err) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
finishUsingConsoleFilter();
|
2022-03-18 22:35:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-21 19:23:58 +00:00
|
|
|
function renderToStaticMarkup(
|
|
|
|
this: RendererContext,
|
|
|
|
Component: any,
|
|
|
|
props: Record<string, any>,
|
|
|
|
{ default: children, ...slotted }: Record<string, any>
|
|
|
|
) {
|
2022-09-21 19:21:21 +00:00
|
|
|
const ctx = getContext(this.result);
|
|
|
|
|
|
|
|
const slots: Record<string, ReturnType<typeof h>> = {};
|
2022-06-23 15:10:54 +00:00
|
|
|
for (const [key, value] of Object.entries(slotted)) {
|
|
|
|
const name = slotName(key);
|
|
|
|
slots[name] = h(StaticHtml, { value, name });
|
|
|
|
}
|
2022-09-21 19:21:21 +00:00
|
|
|
|
|
|
|
// Restore signals back onto props so that they will be passed as-is to components
|
|
|
|
let propsMap = restoreSignalsOnProps(ctx, props);
|
|
|
|
|
2022-06-23 15:12:46 +00:00
|
|
|
const newProps = { ...props, ...slots };
|
2022-09-21 19:21:21 +00:00
|
|
|
|
|
|
|
const attrs: AstroPreactAttrs = {};
|
|
|
|
serializeSignals(ctx, props, attrs, propsMap);
|
|
|
|
|
2022-04-02 20:15:41 +00:00
|
|
|
const html = render(
|
2022-06-23 15:10:54 +00:00
|
|
|
h(Component, newProps, children != null ? h(StaticHtml, { value: children }) : children)
|
2022-04-02 20:15:41 +00:00
|
|
|
);
|
2022-09-21 19:21:21 +00:00
|
|
|
return {
|
|
|
|
attrs,
|
2022-09-21 19:23:58 +00:00
|
|
|
html,
|
2022-09-21 19:21:21 +00:00
|
|
|
};
|
2022-03-18 22:35:45 +00:00
|
|
|
}
|
|
|
|
|
2022-06-30 12:10:56 +00:00
|
|
|
/**
|
|
|
|
* Reduces console noise by filtering known non-problematic errors.
|
2022-06-30 12:12:27 +00:00
|
|
|
*
|
2022-06-30 12:10:56 +00:00
|
|
|
* Performs reference counting to allow parallel usage from async code.
|
2022-06-30 12:12:27 +00:00
|
|
|
*
|
2022-06-30 12:10:56 +00:00
|
|
|
* To stop filtering, please ensure that there always is a matching call
|
|
|
|
* to `finishUsingConsoleFilter` afterwards.
|
|
|
|
*/
|
|
|
|
function useConsoleFilter() {
|
|
|
|
consoleFilterRefs++;
|
|
|
|
|
|
|
|
if (!originalConsoleError) {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
originalConsoleError = console.error;
|
|
|
|
|
|
|
|
try {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error = filteredConsoleError;
|
|
|
|
} catch (error) {
|
|
|
|
// If we're unable to hook `console.error`, just accept it
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates that the filter installed by `useConsoleFilter`
|
|
|
|
* is no longer needed by the calling code.
|
|
|
|
*/
|
|
|
|
function finishUsingConsoleFilter() {
|
|
|
|
consoleFilterRefs--;
|
|
|
|
|
|
|
|
// Note: Instead of reverting `console.error` back to the original
|
|
|
|
// when the reference counter reaches 0, we leave our hook installed
|
|
|
|
// to prevent potential race conditions once `check` is made async
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook/wrapper function for the global `console.error` function.
|
2022-06-30 12:12:27 +00:00
|
|
|
*
|
2022-06-30 12:10:56 +00:00
|
|
|
* Ignores known non-problematic errors while any code is using the console filter.
|
|
|
|
* Otherwise, simply forwards all arguments to the original function.
|
|
|
|
*/
|
2022-09-21 19:21:21 +00:00
|
|
|
function filteredConsoleError(msg: string, ...rest: any[]) {
|
2022-06-30 12:10:56 +00:00
|
|
|
if (consoleFilterRefs > 0 && typeof msg === 'string') {
|
|
|
|
// In `check`, we attempt to render JSX components through Preact.
|
|
|
|
// When attempting this on a React component, React may output
|
|
|
|
// the following error, which we can safely filter out:
|
|
|
|
const isKnownReactHookError =
|
|
|
|
msg.includes('Warning: Invalid hook call.') &&
|
|
|
|
msg.includes('https://reactjs.org/link/invalid-hook-call');
|
2022-06-30 12:12:27 +00:00
|
|
|
if (isKnownReactHookError) return;
|
2022-06-30 12:10:56 +00:00
|
|
|
}
|
|
|
|
originalConsoleError(msg, ...rest);
|
|
|
|
}
|
|
|
|
|
2022-03-18 22:35:45 +00:00
|
|
|
export default {
|
|
|
|
check,
|
|
|
|
renderToStaticMarkup,
|
|
|
|
};
|