Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: load hexo plugin in the theme's package.json #4771

Merged
merged 5 commits into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 3 additions & 5 deletions lib/hexo/index.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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.map([ctx.base_dir, ctx.theme_dir], basedir => loadModuleList(ctx, basedir))
.then(([hexoModuleList, themeModuleList]) => {
return Object.entries(Object.assign(hexoModuleList, themeModuleList));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that the plugin defined in the theme will override the one under hexo. The priority in the theme is higher, but the plugin under hexo is added by the user. I think it should have a higher priority.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two different considerations:

  • The priority in the Hexo package.json is higher: If the theme is not maintained, the users will be able to override the outdated plugins defined in theme package.json
  • The priority in the theme is higher: The plugin in Hexo package.json may not be compatible with the theme, and the theme could provide a proper version for the users

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two different considerations:

Yesh, but I prefer the first one. After all, if you use the plugin defined by the theme, you only need to remove the relevant plugins in your package.json. In the second option, I will not be able to cover the plugins inside the theme.

Or take a poll? 😂

})
.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
45 changes: 27 additions & 18 deletions test/scripts/hexo/load_plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,31 @@ describe('Load plugins', () => {
delete hexo._script_test;
}

function createPackageFile(...args) {
function createPackageFile(name, path) {
const pkg = {
name: 'hexo-site',
version: '0.0.0',
private: true,
dependencies: {}
dependencies: {
[name]: '*'
}
};

for (const arg of args) {
pkg.dependencies[arg] = '*';
}

return fs.writeFile(join(hexo.base_dir, 'package.json'), JSON.stringify(pkg, null, ' '));
path = path || join(hexo.base_dir, 'package.json');
return fs.writeFile(path, JSON.stringify(pkg, null, ' '));
}

function createPackageFileWithDevDeps(...args) {
function createPackageFileWithDevDeps(name) {
const pkg = {
name: 'hexo-site',
version: '0.0.0',
private: true,
dependencies: {},
devDependencies: {}
devDependencies: {
[name]: '*'
}
};

for (let i = 0, len = args.length; i < len; i++) {
pkg.devDependencies[args[i]] = '*';
}

return fs.writeFile(join(hexo.base_dir, 'package.json'), JSON.stringify(pkg, null, ' '));
}

Expand All @@ -67,6 +64,10 @@ describe('Load plugins', () => {

after(() => fs.rmdir(hexo.base_dir));

afterEach(() => {
createPackageFile('hexo-another-plugin');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line will reset Hexo package.json, to make sure in the following unit tests, hexo-plugin-test plugin is loaded from theme's package.json, not Hexo package.json

});

it('load plugins', () => {
const name = 'hexo-plugin-test';
const path = join(hexo.plugin_dir, name, 'index.js');
Expand Down Expand Up @@ -106,9 +107,19 @@ describe('Load plugins', () => {
});
});

it('ignore plugin whose name is "hexo-theme-[hexo.config.theme]"', async () => {
hexo.config.theme = 'test_theme';
it('load plugins in the theme\'s package.json', async () => {
const name = 'hexo-plugin-test';
const path = join(hexo.plugin_dir, name, 'index.js');
return Promise.all([
createPackageFile(name, join(hexo.theme_dir, 'package.json')),
fs.writeFile(path, script)
]).then(() => loadPlugins(hexo)).then(() => {
validate(path);
return fs.unlink(path);
});
});

it('ignore plugin whose name is started with "hexo-theme-"', async () => {
const script = 'hexo._script_test = true';
const name = 'hexo-theme-test_theme';
const path = join(hexo.plugin_dir, name, 'index.js');
Expand All @@ -124,9 +135,7 @@ describe('Load plugins', () => {
return fs.unlink(path);
});

it('ignore plugin whose name endswith "hexo-theme-[hexo.config.theme]"', async () => {
hexo.config.theme = 'test_theme';

it('ignore scoped plugin whose name is started with "hexo-theme-"', async () => {
const script = 'hexo._script_test = true';
const name = '@hexojs/hexo-theme-test_theme';
const path = join(hexo.plugin_dir, name, 'index.js');
Expand Down