Skip to content

Instantly share code, notes, and snippets.

@WebReflection
Last active February 15, 2018 08:31
Show Gist options
  • Save WebReflection/ecfe5786eddbe3931875f6e7365cad15 to your computer and use it in GitHub Desktop.
Save WebReflection/ecfe5786eddbe3931875f6e7365cad15 to your computer and use it in GitHub Desktop.
A loader for both `.mjs` and `.m.js` extension.
import _url from 'url';
const builtins = new Set(
Object.keys(process.binding('natives')).filter(str =>
/^(?!(?:internal|node|v8)\/)/.test(str))
);
const esmIfExplicit = url => {
const eof = url.slice(-5);
return {
url,
format: eof === '.m.js' || eof.slice(1) === '.mjs' ?
'esm' : 'dynamic'
};
};
export const resolve = (specifier, base) =>
builtins.has(specifier) ?
{url: specifier, format: 'builtin'} :
esmIfExplicit(new _url.URL(specifier, base).href;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment