Skip to content

Commit 902f5b2

Browse files
epelcphated
andauthored
feat: Support top-level await on Node 22.12+ (#269)
--------- Co-authored-by: Blaine Bublitz <blaine.bublitz@gmail.com>
1 parent d517d2e commit 902f5b2

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

lib/shared/require-or-import.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ function requireOrImport(path, callback) {
2323
if (pathToFileURL && importESM) {
2424
// Because e.code is undefined on nyc process.
2525
/* istanbul ignore else */
26-
if (e.code === 'ERR_REQUIRE_ESM' || process.env.NYC_CONFIG) {
26+
// Check 'ERR_REQUIRE_ASYNC_MODULE' if on node v22.12.0 or later to allow importing from files using top level await.
27+
if (e.code === 'ERR_REQUIRE_ESM' || process.env.NYC_CONFIG || e.code === 'ERR_REQUIRE_ASYNC_MODULE') {
2728
// This is needed on Windows, because import() fails if providing a Windows file path.
2829
var url = pathToFileURL(path);
2930
importESM(url).then(function(esm) { callback(null, esm); }, callback);

test/esm.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,29 @@ describe('ESM', function() {
6060
}
6161
});
6262

63+
it('prints the task list (top-level await)', function(done) {
64+
if (shouldSkip()) {
65+
this.skip();
66+
}
67+
68+
if (semver.satisfies(process.version, '10 || 12')) {
69+
this.skip();
70+
}
71+
72+
var options = '--tasks --sort-tasks --gulpfile ./test/fixtures/gulpfiles/gulpfile-tla.mjs';
73+
var trailingLines = 1;
74+
75+
var opts = { cwd: baseDir };
76+
exec(gulp(options), opts, cb);
77+
78+
function cb(err, stdout, stderr) {
79+
expect(err).toBeNull();
80+
expect(stderr).toEqual('');
81+
var filepath = path.join(expectedDir, 'esm.txt');
82+
var expected = fs.readFileSync(filepath, 'utf-8');
83+
expect(sliceLines(stdout, trailingLines)).toEqual(expected);
84+
done(err);
85+
}
86+
});
87+
6388
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const dynamicNoop = await Promise.resolve(function noop(cb) {
2+
cb();
3+
});
4+
5+
export const registered = dynamicNoop;
6+
export function exported(){};
7+
export const string = 'no function';

0 commit comments

Comments
 (0)