From b49237b1ba09677cf552dd25c1bb459b9569e375 Mon Sep 17 00:00:00 2001 From: Juanfran Date: Wed, 9 Oct 2024 09:14:06 +0200 Subject: [PATCH] feat(runtime): unload plugin --- libs/plugins-runtime/src/index.ts | 2 ++ libs/plugins-runtime/src/lib/create-plugin.ts | 1 + libs/plugins-runtime/src/lib/global.d.ts | 1 + libs/plugins-runtime/src/lib/load-plugin.ts | 8 ++++++++ 4 files changed, 12 insertions(+) diff --git a/libs/plugins-runtime/src/index.ts b/libs/plugins-runtime/src/index.ts index cc69c543..1e0cb8e9 100644 --- a/libs/plugins-runtime/src/index.ts +++ b/libs/plugins-runtime/src/index.ts @@ -5,6 +5,7 @@ import { ɵloadPlugin, setContextBuilder, ɵloadPluginByUrl, + ɵunloadPlugin, } from './lib/load-plugin.js'; import type { Context } from '@penpot/plugin-types'; @@ -30,6 +31,7 @@ globalThisAny$.initPluginsRuntime = ( globalThisAny$.ɵcontext = contextBuilder('TEST'); globalThis.ɵloadPlugin = ɵloadPlugin; globalThis.ɵloadPluginByUrl = ɵloadPluginByUrl; + globalThis.ɵunloadPlugin = ɵunloadPlugin; } catch (err) { console.error(err); } diff --git a/libs/plugins-runtime/src/lib/create-plugin.ts b/libs/plugins-runtime/src/lib/create-plugin.ts index d1260667..a41044c6 100644 --- a/libs/plugins-runtime/src/lib/create-plugin.ts +++ b/libs/plugins-runtime/src/lib/create-plugin.ts @@ -36,6 +36,7 @@ export async function createPlugin( return { plugin, + manifest, compartment: sandbox, }; } diff --git a/libs/plugins-runtime/src/lib/global.d.ts b/libs/plugins-runtime/src/lib/global.d.ts index e63189c8..6d8c8a8c 100644 --- a/libs/plugins-runtime/src/lib/global.d.ts +++ b/libs/plugins-runtime/src/lib/global.d.ts @@ -4,5 +4,6 @@ export declare global { declare namespace globalThis { function ɵloadPlugin(cofig: Manifest): Promise; function ɵloadPluginByUrl(url: string): Promise; + function ɵunloadPlugin(id: Manifest['pluginId']): Promise; } } diff --git a/libs/plugins-runtime/src/lib/load-plugin.ts b/libs/plugins-runtime/src/lib/load-plugin.ts index bbeabbb4..bf76842d 100644 --- a/libs/plugins-runtime/src/lib/load-plugin.ts +++ b/libs/plugins-runtime/src/lib/load-plugin.ts @@ -68,3 +68,11 @@ export const ɵloadPluginByUrl = async function (manifestUrl: string) { const manifest = await loadManifest(manifestUrl); ɵloadPlugin(manifest); }; + +export const ɵunloadPlugin = function (id: Manifest['pluginId']) { + const plugin = plugins.find((plugin) => plugin.manifest.pluginId === id); + + if (plugin) { + plugin.plugin.close(); + } +};