Skip to main content

turbo(实验)

可以自定义 Turbopack 以转换不同的文件并更改模块的解析方式。

¥Turbopack can be customized to transform different files and change how modules are resolved.

很高兴知道:

¥Good to know:

  • 这些功能是实验性的,仅适用于 next --turbo

    ¥These features are experimental and will only work with next --turbo.

  • Turbopack for Next.js 不需要加载程序,也不需要加载程序配置来实现内置功能。Turbopack 内置了对 css 和编译现代 JavaScript 的支持,因此如果你使用 @babel/preset-env,则无需 css-loaderpostcss-loaderbabel-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, or babel-loader if you're using @babel/preset-env.

webpack 加载器

¥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()d 插件模块作为选项值传递。

    ¥Options passed to webpack loaders must be plain JavaScript primitives, objects, and arrays. For example, it's not possible to pass require()d 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:

module.exports = {
experimental: {
turbo: {
rules: {
'*.svg': {
loaders: ['@svgr/webpack'],
as: '*.js',
},
},
},
},
}

很高兴知道:在 Next.js 版本 13.4.4 之前,experimental.turbo.rules 被命名为 experimental.turbo.loaders,并且仅接受 .mdx 等文件扩展名,而不接受 *.mdx

¥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

以下加载器已经过测试,可以与 Turbopack 的 webpack 加载器实现一起使用:

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

解析别名

¥Resolve aliases

通过 next.config.js,Turbopack 可以配置为通过别名修改模块解析,类似于 webpack 的 resolve.alias 配置。

¥Through next.config.js, 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:

module.exports = {
experimental: {
turbo: {
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's 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.

解决扩展问题

¥Resolve extensions

通过 next.config.js,Turbopack 可以配置为解析具有自定义扩展的模块,类似于 webpack 的 resolve.extensions 配置。

¥Through next.config.js, 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:

module.exports = {
experimental: {
turbo: {
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.