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:
parent
ab2972be83
commit
271cfe6ce3
14 changed files with 169 additions and 16 deletions
|
@ -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 {
|
||||
|
|
3
packages/astro/test/fixtures/preact-component/snowpack.config.json
vendored
Normal file
3
packages/astro/test/fixtures/preact-component/snowpack.config.json
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"workspaceRoot": "../../../../../"
|
||||
}
|
7
packages/astro/test/fixtures/preact-component/src/components/Class.jsx
vendored
Normal file
7
packages/astro/test/fixtures/preact-component/src/components/Class.jsx
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
import { h, Component } from 'preact';
|
||||
|
||||
export default class extends Component {
|
||||
render() {
|
||||
return <div id="class-component"></div>
|
||||
}
|
||||
}
|
5
packages/astro/test/fixtures/preact-component/src/components/Function.jsx
vendored
Normal file
5
packages/astro/test/fixtures/preact-component/src/components/Function.jsx
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
import { h, Component } from 'preact';
|
||||
|
||||
export default function() {
|
||||
return <div id="fn-component"></div>;
|
||||
}
|
7
packages/astro/test/fixtures/preact-component/src/components/Hooks.jsx
vendored
Normal file
7
packages/astro/test/fixtures/preact-component/src/components/Hooks.jsx
vendored
Normal 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>;
|
||||
}
|
12
packages/astro/test/fixtures/preact-component/src/pages/class.astro
vendored
Normal file
12
packages/astro/test/fixtures/preact-component/src/pages/class.astro
vendored
Normal file
|
@ -0,0 +1,12 @@
|
|||
---
|
||||
import ClassComponent from '../components/Class.jsx';
|
||||
---
|
||||
|
||||
<html>
|
||||
<head>
|
||||
<title>Preact class component</title>
|
||||
</head>
|
||||
<body>
|
||||
<ClassComponent />
|
||||
</body>
|
||||
</html>
|
12
packages/astro/test/fixtures/preact-component/src/pages/fn.astro
vendored
Normal file
12
packages/astro/test/fixtures/preact-component/src/pages/fn.astro
vendored
Normal file
|
@ -0,0 +1,12 @@
|
|||
---
|
||||
import FunctionComponent from '../components/Function.jsx';
|
||||
---
|
||||
|
||||
<html>
|
||||
<head>
|
||||
<title>Preact function component</title>
|
||||
</head>
|
||||
<body>
|
||||
<FunctionComponent />
|
||||
</body>
|
||||
</html>
|
11
packages/astro/test/fixtures/preact-component/src/pages/hooks.astro
vendored
Normal file
11
packages/astro/test/fixtures/preact-component/src/pages/hooks.astro
vendored
Normal file
|
@ -0,0 +1,11 @@
|
|||
---
|
||||
import Hooks from '../components/Hooks.jsx';
|
||||
---
|
||||
<html>
|
||||
<head>
|
||||
<title>Preact hooks</title>
|
||||
</head>
|
||||
<body>
|
||||
<Hooks />
|
||||
</body>
|
||||
</html>
|
5
packages/astro/test/fixtures/react-component/src/components/ForgotImport.jsx
vendored
Normal file
5
packages/astro/test/fixtures/react-component/src/components/ForgotImport.jsx
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
|
||||
|
||||
export default function ({}) {
|
||||
return <h2>oops</h2>;
|
||||
}
|
12
packages/astro/test/fixtures/react-component/src/pages/forgot-import.astro
vendored
Normal file
12
packages/astro/test/fixtures/react-component/src/pages/forgot-import.astro
vendored
Normal file
|
@ -0,0 +1,12 @@
|
|||
---
|
||||
import ForgotImport from '../components/ForgotImport.jsx';
|
||||
---
|
||||
|
||||
<html>
|
||||
<head>
|
||||
<title>Here we are</title>
|
||||
</head>
|
||||
<body>
|
||||
<ForgotImport />
|
||||
</body>
|
||||
</html>
|
34
packages/astro/test/preact-component.test.js
Normal file
34
packages/astro/test/preact-component.test.js
Normal 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();
|
|
@ -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();
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in a new issue