Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: plugin `requrie` function supports loading node modules (#9803)

Yingyi / 颖逸0d93d143688e
diff --git a/app/src/plugin/loader.ts b/app/src/plugin/loader.ts
index b0cf39aeab..22429cda37 100644
--- a/app/src/plugin/loader.ts
+++ b/app/src/plugin/loader.ts
@@ -8,13 +8,17 @@ import {API} from "./API";
import {getFrontend, isMobile, isWindow} from "../util/functions";
import {Constants} from "../constants";
-const getObject = (key: string) => {
- const api = {
- siyuan: API
- };
+const modules = {
+ siyuan: API
+};
+const requireFunc = (key: string) => {
// @ts-ignore
- return api[key];
+ return modules[key]
+ ?? window.require?.(key);
};
+if (window.require instanceof Function) {
+ requireFunc.__proto__ = window.require;
+}
const runCode = (code: string, sourceURL: string) => {
return window.eval("(function anonymous(require, module, exports){".concat(code, "\n})\n//# sourceURL=").concat(sourceURL, "\n"));
@@ -40,7 +44,7 @@ const loadPluginJS = async (app: App, item: IPluginData) => {
const exportsObj: { [key: string]: any } = {};
const moduleObj = {exports: exportsObj};
try {
- runCode(item.js, "plugin:" + encodeURIComponent(item.name))(getObject, moduleObj, exportsObj);
+ runCode(item.js, "plugin:" + encodeURIComponent(item.name))(requireFunc, moduleObj, exportsObj);
} catch (e) {
console.error(`plugin ${item.name} run error:`, e);
return;