0
Fork 0
mirror of https://github.com/penpot/penpot-plugins.git synced 2025-01-08 07:50:44 -05:00
penpot-plugins/docs/create-plugin.md

109 lines
3.3 KiB
Markdown
Raw Normal View History

2024-03-27 07:57:02 -05:00
# Creating a Plugin
2024-03-12 01:48:02 -05:00
2024-06-19 07:15:23 -05:00
This guide walks you through the steps to create a plugin for our platform. You'll start by setting up the basic structure, configuring necessary files, and then running a local server to preview your plugin.
If you prefer to create the plugin with angular, there's also a [Creating a Plugin (angular)](./create-angular-plugin.md).
Keep in mind that this guide is for creating a plugin **inside `penpot-plugins` monorepo**. If you want to create a plugin outside our environment you can check the [Penpot Plugin Starter Template](https://github.com/penpot/penpot-plugin-starter-template) or the documentation at [Create a Plugin](https://help.penpot.app/technical-guide/plugins/create-a-plugin/).
Let's dive in.
2024-03-12 01:48:02 -05:00
2024-03-27 07:57:02 -05:00
### Step 1: Initialize the Plugin
First, you need to create the scaffolding for your plugin. Use the following command, replacing `example-plugin` with the name of your plugin:
```sh
2024-02-06 06:16:28 -05:00
npx nx g @nx/web:application example-plugin --directory=apps/example-plugin
2024-03-12 01:48:02 -05:00
```
2024-02-06 06:16:28 -05:00
2024-07-10 04:49:02 -05:00
### Step 2: Migrate eslint to ESM
Replace `module.exports = [` with `export default [` and const `baseConfig = require('../../eslint.base.config.js');` with `import baseConfig from '../../eslint.config.js';`.
### Step 3: Configure the Manifest
2024-03-27 07:57:02 -05:00
Next, create a `manifest.json` file inside the `/public` directory. This file is crucial as it defines key properties of your plugin, including permissions and the entry point script.
2024-02-06 06:16:28 -05:00
```json
{
2024-03-27 07:57:02 -05:00
"name": "Example Plugin",
2024-05-30 02:33:05 -05:00
"host": "http://localhost:4201",
"code": "/plugin.js",
"icon": "/icon.png",
2024-07-02 03:55:49 -05:00
"permissions": [
"content:read",
"content:write",
"library:read",
"library:write",
"user:read"
]
2024-02-06 06:16:28 -05:00
}
```
2024-07-10 04:49:02 -05:00
### Step 4: Update Vite Configuration
2024-02-06 06:16:28 -05:00
2024-03-27 07:57:02 -05:00
Now, add the following configuration to your `vite.config.ts` to specify the entry points for the build process:
```typescript
2024-02-06 06:16:28 -05:00
build: {
rollupOptions: {
input: {
plugin: 'src/plugin.ts',
index: './index.html',
},
output: {
entryFileNames: '[name].js',
},
},
}
```
2024-07-10 04:49:02 -05:00
### Step 5: Modify TypeScript Configuration
2024-03-27 07:57:02 -05:00
Update your `tsconfig.app.json` to include the necessary TypeScript files for your plugin:
2024-02-27 08:50:38 -05:00
```json
2024-03-27 07:57:02 -05:00
{
"include": ["src/**/*.ts", "../../libs/plugin-types/index.d.ts"]
}
2024-02-27 08:50:38 -05:00
```
2024-07-10 04:49:02 -05:00
### Step 6: Run a Static Server
2024-02-06 06:16:28 -05:00
2024-03-27 07:57:02 -05:00
To preview your plugin, start a static server by running:
```sh
npx nx run example-plugin:build --watch & npx nx run example-plugin:preview
2024-03-12 01:48:02 -05:00
```
2024-07-10 04:49:02 -05:00
### Step 7: Add TS parser to eslint
2024-06-12 07:30:32 -05:00
Add these options to the end of the `eslint.config.js` file to allow linting with type information:
```js
{
languageOptions: {
parserOptions: {
project: './tsconfig.*?.json',
tsconfigRootDir: import.meta.dirname,
},
},
},
```
2024-07-10 04:49:02 -05:00
### Step 8: Load the Plugin in Penpot
2024-03-27 07:57:02 -05:00
2024-06-19 07:15:23 -05:00
To load your plugin into Penpot you can use the shortcut `Ctrl + Alt + P` to directly open the Plugin manager modal. There you need to provide the plugin's manifest URL (example: `http://plugin.example/manifest.json`) for the installation. If there's no issues the plugin will be installed and then you would be able to open it whenever you like.
2024-02-06 06:16:28 -05:00
2024-06-19 07:15:23 -05:00
You can also open the Plugin manager modal via:
2024-03-12 01:48:02 -05:00
2024-06-19 07:15:23 -05:00
- Menu
2024-03-27 07:57:02 -05:00
2024-06-19 07:15:23 -05:00
![Penpot's menu image](./images/plugin-menu.png)
2024-03-27 07:57:02 -05:00
2024-06-19 07:15:23 -05:00
### Learn More About Plugin Development
2024-03-27 07:57:02 -05:00
2024-06-19 07:15:23 -05:00
For more detailed information on plugin development, check out our guides:
2024-03-12 01:48:02 -05:00
2024-06-19 07:15:23 -05:00
- [Create API Documentation](./create-api.md)