UNPKG

4.24 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true
5});
6exports.default = defaultResolver;
7exports.clearDefaultResolverCache = clearDefaultResolverCache;
8
9function fs() {
10 const data = _interopRequireWildcard(require('fs'));
11
12 fs = function () {
13 return data;
14 };
15
16 return data;
17}
18
19function _resolve() {
20 const data = require('resolve');
21
22 _resolve = function () {
23 return data;
24 };
25
26 return data;
27}
28
29function _browserResolve() {
30 const data = require('browser-resolve');
31
32 _browserResolve = function () {
33 return data;
34 };
35
36 return data;
37}
38
39function _realpathNative() {
40 const data = require('realpath-native');
41
42 _realpathNative = function () {
43 return data;
44 };
45
46 return data;
47}
48
49function _jestPnpResolver() {
50 const data = _interopRequireDefault(require('jest-pnp-resolver'));
51
52 _jestPnpResolver = function () {
53 return data;
54 };
55
56 return data;
57}
58
59function _interopRequireDefault(obj) {
60 return obj && obj.__esModule ? obj : {default: obj};
61}
62
63function _getRequireWildcardCache() {
64 if (typeof WeakMap !== 'function') return null;
65 var cache = new WeakMap();
66 _getRequireWildcardCache = function () {
67 return cache;
68 };
69 return cache;
70}
71
72function _interopRequireWildcard(obj) {
73 if (obj && obj.__esModule) {
74 return obj;
75 }
76 if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
77 return {default: obj};
78 }
79 var cache = _getRequireWildcardCache();
80 if (cache && cache.has(obj)) {
81 return cache.get(obj);
82 }
83 var newObj = {};
84 var hasPropertyDescriptor =
85 Object.defineProperty && Object.getOwnPropertyDescriptor;
86 for (var key in obj) {
87 if (Object.prototype.hasOwnProperty.call(obj, key)) {
88 var desc = hasPropertyDescriptor
89 ? Object.getOwnPropertyDescriptor(obj, key)
90 : null;
91 if (desc && (desc.get || desc.set)) {
92 Object.defineProperty(newObj, key, desc);
93 } else {
94 newObj[key] = obj[key];
95 }
96 }
97 }
98 newObj.default = obj;
99 if (cache) {
100 cache.set(obj, newObj);
101 }
102 return newObj;
103}
104
105/**
106 * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
107 *
108 * This source code is licensed under the MIT license found in the
109 * LICENSE file in the root directory of this source tree.
110 */
111function defaultResolver(path, options) {
112 // @ts-ignore: the "pnp" version named isn't in DefinitelyTyped
113 if (process.versions.pnp) {
114 return (0, _jestPnpResolver().default)(path, options);
115 }
116
117 const resolve = options.browser ? _browserResolve().sync : _resolve().sync;
118 const result = resolve(path, {
119 basedir: options.basedir,
120 extensions: options.extensions,
121 isDirectory,
122 isFile,
123 moduleDirectory: options.moduleDirectory,
124 paths: options.paths,
125 preserveSymlinks: false
126 });
127
128 try {
129 // Dereference symlinks to ensure we don't create a separate
130 // module instance depending on how it was referenced.
131 const resolved = (0, _realpathNative().sync)(result);
132
133 if (resolved) {
134 return resolved;
135 }
136 } catch (_unused) {
137 // ignore
138 }
139
140 return result;
141}
142
143function clearDefaultResolverCache() {
144 checkedPaths.clear();
145}
146
147var IPathType;
148
149(function (IPathType) {
150 IPathType[(IPathType['FILE'] = 1)] = 'FILE';
151 IPathType[(IPathType['DIRECTORY'] = 2)] = 'DIRECTORY';
152 IPathType[(IPathType['OTHER'] = 3)] = 'OTHER';
153})(IPathType || (IPathType = {}));
154
155const checkedPaths = new Map();
156
157function statSyncCached(path) {
158 const result = checkedPaths.get(path);
159
160 if (result !== undefined) {
161 return result;
162 }
163
164 let stat;
165
166 try {
167 stat = fs().statSync(path);
168 } catch (e) {
169 if (!(e && (e.code === 'ENOENT' || e.code === 'ENOTDIR'))) {
170 throw e;
171 }
172 }
173
174 if (stat) {
175 if (stat.isFile() || stat.isFIFO()) {
176 checkedPaths.set(path, IPathType.FILE);
177 return IPathType.FILE;
178 } else if (stat.isDirectory()) {
179 checkedPaths.set(path, IPathType.DIRECTORY);
180 return IPathType.DIRECTORY;
181 }
182 }
183
184 checkedPaths.set(path, IPathType.OTHER);
185 return IPathType.OTHER;
186}
187/*
188 * helper functions
189 */
190
191function isFile(file) {
192 return statSyncCached(file) === IPathType.FILE;
193}
194
195function isDirectory(dir) {
196 return statSyncCached(dir) === IPathType.DIRECTORY;
197}