Skip to content

Commit 8eb3a8c

Browse files
matthewpnatemoo-re
andauthored
Add test for mdx + React usage (#4174)
* Add test for mdx + React usage * Add a changeset Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
1 parent 14d27c1 commit 8eb3a8c

8 files changed

Lines changed: 74 additions & 4 deletions

File tree

.changeset/metal-dodos-sin.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@astrojs/mdx': patch
3+
'@astrojs/react': patch
4+
---
5+
6+
Allows using React with automatic imports alongside MDX
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import mdx from '@astrojs/mdx';
2+
import react from '@astrojs/react';
3+
4+
export default {
5+
integrations: [react(), mdx()]
6+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "@test/mdx-plus-react",
3+
"dependencies": {
4+
"astro": "workspace:*",
5+
"@astrojs/mdx": "workspace:*",
6+
"@astrojs/react": "workspace:*"
7+
}
8+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const Component = () => {
2+
return <p>Hello world</p>;
3+
};
4+
5+
export default Component;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
import Component from "../components/Component.jsx";
3+
---
4+
<html>
5+
<head>
6+
<title>Testing</title>
7+
</head>
8+
<body>
9+
<Component />
10+
</body>
11+
</html>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import mdx from '@astrojs/mdx';
2+
3+
import { expect } from 'chai';
4+
import { parseHTML } from 'linkedom';
5+
import { loadFixture } from '../../../astro/test/test-utils.js';
6+
7+
describe('MDX and React', () => {
8+
let fixture;
9+
10+
before(async () => {
11+
fixture = await loadFixture({
12+
root: new URL('./fixtures/mdx-plus-react/', import.meta.url),
13+
});
14+
await fixture.build();
15+
});
16+
17+
it('can be used in the same project', async () => {
18+
const html = await fixture.readFile('/index.html');
19+
const { document } = parseHTML(html);
20+
21+
const p = document.querySelector('p');
22+
23+
expect(p.textContent).to.equal('Hello world');
24+
});
25+
});

packages/integrations/react/src/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ function getRenderer() {
1212
: '@astrojs/react/server-v17.js',
1313
jsxImportSource: 'react',
1414
jsxTransformOptions: async () => {
15-
const {
16-
default: { default: jsx },
17-
// @ts-expect-error types not found
18-
} = await import('@babel/plugin-transform-react-jsx');
15+
// @ts-expect-error types not found
16+
const babelPluginTransformReactJsxModule = await import('@babel/plugin-transform-react-jsx');
17+
const jsx = babelPluginTransformReactJsxModule?.default?.default ?? babelPluginTransformReactJsxModule?.default;
1918
return {
2019
plugins: [
2120
jsx(

pnpm-lock.yaml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)