生成代码
将你的 monorepo 拆分为包是组织代码、加快任务速度和改善本地开发体验的好方法。使用 Turborepo 的代码生成功能,你可以轻松地以结构化的方式为包、模块甚至单个 UI 组件生成新的源代码,并将其与存储库的其余部分集成。
¥Splitting your monorepo into packages is a great way to organize your code, speed up tasks, and improve the local development experience. With Turborepo's code generation, it's easy to generate new source code for packages, modules, and even individual UI components in a structured way that integrates with the rest of your repository.
添加空包
¥Add an empty package
将一个新的空应用或包添加到你的 monorepo 中。
¥Add a new, empty app or package to your monorepo.
查看所有适用于 gen workspace
的 options。
¥View all available options for gen workspace
.
复制现有包
¥Copy an existing package
你可以将现有工作区用作新应用或软件包的模板。这适用于你现有 monorepo 中的工作区,以及来自其他存储库(通过 GitHub URL 指定)的远程工作区。
¥You can use an existing workspace as a template for your new app or package. This works for both workspaces within your existing monorepo, and remote workspaces from other repositories (specified via GitHub URL).
示例
¥Examples
通过从代码库中现有的包复制,在你的 monorepo 中创建一个新包。
¥Create a new package in your monorepo by copying from an existing package in your repo.
通过从远程包复制,在你的 monorepo 中创建一个新的工作区。
¥Create a new workspace in your monorepo by copying from a remote package.
注意:从远程源添加时,Turborepo 无法验证你的代码库是否包含所有必需的依赖,以及是否使用了正确的包管理器。在这种情况下,可能需要进行一些手动修改才能使新的工作区在你的代码库中按预期工作。
¥Note: When adding from a remote source, Turborepo is unable to verify that your repo has all of the required dependencies, and is using the correct package manager. In this case, some manual modifications may be required to get the new workspace working as expected within your repository.
查看所有适用于 gen workspace --copy
的 options。
¥View all available options for gen workspace --copy
.
自定义生成器
¥Custom generators
如果内置生成器无法满足你的需求,你可以使用 Plop 配置创建自己的自定义生成器。Turborepo 会自动检测你仓库中的所有生成器配置,并使它们可从命令行运行。
¥If a built-in generator does not fit your needs, you can create your own custom generator using Plop configurations. Turborepo will automatically detect any generator configurations within your repo, and make them available to run from the command line.
虽然 Turborepo 生成器基于 Plop 构建,但它们不需要将 plop
作为依赖安装在你的代码库中。
¥While Turborepo Generators are built on top of Plop, they don't require plop
to be installed as a dependency in your repo.
Turborepo 可以理解所有 Plop 配置选项和功能,它还提供了几个附加功能来提升在配置了 Turborepo 的仓库中编写生成器的体验。
¥While Turborepo understands all Plop configuration options and features, it provides a few additional features to improve the experience of writing generators within a repo configured with Turborepo.
-
生成器会在每个工作区自动发现、加载和组织(无需在单个配置文件中手动
load
生成器)。¥Generators are automatically discovered, loaded, and organized per workspace (no need to manually
load
them within a single configuration file) -
生成器会自动从定义它们的工作区根目录运行。
¥Generators are automatically run from the root of the workspace where they are defined
-
生成器可以在代码库中的任何位置调用(或通过
--root
标志在代码库外部调用)。¥Generators can be invoked from anywhere within your repo (or outside it via the
--root
flag) -
TypeScript 生成器无需配置即可支持
¥TypeScript generators are supported with zero configuration
-
plop
不需要作为你的代码库的依赖安装。¥
plop
is not required to be installed as a dependency of your repo
Known issue
自定义生成器目前不支持 ESM 依赖。
¥ESM dependencies are not currently supported within custom generators.
入门
¥Getting started
要构建并运行自定义生成器,请使用 Turborepo 在 monorepo 中的任何位置运行以下命令。
¥To build and run a custom generator, run the following command from anywhere within your monorepo using Turborepo.
系统将提示你选择一个现有的生成器,或者如果你还没有生成器,则需要创建一个。你也可以在代码库根目录下的 turbo/generators/config.ts
(或 config.js
)手动创建配置。 - 或在任何工作区内。
¥You'll be prompted to select an existing generator or to create one if you don't have any yet. You can also create your configuration
manually at turbo/generators/config.ts
(or config.js
) at the root of your repo - or within any workspace.
如果你使用的是 TypeScript,则需要将 @turbo/gen
包 安装为 devDependency
才能访问所需的 TS 类型。
¥If you are using TypeScript, you will need to install the @turbo/gen
package as
a devDependency
to access the required TS types.
例如,下面展示了一个包含三个生成器位置的 monorepo:
¥For example, the following illustrates a monorepo with three locations for generators:
在工作区内创建的生成器会自动从工作区根目录运行,而不是代码库根目录,也不是生成器配置的位置。
¥Generators created within workspaces are automatically run from the workspace root, not the repo root, nor the location of the generator configuration.
这使得你的生成器更易于编写。在 [workspace-root]
目录下创建文件只需指定为 <file>
目录,而不是 ../../<file>
目录。
¥This makes your generators more simple to write. Creating a file at [workspace-root]
only needs to be specified as <file>
rather than ../../<file>
.
了解更多关于 使用 Plop 创建自定义生成器 的信息。
¥Learn more about creating custom generators using Plop.
正在编写生成器
¥Writing generators
生成器配置文件是一个返回 Plop 配置对象的函数。该配置对象用于定义生成器的提示和操作。
¥A generator configuration file is a function that returns a Plop configuration object. The configuration object is used to define the generator's prompts, and actions.
最简单的生成器配置文件如下所示:
¥In its simplest form, a generator configuration file looks like:
提示
¥Prompts
提示符使用 Plop 提示 编写,用于收集用户信息。
¥Prompts are written using Plop prompts and are used to gather information from the user.
操作
¥Actions
操作可以使用你自己定义的 内置 Plop 操作 或 自定义操作函数:
¥Actions can use built-in Plop actions, or custom action functions that you define yourself:
运行生成器
¥Running generators
创建生成器配置文件后,你可以跳过选择提示,直接使用以下命令运行指定的生成器:
¥Once you have created your generator configuration file, you can skip the selection prompt and directly run a specified generator with:
也可以使用 --args
将参数直接传递给生成器提示符。
¥Arguments can also be passed directly to the generator prompts using --args
有关更多信息,请参阅 Plop 文档中的 绕过提示。
¥See bypassing prompts in the Plop documentation for more information.
查看所有适用于 gen
的 options。
¥View all available options for gen
.