mirror of https://github.com/lukechilds/node.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
767 B
30 lines
767 B
8 years ago
|
import module from 'module';
|
||
|
|
||
|
const builtins = new Set(
|
||
|
Object.keys(process.binding('natives')).filter(str =>
|
||
|
/^(?!(?:internal|node|v8)\/)/.test(str))
|
||
|
)
|
||
|
|
||
|
export function resolve (specifier, base, defaultResolver) {
|
||
|
if (builtins.has(specifier)) {
|
||
|
return {
|
||
|
url: `node:${specifier}`,
|
||
|
format: 'dynamic'
|
||
|
};
|
||
|
}
|
||
|
return defaultResolver(specifier, base);
|
||
|
}
|
||
|
|
||
|
export async function dynamicInstantiate (url) {
|
||
|
const builtinInstance = module._load(url.substr(5));
|
||
|
const builtinExports = ['default', ...Object.keys(builtinInstance)];
|
||
|
return {
|
||
|
exports: builtinExports,
|
||
|
execute: exports => {
|
||
|
for (let name of builtinExports)
|
||
|
exports[name].set(builtinInstance[name]);
|
||
|
exports.default.set(builtinInstance);
|
||
|
}
|
||
|
};
|
||
|
}
|