Skip to content

Commit ab8b787

Browse files
committed
Update CRA guide
Create React App is the most looked at resource by users here on GitHub. But it’s suggesting an old, unmaintained, and buggy way to use MDX. This instead updates the guide to use our maintained projects, without having to eject from CRA. As CRA itself is an ever-changing “init” tool, which can support MDX by following a couple steps, I don’t think it’s wise to have an example in the project: we want folks to do `npx create-react-app ...`, not clone our custom example. Not having CRA checked in also makes for a faster `yarn install`. Perhaps developing our own [CRA template](https://create-react-app.dev/docs/custom-templates) might be nice for the future, but for now I’ve kept it at an up to date and working guide. Related to GH-1015. Related to GH-1388. Closes GH-365. Closes GH-589.
1 parent a697301 commit ab8b787

File tree

21 files changed

+296
-1775
lines changed

21 files changed

+296
-1775
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ bundle.js
1111
tmp
1212
artifacts
1313
.*cache
14-
public
1514
.npmrc
1615
.nyc_output/
1716
coverage/
Lines changed: 74 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,92 @@
11
# Create React App
22

3-
Please note there’s a [known bug][] with
4-
the macro so live reloading doesn’t
5-
currently work.
6-
7-
With Create React App you will need to use
8-
[`mdx.macro`][mdx-macro].
3+
First, create a new app with [CRA][] and change directory to enter it:
94

105
```sh
116
npx create-react-app my-app
12-
yarn add mdx.macro
7+
cd my-app
8+
```
9+
10+
Second, we need to configure some build tools.
11+
Install [`@mdx-js/loader`][loader] and [`babel-loader`][babel-loader] as dev
12+
dependencies:
13+
14+
```sh
15+
yarn add @mdx-js/loader babel-loader --dev
16+
```
17+
18+
Also make sure that JSX in MDX is handled by Babel by creating a `.babelrc` JSON
19+
file in the project root of `my-app`:
20+
21+
```json
22+
{
23+
"presets": ["@babel/preset-react"]
24+
}
25+
```
26+
27+
Third, we can add our MDX content.
28+
Create an MDX file `Content.mdx` in the `src/` folder:
29+
30+
```mdx
31+
export const Box = () => (
32+
<div style={{padding: 20, backgroundColor: 'tomato'}} />
33+
)
34+
35+
# Hello, world!
36+
37+
This is **markdown** with <span style={{color: "red"}}>JSX</span>: MDX!
38+
39+
<Box />
1340
```
1441

15-
Then create the following `src/App.js`:
42+
And to use that MDX content in the app, replace the contents of `App.js` in the
43+
`src/` folder with:
1644

1745
```js
18-
// src/App.js
19-
20-
import React, {lazy, Component, Suspense} from 'react'
21-
import {importMDX} from 'mdx.macro'
22-
23-
const Content = lazy(() => importMDX('./Content.mdx'))
24-
25-
class App extends Component {
26-
render() {
27-
return (
28-
<div>
29-
<Suspense fallback={<div>Loading...</div>}>
30-
<Content />
31-
</Suspense>
32-
</div>
33-
)
34-
}
46+
/* eslint-disable import/no-webpack-loader-syntax */
47+
import Content from '!babel-loader!@mdx-js/loader!./Content.mdx'
48+
49+
function App() {
50+
return <Content />
3551
}
3652

3753
export default App
3854
```
3955

40-
And then create the following `src/Content.mdx`:
56+
We’re almost done!
57+
To start the development server, run:
4158

42-
```md
43-
# Hello, world!
59+
```sh
60+
yarn start
4461
```
4562

46-
[See the full example][cra-example]
63+
**…but you will probably get this error**:
64+
65+
```txt
66+
$ react-scripts start
67+
68+
There might be a problem with the project dependency tree.
69+
It is likely not a bug in Create React App, but something you need to fix locally.
70+
71+
The react-scripts package provided by Create React App requires a dependency:
72+
73+
"babel-loader": "$VERSION"
74+
75+
```
76+
77+
CRA expects a certain `babel-loader` version but MDX also needs one.
78+
To fix the error, make sure the two `babel-loader` versions match, by running
79+
the following:
80+
81+
```sh
82+
yarn add babel-loader@$VERSION --dev
83+
```
84+
85+
(where `$VERSION` is the version number printed in the error message, such as
86+
`8.1.0`).
87+
88+
Then, running `yarn start` again should do the trick! ✨
4789

48-
[mdx-macro]: https://www.npmjs.com/package/mdx.macro
49-
[cra-example]: https://github.com/mdx-js/mdx/tree/master/examples/create-react-app
50-
[known bug]: https://github.com/facebook/create-react-app/issues/5580
90+
[cra]: https://github.com/facebook/create-react-app
91+
[loader]: https://github.com/mdx-js/mdx/tree/main/packages/loader
92+
[babel-loader]: https://webpack.js.org/loaders/babel-loader/

docs/getting-started/index.mdx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,6 @@ playing around you can use `npm init`:
411411
- `npm init mdx` [`webpack`](/getting-started/webpack)
412412
- `npm init mdx` [`parcel`](/getting-started/parcel)
413413
- `npm init mdx` [`next`](/getting-started/next)
414-
- `npm init mdx` [`create-react-app`](/getting-started/create-react-app)
415414
- `npm init mdx` [`gatsby`](/getting-started/gatsby)
416415
- `npm init mdx` [`react-static`](/getting-started/react-static)
417416

examples/create-react-app/.babelrc

Lines changed: 0 additions & 13 deletions
This file was deleted.

examples/create-react-app/.env

Lines changed: 0 additions & 1 deletion
This file was deleted.

examples/create-react-app/.gitignore

Lines changed: 0 additions & 23 deletions
This file was deleted.

examples/create-react-app/package.json

Lines changed: 0 additions & 34 deletions
This file was deleted.
-3.78 KB
Binary file not shown.

examples/create-react-app/public/index.html

Lines changed: 0 additions & 43 deletions
This file was deleted.

examples/create-react-app/public/manifest.json

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)