Skip to main content

Turbopack

Turbopack(测试版)是一个针对 JavaScript 和 TypeScript 进行优化的增量打包器,用 Rust 编写,并内置于 Next.js 中。

¥Turbopack (beta) is an incremental bundler optimized for JavaScript and TypeScript, written in Rust, and built into Next.js.

用法

¥Usage

Turbopack 可在 pagesapp 目录中的 Next.js 中使用,以加快本地开发速度。要启用 Turbopack,请在运行 Next.js 开发服务器时使用 --turbo 标志。

¥Turbopack can be used in Next.js in both the pages and app directories for faster local development. To enable Turbopack, use the --turbo flag when running the Next.js development server.

{
"scripts": {
"dev": "next dev --turbo",
"build": "next build",
"start": "next start",
"lint": "next lint"
}
}

支持的功能

¥Supported features

Next.js 中的 Turbopack 需要大多数用户进行零配置,并且可以针对更高级的用例进行扩展。要了解有关 Turbopack 当前支持的功能的更多信息,请查看 API 参考

¥Turbopack in Next.js requires zero-configuration for most users and can be extended for more advanced use cases. To learn more about the currently supported features for Turbopack, view the API Reference.

不支持的功能

¥Unsupported features

Turbopack 目前仅支持 next dev,不支持 next build。随着我们越来越接近稳定,我们目前正在努力提供对构建的支持。

¥Turbopack currently only supports next dev and does not support next build. We are currently working on support for builds as we move closer towards stability.

目前不支持这些功能:

¥These features are currently not supported:

  • next.config.js 中的 webpack() 配置

    ¥webpack() configuration in next.config.js

    • Turbopack 取代了 Webpack,这意味着不支持 webpack 配置。

      ¥Turbopack replaces Webpack, this means that webpack configuration is not supported.

    • 要配置 Turbopack,请参阅文档

      ¥To configure Turbopack, see the documentation.

    • Turbopack 支持 Webpack 加载器 的子集。

      ¥A subset of Webpack loaders are supported in Turbopack.

  • Babel (.babelrc)

    • Turbopack 利用 SWC 编译器进行所有转译和优化。这意味着默认情况下不包含 Babel。

      ¥Turbopack leverages the SWC compiler for all transpilation and optimizations. This means that Babel is not included by default.

    • 如果你有 .babelrc 文件,你可能不再需要它,因为 Next.js 包含常见的 Babel 插件作为可以启用的 SWC 转换。你可以在 编译器文档 中阅读更多相关内容。

      ¥If you have a .babelrc file, you might no longer need it because Next.js includes common Babel plugins as SWC transforms that can be enabled. You can read more about this in the compiler documentation.

    • 如果在验证你的特定用例未涵盖后仍然需要使用 Babel,你可以利用 Turbopack 的 支持自定义 webpack 加载器 来包含 babel-loader

      ¥If you still need to use Babel after verifying your particular use case is not covered, you can leverage Turbopack's support for custom webpack loaders to include babel-loader.

  • 在 App Router 中自动创建根布局。

    ¥Creating a root layout automatically in App Router.

    • 目前不支持此行为,因为它会更改输入文件,相反,如果你在所需位置手动添加根布局,则会显示错误。

      ¥This behavior is currently not supported since it changes input files, instead, an error will be shown for you manually add a root layout in the desired location.

  • @next/font(旧字体支持)。

    ¥@next/font (legacy font support).

    • @next/font 已被弃用,取而代之的是 next/font。Turbopack 完全支持 next/font

      ¥@next/font is deprecated in favor of next/font. next/font is fully supported with Turbopack.

  • new Worker('file', import.meta.url)

    • 我们计划在未来实现这一点。

      ¥We are planning to implement this in the future.

  • 继电器变换

    ¥Relay transforms

    • 我们计划在未来实现这一点。

      ¥We are planning to implement this in the future.

  • experimental.nextScriptWorkers

    • 我们计划在未来实现这一点。

      ¥We are planning to implement this in the future.

  • AMP

    • 我们目前不打算通过 Turbopack 在 Next.js 中支持 AMP。

      ¥We are currently not planning to support AMP in Next.js with Turbopack.

  • Yarn 即插即用

    ¥Yarn PnP

    • 我们目前不打算通过 Turbopack 在 Next.js 中支持 Yarn PnP。

      ¥We are currently not planning to support Yarn PnP in Next.js with Turbopack.

  • experimental.urlImports

    • 我们目前不打算通过 Turbopack 在 Next.js 中支持 experimental.urlImports

      ¥We are currently not planning to support experimental.urlImports in Next.js with Turbopack.

生成跟踪文件

¥Generating Trace Files

跟踪文件允许 Next.js 团队调查和改进性能指标和内存使用情况。要生成跟踪文件,请将 NEXT_TURBOPACK_TRACING=1 附加到 next dev --turbo 命令,这将生成 .next/trace.log 文件。

¥Trace files allow the Next.js team to investigate and improve performance metrics and memory usage. To generate a trace file, append NEXT_TURBOPACK_TRACING=1 to the next dev --turbo command, this will generate a .next/trace.log file.

报告与 Turbopack 性能和内存使用相关的问题时,请在 GitHub 问题中包含跟踪文件。

¥When reporting issues related to Turbopack performance and memory usage, please include the trace file in your GitHub issue.