Skip to content

feat: Support top-level await on Node 22.12+ #269

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

Merged
merged 3 commits into from
Jun 1, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/shared/require-or-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ function requireOrImport(path, callback) {
if (pathToFileURL && importESM) {
// Because e.code is undefined on nyc process.
/* istanbul ignore else */
if (e.code === 'ERR_REQUIRE_ESM' || process.env.NYC_CONFIG) {
// Check 'ERR_REQUIRE_ASYNC_MODULE' if on node v22.12.0 or later to allow importing from files using top level await.
if (e.code === 'ERR_REQUIRE_ESM' || process.env.NYC_CONFIG || e.code === 'ERR_REQUIRE_ASYNC_MODULE') {
// This is needed on Windows, because import() fails if providing a Windows file path.
var url = pathToFileURL(path);
importESM(url).then(function(esm) { callback(null, esm); }, callback);
Expand Down
25 changes: 25 additions & 0 deletions test/esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,29 @@ describe('ESM', function() {
}
});

it('prints the task list (top-level await)', function(done) {
if (shouldSkip()) {
this.skip();
}

if (semver.satisfies(process.version, '10 || 12')) {
this.skip();
}

var options = '--tasks --sort-tasks --gulpfile ./test/fixtures/gulpfiles/gulpfile-tla.mjs';
var trailingLines = 1;

var opts = { cwd: baseDir };
exec(gulp(options), opts, cb);

function cb(err, stdout, stderr) {
expect(err).toBeNull();
expect(stderr).toEqual('');
var filepath = path.join(expectedDir, 'esm.txt');
var expected = fs.readFileSync(filepath, 'utf-8');
expect(sliceLines(stdout, trailingLines)).toEqual(expected);
done(err);
}
});

});
7 changes: 7 additions & 0 deletions test/fixtures/gulpfiles/gulpfile-tla.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const dynamicNoop = await Promise.resolve(function noop(cb) {
cb();
});

export const registered = dynamicNoop;
export function exported(){};
export const string = 'no function';
Loading