Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(web): import builtin plugin from external url #625

Merged
merged 4 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function processUnsafeBuiltinBlocks(plugin?: UnsafeBuiltinPlugin[]) {

const unsafeBlocks: UnsafeBuiltinBlocks<Component> | undefined = plugin
.map(p =>
p.widgets.map(w => {
p.blocks?.map(w => {
return {
widgetId: `${p.id}/${w.extensionId}`,
...w,
Expand All @@ -23,11 +23,11 @@ function processUnsafeBuiltinBlocks(plugin?: UnsafeBuiltinPlugin[]) {
)
.reduce((a, b) => {
const newObject: { [key: string]: Component } = {};
b.forEach(w => {
b?.forEach(w => {
newObject[w.widgetId] = w.component;
});
return merge(a, newObject);
}, {});

return unsafeBlocks;
return unsafeBlocks || [];
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function processUnsafeBuiltinWidgets(plugin?: UnsafeBuiltinPlugin[]) {

const unsafeWidgets: UnsafeBuiltinWidgets<Component> | undefined = plugin
.map(p =>
p.widgets.map(w => {
p.widgets?.map(w => {
return {
widgetId: `${p.id}/${w.extensionId}`,
...w,
Expand All @@ -25,11 +25,11 @@ function processUnsafeBuiltinWidgets(plugin?: UnsafeBuiltinPlugin[]) {
)
.reduce((a, b) => {
const newObject: { [key: string]: Component } = {};
b.forEach(w => {
b?.forEach(w => {
newObject[w.widgetId] = w.component;
});
return merge(a, newObject);
}, {});

return unsafeWidgets;
return unsafeWidgets || [];
}
4 changes: 0 additions & 4 deletions web/src/beta/lib/unsafeBuiltinPlugins/.gitignore

This file was deleted.

6 changes: 3 additions & 3 deletions web/src/beta/lib/unsafeBuiltinPlugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// import { default as myExampleUnsafePlugin } from "./myExampleUnsafePlugin/index.jsx";
import { loadPlugins } from "./load";

// export default [myExampleUnsafePlugin];
export * from "./types";

export default [];
export default await loadPlugins();
20 changes: 20 additions & 0 deletions web/src/beta/lib/unsafeBuiltinPlugins/load.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { UnsafeBuiltinPlugin } from "./types";

export const loadPlugins = async () => {
const urls = window.REEARTH_CONFIG?.unsafePluginUrls;
KaWaite marked this conversation as resolved.
Show resolved Hide resolved
return urls
? (
await Promise.all(
urls.map(async url => {
try {
const plugin: UnsafeBuiltinPlugin = (await import(/* @vite-ignore */ url)).default;
console.log("Hello", plugin);
return plugin;
} catch (e) {
throw new Error(`Specified unsafe built-in module could not find: ${url} ${e}`);
}
}),
)
).filter(Boolean)
: [];
};
21 changes: 21 additions & 0 deletions web/src/beta/lib/unsafeBuiltinPlugins/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { FC } from "react";

export type UnsafeBuiltinPlugin = {
id: string;
name: string;
widgets?: UnsafeBuiltinWidget[];
blocks?: UnsafeBuiltinBlock[];
};

type UnsafeBuiltinWidget = UnsafeBuiltinPluginExtension<"widget">;

type UnsafeBuiltinBlock = UnsafeBuiltinPluginExtension<"block">;

type UnsafeBuiltinPluginExtension<T extends "widget" | "block"> = {
type: T;
extensionId: string;
name: string;
component: FC;
};

export type UnsafeBuiltinWidgets<T = unknown> = Record<string, T>;
5 changes: 3 additions & 2 deletions web/src/services/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export type Config = {
documentationUrl?: string;
marketplaceUrl?: string;
extensionUrls?: string[];
unsafePluginUrls?: string[];
extensions?: Extensions;
unsafeBuiltinPlugins?: UnsafeBuiltinPlugin[];
};
Expand Down Expand Up @@ -80,9 +81,9 @@ export default async function loadConfig() {
config.extensions = extensions;
}

config.unsafeBuiltinPlugins = await loadUnsafeBuiltinPlugins();

window.REEARTH_CONFIG = config;

config.unsafeBuiltinPlugins = await loadUnsafeBuiltinPlugins();
KaWaite marked this conversation as resolved.
Show resolved Hide resolved
}

export function config(): Config | undefined {
Expand Down
20 changes: 2 additions & 18 deletions web/src/services/config/unsafeBuiltinPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
export type UnsafeBuiltinPlugin = {
id: string;
name: string;
widgets: UnsafeBuiltinWidget[];
blocks: UnsafeBuiltinBlock[];
};
import type { UnsafeBuiltinPlugin } from "../../beta/lib/unsafeBuiltinPlugins";

type UnsafeBuiltinWidget = UnsafeBuiltinPluginExtension<"widget">;

type UnsafeBuiltinBlock = UnsafeBuiltinPluginExtension<"block">;

type UnsafeBuiltinPluginExtension<T extends "widget" | "block"> = {
type: T;
extensionId: string;
name: string;
component: React.FC;
};

export type UnsafeBuiltinWidgets<T = unknown> = Record<string, T>;
export type { UnsafeBuiltinPlugin } from "../../beta/lib/unsafeBuiltinPlugins";

export async function loadUnsafeBuiltinPlugins() {
try {
Expand Down
Loading