-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add @next/mdx
to Next.js getting started guide
#2040
Changes from all commits
7e5b7d6
4d847a3
53c814a
f2586e9
c634403
b3b2c09
2638533
6be23f7
b6dd3a2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -549,7 +549,7 @@ the loader to the webpack config, by rewiring `react-scripts` using | |
|
||
<Note type="info"> | ||
**Note**: warnings about CRACO having `incorrect peer dependency | ||
"react-scripts@^4.0.0"` can be ignored for the above to work. | ||
"react-scripts@^4.0.0"` can be ignored for the above to work. | ||
</Note> | ||
|
||
See also [¶ Webpack][webpack], which is used in CRA, and see [¶ React][react], | ||
|
@@ -573,40 +573,67 @@ on how to use MDX with Gatsby. | |
<summary>Expand example</summary> | ||
|
||
```js path="next.config.js" | ||
module.exports = { | ||
// Prefer loading of ES Modules over CommonJS | ||
experimental: {esmExternals: true}, | ||
import nextMDX from '@next/mdx' | ||
|
||
const withMDX = nextMDX({ | ||
// By default only the .mdx extension is supported. | ||
extension: /\.mdx?$/, | ||
options: {/* providerImportSource: …, otherOptions… */} | ||
}) | ||
|
||
export default withMDX({ | ||
// Support MDX files as pages: | ||
pageExtensions: ['md', 'mdx', 'tsx', 'ts', 'jsx', 'js'], | ||
// Support loading `.md`, `.mdx`: | ||
webpack(config, options) { | ||
config.module.rules.push({ | ||
test: /\.mdx?$/, | ||
use: [ | ||
// The default `babel-loader` used by Next: | ||
options.defaultLoaders.babel, | ||
{ | ||
loader: '@mdx-js/loader', | ||
/** @type {import('@mdx-js/loader').Options} */ | ||
options: {/* jsxImportSource: …, otherOptions… */} | ||
} | ||
] | ||
}) | ||
|
||
return config | ||
} | ||
} | ||
}) | ||
``` | ||
</details> | ||
|
||
[Next](https://nextjs.org) supports webpack loaders by overwriting the webpack | ||
config in `next.config.js`. | ||
[Next.js](https://nextjs.org) has its own package to support MDX. | ||
|
||
Install and configure the webpack loader [`@mdx-js/loader`][webpack]. | ||
Install and configure [`@next/mdx`][@next/mdx]. | ||
remcohaszing marked this conversation as resolved.
Show resolved
Hide resolved
|
||
There is no need to configure your JSX runtime as React is already set up. | ||
|
||
See also [¶ Webpack][webpack] and [¶ React][react], which you’re using, for | ||
more on those tools. | ||
The MDX provider can be configured in [`pages/_app.js`][next-app]. | ||
In order to use it, you need to configure the `providerImportSource` as | ||
well. | ||
|
||
<details> | ||
<summary>Expand example</summary> | ||
|
||
```js path="next.config.js" | ||
import nextMDX from '@next/mdx' | ||
|
||
const withMDX = nextMDX({ | ||
// By default only the .mdx extension is supported. | ||
extension: /\.mdx?$/, | ||
options: {providerImportSource: '@mdx-js/react', /* otherOptions… */} | ||
}) | ||
|
||
export default withMDX({ | ||
// Support MDX files as pages: | ||
pageExtensions: ['md', 'mdx', 'tsx', 'ts', 'jsx', 'js'], | ||
}) | ||
``` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it needed to repeat this? Maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It’s different. It includes the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps a diff would be clearer, to focus on only adding the provider import source? |
||
|
||
```jsx path="pages/_app.js" | ||
import {MDXProvider} from '@mdx-js/react' | ||
import {Header} from '../components/Header.js' | ||
|
||
const components = { | ||
h1: Header | ||
} | ||
|
||
export default function App({Component, pageProps}) { | ||
return ( | ||
<MDXProvider components={components}> | ||
<Component {...pageProps} /> | ||
</MDXProvider> | ||
) | ||
} | ||
``` | ||
</details> | ||
|
||
See [Using MDX with Next.js][next-mdx] for more details. | ||
remcohaszing marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#### Parcel | ||
|
||
|
@@ -842,6 +869,8 @@ See their readmes on how to configure them. | |
* If you’re getting errors integrating MDX, see | ||
[§ Troubleshooting MDX][trouble] or [§ Support][support] | ||
|
||
[@next/mdx]: https://github.com/vercel/next.js/tree/canary/packages/next-mdx | ||
remcohaszing marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
[svelte-jsx]: https://github.com/kenoxa/svelte-jsx | ||
|
||
[jsx]: #jsx | ||
|
@@ -874,6 +903,10 @@ See their readmes on how to configure them. | |
|
||
[mdx-vue]: /packages/vue/ | ||
|
||
[next-app]: https://nextjs.org/docs/advanced-features/custom-app | ||
|
||
[next-mdx]: https://nextjs.org/docs/advanced-features/using-mdx | ||
|
||
[evaluate]: /packages/mdx/#evaluatefile-options | ||
|
||
[options-jsximportsource]: /packages/mdx/#optionsjsximportsource | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe Next itself, on pages, asks to first assign to a const, and the export default, so that there’s a name attached.
Should we do the same, or is it fine?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don’t really understand what you mean, but this syntax is very similar to this paragraph on using MDX.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe there’s a warning in Next.js for:
It wants you to assign to a name:
That is to attach a name. I think that’s good here too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn’t a page, it’s the configuration file (
next.config.js
).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand it is not a page.
I use Next.js recommendation as an example.
It is still good to name things.
My assumption is that errors will occur and a name will improve stack traces.