From 87c2b50162434a5d86f1988a3b7b8f2a38a444c4 Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Tue, 7 Sep 2021 17:43:39 +0800 Subject: [PATCH] Load hexo plugin in the theme's package.json --- lib/hexo/index.js | 8 +++----- lib/hexo/load_plugins.js | 16 ++++++++++------ 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/hexo/index.js b/lib/hexo/index.js index 944afd714d..dc345a65d7 100644 --- a/lib/hexo/index.js +++ b/lib/hexo/index.js @@ -268,16 +268,14 @@ class Hexo extends EventEmitter { return this.database.model(name, schema); } - resolvePlugin(name) { - const baseDir = this.base_dir; - + resolvePlugin(name, basedir) { try { // Try to resolve the plugin with the resolve.sync. - return sync(name, { basedir: baseDir }); + return sync(name, { basedir }); } catch (err) { // There was an error (likely the plugin wasn't found), so return a possibly // non-existing path that a later part of the resolution process will check. - return join(baseDir, 'node_modules', name); + return join(basedir, 'node_modules', name); } } diff --git a/lib/hexo/load_plugins.js b/lib/hexo/load_plugins.js index 03f733b3a9..0265cee82c 100644 --- a/lib/hexo/load_plugins.js +++ b/lib/hexo/load_plugins.js @@ -11,8 +11,8 @@ module.exports = ctx => { return loadModules(ctx).then(() => loadScripts(ctx)); }; -function loadModuleList(ctx) { - const packagePath = join(ctx.base_dir, 'package.json'); +function loadModuleList(ctx, basedir) { + const packagePath = join(basedir, 'package.json'); // Make sure package.json exists return exists(packagePath).then(exist => { @@ -37,15 +37,19 @@ function loadModuleList(ctx) { if (name.startsWith('@types/')) return false; // Make sure the plugin exists - const path = ctx.resolvePlugin(name); + const path = ctx.resolvePlugin(name, basedir); return exists(path); + }).then(modules => { + return Object.fromEntries(modules.map(name => [name, ctx.resolvePlugin(name, basedir)])); }); } function loadModules(ctx) { - return loadModuleList(ctx).map(name => { - const path = ctx.resolvePlugin(name); - + return Promise.all([ctx.base_dir, ctx.theme_dir].map(basedir => loadModuleList(ctx, basedir))) + .then(([hexoModuleList, themeModuleList]) => { + return Object.entries(Object.assign(hexoModuleList, themeModuleList)); + }) + .map(([name, path]) => { // Load plugins return ctx.loadPlugin(path).then(() => { ctx.log.debug('Plugin loaded: %s', magenta(name));