Skip to content

Commit

Permalink
Merge ce02bcf into 042f862
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenjoezhang committed Sep 7, 2021
2 parents 042f862 + ce02bcf commit 39f87e5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
8 changes: 3 additions & 5 deletions lib/hexo/index.js
Expand Up @@ -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);
}
}

Expand Down
30 changes: 17 additions & 13 deletions lib/hexo/load_plugins.js
Expand Up @@ -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 => {
Expand All @@ -24,7 +24,7 @@ function loadModuleList(ctx) {
const deps = Object.keys(json.dependencies || {});
const devDeps = Object.keys(json.devDependencies || {});

return deps.concat(devDeps);
return basedir === ctx.base_dir ? deps.concat(devDeps) : deps;
});
}).filter(name => {
// Ignore plugins whose name is not started with "hexo-"
Expand All @@ -37,22 +37,26 @@ 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);

// Load plugins
return ctx.loadPlugin(path).then(() => {
ctx.log.debug('Plugin loaded: %s', magenta(name));
}).catch(err => {
ctx.log.error({err}, 'Plugin load failed: %s', magenta(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));
}).catch(err => {
ctx.log.error({err}, 'Plugin load failed: %s', magenta(name));
});
});
});
}

function loadScripts(ctx) {
Expand Down

0 comments on commit 39f87e5

Please sign in to comment.