From ce287efa39b7749799ec102e5c94aec44ee19fc1 Mon Sep 17 00:00:00 2001 From: NP Date: Wed, 13 Sep 2023 11:06:05 +0300 Subject: [PATCH] fix(watch): relay dependency paths for Node.js 20 (#64) Co-authored-by: Hiroki Osame --- src/loaders.ts | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/loaders.ts b/src/loaders.ts index 6cb534c..c68494f 100644 --- a/src/loaders.ts +++ b/src/loaders.ts @@ -36,6 +36,13 @@ 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 @@ -43,9 +50,18 @@ const isolatedLoader = compareNodeVersion([20, 0, 0]) >= 0; 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 `; }; @@ -220,8 +236,8 @@ export const load: LoadHook = async function ( context, defaultLoad, ) { - if (process.send) { - process.send({ + if (sendToParent) { + sendToParent({ type: 'dependency', path: url, });