插件创建
¥Plugin creation
创建 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 项目外部的插件。但是,如果你想在现有项目中开发插件,步骤基本上保持不变。如果你不是 使用单一存储库,步骤完全相同。
¥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
)。
¥yalc must be installed globally (with npm install -g yalc
or 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
).