astro/packages/renderers/renderer-react/server.js

52 lines
1.3 KiB
JavaScript
Raw Normal View History

import { Component as BaseComponent, createElement as h } from 'react';
import { renderToStaticMarkup as reactRenderToStaticMarkup, renderToString } from 'react-dom/server.js';
Renderer plugins (#231) * refactor: pluggable renderers * refactor: cache renderer per component * docs: update comments on snowpack plugin `transform` method * docs: add comments to renderer plugins * refactor: convert components to Map * fix: pass children through to astro __render * refactor: move Components/ComponentInfo to shared types * refactor: remove `gatherRuntimes` step, just scan output for imports * refactor: update isComponentTag logic * chore: move dependencies to renderers * fix: cross-platform transform injection * feat: defer renderer to react, fallback to preact * fix: use double quotes in generated script * test: fix failing children tests * test: add workspaceRoot to all tests * fix: pass props to renderer check * chore: add test:core script back for convenience * chore: remove unused external * chore: rename renderers * chore: add astring, estree-util-value-to-estree * chore: render-component => __astro_component * refactor: split hydrate logic to own file * refactor: use `astro-fragment` rather than `div` * chore: remove unused hooks * chore: delete unused file * chore: add changesets * fix: Astro renderer should be async * fix: remove <astro-fragment> for static content * test: fix failing test * chore: normalize config interface * feat: allow renderers to inject a snowpackPlugin * fix: resolve import URL before using dynamic import * refactor: update renderers to use separate /server entrypoint * refactor: update server renderer interface * fix: get renderers working again * test: debug failing test * test: better debug * test: better debug * test: remove debug * fix: support esm and cjs packages via "resolve" * refactor: split hydrate functions into individual files * fix: dependency resolution relative to projectRoot * fix: @snowpack/plugin-postcss needs to be hoisted * fix: do not test prettier-plugin-astro as it's not ready for primetime
2021-05-26 18:30:22 +00:00
import StaticHtml from './static-html.js';
const reactTypeof = Symbol.for('react.element');
function check(Component, props, children) {
2021-06-14 12:36:32 +00:00
if (typeof Component !== 'function') return false;
if (Component.prototype != null && typeof Component.prototype.render === 'function') {
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) {
isReactComponent = true;
}
2021-06-14 12:36:32 +00:00
} catch (err) {
error = err;
}
return h('div');
}
renderToStaticMarkup(Tester, props, children, {});
2021-06-14 12:36:32 +00:00
if (error) {
throw error;
}
return isReactComponent;
2021-05-26 18:31:27 +00:00
}
Renderer plugins (#231) * refactor: pluggable renderers * refactor: cache renderer per component * docs: update comments on snowpack plugin `transform` method * docs: add comments to renderer plugins * refactor: convert components to Map * fix: pass children through to astro __render * refactor: move Components/ComponentInfo to shared types * refactor: remove `gatherRuntimes` step, just scan output for imports * refactor: update isComponentTag logic * chore: move dependencies to renderers * fix: cross-platform transform injection * feat: defer renderer to react, fallback to preact * fix: use double quotes in generated script * test: fix failing children tests * test: add workspaceRoot to all tests * fix: pass props to renderer check * chore: add test:core script back for convenience * chore: remove unused external * chore: rename renderers * chore: add astring, estree-util-value-to-estree * chore: render-component => __astro_component * refactor: split hydrate logic to own file * refactor: use `astro-fragment` rather than `div` * chore: remove unused hooks * chore: delete unused file * chore: add changesets * fix: Astro renderer should be async * fix: remove <astro-fragment> for static content * test: fix failing test * chore: normalize config interface * feat: allow renderers to inject a snowpackPlugin * fix: resolve import URL before using dynamic import * refactor: update renderers to use separate /server entrypoint * refactor: update server renderer interface * fix: get renderers working again * test: debug failing test * test: better debug * test: better debug * test: remove debug * fix: support esm and cjs packages via "resolve" * refactor: split hydrate functions into individual files * fix: dependency resolution relative to projectRoot * fix: @snowpack/plugin-postcss needs to be hoisted * fix: do not test prettier-plugin-astro as it's not ready for primetime
2021-05-26 18:30:22 +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) {
html = renderToString(vnode);
} else {
html = reactRenderToStaticMarkup(vnode);
}
Renderer plugins (#231) * refactor: pluggable renderers * refactor: cache renderer per component * docs: update comments on snowpack plugin `transform` method * docs: add comments to renderer plugins * refactor: convert components to Map * fix: pass children through to astro __render * refactor: move Components/ComponentInfo to shared types * refactor: remove `gatherRuntimes` step, just scan output for imports * refactor: update isComponentTag logic * chore: move dependencies to renderers * fix: cross-platform transform injection * feat: defer renderer to react, fallback to preact * fix: use double quotes in generated script * test: fix failing children tests * test: add workspaceRoot to all tests * fix: pass props to renderer check * chore: add test:core script back for convenience * chore: remove unused external * chore: rename renderers * chore: add astring, estree-util-value-to-estree * chore: render-component => __astro_component * refactor: split hydrate logic to own file * refactor: use `astro-fragment` rather than `div` * chore: remove unused hooks * chore: delete unused file * chore: add changesets * fix: Astro renderer should be async * fix: remove <astro-fragment> for static content * test: fix failing test * chore: normalize config interface * feat: allow renderers to inject a snowpackPlugin * fix: resolve import URL before using dynamic import * refactor: update renderers to use separate /server entrypoint * refactor: update server renderer interface * fix: get renderers working again * test: debug failing test * test: better debug * test: better debug * test: remove debug * fix: support esm and cjs packages via "resolve" * refactor: split hydrate functions into individual files * fix: dependency resolution relative to projectRoot * fix: @snowpack/plugin-postcss needs to be hoisted * fix: do not test prettier-plugin-astro as it's not ready for primetime
2021-05-26 18:30:22 +00:00
return { html };
}
2021-05-26 18:31:27 +00:00
export default {
check,
renderToStaticMarkup,
Renderer plugins (#231) * refactor: pluggable renderers * refactor: cache renderer per component * docs: update comments on snowpack plugin `transform` method * docs: add comments to renderer plugins * refactor: convert components to Map * fix: pass children through to astro __render * refactor: move Components/ComponentInfo to shared types * refactor: remove `gatherRuntimes` step, just scan output for imports * refactor: update isComponentTag logic * chore: move dependencies to renderers * fix: cross-platform transform injection * feat: defer renderer to react, fallback to preact * fix: use double quotes in generated script * test: fix failing children tests * test: add workspaceRoot to all tests * fix: pass props to renderer check * chore: add test:core script back for convenience * chore: remove unused external * chore: rename renderers * chore: add astring, estree-util-value-to-estree * chore: render-component => __astro_component * refactor: split hydrate logic to own file * refactor: use `astro-fragment` rather than `div` * chore: remove unused hooks * chore: delete unused file * chore: add changesets * fix: Astro renderer should be async * fix: remove <astro-fragment> for static content * test: fix failing test * chore: normalize config interface * feat: allow renderers to inject a snowpackPlugin * fix: resolve import URL before using dynamic import * refactor: update renderers to use separate /server entrypoint * refactor: update server renderer interface * fix: get renderers working again * test: debug failing test * test: better debug * test: better debug * test: remove debug * fix: support esm and cjs packages via "resolve" * refactor: split hydrate functions into individual files * fix: dependency resolution relative to projectRoot * fix: @snowpack/plugin-postcss needs to be hoisted * fix: do not test prettier-plugin-astro as it's not ready for primetime
2021-05-26 18:30:22 +00:00
};