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: allow top-level await in plugins or scripts #5228

Merged
merged 2 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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