Last active
February 15, 2018 08:31
-
-
Save WebReflection/ecfe5786eddbe3931875f6e7365cad15 to your computer and use it in GitHub Desktop.
A loader for both `.mjs` and `.m.js` extension.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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