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

Commit ce287ef

Browse files
fix(watch): relay dependency paths for Node.js 20 (#64)
Co-authored-by: Hiroki Osame <[email protected]>
1 parent 1c333aa commit ce287ef

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/loaders.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,32 @@ type resolve = (
3636

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

39+
type SendToParent = (data: {
40+
type: 'dependency';
41+
path: string;
42+
}) => void;
43+
44+
let sendToParent: SendToParent | undefined = process.send ? process.send.bind(process) : undefined;
45+
3946
/**
4047
* Technically globalPreload is deprecated so it should be in loaders-deprecated
4148
* but it shares a closure with the new load hook
4249
*/
4350
let mainThreadPort: MessagePort | undefined;
4451
const _globalPreload: GlobalPreloadHook = ({ port }) => {
4552
mainThreadPort = port;
53+
sendToParent = port.postMessage.bind(port);
54+
4655
return `
4756
const require = getBuiltin('module').createRequire("${import.meta.url}");
4857
require('@esbuild-kit/core-utils').installSourceMapSupport(port);
58+
if (process.send) {
59+
port.addListener('message', (message) => {
60+
if (message.type === 'dependency') {
61+
process.send(message);
62+
}
63+
});
64+
}
4965
port.unref(); // Allows process to exit without waiting for port to close
5066
`;
5167
};
@@ -220,8 +236,8 @@ export const load: LoadHook = async function (
220236
context,
221237
defaultLoad,
222238
) {
223-
if (process.send) {
224-
process.send({
239+
if (sendToParent) {
240+
sendToParent({
225241
type: 'dependency',
226242
path: url,
227243
});

0 commit comments

Comments
 (0)