插件创建
🌐 Plugin creation
Page summary:插件 SDK 可以在没有 Strapi 项目的情况下生成插件,并使用
watch:link和 yalc 将它们链接到现有应用。在本指南中:构建和验证命令用于打包插件以发布到 npm 或市场,以及用于 monorepo 和本地设置的信息。
创建 Strapi 5 插件的方法有很多,但最快和推荐的方法是使用插件 SDK。
🌐 There are many ways to create a Strapi 5 plugin, but the fastest and recommended way is to use the Plugin SDK.
插件 SDK 是一组命令,用于开发插件以将其用作本地插件或将它们发布在 NPM 上和/或将它们提交到 Marketplace。
🌐 The Plugin SDK is a set of commands orientated around developing plugins to use them as local plugins or to publish them on NPM and/or submit them to the Marketplace.
使用插件 SDK,你无需在创建插件之前设置 Strapi 项目。
🌐 With the Plugin SDK, you do not need to set up a Strapi project before creating a plugin.
本指南涵盖了从零创建插件、将其链接到现有 Strapi 项目以及发布插件。如果你已经有现有插件,则可以改为对插件设置进行改造以使用插件 SDK 命令(请参阅 插件 SDK 参考 获取可用命令的完整列表)。
🌐 The present guide covers creating a plugin from scratch, linking it to an existing Strapi project, and publishing the plugin. If you already have an existing plugin, you can instead retrofit the plugin setup to utilise the Plugin SDK commands (please refer to the Plugin SDK reference for a full list of available commands).
本指南假设你希望开发一个独立于你的 Strapi 项目的插件。然而,如果你想在现有项目中开发插件,步骤基本保持不变。如果你没有使用 monorepo,步骤完全相同。
🌐 This guide assumes you want to develop a plugin external to your Strapi project. However, the steps largely remain the same if you want to develop a plugin within your existing project. If you are not using a monorepo the steps are exactly the same.
yalc 必须全局安装(使用 npm install -g yalc 或 yarn global add yalc)。
开始使用插件 SDK
🌐 Getting started with the Plugin SDK
插件 SDK 可帮助你创建插件、将其链接到现有 Strapi 项目并构建它以供发布。
🌐 The Plugin SDK helps you creating a plugin, linking it to an existing Strapi project, and building it for publishing.
命令及其参数的完整列表可在 插件 SDK 参考 中找到。本页面将指导如何使用主要命令。
🌐 The full list of commands and their parameters are available in the Plugin SDK reference. The present page will guide on using the main ones.
创建插件
🌐 Creating the plugin
要创建插件,请确保你位于要创建插件的父目录中,然后运行以下命令:
🌐 To create your plugin, ensure you are in the parent directory of where you want it to be created and run the following command:
- Yarn
- NPM
yarn dlx @strapi/sdk-plugin init my-strapi-plugin
npx @strapi/sdk-plugin init my-strapi-plugin
路径 my-strapi-plugin 可以替换为你想为插件命名的任何名称,包括它应该创建的位置的路径(例如,code/strapi-plugins/my-new-strapi-plugin)。
🌐 The path my-strapi-plugin can be replaced with whatever you want to call your plugin, including the path to where it should be created (e.g., code/strapi-plugins/my-new-strapi-plugin).
你将会通过一系列提示来帮助你设置插件。如果你对所有选项都选择了“是”,最终的结构将类似于默认的 插件结构。
🌐 You will be ran through a series of prompts to help you setup your plugin. If you selected yes to all options the final structure will be similar to the default plugin structure.