diff --git a/lib/shared/require-or-import.js b/lib/shared/require-or-import.js index 4cf5877e..af98aac5 100644 --- a/lib/shared/require-or-import.js +++ b/lib/shared/require-or-import.js @@ -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); diff --git a/test/esm.js b/test/esm.js index 52affdab..784a4036 100644 --- a/test/esm.js +++ b/test/esm.js @@ -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); + } + }); + }); diff --git a/test/fixtures/gulpfiles/gulpfile-tla.mjs b/test/fixtures/gulpfiles/gulpfile-tla.mjs new file mode 100644 index 00000000..c6a97a4d --- /dev/null +++ b/test/fixtures/gulpfiles/gulpfile-tla.mjs @@ -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';