Skip to content

Commit

Permalink
Add better errors when referencing missing components (#1811)
Browse files Browse the repository at this point in the history
Previously, React or other JSX runtimes threw rather hard to read errors
when a component was undefined (because it wasn’t imported, passed, or
provided), essentially only pointing to *something* missing.
Now we throw proper errors when a component is missing at runtime,
including what exact component (or object) is undefined.

In addition, this adds a `development` option, which defaults to
`false` but can be configured explicitly or turned on with
`NODE_ENV=development`.
When it’s `true`, the exact place that references the missing component
or object, and which file did that, is included in the error message.

Related-to: #1775.
Backports: wooorm/xdm@62e6f30.
  • Loading branch information
wooorm authored Nov 13, 2021
1 parent e86e9e8 commit 2f96fba
Show file tree
Hide file tree
Showing 12 changed files with 415 additions and 60 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@
"react/prop-types": "off",
"unicorn/prefer-node-protocol": "off",
"capitalized-comments": "off",
"complexity": "off"
"complexity": "off",
"max-depth": "off"
},
"overrides": [
{
Expand Down
1 change: 1 addition & 0 deletions packages/mdx/lib/condition.browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const development = false
3 changes: 3 additions & 0 deletions packages/mdx/lib/condition.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import process from 'node:process'

This comment has been minimized.

Copy link
@dvargas92495

dvargas92495 Nov 27, 2021

I'm not familiar with this syntax. Why is this needed? esbuild is throwing compilation errors on my end because it can't resolve this module

This comment has been minimized.

Copy link
@ChristianMurphy

ChristianMurphy Nov 27, 2021

Member

@dvargas92495 this would be better to move over to a discussion https://github.com/mdx-js/mdx/discussions

Also, looking at the esbuild issue tracker, it appears the node protocol is supported by esbuild, see evanw/esbuild#1466

This comment has been minimized.

Copy link
@dvargas92495

dvargas92495 Nov 27, 2021

Ah ok, I'll need to debug my setup further then. Thank you!


export const development = process.env.NODE_ENV === 'development'
4 changes: 3 additions & 1 deletion packages/mdx/lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {rehypeRecma} from './plugin/rehype-recma.js'
import {rehypeRemoveRaw} from './plugin/rehype-remove-raw.js'
import {remarkMarkAndUnravel} from './plugin/remark-mark-and-unravel.js'
import {nodeTypes} from './node-types.js'
import {development as defaultDevelopment} from './condition.js'

const removedOptions = [
'filepath',
Expand All @@ -53,6 +54,7 @@ const removedOptions = [
*/
export function createProcessor(options = {}) {
const {
development = defaultDevelopment,
jsx,
format,
outputFormat,
Expand Down Expand Up @@ -102,7 +104,7 @@ export function createProcessor(options = {}) {
pipeline
.use(rehypeRecma)
.use(recmaDocument, {...rest, outputFormat})
.use(recmaJsxRewrite, {providerImportSource, outputFormat})
.use(recmaJsxRewrite, {development, providerImportSource, outputFormat})

if (!jsx) {
pipeline.use(recmaJsxBuild, {outputFormat})
Expand Down
12 changes: 5 additions & 7 deletions packages/mdx/lib/plugin/recma-jsx-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import {buildJsx} from 'estree-util-build-jsx'
import {specifiersToDeclarations} from '../util/estree-util-specifiers-to-declarations.js'
import {toIdOrMemberExpression} from '../util/estree-util-to-id-or-member-expression.js'

/**
* A plugin to build JSX into function calls.
Expand All @@ -33,13 +34,10 @@ export function recmaJsxBuild(options = {}) {
tree.body[0] = {
type: 'VariableDeclaration',
kind: 'const',
declarations: specifiersToDeclarations(tree.body[0].specifiers, {
type: 'MemberExpression',
object: {type: 'Identifier', name: 'arguments'},
property: {type: 'Literal', value: 0},
computed: true,
optional: false
})
declarations: specifiersToDeclarations(
tree.body[0].specifiers,
toIdOrMemberExpression(['arguments', 0])
)
}
}
}
Expand Down
Loading

1 comment on commit 2f96fba

@vercel
Copy link

@vercel vercel bot commented on 2f96fba Nov 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

Please sign in to comment.