主题
turbopack
turbopack
选项允许你自定义 Turbopack 以转换不同的文件并更改模块的解析方式。
¥The turbopack
option lets you customize Turbopack to transform different files and change how modules are resolved.
ts
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
turbopack: {
// ...
},
}
export default nextConfig
js
/** @type {import('next').NextConfig} */
const nextConfig = {
turbopack: {
// ...
},
}
module.exports = nextConfig
需要了解:
¥Good to know:
Turbopack for Next.js 不需要加载程序,也不需要加载程序配置来实现内置功能。Turbopack 内置了对 CSS 和编译现代 JavaScript 的支持,因此如果你使用
@babel/preset-env
,则不需要css-loader
、postcss-loader
或babel-loader
。¥Turbopack for Next.js does not require loaders nor loader configuration for built-in functionality. Turbopack has built-in support for CSS and compiling modern JavaScript, so there's no need for
css-loader
,postcss-loader
, orbabel-loader
if you're using@babel/preset-env
.
参考
¥Reference
选项
¥Options
turbo
配置有以下选项:
¥The following options are available for the turbo
configuration:
选项 | 描述 |
---|---|
root | 设置应用根目录。应为绝对路径。 |
rules | 使用 Turbopack 运行时要应用的受支持 webpack 加载器列表。 |
resolveAlias | 将别名导入映射到模块以加载到其位置。 |
resolveExtensions | 导入文件时要解析的扩展列表。 |
支持的加载器
¥Supported loaders
以下加载器已经过测试,可以与 Turbopack 的 webpack 加载器实现一起使用:
¥The following loaders have been tested to work with Turbopack's webpack loader implementation:
示例
¥Examples
根目录
¥Root directory
Turbopack 使用根目录来解析模块。项目根目录之外的文件不会被解析。
¥Turbopack uses the root directory to resolve modules. Files outside of the project root are not resolved.
Next.js 会自动检测项目的根目录。它通过查找以下文件之一来实现:
¥Next.js automatically detects the root directory of your project. It does so by looking for one of these files:
pnpm-lock.yaml
package-lock.json
yarn.lock
bun.lock
bun.lockb
如果你的项目结构不同,例如不使用工作区,你可以手动设置 root
选项:
¥If you have a different project structure, for example if you don't use workspaces, you can manually set the root
option:
js
const path = require('path')
module.exports = {
turbopack: {
root: path.join(__dirname, '..'),
},
}
配置 webpack 加载器
¥Configuring webpack loaders
如果你需要内置之外的加载器支持,许多 webpack 加载器已经可以与 Turbopack 配合使用。目前存在一些限制:
¥If you need loader support beyond what's built in, many webpack loaders already work with Turbopack. There are currently some limitations:
仅实现了 webpack 加载器 API 的核心子集。目前,对于一些流行的加载器已经有足够的覆盖,我们将在未来扩展我们的 API 支持。
¥Only a core subset of the webpack loader API is implemented. Currently, there is enough coverage for some popular loaders, and we'll expand our API support in the future.
仅支持返回 JavaScript 代码的加载器。目前不支持转换样式表或图片等文件的加载器。
¥Only loaders that return JavaScript code are supported. Loaders that transform files like stylesheets or images are not currently supported.
传递给 webpack 加载器的选项必须是纯 JavaScript 原语、对象和数组。例如,无法将
require()
插件模块作为选项值传递。¥Options passed to webpack loaders must be plain JavaScript primitives, objects, and arrays. For example, it's not possible to pass
require()
plugin modules as option values.
要配置加载器,请在 next.config.js
中添加已安装加载器的名称及其所有选项,并将文件扩展名映射到加载器列表。
¥To configure loaders, add the names of the loaders you've installed and any options in next.config.js
, mapping file extensions to a list of loaders.
下面是一个使用 @svgr/webpack
加载器的示例,它可以导入 .svg
文件并将其渲染为 React 组件。
¥Here is an example below using the @svgr/webpack
loader, which enables importing .svg
files and rendering them as React components.
js
module.exports = {
turbopack: {
rules: {
'*.svg': {
loaders: ['@svgr/webpack'],
as: '*.js',
},
},
},
}
需要了解:在 Next.js 版本 13.4.4 之前,
turbo.rules
被命名为turbo.loaders
,并且仅接受.mdx
等文件扩展名,而不接受*.mdx
。¥Good to know: Prior to Next.js version 13.4.4,
turbo.rules
was namedturbo.loaders
and only accepted file extensions like.mdx
instead of*.mdx
.
解析别名
¥Resolving aliases
Turbopack 可以配置为通过别名修改模块解析,类似于 webpack 的 resolve.alias
配置。
¥Turbopack can be configured to modify module resolution through aliases, similar to webpack's resolve.alias
configuration.
要配置解析别名,请将导入的模式映射到 next.config.js
中的新目标:
¥To configure resolve aliases, map imported patterns to their new destination in next.config.js
:
js
module.exports = {
turbopack: {
resolveAlias: {
underscore: 'lodash',
mocha: { browser: 'mocha/browser-entry.js' },
},
},
}
这会将 underscore
包的导入别名为 lodash
包。换句话说,import underscore from 'underscore'
将加载 lodash
模块而不是 underscore
。
¥This aliases imports of the underscore
package to the lodash
package. In other words, import underscore from 'underscore'
will load the lodash
module instead of underscore
.
Turbopack 还支持通过此字段进行条件别名,类似于 Node.js 的 条件导出。目前仅支持 browser
条件。在上述情况下,当 Turbopack 面向浏览器环境时,mocha
模块的导入将被别名为 mocha/browser-entry.js
。
¥Turbopack also supports conditional aliasing through this field, similar to Node.js' conditional exports. At the moment only the browser
condition is supported. In the case above, imports of the mocha
module will be aliased to mocha/browser-entry.js
when Turbopack targets browser environments.
解析自定义扩展
¥Resolving custom extensions
Turbopack 可以配置为使用自定义扩展来解析模块,类似于 webpack 的 resolve.extensions
配置。
¥Turbopack can be configured to resolve modules with custom extensions, similar to webpack's resolve.extensions
configuration.
要配置解析扩展,请使用 next.config.js
中的 resolveExtensions
字段:
¥To configure resolve extensions, use the resolveExtensions
field in next.config.js
:
js
module.exports = {
turbopack: {
resolveExtensions: ['.mdx', '.tsx', '.ts', '.jsx', '.js', '.mjs', '.json'],
},
}
这会使用提供的列表覆盖原始解析扩展。确保包含默认扩展名。
¥This overwrites the original resolve extensions with the provided list. Make sure to include the default extensions.
有关如何将应用从 webpack 迁移到 Turbopack 的更多信息和指南,请参阅 Turbopack 有关 webpack 兼容性的文档。
¥For more information and guidance for how to migrate your app to Turbopack from webpack, see Turbopack's documentation on webpack compatibility.
版本历史
¥Version History
版本 | 更改 |
---|---|
15.3.0 | experimental.turbo 已更改为 turbopack 。 |
13.0.0 | experimental.turbo 已引入。 |