| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|---|
| 4 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 5 | value: true
|
|---|
| 6 | });
|
|---|
| 7 | exports["default"] = patchRequire;
|
|---|
| 8 | var path = _interopRequireWildcard(require("path"));
|
|---|
| 9 | function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
|
|---|
| 10 | var isWin32 = process.platform === 'win32';
|
|---|
| 11 | var correctPath = isWin32 ? require('./correctPath').correctPath : function (p) {
|
|---|
| 12 | return p;
|
|---|
| 13 | };
|
|---|
| 14 | function stripBOM(content) {
|
|---|
| 15 | if (content.charCodeAt(0) === 0xFEFF) {
|
|---|
| 16 | content = content.slice(1);
|
|---|
| 17 | }
|
|---|
| 18 | return content;
|
|---|
| 19 | }
|
|---|
| 20 | function patchRequire(vol) {
|
|---|
| 21 | var unixifyPaths = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|---|
| 22 | var Module = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : require('module');
|
|---|
| 23 | if (isWin32 && unixifyPaths) {
|
|---|
| 24 | var original = vol;
|
|---|
| 25 | vol = {
|
|---|
| 26 | readFileSync: function readFileSync(path, options) {
|
|---|
| 27 | return original.readFileSync(correctPath(path), options);
|
|---|
| 28 | },
|
|---|
| 29 | realpathSync: function realpathSync(path) {
|
|---|
| 30 | return original.realpathSync(correctPath(path));
|
|---|
| 31 | },
|
|---|
| 32 | statSync: function statSync(path) {
|
|---|
| 33 | return original.statSync(correctPath(path));
|
|---|
| 34 | }
|
|---|
| 35 | };
|
|---|
| 36 | }
|
|---|
| 37 | function internalModuleReadFile(path) {
|
|---|
| 38 | try {
|
|---|
| 39 | return vol.readFileSync(path, 'utf8');
|
|---|
| 40 | } catch (err) {}
|
|---|
| 41 | }
|
|---|
| 42 | function internalModuleStat(filename) {
|
|---|
| 43 | try {
|
|---|
| 44 | return vol.statSync(filename).isDirectory() ? 1 : 0;
|
|---|
| 45 | } catch (err) {
|
|---|
| 46 | return -2;
|
|---|
| 47 | }
|
|---|
| 48 | }
|
|---|
| 49 | function stat(filename) {
|
|---|
| 50 | filename = path._makeLong(filename);
|
|---|
| 51 | var cache = stat.cache;
|
|---|
| 52 | if (cache !== null) {
|
|---|
| 53 | var _result = cache.get(filename);
|
|---|
| 54 | if (_result !== undefined) return _result;
|
|---|
| 55 | }
|
|---|
| 56 | var result = internalModuleStat(filename);
|
|---|
| 57 | if (cache !== null) cache.set(filename, result);
|
|---|
| 58 | return result;
|
|---|
| 59 | }
|
|---|
| 60 | stat.cache = null;
|
|---|
| 61 | var preserveSymlinks = false;
|
|---|
| 62 | function toRealPath(requestPath) {
|
|---|
| 63 | return vol.realpathSync(requestPath);
|
|---|
| 64 | }
|
|---|
| 65 | var packageMainCache = Object.create(null);
|
|---|
| 66 | function readPackage(requestPath) {
|
|---|
| 67 | var entry = packageMainCache[requestPath];
|
|---|
| 68 | if (entry) return entry;
|
|---|
| 69 | var jsonPath = path.resolve(requestPath, 'package.json');
|
|---|
| 70 | var json = internalModuleReadFile(path._makeLong(jsonPath));
|
|---|
| 71 | if (json === undefined) {
|
|---|
| 72 | return false;
|
|---|
| 73 | }
|
|---|
| 74 | var pkg;
|
|---|
| 75 | try {
|
|---|
| 76 | var pkgJson = JSON.parse(json);
|
|---|
| 77 | pkg = packageMainCache[requestPath] = pkgJson.exports && pkgJson.exports.require || pkgJson.main;
|
|---|
| 78 | } catch (e) {
|
|---|
| 79 | e.path = jsonPath;
|
|---|
| 80 | e.message = 'Error parsing ' + jsonPath + ': ' + e.message;
|
|---|
| 81 | throw e;
|
|---|
| 82 | }
|
|---|
| 83 | return pkg;
|
|---|
| 84 | }
|
|---|
| 85 | function tryFile(requestPath, isMain) {
|
|---|
| 86 | var rc = stat(requestPath);
|
|---|
| 87 | if (preserveSymlinks && !isMain) {
|
|---|
| 88 | return rc === 0 && path.resolve(requestPath);
|
|---|
| 89 | }
|
|---|
| 90 | return rc === 0 && toRealPath(requestPath);
|
|---|
| 91 | }
|
|---|
| 92 | function tryExtensions(p, exts, isMain) {
|
|---|
| 93 | for (var i = 0; i < exts.length; i++) {
|
|---|
| 94 | var filename = tryFile(p + exts[i], isMain);
|
|---|
| 95 | if (filename) {
|
|---|
| 96 | return filename;
|
|---|
| 97 | }
|
|---|
| 98 | }
|
|---|
| 99 | return false;
|
|---|
| 100 | }
|
|---|
| 101 | function tryPackage(requestPath, exts, isMain) {
|
|---|
| 102 | var pkg = readPackage(requestPath);
|
|---|
| 103 | if (!pkg) return false;
|
|---|
| 104 | var filename = path.resolve(requestPath, pkg);
|
|---|
| 105 | return tryFile(filename, isMain) || tryExtensions(filename, exts, isMain) || tryExtensions(path.resolve(filename, 'index'), exts, isMain);
|
|---|
| 106 | }
|
|---|
| 107 | Module._extensions['.js'] = function (module, filename) {
|
|---|
| 108 | var content = vol.readFileSync(filename, 'utf8');
|
|---|
| 109 | module._compile(stripBOM(content), filename);
|
|---|
| 110 | };
|
|---|
| 111 | Module._extensions['.json'] = function (module, filename) {
|
|---|
| 112 | var content = vol.readFileSync(filename, 'utf8');
|
|---|
| 113 | try {
|
|---|
| 114 | module.exports = JSON.parse(stripBOM(content));
|
|---|
| 115 | } catch (err) {
|
|---|
| 116 | err.message = filename + ': ' + err.message;
|
|---|
| 117 | throw err;
|
|---|
| 118 | }
|
|---|
| 119 | };
|
|---|
| 120 | var warned = true;
|
|---|
| 121 | Module._findPath = function (request, paths, isMain) {
|
|---|
| 122 | if (path.isAbsolute(request)) {
|
|---|
| 123 | paths = [''];
|
|---|
| 124 | } else if (!paths || paths.length === 0) {
|
|---|
| 125 | return false;
|
|---|
| 126 | }
|
|---|
| 127 | var cacheKey = request + '\x00' + (paths.length === 1 ? paths[0] : paths.join('\x00'));
|
|---|
| 128 | var entry = Module._pathCache[cacheKey];
|
|---|
| 129 | if (entry) return entry;
|
|---|
| 130 | var exts;
|
|---|
| 131 | var trailingSlash = request.length > 0 && request.charCodeAt(request.length - 1) === 47;
|
|---|
| 132 | for (var i = 0; i < paths.length; i++) {
|
|---|
| 133 | var curPath = paths[i];
|
|---|
| 134 | if (curPath && stat(curPath) < 1) continue;
|
|---|
| 135 | var basePath = correctPath(path.resolve(curPath, request));
|
|---|
| 136 | var filename;
|
|---|
| 137 | var rc = stat(basePath);
|
|---|
| 138 | if (!trailingSlash) {
|
|---|
| 139 | if (rc === 0) {
|
|---|
| 140 | if (preserveSymlinks && !isMain) {
|
|---|
| 141 | filename = path.resolve(basePath);
|
|---|
| 142 | } else {
|
|---|
| 143 | filename = toRealPath(basePath);
|
|---|
| 144 | }
|
|---|
| 145 | } else if (rc === 1) {
|
|---|
| 146 | if (exts === undefined) exts = Object.keys(Module._extensions);
|
|---|
| 147 | filename = tryPackage(basePath, exts, isMain);
|
|---|
| 148 | }
|
|---|
| 149 | if (!filename) {
|
|---|
| 150 | if (exts === undefined) exts = Object.keys(Module._extensions);
|
|---|
| 151 | filename = tryExtensions(basePath, exts, isMain);
|
|---|
| 152 | }
|
|---|
| 153 | }
|
|---|
| 154 | if (!filename && rc === 1) {
|
|---|
| 155 | if (exts === undefined) exts = Object.keys(Module._extensions);
|
|---|
| 156 | filename = tryPackage(basePath, exts, isMain);
|
|---|
| 157 | }
|
|---|
| 158 | if (!filename && rc === 1) {
|
|---|
| 159 | if (exts === undefined) exts = Object.keys(Module._extensions);
|
|---|
| 160 | filename = tryExtensions(path.resolve(basePath, 'index'), exts, isMain);
|
|---|
| 161 | }
|
|---|
| 162 | if (filename) {
|
|---|
| 163 | if (request === '.' && i > 0) {
|
|---|
| 164 | if (!warned) {
|
|---|
| 165 | warned = true;
|
|---|
| 166 | process.emitWarning('warning: require(\'.\') resolved outside the package ' + 'directory. This functionality is deprecated and will be removed ' + 'soon.', 'DeprecationWarning', 'DEP0019');
|
|---|
| 167 | }
|
|---|
| 168 | }
|
|---|
| 169 | Module._pathCache[cacheKey] = filename;
|
|---|
| 170 | return filename;
|
|---|
| 171 | }
|
|---|
| 172 | }
|
|---|
| 173 | return false;
|
|---|
| 174 | };
|
|---|
| 175 | } |
|---|