Skip to content

Commit

Permalink
feat: support Node v21 (#378)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber authored Nov 10, 2023
1 parent 4833f86 commit 02b43e5
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 12 deletions.
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"@ampproject/remapping": "^2.2.1",
"@pvtnbr/eslint-config": "^0.37.0",
"@types/cross-spawn": "^6.0.3",
"@types/node": "^20.6.0",
"@types/node": "^20.9.0",
"@types/react": "^18.2.21",
"@types/semver": "^7.5.1",
"@types/source-map-support": "^0.5.7",
Expand Down Expand Up @@ -95,5 +95,10 @@
"ignorePatterns": [
"tests/fixtures"
]
},
"pnpm": {
"patchedDependencies": {
"@types/[email protected]": "patches/@[email protected]"
}
}
}
14 changes: 14 additions & 0 deletions patches/@[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
diff --git a/module.d.ts b/module.d.ts
index 3d04c3303f0eae7b23af318b68bc101ad6fe2613..d7797b36b268f7d4eca81dda508cc17b9f10d159 100644
--- a/module.d.ts
+++ b/module.d.ts
@@ -211,7 +211,8 @@ declare module "module" {
/**
* An object whose key-value pairs represent the assertions for the module to import
*/
- importAssertions: ImportAssertions;
+ importAssertions?: ImportAssertions;
+ importAttributes?: ImportAssertions;
}
interface LoadFnOutput {
format: ModuleFormat;
26 changes: 19 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions src/esm/loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { TransformOptions } from 'esbuild';
import { transform, transformDynamicImport } from '../utils/transform';
import { resolveTsPath } from '../utils/resolve-ts-path';
import { installSourceMapSupport, shouldStripSourceMap, stripSourceMap } from '../source-map';
import { importAttributes } from '../utils/node-features';
import {
tsconfigPathsMatcher,
fileMatcher,
Expand Down Expand Up @@ -245,6 +246,8 @@ export const resolve: resolve = async function (
}
};

const contextAttributesProperty = importAttributes ? 'importAttributes' : 'importAssertions';

export const load: LoadHook = async function (

Check warning on line 251 in src/esm/loaders.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Unexpected unnamed async function

Check warning on line 251 in src/esm/loaders.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Async function has a complexity of 11. Maximum allowed is 10
url,
context,
Expand All @@ -262,10 +265,11 @@ export const load: LoadHook = async function (
}

if (isJsonPattern.test(url)) {
if (!context.importAssertions) {
context.importAssertions = {};
if (!context[contextAttributesProperty]) {
context[contextAttributesProperty] = {};
}
context.importAssertions.type = 'json';

context[contextAttributesProperty]!.type = 'json';
}

const loaded = await defaultLoad(url, context);
Expand Down
2 changes: 2 additions & 0 deletions src/utils/node-features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ const compareNodeVersion = (version: Version) => (
export const isolatedLoader = compareNodeVersion([20, 0, 0]) >= 0;

export const supportsModuleRegister = compareNodeVersion([20, 6, 0]) >= 0;

export const importAttributes = compareNodeVersion([21, 0, 0]) >= 0;
3 changes: 2 additions & 1 deletion tests/utils/node-versions.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
export const nodeVersions = [
'20',
'21',
...(
(
process.env.CI
&& process.platform !== 'win32'
)
? [
'20',
'18',
] as const
: [] as const
Expand Down

0 comments on commit 02b43e5

Please sign in to comment.