Skip to content

Commit 7157e85

Browse files
authored
feat: allow top-level await in plugins or scripts (#5228)
1 parent c9ad8c6 commit 7157e85

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

lib/hexo/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ class Hexo extends EventEmitter {
387387
req.extensions = Module._extensions;
388388
req.cache = Module._cache;
389389

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

392392
const fn = runInThisContext(script, path);
393393

test/scripts/hexo/load_plugins.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ describe('Load plugins', () => {
1818
'}'
1919
].join('\n');
2020

21+
const asyncScript = [
22+
'async function afunc() {',
23+
' return new Promise(resolve => resolve());',
24+
'}',
25+
'await afunc()',
26+
'hexo._script_test = {',
27+
' filename: __filename,',
28+
' dirname: __dirname,',
29+
' module: module,',
30+
' require: require',
31+
'}'
32+
].join('\n');
2133
function validate(path) {
2234
const result = hexo._script_test;
2335

@@ -81,6 +93,19 @@ describe('Load plugins', () => {
8193
});
8294
});
8395

96+
it('load async plugins', () => {
97+
const name = 'hexo-async-plugin-test';
98+
const path = join(hexo.plugin_dir, name, 'index.js');
99+
100+
return Promise.all([
101+
createPackageFile(name),
102+
fs.writeFile(path, asyncScript)
103+
]).then(() => loadPlugins(hexo)).then(() => {
104+
validate(path);
105+
return fs.unlink(path);
106+
});
107+
});
108+
84109
it('load scoped plugins', () => {
85110
const name = '@some-scope/hexo-plugin-test';
86111
const path = join(hexo.plugin_dir, name, 'index.js');

0 commit comments

Comments
 (0)