mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-22 17:40:13 +01:00
🎨 plugin requrie function supports loading node modules (#9803)
This commit is contained in:
parent
2f34408577
commit
0d93d14368
1 changed files with 11 additions and 7 deletions
|
|
@ -8,13 +8,17 @@ import {API} from "./API";
|
||||||
import {getFrontend, isMobile, isWindow} from "../util/functions";
|
import {getFrontend, isMobile, isWindow} from "../util/functions";
|
||||||
import {Constants} from "../constants";
|
import {Constants} from "../constants";
|
||||||
|
|
||||||
const getObject = (key: string) => {
|
const modules = {
|
||||||
const api = {
|
siyuan: API
|
||||||
siyuan: API
|
|
||||||
};
|
|
||||||
// @ts-ignore
|
|
||||||
return api[key];
|
|
||||||
};
|
};
|
||||||
|
const requireFunc = (key: string) => {
|
||||||
|
// @ts-ignore
|
||||||
|
return modules[key]
|
||||||
|
?? window.require?.(key);
|
||||||
|
};
|
||||||
|
if (window.require instanceof Function) {
|
||||||
|
requireFunc.__proto__ = window.require;
|
||||||
|
}
|
||||||
|
|
||||||
const runCode = (code: string, sourceURL: string) => {
|
const runCode = (code: string, sourceURL: string) => {
|
||||||
return window.eval("(function anonymous(require, module, exports){".concat(code, "\n})\n//# sourceURL=").concat(sourceURL, "\n"));
|
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 exportsObj: { [key: string]: any } = {};
|
||||||
const moduleObj = {exports: exportsObj};
|
const moduleObj = {exports: exportsObj};
|
||||||
try {
|
try {
|
||||||
runCode(item.js, "plugin:" + encodeURIComponent(item.name))(getObject, moduleObj, exportsObj);
|
runCode(item.js, "plugin:" + encodeURIComponent(item.name))(requireFunc, moduleObj, exportsObj);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(`plugin ${item.name} run error:`, e);
|
console.error(`plugin ${item.name} run error:`, e);
|
||||||
return;
|
return;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue