Skip to content
This repository was archived by the owner on Oct 18, 2023. It is now read-only.

Commit 220a145

Browse files
feat: Support Node.js v20 (#71)
1 parent 3a0d715 commit 220a145

File tree

5 files changed

+41
-7
lines changed

5 files changed

+41
-7
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"prepack": "pnpm build && clean-pkg-json"
3333
},
3434
"dependencies": {
35-
"@esbuild-kit/core-utils": "^3.2.3",
35+
"@esbuild-kit/core-utils": "^3.3.0",
3636
"get-tsconfig": "^4.7.0"
3737
},
3838
"devDependencies": {

pnpm-lock.yaml

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/loaders.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import type { MessagePort } from 'node:worker_threads';
12
import path from 'path';
23
import { pathToFileURL, fileURLToPath } from 'url';
3-
import type { ResolveFnOutput, ResolveHookContext, LoadHook } from 'module';
4+
import type {
5+
ResolveFnOutput, ResolveHookContext, LoadHook, GlobalPreloadHook,
6+
} from 'module';
47
import {
58
transform,
69
transformDynamicImport,
@@ -31,6 +34,20 @@ type resolve = (
3134
recursiveCall?: boolean,
3235
) => MaybePromise<ResolveFnOutput>;
3336

37+
/**
38+
* Technically globalPreload is deprecated so it should be in loaders-deprecated
39+
* but it shares a closure with the new load hook
40+
*/
41+
let mainThreadPort: MessagePort | undefined;
42+
export const globalPreload: GlobalPreloadHook = ({ port }) => {
43+
mainThreadPort = port;
44+
return `
45+
const require = getBuiltin('module').createRequire(getBuiltin('process').cwd() + '/<preload>');
46+
require('@esbuild-kit/core-utils').installSourceMapSupport(port);
47+
port.unref(); // Allows process to exit without waiting for port to close
48+
`;
49+
};
50+
3451
const extensions = ['.js', '.json', '.ts', '.tsx', '.jsx'] as const;
3552

3653
async function tryExtensions(
@@ -223,6 +240,7 @@ export const load: LoadHook = async function (
223240
const code = loaded.source.toString();
224241

225242
if (
243+
// Support named imports in JSON modules
226244
loaded.format === 'json'
227245
|| tsExtensionsPattern.test(url)
228246
) {
@@ -236,7 +254,7 @@ export const load: LoadHook = async function (
236254

237255
return {
238256
format: 'module',
239-
source: applySourceMap(transformed, url),
257+
source: applySourceMap(transformed, url, mainThreadPort),
240258
};
241259
}
242260

@@ -246,6 +264,7 @@ export const load: LoadHook = async function (
246264
loaded.source = applySourceMap(
247265
dynamicImportTransformed,
248266
url,
267+
mainThreadPort,
249268
);
250269
}
251270
}

tests/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const nodeVersions = [
1111
'16',
1212
'17',
1313
'18',
14+
'20',
1415
]
1516
: []
1617
),

tests/specs/typescript/tsconfig.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import path from 'path';
2+
import fs from 'fs/promises';
13
import { testSuite, expect } from 'manten';
24
import { createFixture } from 'fs-fixture';
35
import type { NodeApis } from '../../utils/node-with-loader.js';
@@ -37,6 +39,12 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
3739
},
3840
});
3941

42+
await fs.symlink(
43+
path.resolve('node_modules'),
44+
path.join(fixture.path, 'node_modules'),
45+
'dir',
46+
);
47+
4048
onTestFinish(async () => await fixture.rm());
4149

4250
// Strict mode is not tested because ESM is strict by default
@@ -71,6 +79,12 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
7179
'src/jsx.jsx': checkJsx,
7280
});
7381

82+
await fs.symlink(
83+
path.resolve('node_modules'),
84+
path.join(fixture.path, 'node_modules'),
85+
'dir',
86+
);
87+
7488
onTestFinish(async () => await fixture.rm());
7589

7690
const jsxJs = await node.load('./src/jsx.jsx', {

0 commit comments

Comments
 (0)