Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Improve detection of ESM support
  • Loading branch information
fisker committed Feb 23, 2020
1 parent d0e2161 commit 8831f54
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
2 changes: 0 additions & 2 deletions lib/esm-probe.mjs

This file was deleted.

30 changes: 17 additions & 13 deletions lib/worker/subprocess.js
Expand Up @@ -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);
Expand Down Expand Up @@ -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));
}

Expand Down

0 comments on commit 8831f54

Please sign in to comment.