Skip to content

Instantly share code, notes, and snippets.

@jkrems
Last active June 8, 2020 18:54
Show Gist options
  • Save jkrems/42ffe551614a2c41200f1e9055efcac5 to your computer and use it in GitHub Desktop.
Save jkrems/42ffe551614a2c41200f1e9055efcac5 to your computer and use it in GitHub Desktop.
Exports Hash
module.exports = 'ok';
module.exports = 42;
{
"name": "exports-hash",
"exports": {
"./#foo": "./ok.js",
"./module": "./ok.js",
"./๐ŸŽ‰": "./ok.js",
"./%F0%9F%8E%89": "./other.js",
"./bar#foo": "./ok.js",
"./#zapp/": "./"
}
}
import {createRequire} from 'module';
const requireSync = createRequire(import.meta.url);
async function require(specifier) {
return {default: requireSync(specifier)};
}
const specifiers = [
// Does resolve (exact match)
'exports-hash/#foo',
'exports-hash/bar#foo',
'exports-hash/module',
// Does resolve (prefix matching)
'exports-hash/#zapp/ok.js#abc',
'exports-hash/#zapp/ok.js?abc',
// Does resolve, since it's prefix-matched, it gives the same result.
'exports-hash/#zapp/๐ŸŽ‰.js',
'exports-hash/#zapp/%F0%9F%8E%89.js',
// Does resolve but no URL-style normalization, different result!
'exports-hash/๐ŸŽ‰',
'exports-hash/%F0%9F%8E%89',
// Does *not* resolve (exact match)
'exports-hash/module#foo',
'exports-hash/module?foo',
];
function log(specifier, promise) {
promise.then(value => {
console.error('loaded %j', specifier, value);
}).catch(e => {
console.error('failed %j', specifier, e);
});
}
async function main() {
for (const specifier of specifiers) {
log(specifier, import(specifier));
log(specifier, require(specifier));
}
}
main();
module.exports = 'tada';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment