diff --git a/.travis.yml b/.travis.yml index 25d33de..a4ebd4d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,7 @@ jobs: script: npm run lint language: node_js node_js: + - 20 - 18 - 16 - 14 diff --git a/lib/loaders/ipc.mjs b/lib/loaders/ipc.mjs index 61c48c4..8fc1aad 100644 --- a/lib/loaders/ipc.mjs +++ b/lib/loaders/ipc.mjs @@ -3,3 +3,7 @@ const cmd = 'NODE_DEV'; export const send = m => { if (process.connected) process.send({ ...m, cmd }); }; + +export const sendPort = (port, m) => { + if (port) port.postMessage({ ...m, cmd }); +}; diff --git a/lib/loaders/load.mjs b/lib/loaders/load.mjs index 2772f60..97b30b0 100644 --- a/lib/loaders/load.mjs +++ b/lib/loaders/load.mjs @@ -1,13 +1,17 @@ import { createRequire } from 'module'; import { fileURLToPath } from 'url'; -import { send } from './ipc.mjs'; +import { sendPort } from './ipc.mjs'; const require = createRequire(import.meta.url); +// Port used for communication between the loader and ESM modules +// https://nodejs.org/api/esm.html#globalpreload +let port; + export async function load(url, context, defaultLoad) { const required = url.startsWith('file://') ? fileURLToPath(url) : url; - send({ required }); + sendPort(port, { required }); try { return await defaultLoad(url, context, defaultLoad); @@ -19,3 +23,15 @@ export async function load(url, context, defaultLoad) { }); } } + +export const globalPreload = (context) => { + // Store port + port = context.port; + + // Inject code to forward loader events to the parent + return ` + port.onmessage = (evt) => { + if (process.connected) process.send(evt.data); + }; + `; +}; diff --git a/test/fixture/experimental-specifier-resolution/index.mjs b/test/fixture/experimental-specifier-resolution/index.mjs deleted file mode 100644 index 0ac70ad..0000000 --- a/test/fixture/experimental-specifier-resolution/index.mjs +++ /dev/null @@ -1 +0,0 @@ -export default 'experimental-specifier-resolution'; diff --git a/test/fixture/resolution.mjs b/test/fixture/resolution.mjs deleted file mode 100644 index c4d373c..0000000 --- a/test/fixture/resolution.mjs +++ /dev/null @@ -1,7 +0,0 @@ -import resolution from './experimental-specifier-resolution'; -import message from './message'; - -setTimeout(() => {}, 10000); - -console.log(resolution); -console.log(message); diff --git a/test/spawn/esmodule.js b/test/spawn/esmodule.js index 1b6b816..1fa2c5f 100644 --- a/test/spawn/esmodule.js +++ b/test/spawn/esmodule.js @@ -2,20 +2,6 @@ const tap = require('tap'); const { spawn, touchFile } = require('../utils'); -tap.test('Supports ECMAScript modules with experimental-specifier-resolution', t => { - spawn('--experimental-specifier-resolution=node resolution.mjs', out => { - if (out.match(/touch message.js/)) { - touchFile('message.js'); - return out2 => { - if (out2.match(/Restarting/)) { - t.match(out2, /\[INFO\] \d{2}:\d{2}:\d{2} Restarting/); - return { exit: t.end.bind(t) }; - } - }; - } - }); -}); - tap.test('Supports ECMAScript modules', t => { spawn('ecma-script-modules.mjs', out => { if (out.match(/touch message.mjs/)) {