Improved JSX framework detection (#382)

* Improved JSX framework detection

This improves the JSX detection and adds a bunch of test. The following is improved:

* If an error throws because of a coding mistake, those errors will be reported.
* We properly detect class components for both Preact and React. We don't have to "try to render" these.

It's still possible that error messages might be obscured in this scenario:

A Preact function component that uses hooks (or another preact specific feature) that has a coding mistake. The React renderer might throw when it uses the Preact hook. That error will be reported rather than the real coding mistake.

This is because we can't distinguish between errors that are due to the wrong framework and errors that the user caused.

I might reach out to the Preact community and see if they can think of a better solution to this problem. This will come up when other JSX based frameworks have renderers. I still think that having multiple frameworks in the same project is a feature worth trying to preserve.

* Move try/catch into the __astro_component
This commit is contained in:
Matthew Phillips 2021-06-14 08:35:25 -04:00 committed by GitHub
parent ab2972be83
commit 271cfe6ce3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 169 additions and 16 deletions

View file

@ -32,16 +32,27 @@ async function resolveRenderer(Component: any, props: any = {}, children?: strin
return rendererCache.get(Component);
}
const errors: Error[] = [];
for (const __renderer of __renderers) {
// Yes, we do want to `await` inside of this loop!
// __renderer.check can't be run in parallel, it
// returns the first match and skips any subsequent checks
const shouldUse = await __renderer.check(Component, props, children);
if (shouldUse) {
rendererCache.set(Component, __renderer);
return __renderer;
try {
const shouldUse: boolean = await __renderer.check(Component, props, children);
if(shouldUse) {
rendererCache.set(Component, __renderer);
return __renderer;
}
} catch(err) {
errors.push(err);
}
}
if(errors.length) {
// For now just throw the first error we encounter.
throw errors[0];
}
}
interface AstroComponentProps {

View file

@ -0,0 +1,3 @@
{
"workspaceRoot": "../../../../../"
}

View file

@ -0,0 +1,7 @@
import { h, Component } from 'preact';
export default class extends Component {
render() {
return <div id="class-component"></div>
}
}

View file

@ -0,0 +1,5 @@
import { h, Component } from 'preact';
export default function() {
return <div id="fn-component"></div>;
}

View file

@ -0,0 +1,7 @@
import { h } from 'preact';
import { useState } from 'preact/hooks';
export default function() {
const [val] = useState('world');
return <div id={val}></div>;
}

View file

@ -0,0 +1,12 @@
---
import ClassComponent from '../components/Class.jsx';
---
<html>
<head>
<title>Preact class component</title>
</head>
<body>
<ClassComponent />
</body>
</html>

View file

@ -0,0 +1,12 @@
---
import FunctionComponent from '../components/Function.jsx';
---
<html>
<head>
<title>Preact function component</title>
</head>
<body>
<FunctionComponent />
</body>
</html>

View file

@ -0,0 +1,11 @@
---
import Hooks from '../components/Hooks.jsx';
---
<html>
<head>
<title>Preact hooks</title>
</head>
<body>
<Hooks />
</body>
</html>

View file

@ -0,0 +1,5 @@
export default function ({}) {
return <h2>oops</h2>;
}

View file

@ -0,0 +1,12 @@
---
import ForgotImport from '../components/ForgotImport.jsx';
---
<html>
<head>
<title>Here we are</title>
</head>
<body>
<ForgotImport />
</body>
</html>

View file

@ -0,0 +1,34 @@
import { suite } from 'uvu';
import * as assert from 'uvu/assert';
import { doc } from './test-utils.js';
import { setup } from './helpers.js';
const PreactComponent = suite('Preact component test');
setup(PreactComponent, './fixtures/preact-component');
PreactComponent('Can load class component', async ({ runtime }) => {
const result = await runtime.load('/class');
if (result.error) throw new Error(result.error);
const $ = doc(result.contents);
assert.equal($('#class-component').length, 1, 'Can use class components');
});
PreactComponent('Can load function component', async ({ runtime }) => {
const result = await runtime.load('/fn');
if (result.error) throw new Error(result.error);
const $ = doc(result.contents);
assert.equal($('#fn-component').length, 1, 'Can use function components');
});
PreactComponent('Can use hooks', async ({ runtime }) => {
const result = await runtime.load('/hooks');
if (result.error) throw new Error(result.error);
const $ = doc(result.contents);
assert.equal($('#world').length, 1);
});
PreactComponent.run();

View file

@ -49,4 +49,11 @@ React('Can load Vue', async () => {
assert.equal($('#vue-h2').text(), 'Hasta la vista, baby');
});
React('Get good error message when react import is forgotten', async () => {
const result = await runtime.load('/forgot-import');
assert.ok(result.error instanceof ReferenceError);
assert.equal(result.error.message, 'React is not defined');
});
React.run();

View file

@ -1,13 +1,16 @@
import { h } from 'preact';
import { h, Component as BaseComponent } from 'preact';
import { renderToString } from 'preact-render-to-string';
import StaticHtml from './static-html.js';
function check(Component, props, children) {
try {
const { html } = renderToStaticMarkup(Component, props, children);
return Boolean(html);
} catch (e) {}
return false;
if(typeof Component !== 'function') return false;
if(typeof Component.prototype.render === 'function') {
return BaseComponent.isPrototypeOf(Component);
}
const { html } = renderToStaticMarkup(Component, props, children);
return Boolean(html);
}
function renderToStaticMarkup(Component, props, children) {

View file

@ -1,13 +1,37 @@
import { createElement as h } from 'react';
import { Component as BaseComponent, createElement as h } from 'react';
import { renderToStaticMarkup as renderToString } from 'react-dom/server.js';
import StaticHtml from './static-html.js';
const reactTypeof = Symbol.for('react.element');
function check(Component, props, children) {
try {
const { html } = renderToStaticMarkup(Component, props, children);
return Boolean(html);
} catch (e) {}
return false;
if(typeof Component !== 'function') return false;
if(typeof Component.prototype.render === 'function') {
return BaseComponent.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) {
error = err;
}
return h('div');
}
renderToStaticMarkup(Tester, props, children);
if(error) {
throw error;
}
return isReactComponent;
}
function renderToStaticMarkup(Component, props, children) {