diff --git a/.github/workflows/ci-release.yml b/.github/workflows/ci-release.yml index 4709ee43..5b2d8c03 100644 --- a/.github/workflows/ci-release.yml +++ b/.github/workflows/ci-release.yml @@ -87,20 +87,17 @@ jobs: os: windows-latest shell: cmd node-version: - - 18.17.0 - - 18.x - - 20.5.0 + - 20.17.0 - 20.x + - 22.9.0 - 22.x exclude: - platform: { name: macOS, os: macos-13, shell: bash } - node-version: 18.17.0 - - platform: { name: macOS, os: macos-13, shell: bash } - node-version: 18.x - - platform: { name: macOS, os: macos-13, shell: bash } - node-version: 20.5.0 + node-version: 20.17.0 - platform: { name: macOS, os: macos-13, shell: bash } node-version: 20.x + - platform: { name: macOS, os: macos-13, shell: bash } + node-version: 22.9.0 - platform: { name: macOS, os: macos-13, shell: bash } node-version: 22.x runs-on: ${{ matrix.platform.os }} diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b934a8d5..3873d0b9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -68,20 +68,17 @@ jobs: os: windows-latest shell: cmd node-version: - - 18.17.0 - - 18.x - - 20.5.0 + - 20.17.0 - 20.x + - 22.9.0 - 22.x exclude: - platform: { name: macOS, os: macos-13, shell: bash } - node-version: 18.17.0 - - platform: { name: macOS, os: macos-13, shell: bash } - node-version: 18.x - - platform: { name: macOS, os: macos-13, shell: bash } - node-version: 20.5.0 + node-version: 20.17.0 - platform: { name: macOS, os: macos-13, shell: bash } node-version: 20.x + - platform: { name: macOS, os: macos-13, shell: bash } + node-version: 22.9.0 - platform: { name: macOS, os: macos-13, shell: bash } node-version: 22.x runs-on: ${{ matrix.platform.os }} diff --git a/lib/util/import-or-require.js b/lib/util/import-or-require.js index 0fb0ef4b..f9098ac2 100644 --- a/lib/util/import-or-require.js +++ b/lib/util/import-or-require.js @@ -14,9 +14,18 @@ const importOrRequire = async path => { let content = {} try { content = require(path) + // this is for node 22+ + // istanbul ignore next + if (content.__esModule) { + return content.default + } } catch { + // istanbul ignore next try { - content = await import(pathToFileURL(path)).then(r => r.default) + // this is for node under 20 + const results = await import(pathToFileURL(path)) + // istanbul ignore next + return results.default } catch { // its ok if this fails since the content dir might only be to provide // other files. the index.js is optional diff --git a/package.json b/package.json index bf3462d0..006b970b 100644 --- a/package.json +++ b/package.json @@ -94,7 +94,7 @@ "prettier": true }, "engines": { - "node": "^18.17.0 || >=20.5.0" + "node": "^20.17.0 || >=22.9.0" }, "workspaces": [ "workspace/test-workspace" diff --git a/test/apply/esm.js b/test/apply/esm.js index aeaa9f18..74eb5918 100644 --- a/test/apply/esm.js +++ b/test/apply/esm.js @@ -18,6 +18,6 @@ t.test('basic', async t => { }) await s.apply() - const file = await s.readFile('file.js') + const file = await s.readFile('content_dir/file.js') t.match(file, 'var x = 1;') }) diff --git a/test/apply/import-or-require.js b/test/apply/import-or-require.js new file mode 100644 index 00000000..b486735c --- /dev/null +++ b/test/apply/import-or-require.js @@ -0,0 +1,27 @@ +const t = require('tap') +const importOrRequire = require('../../lib/util/import-or-require.js') +const path = require('path') + +t.test('importOrRequire', async t => { + const dir = t.testdir({ + esm: { + 'package.json': JSON.stringify({ + type: 'module', + }), + 'index.js': 'export default "type module";', + }, + 'esm.js': 'export default "esm";', + 'mjs.mjs': 'export default "mjs";', + 'cjs.cjs': 'module.exports = "cjs";', + 'js.js': 'module.exports = "js";', + 'invalid.js': 'invalid', + }) + + await Promise.all( + // double 'js' triggers the cache + ['mjs', 'cjs', 'js', 'js'].map(async type => { + const output = await importOrRequire(path.join(dir, `${type}.${type}`)) + t.same(output, type) + }), + ) +})