Skip to content
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

Update documentation on webpack loaders in Turbopack #64296

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 5 additions & 59 deletions docs/02-app/02-api-reference/05-next-config-js/turbo.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -28,83 +28,29 @@ module.exports = {
experimental: {
turbo: {
rules: {
// Option format
'*.md': [
{
loader: '@mdx-js/loader',
options: {
format: 'md',
},
},
],
// Option-less format
'*.mdx': ['@mdx-js/loader'],
'*.svg': {
loaders: ['@svgr/webpack'],
as: '*.js',
},
},
},
},
}
```

Using the configuration above, you can use transformed code in your app:

```js
import MyDoc from './my-doc.mdx'

export default function Home() {
return <MyDoc />
}
```

> **Good to know**: Prior to Next.js version 13.4.4, `experimental.turbo.rules` was named `experimental.turbo.loaders` and only accepted file extensions like `.mdx` instead of `*.mdx`.

### Supported loaders

The following loaders have been tested to work with Turbopack's webpack loader implementation:

- [`babel-loader`](https://www.npmjs.com/package/babel-loader)
- [`@mdx-js/loader`](https://www.npmjs.com/package/@mdx-js/loader)
- [`@svgr/webpack`](https://www.npmjs.com/package/@svgr/webpack)
- [`svg-inline-loader`](https://www.npmjs.com/package/svg-inline-loader)
- [`yaml-loader`](https://www.npmjs.com/package/yaml-loader)
- [`string-replace-loader`](https://www.npmjs.com/package/string-replace-loader)
- [`raw-loader`](https://www.npmjs.com/package/raw-loader)

### Custom loaders

If you need to pass something like the result of importing an external package as a loader option, it's possible to wrap the webpack loader with your own, specifying options there. **This is an interim solution and should not be necessary in the future.**

This loader wraps `@mdx-js/loader` and configures the `rehypePrism` rehype plugin:

```js filename="my-mdx-loader.js"
const mdxLoader = require('@mdx-js/loader')
const rehypePrism = require('@mapbox/rehype-prism')

module.exports = function (code) {
const prevGetOptions = this.getOptions.bind(this)
this.getOptions = function getOptions(...args) {
return {
...prevGetOptions(...args),
rehypePlugins: [rehypePrism],
}
}

mdxLoader.call(this, code)
}
```

Then, configure Next.js to load the wrapper loader:

```js filename="next.config.js"
module.exports = {
experimental: {
turbo: {
rules: {
'*.mdx': ['./my-mdx-loader'],
},
},
},
}
```
- ['sass-loader'](https://www.npmjs.com/package/sass-loader)

## Resolve aliases

Expand Down