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

Turbopack experimental fields docs and schema #45560

Merged
merged 16 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
28 changes: 28 additions & 0 deletions docs/api-reference/next.config.js/resolve-alias.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
description: Configure Next.js with Turbopack to alias module resolution
---

# Resolve Alias (`next --turbo` only)

Through `next.config.js`, Turbopack can be configured to modify module resolution through aliases, similar to Webpack's [`resolve.alias`](https://Webpack.js.org/configuration/resolve/#resolvealias) configuration.
wbinnssmith marked this conversation as resolved.
Show resolved Hide resolved

> **Warning**: This feature is experimental and will only work with `next --turbo`

To configure resolve aliases, map imported patterns to their new destination in `next.config.js`:

```js
module.exports = {
experimental: {
resolveAlias: {
foo: 'bar',
baz: { browser: 'baz/browser' },
wbinnssmith marked this conversation as resolved.
Show resolved Hide resolved
},
},
}
```

This aliases imports of the `foo` package to the `bar` package. In other words, `import 'foo'` will result in loading the `bar` module instead of `foo`.

Turbopack also supports conditional aliasing through this field, similar to Node.js's [conditional exports](https://nodejs.org/docs/latest-v18.x/api/packages.html#conditional-exports). At the moment only the `browser` condition is supported. In the case above, imports of the `baz` module will be aliased to `baz/browser` when Turbopack targets browser environments.

For more information and guidance for how to migrate your app to Turbopack from Webpack, see [Turbopack's documentation on Webpack compatibility](https://turbo.build/pack/docs/migrating-from-webpack).
41 changes: 41 additions & 0 deletions docs/api-reference/next.config.js/turbopack-loaders.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
description: Configure Next.js with Turbopack to load Webpack loaders
---

# Turbopack Loaders (`next --turbo` only)

Currently, Turbopack supports a subset of Webpack's loader API, allowing you to use some Webpack loaders to transform code in Turbopack.
wbinnssmith marked this conversation as resolved.
Show resolved Hide resolved

> **Warning**: This feature is experimental and will only work with `next --turbo`

To configure loaders, add the names of the loaders you've installed and any options in `next.config.js`:

```js
module.exports = {
experimental: {
turbopackLoaders: {
'.mdx': [
{
loader: '@mdx-js/loader',
options: {
format: 'mdx',
},
},
],
'.foo': 'my-foo-loader',
wbinnssmith marked this conversation as resolved.
Show resolved Hide resolved
},
},
}
```

Then, given the above configuration, you can use transformed code from your app:

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

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

For more information and guidance for how to migrate your app to Turbopack from Webpack, see [Turbopack's documentation on Webpack compatibility](https://turbo.build/pack/docs/migrating-from-webpack).
8 changes: 8 additions & 0 deletions docs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,14 @@
{
"title": "Build indicator",
"path": "/docs/api-reference/next.config.js/build-indicator.md"
},
{
"title": "Turbopack Loaders",
"path": "/docs/api-reference/next.config.js/turbopack-loaders.md"
},
{
"title": "Resolve Alias",
"path": "/docs/api-reference/next.config.js/resolve-alias.md"
wbinnssmith marked this conversation as resolved.
Show resolved Hide resolved
}
]
}
Expand Down
3 changes: 3 additions & 0 deletions packages/next/src/server/config-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,9 @@ const configSchema = {
webpackBuildWorker: {
type: 'boolean',
},
resolveAlias: {
type: 'object',
},
turbopackLoaders: {
type: 'object',
},
Expand Down
33 changes: 31 additions & 2 deletions packages/next/src/server/config-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ export interface TypeScriptConfig {
tsconfigPath?: string
}

type JSONValue =
| string
| number
| boolean
| JSONValue[]
| { [k: string]: JSONValue }

type TurbopackLoaderItem =
| string
| {
loader: string
// At the moment, Turbopack options must be JSON-serializable, so restrict values.
options: Record<string, JSONValue>
}

export interface WebpackConfigContext {
/** Next.js root directory */
dir: string
Expand Down Expand Up @@ -157,8 +172,22 @@ export interface ExperimentalConfig {

webVitalsAttribution?: Array<typeof WEB_VITALS[number]>

// webpack loaders to use when running turbopack
turbopackLoaders?: Record<string, string | string[]>
/**
* (`next --turbo` only) A mapping of aliased imports to modules to load in their place.
*
* @see [Resolve Alias](https://nextjs.org/docs/api-reference/next.config.js/resolve-alias)
*/
resolveAlias?: Record<
wbinnssmith marked this conversation as resolved.
Show resolved Hide resolved
string,
string | string[] | Record<string, string | string[]>
>

/**
* (`next --turbo` only) A list of webpack loaders to apply when running with Turbopack.
*
* @see [Turbopack Loaders](https://nextjs.org/docs/api-reference/next.config.js/turbopack-loaders)
*/
turbopackLoaders?: Record<string, TurbopackLoaderItem | TurbopackLoaderItem[]>

turbotrace?: {
logLevel?:
Expand Down