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