Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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.

resolver.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. function path() {
  7. const data = _interopRequireWildcard(require('path'));
  8. path = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _chalk() {
  14. const data = _interopRequireDefault(require('chalk'));
  15. _chalk = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _slash() {
  21. const data = _interopRequireDefault(require('slash'));
  22. _slash = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. function _jestUtil() {
  28. const data = require('jest-util');
  29. _jestUtil = function () {
  30. return data;
  31. };
  32. return data;
  33. }
  34. var _ModuleNotFoundError = _interopRequireDefault(
  35. require('./ModuleNotFoundError')
  36. );
  37. var _defaultResolver = _interopRequireWildcard(require('./defaultResolver'));
  38. var _isBuiltinModule = _interopRequireDefault(require('./isBuiltinModule'));
  39. var _nodeModulesPaths = _interopRequireDefault(require('./nodeModulesPaths'));
  40. var _shouldLoadAsEsm = _interopRequireWildcard(require('./shouldLoadAsEsm'));
  41. function _interopRequireDefault(obj) {
  42. return obj && obj.__esModule ? obj : {default: obj};
  43. }
  44. function _getRequireWildcardCache(nodeInterop) {
  45. if (typeof WeakMap !== 'function') return null;
  46. var cacheBabelInterop = new WeakMap();
  47. var cacheNodeInterop = new WeakMap();
  48. return (_getRequireWildcardCache = function (nodeInterop) {
  49. return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
  50. })(nodeInterop);
  51. }
  52. function _interopRequireWildcard(obj, nodeInterop) {
  53. if (!nodeInterop && obj && obj.__esModule) {
  54. return obj;
  55. }
  56. if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
  57. return {default: obj};
  58. }
  59. var cache = _getRequireWildcardCache(nodeInterop);
  60. if (cache && cache.has(obj)) {
  61. return cache.get(obj);
  62. }
  63. var newObj = {};
  64. var hasPropertyDescriptor =
  65. Object.defineProperty && Object.getOwnPropertyDescriptor;
  66. for (var key in obj) {
  67. if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) {
  68. var desc = hasPropertyDescriptor
  69. ? Object.getOwnPropertyDescriptor(obj, key)
  70. : null;
  71. if (desc && (desc.get || desc.set)) {
  72. Object.defineProperty(newObj, key, desc);
  73. } else {
  74. newObj[key] = obj[key];
  75. }
  76. }
  77. }
  78. newObj.default = obj;
  79. if (cache) {
  80. cache.set(obj, newObj);
  81. }
  82. return newObj;
  83. }
  84. function _defineProperty(obj, key, value) {
  85. if (key in obj) {
  86. Object.defineProperty(obj, key, {
  87. value: value,
  88. enumerable: true,
  89. configurable: true,
  90. writable: true
  91. });
  92. } else {
  93. obj[key] = value;
  94. }
  95. return obj;
  96. }
  97. const NATIVE_PLATFORM = 'native'; // We might be inside a symlink.
  98. const resolvedCwd = (0, _jestUtil().tryRealpath)(process.cwd());
  99. const {NODE_PATH} = process.env;
  100. const nodePaths = NODE_PATH
  101. ? NODE_PATH.split(path().delimiter)
  102. .filter(Boolean) // The resolver expects absolute paths.
  103. .map(p => path().resolve(resolvedCwd, p))
  104. : undefined;
  105. class Resolver {
  106. constructor(moduleMap, options) {
  107. _defineProperty(this, '_options', void 0);
  108. _defineProperty(this, '_moduleMap', void 0);
  109. _defineProperty(this, '_moduleIDCache', void 0);
  110. _defineProperty(this, '_moduleNameCache', void 0);
  111. _defineProperty(this, '_modulePathCache', void 0);
  112. _defineProperty(this, '_supportsNativePlatform', void 0);
  113. this._options = {
  114. defaultPlatform: options.defaultPlatform,
  115. extensions: options.extensions,
  116. hasCoreModules:
  117. options.hasCoreModules === undefined ? true : options.hasCoreModules,
  118. moduleDirectories: options.moduleDirectories || ['node_modules'],
  119. moduleNameMapper: options.moduleNameMapper,
  120. modulePaths: options.modulePaths,
  121. platforms: options.platforms,
  122. resolver: options.resolver,
  123. rootDir: options.rootDir
  124. };
  125. this._supportsNativePlatform = options.platforms
  126. ? options.platforms.includes(NATIVE_PLATFORM)
  127. : false;
  128. this._moduleMap = moduleMap;
  129. this._moduleIDCache = new Map();
  130. this._moduleNameCache = new Map();
  131. this._modulePathCache = new Map();
  132. }
  133. static tryCastModuleNotFoundError(error) {
  134. if (error instanceof _ModuleNotFoundError.default) {
  135. return error;
  136. }
  137. const casted = error;
  138. if (casted.code === 'MODULE_NOT_FOUND') {
  139. return _ModuleNotFoundError.default.duckType(casted);
  140. }
  141. return null;
  142. }
  143. static clearDefaultResolverCache() {
  144. (0, _defaultResolver.clearDefaultResolverCache)();
  145. (0, _shouldLoadAsEsm.clearCachedLookups)();
  146. }
  147. static findNodeModule(path, options) {
  148. const resolver = options.resolver
  149. ? require(options.resolver)
  150. : _defaultResolver.default;
  151. const paths = options.paths;
  152. try {
  153. return resolver(path, {
  154. basedir: options.basedir,
  155. browser: options.browser,
  156. conditions: options.conditions,
  157. defaultResolver: _defaultResolver.default,
  158. extensions: options.extensions,
  159. moduleDirectory: options.moduleDirectory,
  160. paths: paths ? (nodePaths || []).concat(paths) : nodePaths,
  161. rootDir: options.rootDir
  162. });
  163. } catch (e) {
  164. if (options.throwIfNotFound) {
  165. throw e;
  166. }
  167. }
  168. return null;
  169. } // unstable as it should be replaced by https://github.com/nodejs/modules/issues/393, and we don't want people to use it
  170. resolveModuleFromDirIfExists(dirname, moduleName, options) {
  171. const paths =
  172. (options === null || options === void 0 ? void 0 : options.paths) ||
  173. this._options.modulePaths;
  174. const moduleDirectory = this._options.moduleDirectories;
  175. const stringifiedOptions = options ? JSON.stringify(options) : '';
  176. const key = dirname + path().delimiter + moduleName + stringifiedOptions;
  177. const defaultPlatform = this._options.defaultPlatform;
  178. const extensions = this._options.extensions.slice();
  179. let module;
  180. if (this._supportsNativePlatform) {
  181. extensions.unshift(
  182. ...this._options.extensions.map(ext => '.' + NATIVE_PLATFORM + ext)
  183. );
  184. }
  185. if (defaultPlatform) {
  186. extensions.unshift(
  187. ...this._options.extensions.map(ext => '.' + defaultPlatform + ext)
  188. );
  189. } // 1. If we have already resolved this module for this directory name,
  190. // return a value from the cache.
  191. const cacheResult = this._moduleNameCache.get(key);
  192. if (cacheResult) {
  193. return cacheResult;
  194. } // 2. Check if the module is a haste module.
  195. module = this.getModule(moduleName);
  196. if (module) {
  197. this._moduleNameCache.set(key, module);
  198. return module;
  199. } // 3. Check if the module is a node module and resolve it based on
  200. // the node module resolution algorithm. If skipNodeResolution is given we
  201. // ignore all modules that look like node modules (ie. are not relative
  202. // requires). This enables us to speed up resolution when we build a
  203. // dependency graph because we don't have to look at modules that may not
  204. // exist and aren't mocked.
  205. const skipResolution =
  206. options && options.skipNodeResolution && !moduleName.includes(path().sep);
  207. const resolveNodeModule = (name, throwIfNotFound = false) => {
  208. if (this.isCoreModule(name)) {
  209. return name;
  210. }
  211. return Resolver.findNodeModule(name, {
  212. basedir: dirname,
  213. conditions:
  214. options === null || options === void 0 ? void 0 : options.conditions,
  215. extensions,
  216. moduleDirectory,
  217. paths,
  218. resolver: this._options.resolver,
  219. rootDir: this._options.rootDir,
  220. throwIfNotFound
  221. });
  222. };
  223. if (!skipResolution) {
  224. module = resolveNodeModule(moduleName, Boolean(process.versions.pnp));
  225. if (module) {
  226. this._moduleNameCache.set(key, module);
  227. return module;
  228. }
  229. } // 4. Resolve "haste packages" which are `package.json` files outside of
  230. // `node_modules` folders anywhere in the file system.
  231. const parts = moduleName.split('/');
  232. const hastePackage = this.getPackage(parts.shift());
  233. if (hastePackage) {
  234. try {
  235. const module = path().join.apply(
  236. path(),
  237. [path().dirname(hastePackage)].concat(parts)
  238. ); // try resolving with custom resolver first to support extensions,
  239. // then fallback to require.resolve
  240. const resolvedModule =
  241. resolveNodeModule(module) || require.resolve(module);
  242. this._moduleNameCache.set(key, resolvedModule);
  243. return resolvedModule;
  244. } catch {}
  245. }
  246. return null;
  247. }
  248. resolveModule(from, moduleName, options) {
  249. const dirname = path().dirname(from);
  250. const module =
  251. this.resolveStubModuleName(from, moduleName) ||
  252. this.resolveModuleFromDirIfExists(dirname, moduleName, options);
  253. if (module) return module; // 5. Throw an error if the module could not be found. `resolve.sync` only
  254. // produces an error based on the dirname but we have the actual current
  255. // module name available.
  256. const relativePath =
  257. (0, _slash().default)(path().relative(this._options.rootDir, from)) ||
  258. '.';
  259. throw new _ModuleNotFoundError.default(
  260. `Cannot find module '${moduleName}' from '${relativePath}'`,
  261. moduleName
  262. );
  263. }
  264. _isAliasModule(moduleName) {
  265. const moduleNameMapper = this._options.moduleNameMapper;
  266. if (!moduleNameMapper) {
  267. return false;
  268. }
  269. return moduleNameMapper.some(({regex}) => regex.test(moduleName));
  270. }
  271. isCoreModule(moduleName) {
  272. return (
  273. this._options.hasCoreModules &&
  274. ((0, _isBuiltinModule.default)(moduleName) ||
  275. (moduleName.startsWith('node:') &&
  276. (0, _isBuiltinModule.default)(moduleName.slice('node:'.length)))) &&
  277. !this._isAliasModule(moduleName)
  278. );
  279. }
  280. getModule(name) {
  281. return this._moduleMap.getModule(
  282. name,
  283. this._options.defaultPlatform,
  284. this._supportsNativePlatform
  285. );
  286. }
  287. getModulePath(from, moduleName) {
  288. if (moduleName[0] !== '.' || path().isAbsolute(moduleName)) {
  289. return moduleName;
  290. }
  291. return path().normalize(path().dirname(from) + '/' + moduleName);
  292. }
  293. getPackage(name) {
  294. return this._moduleMap.getPackage(
  295. name,
  296. this._options.defaultPlatform,
  297. this._supportsNativePlatform
  298. );
  299. }
  300. getMockModule(from, name) {
  301. const mock = this._moduleMap.getMockModule(name);
  302. if (mock) {
  303. return mock;
  304. } else {
  305. const moduleName = this.resolveStubModuleName(from, name);
  306. if (moduleName) {
  307. return this.getModule(moduleName) || moduleName;
  308. }
  309. }
  310. return null;
  311. }
  312. getModulePaths(from) {
  313. const cachedModule = this._modulePathCache.get(from);
  314. if (cachedModule) {
  315. return cachedModule;
  316. }
  317. const moduleDirectory = this._options.moduleDirectories;
  318. const paths = (0, _nodeModulesPaths.default)(from, {
  319. moduleDirectory
  320. });
  321. if (paths[paths.length - 1] === undefined) {
  322. // circumvent node-resolve bug that adds `undefined` as last item.
  323. paths.pop();
  324. }
  325. this._modulePathCache.set(from, paths);
  326. return paths;
  327. }
  328. getModuleID(virtualMocks, from, moduleName = '', options) {
  329. const stringifiedOptions = options ? JSON.stringify(options) : '';
  330. const key = from + path().delimiter + moduleName + stringifiedOptions;
  331. const cachedModuleID = this._moduleIDCache.get(key);
  332. if (cachedModuleID) {
  333. return cachedModuleID;
  334. }
  335. const moduleType = this._getModuleType(moduleName);
  336. const absolutePath = this._getAbsolutePath(
  337. virtualMocks,
  338. from,
  339. moduleName,
  340. options
  341. );
  342. const mockPath = this._getMockPath(from, moduleName);
  343. const sep = path().delimiter;
  344. const id =
  345. moduleType +
  346. sep +
  347. (absolutePath ? absolutePath + sep : '') +
  348. (mockPath ? mockPath + sep : '') +
  349. (stringifiedOptions ? stringifiedOptions + sep : '');
  350. this._moduleIDCache.set(key, id);
  351. return id;
  352. }
  353. _getModuleType(moduleName) {
  354. return this.isCoreModule(moduleName) ? 'node' : 'user';
  355. }
  356. _getAbsolutePath(virtualMocks, from, moduleName, options) {
  357. if (this.isCoreModule(moduleName)) {
  358. return moduleName;
  359. }
  360. return this._isModuleResolved(from, moduleName)
  361. ? this.getModule(moduleName)
  362. : this._getVirtualMockPath(virtualMocks, from, moduleName, options);
  363. }
  364. _getMockPath(from, moduleName) {
  365. return !this.isCoreModule(moduleName)
  366. ? this.getMockModule(from, moduleName)
  367. : null;
  368. }
  369. _getVirtualMockPath(virtualMocks, from, moduleName, options) {
  370. const virtualMockPath = this.getModulePath(from, moduleName);
  371. return virtualMocks.get(virtualMockPath)
  372. ? virtualMockPath
  373. : moduleName
  374. ? this.resolveModule(from, moduleName, options)
  375. : from;
  376. }
  377. _isModuleResolved(from, moduleName) {
  378. return !!(
  379. this.getModule(moduleName) || this.getMockModule(from, moduleName)
  380. );
  381. }
  382. resolveStubModuleName(from, moduleName) {
  383. const dirname = path().dirname(from);
  384. const paths = this._options.modulePaths;
  385. const extensions = this._options.extensions.slice();
  386. const moduleDirectory = this._options.moduleDirectories;
  387. const moduleNameMapper = this._options.moduleNameMapper;
  388. const resolver = this._options.resolver;
  389. const defaultPlatform = this._options.defaultPlatform;
  390. if (this._supportsNativePlatform) {
  391. extensions.unshift(
  392. ...this._options.extensions.map(ext => '.' + NATIVE_PLATFORM + ext)
  393. );
  394. }
  395. if (defaultPlatform) {
  396. extensions.unshift(
  397. ...this._options.extensions.map(ext => '.' + defaultPlatform + ext)
  398. );
  399. }
  400. if (moduleNameMapper) {
  401. for (const {moduleName: mappedModuleName, regex} of moduleNameMapper) {
  402. if (regex.test(moduleName)) {
  403. // Note: once a moduleNameMapper matches the name, it must result
  404. // in a module, or else an error is thrown.
  405. const matches = moduleName.match(regex);
  406. const mapModuleName = matches
  407. ? moduleName =>
  408. moduleName.replace(
  409. /\$([0-9]+)/g,
  410. (_, index) => matches[parseInt(index, 10)]
  411. )
  412. : moduleName => moduleName;
  413. const possibleModuleNames = Array.isArray(mappedModuleName)
  414. ? mappedModuleName
  415. : [mappedModuleName];
  416. let module = null;
  417. for (const possibleModuleName of possibleModuleNames) {
  418. const updatedName = mapModuleName(possibleModuleName);
  419. module =
  420. this.getModule(updatedName) ||
  421. Resolver.findNodeModule(updatedName, {
  422. basedir: dirname,
  423. extensions,
  424. moduleDirectory,
  425. paths,
  426. resolver,
  427. rootDir: this._options.rootDir
  428. });
  429. if (module) {
  430. break;
  431. }
  432. }
  433. if (!module) {
  434. throw createNoMappedModuleFoundError(
  435. moduleName,
  436. mapModuleName,
  437. mappedModuleName,
  438. regex,
  439. resolver
  440. );
  441. }
  442. return module;
  443. }
  444. }
  445. }
  446. return null;
  447. }
  448. }
  449. exports.default = Resolver;
  450. _defineProperty(Resolver, 'ModuleNotFoundError', _ModuleNotFoundError.default);
  451. _defineProperty(Resolver, 'unstable_shouldLoadAsEsm', _shouldLoadAsEsm.default);
  452. const createNoMappedModuleFoundError = (
  453. moduleName,
  454. mapModuleName,
  455. mappedModuleName,
  456. regex,
  457. resolver
  458. ) => {
  459. const mappedAs = Array.isArray(mappedModuleName)
  460. ? JSON.stringify(mappedModuleName.map(mapModuleName), null, 2)
  461. : mappedModuleName;
  462. const original = Array.isArray(mappedModuleName)
  463. ? JSON.stringify(mappedModuleName, null, 6) // using 6 because of misalignment when nested below
  464. .slice(0, -1) + ' ]' /// align last bracket correctly as well
  465. : mappedModuleName;
  466. const error = new Error(
  467. _chalk().default.red(`${_chalk().default.bold('Configuration error')}:
  468. Could not locate module ${_chalk().default.bold(moduleName)} mapped as:
  469. ${_chalk().default.bold(mappedAs)}.
  470. Please check your configuration for these entries:
  471. {
  472. "moduleNameMapper": {
  473. "${regex.toString()}": "${_chalk().default.bold(original)}"
  474. },
  475. "resolver": ${_chalk().default.bold(String(resolver))}
  476. }`)
  477. );
  478. error.name = '';
  479. return error;
  480. };