From 87fb1bddbbd68320a87bc83c13ae47ca570a8e92 Mon Sep 17 00:00:00 2001 From: Pcrab Date: Wed, 14 Jun 2023 19:27:01 +0800 Subject: [PATCH 1/2] feat: allow top-level await in plugins or scripts --- lib/hexo/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/hexo/index.ts b/lib/hexo/index.ts index 15a0d6403e..93480baab2 100644 --- a/lib/hexo/index.ts +++ b/lib/hexo/index.ts @@ -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); From 8f0f69454e9a28b8f79f3e82e126688753781510 Mon Sep 17 00:00:00 2001 From: Pcrab Date: Wed, 14 Jun 2023 20:24:41 +0800 Subject: [PATCH 2/2] fix: add test case --- test/scripts/hexo/load_plugins.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/test/scripts/hexo/load_plugins.js b/test/scripts/hexo/load_plugins.js index f6f7b44b17..50dd9620b3 100644 --- a/test/scripts/hexo/load_plugins.js +++ b/test/scripts/hexo/load_plugins.js @@ -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; @@ -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');