Skip to content

Commit

Permalink
Provide a better error message when no jsx renderer configured (#4603)
Browse files Browse the repository at this point in the history
* Provide a better error message when no jsx renderer configured

* Add a changeset
  • Loading branch information
matthewp committed Sep 2, 2022
1 parent 592de3d commit 36dee71
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/tough-zoos-bathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix error when no JSX renderer configured
18 changes: 14 additions & 4 deletions packages/astro/src/vite-plugin-jsx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,14 @@ export default function jsx({ config, logging }: AstroPluginJSXOptions): Plugin
let astroJSXRenderer: AstroRenderer;
// The first JSX renderer provided is considered the default renderer.
// This is a useful reference for when the user only gives a single render.
let defaultJSXRendererEntry: [string, AstroRenderer];
let defaultJSXRendererEntry: [string, AstroRenderer] | undefined;

return {
name: 'astro:jsx',
enforce: 'pre', // run transforms before other plugins
async configResolved(resolvedConfig) {
viteConfig = resolvedConfig;
const possibleRenderers = await collectJSXRenderers(config._ctx.renderers);
const possibleRenderers = collectJSXRenderers(config._ctx.renderers);
for (const [importSource, renderer] of possibleRenderers) {
jsxRenderers.set(importSource, renderer);
if (importSource === 'astro') {
Expand Down Expand Up @@ -224,8 +224,8 @@ export default function jsx({ config, logging }: AstroPluginJSXOptions): Plugin
}

// if we still can’t tell the import source, now is the time to throw an error.
if (!importSource) {
const [defaultRendererName] = defaultJSXRendererEntry[0];
if (!importSource && defaultJSXRendererEntry) {
const [defaultRendererName] = defaultJSXRendererEntry;
error(
logging,
'renderer',
Expand All @@ -234,6 +234,16 @@ Unable to resolve a renderer that handles this file! With more than one renderer
Add ${colors.cyan(
IMPORT_STATEMENTS[defaultRendererName] || `import '${defaultRendererName}';`
)} or ${colors.cyan(`/* jsxImportSource: ${defaultRendererName} */`)} to this file.
`
);
return null;
} else if(!importSource) {
error(
logging,
'renderer',
`${colors.yellow(id)}
Unable to find a renderer for JSX. Do you have one configured in your Astro config? See this page to learn how:
https://docs.astro.build/en/core-concepts/framework-components/#installing-integrations
`
);
return null;
Expand Down

0 comments on commit 36dee71

Please sign in to comment.