Skip to content

Commit 0dc4472

Browse files
authored
Fix crash with esbuild loader and jsx option
Closes GH-2593. Closes GH-2594.
1 parent 6a61e73 commit 0dc4472

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

packages/esbuild/lib/index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,8 @@ const name = '@mdx-js/esbuild'
6666
* Plugin.
6767
*/
6868
export function esbuild(options) {
69-
const {extnames, process} = createFormatAwareProcessors({
70-
...options,
71-
SourceMapGenerator
72-
})
69+
const settings = {...options, SourceMapGenerator};
70+
const {extnames, process} = createFormatAwareProcessors(settings)
7371

7472
return {name, setup}
7573

@@ -142,6 +140,7 @@ export function esbuild(options) {
142140
return {
143141
contents: value || '',
144142
errors,
143+
loader: settings.jsx ? 'jsx' : 'js',
145144
resolveDir: path.resolve(file.cwd, file.dirname),
146145
warnings
147146
}

packages/esbuild/test/index.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,36 @@ test('@mdx-js/esbuild', async function (t) {
570570
await fs.rm(mdxUrl)
571571
await fs.rm(jsUrl)
572572
})
573+
574+
await t.test('should use esbuild "jsx" loader for JSX output', async () => {
575+
const mdxUrl = new URL('esbuild.mdx', import.meta.url)
576+
const jsUrl = new URL('esbuild.js', import.meta.url)
577+
await fs.writeFile(
578+
mdxUrl,
579+
'export function Message() { return <>World!</> }\n\n# Hello, <Message />'
580+
)
581+
582+
await esbuild.build({
583+
entryPoints: [fileURLToPath(mdxUrl)],
584+
outfile: fileURLToPath(jsUrl),
585+
plugins: [esbuildMdx({jsx: true})],
586+
define: {'process.env.NODE_ENV': '"development"'},
587+
format: 'esm',
588+
bundle: true
589+
})
590+
591+
/** @type {MDXModule} */
592+
const result = await import(jsUrl.href + '#' + Math.random())
593+
const Content = result.default
594+
595+
assert.equal(
596+
renderToStaticMarkup(React.createElement(Content)),
597+
'<h1>Hello, World!</h1>'
598+
)
599+
600+
await fs.rm(mdxUrl)
601+
await fs.rm(jsUrl)
602+
})
573603
})
574604

575605
/**

0 commit comments

Comments
 (0)