Skip to content

Commit

Permalink
revert: Improve plugin loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Zuoqiu-Yingyi committed Nov 22, 2023
1 parent 90b57bf commit 78ccf48
Showing 1 changed file with 10 additions and 19 deletions.
29 changes: 10 additions & 19 deletions app/src/plugin/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,20 @@ const runCode = (code: string, sourceURL: string) => {
return window.eval("(function anonymous(require, module, exports){".concat(code, "\n})\n//# sourceURL=").concat(sourceURL, "\n"));
};

export const getStyleElement = (id: string) => {
let style = document.getElementById(id);
if (!style) {
style = document.createElement("style");
style.id = id;
document.head.append(style);
}
return style;
}

export const loadPlugins = async (app: App) => {
const pluginsStyle = getStyleElement("pluginsStyle");
const response = await fetchSyncPost("/api/petal/loadPetals", {frontend: getFrontend()});
const css: string[] = [];
let css = "";
// 为加快启动速度,不进行 await
response.data.forEach((plugin: IPluginData) => {
loadPluginJS(app, plugin);
css.push(
`/* ${plugin.name} */`,
(plugin.css || ""),
);
response.data.forEach((item: IPluginData) => {
loadPluginJS(app, item);
css += item.css || "" + "\n";
});
pluginsStyle.innerHTML = css.join("\n");
const pluginsStyle = document.getElementById("pluginsStyle");
if (pluginsStyle) {
pluginsStyle.innerHTML = css;
} else {
document.head.insertAdjacentHTML("beforeend", `<style id="pluginsStyle">${css}</style>`);
}
};

const loadPluginJS = async (app: App, item: IPluginData) => {
Expand Down

0 comments on commit 78ccf48

Please sign in to comment.