2024-04-23 05:33:38 -05:00
# Creating a Plugin
This guide walks you through the steps to create an Angular plugin.
### 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-05-08 08:32:42 -05:00
npx nx g @nx/angular:app example-plugin --directory=apps/example-plugin --bundler=esbuild
2024-04-23 05:33:38 -05:00
```
### Step 2: Configure the Manifest
Next, create a `manifest.json` file inside the `/src/assets` directory. This file is crucial as it defines key properties of your plugin, including permissions and the entry point script.
```json
{
"name": "Example plugin",
2024-05-30 02:33:05 -05:00
"host": "http://localhost:4200",
"code": "/assets/plugin.js",
"icon": "/assets/icon.png",
2024-04-23 05:33:38 -05:00
"permissions": ["page:read", "file:read", "selection:read"]
}
```
### Step 3: Update Project Configuration
2024-06-06 08:42:52 -05:00
Now, add the plugin tag.
2024-04-23 05:33:38 -05:00
```typescript
2024-05-08 08:32:42 -05:00
"tags": ["type:plugin"],
2024-04-23 05:33:38 -05:00
```
2024-05-30 08:02:56 -05:00
Also, update `targets.build` with the following code to allow the use of Penpot styles and build the plugin code.
2024-04-23 05:33:38 -05:00
```json
2024-05-30 08:02:56 -05:00
"options": {
"styles": [
"libs/plugins-styles/src/lib/styles.css",
"apps/example-plugin/src/styles.css"
],
"optimization": {
"scripts": true,
"styles": true,
"fonts": false
}
},
"dependsOn": ["buildPlugin"]
```
Add the default port to the `serve.configurations.development` task:
```json
"development": {
// ...
"port": 4302,
2024-04-23 05:33:38 -05:00
}
```
### Step 4: Modify TypeScript Configuration
Create ``tsconfig.plugin.json` next to the `tsconfig.json` :
```json
{
"extends": "./tsconfig.json",
"compilerOptions": {
"types": []
},
"files": ["src/plugin.ts"],
"include": ["../../libs/plugin-types/index.d.ts"]
}
```
Add the reference to the main tsconfig.json:
```json
"references": [
{
"path": "./tsconfig.plugin.json"
}
],
```
2024-05-08 08:32:42 -05:00
### Strep 5: Hello world plugin code
Create the file `apps/example-plugin/src/plugin.ts` with the following code:
```ts
console.log('Hello Plugin');
```
### Step 6: Run the plugin
2024-04-23 05:33:38 -05:00
Run this command:
```sh
2024-05-30 08:02:56 -05:00
npx nx run example-plugin:init
2024-04-23 05:33:38 -05:00
```
This will run two tasks: `serve` , the usual Angular server, and `buildPlugin` , which will compile the `plugin.ts` file.
### Step 6: Load the Plugin in Penpot
Finally, to load your plugin into Penpot, execute the following command in the browser's console devtools:
```typescript
2024-05-22 01:21:06 -05:00
ɵloadPluginByUrl('http://localhost:4201/assets/manifest.json');
2024-04-23 05:33:38 -05:00
```
2024-05-30 08:02:56 -05:00
### Step 7: Build plugin
```
npx nx run example-plugin:build
```
2024-04-23 05:33:38 -05:00
### Learn More About Plugin Development
For more detailed information on plugin development, check out our guides:
- [Plugin Usage Documentation ](docs/plugin-usage.md )
- [Create API Documentation ](docs/create-api.md )
### Using a Starter Template
If you prefer to kickstart your plugin development, consider using the [Penpot Plugin Starter Template ](https://github.com/penpot/penpot-plugin-starter-template ). It's a template designed to streamline the creation process for Penpot plugins.