diff --git a/lib/esm-probe.mjs b/lib/esm-probe.mjs deleted file mode 100644 index 36eb89c50..000000000 --- a/lib/esm-probe.mjs +++ /dev/null @@ -1,2 +0,0 @@ -// Keep this file in place. -// It is there to check that ESM dynamic import actually works in the current Node.js version. diff --git a/lib/worker/subprocess.js b/lib/worker/subprocess.js index 1f02f054e..b574e1403 100644 --- a/lib/worker/subprocess.js +++ b/lib/worker/subprocess.js @@ -6,6 +6,15 @@ require('./ensure-forked'); // eslint-disable-line import/no-unassigned-import const ipc = require('./ipc'); +const supportsESM = async () => { + try { + await import('data:text/javascript,'); + return true; + } catch {} + + return false; +}; + ipc.send({type: 'ready-for-options'}); ipc.options.then(async options => { require('./options').set(options); @@ -134,23 +143,18 @@ ipc.options.then(async options => { return null; }).filter(provider => provider !== null); - // Lazily determine support since this prints an experimental warning. - let supportsESM = async () => { - try { - await import('../esm-probe.mjs'); - supportsESM = async () => true; - } catch { - supportsESM = async () => false; - } - - return supportsESM(); - }; - let requireFn = require; + let isESMSupported; const load = async ref => { for (const extension of extensionsToLoadAsModules) { if (ref.endsWith(`.${extension}`)) { - if (await supportsESM()) { // eslint-disable-line no-await-in-loop + if (typeof isESMSupported !== 'boolean') { + // Lazily determine support since this prints an experimental warning. + // eslint-disable-next-line no-await-in-loop + isESMSupported = await supportsESM(); + } + + if (isESMSupported) { return import(pathToFileURL(ref)); }