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

Commit

Permalink
fix(watch): relay dependency paths for Node.js 20 (#64)
Browse files Browse the repository at this point in the history
Co-authored-by: Hiroki Osame <[email protected]>
  • Loading branch information
npdev453 and privatenumber committed Sep 13, 2023
1 parent 1c333aa commit ce287ef
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,32 @@ type resolve = (

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

type SendToParent = (data: {
type: 'dependency';
path: string;
}) => void;

let sendToParent: SendToParent | undefined = process.send ? process.send.bind(process) : undefined;

/**
* 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;
const _globalPreload: GlobalPreloadHook = ({ port }) => {
mainThreadPort = port;
sendToParent = port.postMessage.bind(port);

return `
const require = getBuiltin('module').createRequire("${import.meta.url}");
require('@esbuild-kit/core-utils').installSourceMapSupport(port);
if (process.send) {
port.addListener('message', (message) => {
if (message.type === 'dependency') {
process.send(message);
}
});
}
port.unref(); // Allows process to exit without waiting for port to close
`;
};
Expand Down Expand Up @@ -220,8 +236,8 @@ export const load: LoadHook = async function (
context,
defaultLoad,
) {
if (process.send) {
process.send({
if (sendToParent) {
sendToParent({
type: 'dependency',
path: url,
});
Expand Down

0 comments on commit ce287ef

Please sign in to comment.