Skip to content

Commit

Permalink
Update create-react-app docs to use official library (#359)
Browse files Browse the repository at this point in the history
* Use mdx.macro for create-react-app example

Closes #358

* Update surrounding documentation

* Fix linting

* Nuke esling config in cra so it doesn't get picked up
  • Loading branch information
johno committed Dec 14, 2018
1 parent 3bd75e1 commit 9cb7c3a
Show file tree
Hide file tree
Showing 15 changed files with 4,185 additions and 2,559 deletions.
70 changes: 39 additions & 31 deletions docs/getting-started/create-react-app.md
Original file line number Diff line number Diff line change
@@ -1,44 +1,52 @@
import { Message } from 'rebass'

# Create React App

<Message>
This docs page is a WIP
</Message>
Please note there’s a [known bug][] with
the macro so live reloading doesn’t
currently work.

With Create React App you will need to use
[`create-react-app-rewired`][cra-rewired] and add a `config-overrides.js`.
[`mdx.macro`][mdx-macro].

```sh
npx create-react-app my-app
yarn add mdx.macro
```

Then create the following `src/App.js`:

```js
const { getBabelLoader } = require('react-app-rewired')

module.exports = (config, env) => {
const babelLoader = getBabelLoader(config.module.rules)
config.module.rules.map(rule => {
if (typeof rule.test !== 'undefined' || typeof rule.oneOf === 'undefined') {
return rule
}

rule.oneOf.unshift({
test: /\.mdx$/,
use: [
{
loader: babelLoader.loader,
options: babelLoader.options
},
'@mdx-js/loader'
]
})

return rule
})

return config
// src/App.js

import React, { lazy, Component, Suspense } from 'react';
import { importMDX } from 'mdx.macro';

const Content = lazy(() => importMDX('./Content.mdx'));

class App extends Component {
render() {
return (
<div>
<Suspense fallback={<div>Loading...</div>}>
<Content />
</Suspense>
</div>
);
}
}

export default App;
```

And then create the following `src/Content.mdx`:

```md
# Hello, world!
```

[See the full example][cra-example]

[cra-rewired]: https://github.com/timarney/react-app-rewired
[mdx-macro]: https://www.npmjs.com/package/mdx.macro

[cra-example]: https://github.com/mdx-js/mdx/tree/master/examples/create-react-app

[known bug]: https://github.com/facebook/create-react-app/issues/5580
4 changes: 3 additions & 1 deletion examples/create-react-app/.gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# See https://help.github.com/ignore-files/ for more about ignoring files.
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage
Expand Down
23 changes: 0 additions & 23 deletions examples/create-react-app/config-overrides.js

This file was deleted.

24 changes: 15 additions & 9 deletions examples/create-react-app/package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
{
"name": "create-react-app",
"version": "0.1.0",
"private": true,
"name": "mdx-create-react-app",
"dependencies": {
"@mdx-js/loader": "^0.16.4",
"@mdx-js/mdx": "^0.16.4",
"mdx.macro": "^0.2.7",
"react": "^16.6.3",
"react-app-rewired": "^1.6.2",
"react-dom": "^16.6.3",
"react-scripts": "1.1.5"
"react-scripts": "2.1.1"
},
"scripts": {
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-app-rewired test --env=jsdom"
}
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}
4 changes: 2 additions & 2 deletions examples/create-react-app/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<!--
manifest.json provides metadata used when your web app is added to the
homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
homescreen on Android. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Expand Down
2 changes: 1 addition & 1 deletion examples/create-react-app/public/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"type": "image/x-icon"
}
],
"start_url": "./index.html",
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
Expand Down
50 changes: 44 additions & 6 deletions examples/create-react-app/readme.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,46 @@
# MDX + Create React App
# Create React App

```shell
npm install
npm start
```
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).

[See documentation](https://mdxjs.com/getting-started/create-react-app)
## Available Scripts

In the project directory, you can run:

### `npm start`

Runs the app in the development mode.<br>
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.

The page will reload if you make edits.<br>
You will also see any lint errors in the console.

### `npm test`

Launches the test runner in the interactive watch mode.<br>
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.

### `npm run build`

Builds the app for production to the `build` folder.<br>
It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.<br>
Your app is ready to be deployed!

See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.

### `npm run eject`

**Note: this is a one-way operation. Once you `eject`, you can’t go back!**

If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.

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. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.

You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.

## Learn More

You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).

To learn React, check out the [React documentation](https://reactjs.org/).
32 changes: 32 additions & 0 deletions examples/create-react-app/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
.App {
text-align: center;
}

.App-logo {
animation: App-logo-spin infinite 20s linear;
height: 40vmin;
}

.App-header {
background-color: #282c34;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
}

.App-link {
color: #61dafb;
}

@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
18 changes: 8 additions & 10 deletions examples/create-react-app/src/App.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import React, {Component} from 'react'
import {MDXProvider} from '@mdx-js/tag'
import React, {lazy, Component, Suspense} from 'react'
import {importMDX} from 'mdx.macro'

import Hello from './hello.md'

const components = {
h1: props => <h1 style={{color: 'tomato'}} {...props} />
}
const Content = lazy(() => importMDX('./Content.mdx'))

class App extends Component {
render() {
return (
<MDXProvider components={components}>
<Hello />
</MDXProvider>
<div>
<Suspense fallback={<div>Loading...</div>}>
<Content />
</Suspense>
</div>
)
}
}
Expand Down
8 changes: 8 additions & 0 deletions examples/create-react-app/src/Content.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const Demo = () => (
<div style={{ padding: 20, backgroundColor: "tomato" }} />
);

# Hello, world!

<Demo />

3 changes: 0 additions & 3 deletions examples/create-react-app/src/hello.md

This file was deleted.

14 changes: 14 additions & 0 deletions examples/create-react-app/src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
body {
margin: 0;
padding: 0;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen",
"Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue",
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

code {
font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
monospace;
}
1 change: 1 addition & 0 deletions examples/create-react-app/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react'
import ReactDOM from 'react-dom'
import './index.css'
import App from './App'

ReactDOM.render(<App />, document.getElementById('root'))
7 changes: 7 additions & 0 deletions examples/create-react-app/src/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

1 comment on commit 9cb7c3a

@vercel
Copy link

@vercel vercel bot commented on 9cb7c3a Dec 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully aliased the URL https://mdx-iperqmrboe.now.sh to the following aliases.

Please sign in to comment.