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

Commit

Permalink
feat: Support Node.js v20 (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber authored Sep 11, 2023
1 parent 3a0d715 commit 220a145
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"prepack": "pnpm build && clean-pkg-json"
},
"dependencies": {
"@esbuild-kit/core-utils": "^3.2.3",
"@esbuild-kit/core-utils": "^3.3.0",
"get-tsconfig": "^4.7.0"
},
"devDependencies": {
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

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

23 changes: 21 additions & 2 deletions src/loaders.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type { MessagePort } from 'node:worker_threads';
import path from 'path';
import { pathToFileURL, fileURLToPath } from 'url';
import type { ResolveFnOutput, ResolveHookContext, LoadHook } from 'module';
import type {
ResolveFnOutput, ResolveHookContext, LoadHook, GlobalPreloadHook,
} from 'module';
import {
transform,
transformDynamicImport,
Expand Down Expand Up @@ -31,6 +34,20 @@ type resolve = (
recursiveCall?: boolean,
) => MaybePromise<ResolveFnOutput>;

/**
* Technically globalPreload is deprecated so it should be in loaders-deprecated
* but it shares a closure with the new load hook
*/
let mainThreadPort: MessagePort | undefined;
export const globalPreload: GlobalPreloadHook = ({ port }) => {
mainThreadPort = port;
return `
const require = getBuiltin('module').createRequire(getBuiltin('process').cwd() + '/<preload>');
require('@esbuild-kit/core-utils').installSourceMapSupport(port);
port.unref(); // Allows process to exit without waiting for port to close
`;
};

const extensions = ['.js', '.json', '.ts', '.tsx', '.jsx'] as const;

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

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

return {
format: 'module',
source: applySourceMap(transformed, url),
source: applySourceMap(transformed, url, mainThreadPort),
};
}

Expand All @@ -246,6 +264,7 @@ export const load: LoadHook = async function (
loaded.source = applySourceMap(
dynamicImportTransformed,
url,
mainThreadPort,
);
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const nodeVersions = [
'16',
'17',
'18',
'20',
]
: []
),
Expand Down
14 changes: 14 additions & 0 deletions tests/specs/typescript/tsconfig.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import path from 'path';
import fs from 'fs/promises';
import { testSuite, expect } from 'manten';
import { createFixture } from 'fs-fixture';
import type { NodeApis } from '../../utils/node-with-loader.js';
Expand Down Expand Up @@ -37,6 +39,12 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
},
});

await fs.symlink(
path.resolve('node_modules'),
path.join(fixture.path, 'node_modules'),
'dir',
);

onTestFinish(async () => await fixture.rm());

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

await fs.symlink(
path.resolve('node_modules'),
path.join(fixture.path, 'node_modules'),
'dir',
);

onTestFinish(async () => await fixture.rm());

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

0 comments on commit 220a145

Please sign in to comment.