Skip to content

Commit

Permalink
fix: #9 动态import类库路径不存在时不加载
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Feb 28, 2023
1 parent bef5370 commit f73ba0e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
2 changes: 1 addition & 1 deletion public/lib/plugin/plugin-system.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
35 changes: 29 additions & 6 deletions theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
})()

0 comments on commit f73ba0e

Please sign in to comment.