Skip to content

Commit

Permalink
feat: allow top-level await in plugins or scripts (#5228)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pcrab authored Jun 16, 2023
1 parent c9ad8c6 commit 7157e85
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/hexo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ class Hexo extends EventEmitter {
req.extensions = Module._extensions;
req.cache = Module._cache;

script = `(function(exports, require, module, __filename, __dirname, hexo){${script}\n});`;
script = `(async function(exports, require, module, __filename, __dirname, hexo){${script}\n});`;

const fn = runInThisContext(script, path);

Expand Down
25 changes: 25 additions & 0 deletions test/scripts/hexo/load_plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ describe('Load plugins', () => {
'}'
].join('\n');

const asyncScript = [
'async function afunc() {',
' return new Promise(resolve => resolve());',
'}',
'await afunc()',
'hexo._script_test = {',
' filename: __filename,',
' dirname: __dirname,',
' module: module,',
' require: require',
'}'
].join('\n');
function validate(path) {
const result = hexo._script_test;

Expand Down Expand Up @@ -81,6 +93,19 @@ describe('Load plugins', () => {
});
});

it('load async plugins', () => {
const name = 'hexo-async-plugin-test';
const path = join(hexo.plugin_dir, name, 'index.js');

return Promise.all([
createPackageFile(name),
fs.writeFile(path, asyncScript)
]).then(() => loadPlugins(hexo)).then(() => {
validate(path);
return fs.unlink(path);
});
});

it('load scoped plugins', () => {
const name = '@some-scope/hexo-plugin-test';
const path = join(hexo.plugin_dir, name, 'index.js');
Expand Down

0 comments on commit 7157e85

Please sign in to comment.