From 40cc7af57a5e918bf39c1c20c7a9a18d99916b32 Mon Sep 17 00:00:00 2001 From: Ed Pelc Date: Tue, 31 Dec 2024 10:57:25 -0500 Subject: [PATCH 1/3] use import() if ERR_REQUIRE_ASYNC_MODULE - supports node v22.12.0 and later when using top level await --- lib/shared/require-or-import.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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); From fd1742b26f6ff00a508bf5b5fe77c0634e714ae0 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Sun, 1 Jun 2025 13:34:01 -0700 Subject: [PATCH 2/3] add regression test --- test/esm.js | 29 ++++++++++++++++++++++++ test/fixtures/gulpfiles/gulpfile-tla.mjs | 7 ++++++ 2 files changed, 36 insertions(+) create mode 100644 test/fixtures/gulpfiles/gulpfile-tla.mjs diff --git a/test/esm.js b/test/esm.js index 52affdab..74f10d60 100644 --- a/test/esm.js +++ b/test/esm.js @@ -60,4 +60,33 @@ describe('ESM', function() { } }); + it('prints the task list (top-level await)', function(done) { + if (shouldSkip()) { + this.skip(); + } + + var options = '--tasks --sort-tasks --gulpfile ./test/fixtures/gulpfiles/gulpfile-tla.mjs'; + var trailingLines = 1; + if (!semver.satisfies(process.version, '^12.17.0 || >=13.2.0')) { + options += ' --experimental-modules'; + trailingLines += 2; + } + + var opts = { cwd: baseDir }; + exec(gulp(options), opts, cb); + + function cb(err, stdout, stderr) { + expect(err).toBeNull(); + if (!semver.satisfies(process.version, '^12.20.0 || >=13.14.0')) { + expect(stderr).toMatch('ExperimentalWarning: The ESM module loader is experimental.\n'); + } else { + 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'; From 59ddb2ab6b7c216e78bb33de317ad2af6b14cf71 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Sun, 1 Jun 2025 13:38:10 -0700 Subject: [PATCH 3/3] no await keyword in node 10 or 12 --- test/esm.js | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/test/esm.js b/test/esm.js index 74f10d60..784a4036 100644 --- a/test/esm.js +++ b/test/esm.js @@ -65,23 +65,19 @@ describe('ESM', function() { 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; - if (!semver.satisfies(process.version, '^12.17.0 || >=13.2.0')) { - options += ' --experimental-modules'; - trailingLines += 2; - } var opts = { cwd: baseDir }; exec(gulp(options), opts, cb); function cb(err, stdout, stderr) { expect(err).toBeNull(); - if (!semver.satisfies(process.version, '^12.20.0 || >=13.14.0')) { - expect(stderr).toMatch('ExperimentalWarning: The ESM module loader is experimental.\n'); - } else { - expect(stderr).toEqual(''); - } + expect(stderr).toEqual(''); var filepath = path.join(expectedDir, 'esm.txt'); var expected = fs.readFileSync(filepath, 'utf-8'); expect(sliceLines(stdout, trailingLines)).toEqual(expected);