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

Commit

Permalink
fix: only use globalPreload for Node v20+ (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber authored Sep 13, 2023
1 parent b57bed7 commit 1c333aa
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ type resolve = (
recursiveCall?: boolean,
) => MaybePromise<ResolveFnOutput>;

const isolatedLoader = compareNodeVersion([20, 0, 0]) >= 0;

/**
* 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 }) => {
const _globalPreload: GlobalPreloadHook = ({ port }) => {
mainThreadPort = port;
return `
const require = getBuiltin('module').createRequire("${import.meta.url}");
Expand All @@ -48,6 +50,8 @@ export const globalPreload: GlobalPreloadHook = ({ port }) => {
`;
};

export const globalPreload = isolatedLoader ? _globalPreload : undefined;

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

async function tryExtensions(
Expand Down

0 comments on commit 1c333aa

Please sign in to comment.