123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- const loader = require('./loader');
- const Module = require('module');
- const NATIVE_MODULE_PREFIX = 'node:';
- function isNativeModule(targetPath, parentModule) {
- let lookupPaths;
- let isNative = false;
- if (targetPath.startsWith(NATIVE_MODULE_PREFIX)) {
- isNative = true;
- } else {
- lookupPaths = Module._resolveLookupPaths(targetPath, parentModule, true);
-
- isNative =
- lookupPaths === null ||
- (lookupPaths.length === 2 && lookupPaths[1].length === 0 && lookupPaths[0] === targetPath);
- }
- return isNative;
- }
- class Requizzle {
- constructor(options, cache) {
- this._options = options;
- this._cache = cache || {
- module: {},
- source: {},
- };
- }
-
- requizzle(targetPath) {
- const options = this._options;
- const parentModule = options.parent;
- let targetModule;
- let wrapper;
-
- if (isNativeModule(targetPath, parentModule)) {
- return require(targetPath);
- }
-
- targetPath = Module._resolveFilename(targetPath, parentModule);
- wrapper = loader.createWrapper(targetPath, parentModule, this._cache, this._options);
- targetModule = loader.load(targetPath, parentModule, wrapper, this._cache, this._options);
- return targetModule.exports;
- }
- }
- module.exports = Requizzle;
|