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

21 files changed

+296
-1775
lines changed

.gitignore

-1
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/
+74-32
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

-1
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

-13
This file was deleted.

examples/create-react-app/.env

-1
This file was deleted.

examples/create-react-app/.gitignore

-23
This file was deleted.

examples/create-react-app/package.json

-34
This file was deleted.
-3.78 KB
Binary file not shown.

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

-43
This file was deleted.

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

-15
This file was deleted.

examples/create-react-app/readme.md

+3-49
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,6 @@
11
# Create React App
22

3-
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
3+
<!-- To do: link to website when it’s fixed. -->
44

5-
## Available Scripts
6-
7-
In the project directory, you can run:
8-
9-
### `npm start`
10-
11-
Runs the app in the development mode.<br />
12-
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
13-
14-
The page will reload if you make edits.<br />
15-
You will also see any lint errors in the console.
16-
17-
### `npm test`
18-
19-
Launches the test runner in the interactive watch mode.<br />
20-
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
21-
22-
### `npm run build`
23-
24-
Builds the app for production to the `build` folder.<br />
25-
It correctly bundles React in production mode and optimizes the build for the best performance.
26-
27-
The build is minified and the filenames include the hashes.<br />
28-
Your app is ready to be deployed!
29-
30-
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
31-
32-
### `npm run eject`
33-
34-
**Note: this is a one-way operation.
35-
Once you `eject`, you can’t go back!**
36-
37-
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time.
38-
This command will remove the single build dependency from your project.
39-
40-
Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them.
41-
All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them.
42-
At this point you’re on your own.
43-
44-
You don’t have to ever use `eject`.
45-
The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature.
46-
However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
47-
48-
## Learn More
49-
50-
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
51-
52-
To learn React, check out the [React documentation](https://reactjs.org/).
5+
[See this guide](https://github.com/mdx-js/mdx/blob/main/docs/getting-started/create-react-app.mdx)
6+
for how to use MDX with CRA!

examples/create-react-app/src/App.css

-32
This file was deleted.

examples/create-react-app/src/App.js

-18
This file was deleted.

examples/create-react-app/src/App.test.js

-9
This file was deleted.

0 commit comments

Comments
 (0)