diff --git a/.serena/memories/plugins/core.md b/.serena/memories/plugins/core.md index a793ba866b..84b296a117 100644 --- a/.serena/memories/plugins/core.md +++ b/.serena/memories/plugins/core.md @@ -1,44 +1,6 @@ -# Plugins Architecture and Workflow +# Plugins -`plugins/`: standalone TypeScript/pnpm workspace for Plugin API packages and sample plugins. Related to, distinct from, frontend CLJS Plugin API runtime. - -## Layout - -- `libs/plugin-types`: TypeScript declarations for the public Penpot Plugin API. Type-only package; runtime behavior is implemented elsewhere. -- `libs/plugins-runtime`: runtime that loads plugins and exposes/generated API behavior to plugin code. -- `libs/plugins-styles`: reusable styling package for plugins. Keep its root - `styles.css`: it imports `src/lib/styles.css` and mirrors the bundled file - `tools/scripts/build-css.mjs` publishes. When a consumer depends on the same - version as this package, pnpm links the source tree instead of the published - package, and the documented `@penpot/plugin-styles/styles.css` import must - still resolve. -- `apps/*-plugin`: sample/development plugins. `apps/e2e`: plugin e2e tests. - -## Dev Workflow - -- From `plugins/`: install `pnpm -r install`; runtime dev server `pnpm run start` or `pnpm run start:app:runtime`; sample plugin `pnpm run start:plugin:`; build runtime `pnpm run build:runtime`; build plugins `pnpm run build:plugins`; lint `pnpm run lint`; format `pnpm run format:check` / `pnpm run format`; tests `pnpm run test`; e2e `pnpm run test:e2e`. -- If a change affects public Plugin API types or runtime, update `plugins/CHANGELOG.md`. Prefix type/signature entries with `**plugin-types:**`; runtime behavior entries with `**plugin-runtime:**`. -- Cross-cutting testing principles and anti-patterns: `mem:testing`. -- JS Plugin API behavior inside Penpot app: `mem:frontend/plugin-api-to-cljs-binding`; TS declarations are not runtime code; many API objects are CLJS proxies in `frontend/src/app/plugins/*.cljs`. - -## Sandbox and global cleanup - -- `src/index.ts` calls `bootstrapSES()` (in `src/lib/bootstrap.ts`) at module load. If `repairIntrinsics` throws (e.g. SES 2.2+ rejects a `Temporal` wrapped by a browser extension), the error is caught, `getPluginsRuntimeError()` exposes it, and `initPluginsRuntime` installs no `ɵ*` globals: the frontend app still boots, plugins and MCP are unavailable in that environment. On failure, `Function.prototype.toString` has already been replaced but no intrinsics are hardened and no compartment/sandbox is created — never run plugins on that path. -- The runtime uses SES compartments. Public API return values are passed through `ses.safeReturn` before crossing back to plugin code. -- Plugin `fetch` is sanitized: credentials are omitted and Authorization is blanked. The exposed response only includes ok/status/statusText/url/text/json. -- Timer callbacks are wrapped to mark plugin-originated errors, and timeout/interval IDs are tracked so plugin close can clear them. -- Plugin-originated errors are tracked in a WeakMap instead of mutating error objects, because SES can freeze errors. -- Closing a plugin removes public API keys from the compartment globalThis. - -## Lifecycle - -- Loading a plugin closes existing non-background plugins and removes them from the runtime registry. Background plugins (`allowBackground`) keep running and must stay registered: the registry routes their UI `postMessage` traffic by sender iframe and is what `ɵunloadPlugin` searches. -- If sandbox evaluation fails, the runtime marks the error as plugin-originated, closes the plugin, and rethrows. -- `plugin-manager` removes event listeners, timers, intervals, and modal state on close, and marks the plugin destroyed. Listener callbacks check that flag because Penpot events can fire after close. - -## Modal/UI behavior - -- Modal URL preparation differs by manifest version: v1 uses query string parameters, v2 puts parameters in the URL hash. -- `openModal` is idempotent for the same iframe source and avoids reopening when the target URL is already displayed. -- Modal permissions are derived from manifest permissions (`allow:downloads`, `clipboard:read`, `clipboard:write`). -- `resizeModal` clamps to at least 200x200 and at most the window minus margins, adjusting transform so the modal remains in the viewport. \ No newline at end of file +- `plugins/README.md` covers local development; `plugins/package.json` defines the commands. For test design see `mem:testing`; for local checks see `mem:scripts/ci`. +- `libs/plugin-types/index.d.ts` declares the public Plugin API; it does not implement it. For Penpot's CLJS proxies, permissions and event behavior, see `mem:frontend/plugin-api-to-cljs-binding`. For sandbox startup and plugin lifetime in `libs/plugins-runtime`, see `mem:plugins/runtime`. +- Update `plugins/CHANGELOG.md` for public type or runtime changes. Label entries with `**plugin-types:**` for declarations and `**plugins-runtime:**` for runtime behavior. +- Keep `libs/plugins-styles/styles.css` at the package root. When pnpm links the workspace source instead of the published package, `@penpot/plugin-styles/styles.css` must still resolve to the same styles that `plugins/tools/scripts/build-css.mjs` publishes. diff --git a/.serena/memories/plugins/runtime.md b/.serena/memories/plugins/runtime.md new file mode 100644 index 0000000000..0575da4677 --- /dev/null +++ b/.serena/memories/plugins/runtime.md @@ -0,0 +1,7 @@ +# Plugin runtime + +- Start in `plugins/libs/plugins-runtime/src/index.ts` and `lib/{bootstrap,load-plugin,create-sandbox,plugin-manager}.ts` under `src/`. For how the frontend initializes the runtime and reports failure, see `mem:frontend/plugin-api-to-cljs-binding`. +- SES repairs intrinsics at module load and hardens them when the first sandbox is created. If repair throws, the app still boots without installing the plugin globals; this does not mean SES left the host untouched. Do not run plugins on that path. Do not deep-freeze the host context in the loader: it can freeze shared host prototypes too early. Apply `ses.safeReturn` at the sandbox boundary instead. `load-plugin-real-path.spec.ts` tests the production order without hardening up front. +- Opening a workspace plugin closes other ordinary plugins, not global or background plugins. Survivors must remain in the registry because it routes iframe messages and lets `ɵunloadPlugin` find them. Opening a global plugin does not close existing plugins and ignores a duplicate load, including one still pending. +- `ɵunloadPlugin` cancels a pending load as well as closing a registered instance. A close can happen during startup: guard evaluation and registration after awaits, and only clear the pending entry owned by that load so a late completion cannot erase a newer one. +- Preserve cleanup on evaluation failure and explicit close. Queued callbacks can outlive close, so check `destroyed` before invoking plugin code. Track plugin-originated errors out of band: SES can freeze the error object. diff --git a/plugins/README.md b/plugins/README.md index 16f8bb92f7..207b8847e5 100644 --- a/plugins/README.md +++ b/plugins/README.md @@ -18,28 +18,23 @@ In the `libs` folder you'll find: In the `apps` folder you'll find some examples that use the libraries mentioned above. -- contrast-plugin: to run this example check Create - a plugin from scratch +- contrast-plugin: see [Run Penpot sample plugins](#run-penpot-sample-plugins). - example-styles: to run this example you should run ``` -pnpm run start:styles-example +pnpm run start:app:styles-example ``` Open in your browser: `http://localhost:4202/` ## Run Penpot sample plugins -This guide will help you launch a Penpot plugin from the penpot-plugins -repository. Before proceeding, ensure that you have Penpot running locally by -following the [setup +Before running a sample plugin, start Penpot locally by following the [setup instructions](https://help.penpot.app/technical-guide/developer/devenv/). -In the terminal, navigate to the **penpot-plugins** repository and run `pnpm -r -install` to install the required dependencies. Then, run `pnpm run start` to -launch the plugins runtime. +In the `plugins/` directory, run `pnpm -r install` to install the dependencies. +Then run `pnpm run start` to launch the plugins runtime. After installing the dependencies, choose a plugin to launch. You can either run one of the provided examples or create your own (see "Creating a plugin from @@ -49,12 +44,10 @@ appropriate startup script for the chosen plugin. For instance, to launch the Contrast plugin, use the following command: ``` -// for the contrast plugin pnpm run start:plugin:contrast ``` -Finally, open in your browser the specific port. In this specific example would -be `http://localhost:4302` +Open `http://localhost:4202` in your browser. A table listing the available plugins and their corresponding startup commands is provided below. @@ -68,7 +61,7 @@ is provided below. | icons-plugin | Tool to add icons from [Feather](https://feathericons.com/) | 4202 | pnpm run start:plugin:icons | http://localhost:4202/assets/manifest.json | | lorem-ipsum-plugin | Generate Lorem ipsum text | 4202 | pnpm run start:plugin:loremipsum | http://localhost:4202/assets/manifest.json | | create-palette-plugin | Creates a board with all the palette colors | 4202 | pnpm run start:plugin:palette | http://localhost:4202/assets/manifest.json | -| table-plugin | Create or import table | 4202 | pnpm run start:table-plugin | http://localhost:4202/assets/manifest.json | +| table-plugin | Create or import table | 4202 | pnpm run start:plugin:table | http://localhost:4202/assets/manifest.json | | rename-layers-plugin | Rename layers in bulk | 4202 | pnpm run start:plugin:renamelayers | http://localhost:4202/assets/manifest.json | | colors-to-tokens-plugin | Generate tokens JSON file | 4202 | pnpm run start:plugin:colors-to-tokens | http://localhost:4202/assets/manifest.json | | poc-tokens-plugin | Sandbox plugin to test tokens functionality | 4202 | pnpm run start:plugin:poc-tokens | http://localhost:4202/assets/manifest.json | @@ -78,7 +71,7 @@ is provided below. | App | Description | PORT | Start command | URL | | --------------- | ----------------------------------------------------------------- | ---- | --------------------------------- | ---------------------- | | plugins-runtime | Runtime for the plugins subsystem | 4200 | pnpm run start:app:runtime | | -| example-styles | Showcase of some of the Penpot styles that can be used in plugins | 4201 | pnpm run start:app:styles-example | http://localhost:4201/ | +| example-styles | Showcase of some of the Penpot styles that can be used in plugins | 4202 | pnpm run start:app:styles-example | http://localhost:4202/ | ## Creating a plugin from scratch