Skip to content

Commit

Permalink
Add test for mdx + React usage (#4174)
Browse files Browse the repository at this point in the history
* Add test for mdx + React usage

* Add a changeset

Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
  • Loading branch information
matthewp and natemoo-re committed Aug 5, 2022
1 parent 14d27c1 commit 8eb3a8c
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changeset/metal-dodos-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@astrojs/mdx': patch
'@astrojs/react': patch
---

Allows using React with automatic imports alongside MDX
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import mdx from '@astrojs/mdx';
import react from '@astrojs/react';

export default {
integrations: [react(), mdx()]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/mdx-plus-react",
"dependencies": {
"astro": "workspace:*",
"@astrojs/mdx": "workspace:*",
"@astrojs/react": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const Component = () => {
return <p>Hello world</p>;
};

export default Component;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
import Component from "../components/Component.jsx";
---
<html>
<head>
<title>Testing</title>
</head>
<body>
<Component />
</body>
</html>
25 changes: 25 additions & 0 deletions packages/integrations/mdx/test/mdx-plus-react.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import mdx from '@astrojs/mdx';

import { expect } from 'chai';
import { parseHTML } from 'linkedom';
import { loadFixture } from '../../../astro/test/test-utils.js';

describe('MDX and React', () => {
let fixture;

before(async () => {
fixture = await loadFixture({
root: new URL('./fixtures/mdx-plus-react/', import.meta.url),
});
await fixture.build();
});

it('can be used in the same project', async () => {
const html = await fixture.readFile('/index.html');
const { document } = parseHTML(html);

const p = document.querySelector('p');

expect(p.textContent).to.equal('Hello world');
});
});
7 changes: 3 additions & 4 deletions packages/integrations/react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ function getRenderer() {
: '@astrojs/react/server-v17.js',
jsxImportSource: 'react',
jsxTransformOptions: async () => {
const {
default: { default: jsx },
// @ts-expect-error types not found
} = await import('@babel/plugin-transform-react-jsx');
// @ts-expect-error types not found
const babelPluginTransformReactJsxModule = await import('@babel/plugin-transform-react-jsx');
const jsx = babelPluginTransformReactJsxModule?.default?.default ?? babelPluginTransformReactJsxModule?.default;
return {
plugins: [
jsx(
Expand Down
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8eb3a8c

Please sign in to comment.