From f73ba0e55491d90ff5429f597c7af7844fa5e366 Mon Sep 17 00:00:00 2001 From: terwer Date: Tue, 28 Feb 2023 11:58:20 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20#9=20=E5=8A=A8=E6=80=81import=E7=B1=BB?= =?UTF-8?q?=E5=BA=93=E8=B7=AF=E5=BE=84=E4=B8=8D=E5=AD=98=E5=9C=A8=E6=97=B6?= =?UTF-8?q?=E4=B8=8D=E5=8A=A0=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/lib/plugin/plugin-system.js | 2 +- theme.js | 35 +++++++++++++++++++++++++----- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/public/lib/plugin/plugin-system.js b/public/lib/plugin/plugin-system.js index 0ae9a21f..98a7e283 100644 --- a/public/lib/plugin/plugin-system.js +++ b/public/lib/plugin/plugin-system.js @@ -24,7 +24,7 @@ */ const initPluginSystem = () => { - return ["/appearance/themes/zhi/dist/lib/plugin/plugin-system-hook.js"] + return ["/appearance/themes/zhi/dist-cjs/lib/plugin/plugin-system-hook.js"] } const pluginSystem = { diff --git a/theme.js b/theme.js index f00a9972..0359dc3f 100644 --- a/theme.js +++ b/theme.js @@ -37,17 +37,40 @@ * @author terwer * @since 0.0.1 */ -const getCjsZhiDir = () => { - return `${window.siyuan.config.system.confDir}/appearance/themes/zhi/dist-cjs` +const getRealPath = (libpath) => { + const path = window.require("path") + return path.join(`${window.siyuan.config.system.confDir}`, libpath) } +/** + * 安全的import,路径不存在或者加载出错 + * + * @author terwer + * @since 0.0.1 + */ +const safeImport = async (libpath) => { + const fs = window.require("fs") + const realpath = getRealPath(libpath) + + try { + if (!fs.existsSync(realpath)) { + console.warn("依赖库不存在,请排查。依赖库路径=>", realpath) + return + } + await import(libpath) + } catch (e) { + console.error("依赖库加载失败,请排查。依赖库路径=>", realpath) + console.error(e) + } + } + ;(async () => { - const zhi = window.require(`${getCjsZhiDir()}/zhi.js`) + const zhiLibpath = getRealPath("/appearance/themes/zhi/dist-cjs/zhi.js") + const zhi = window.require(zhiLibpath) // 主流程加载 - await zhi.main([], async function (dynamicImports) { + await zhi.main([], async function(dynamicImports) { for (const item of dynamicImports) { - console.log("开始加载=>", item) - await import(item) + await safeImport(item) } }) })()