UNPKG

3.92 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4function createResolver(_a) {
5 var _this = this;
6 var id = _a.id, load = _a.load;
7 var resolved = null;
8 var resolvePromise = null;
9 var hasTriedSyncResolve = false;
10 var resolvedId = id && id();
11 return {
12 get id() {
13 return resolvedId;
14 },
15 get resolved() {
16 if (resolved == null && !hasTriedSyncResolve) {
17 hasTriedSyncResolve = true;
18 resolved = resolvedId ? trySynchronousResolve(resolvedId) : null;
19 }
20 return resolved;
21 },
22 resolve: function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
23 return tslib_1.__generator(this, function (_a) {
24 switch (_a.label) {
25 case 0:
26 resolvePromise = resolvePromise || resolve(load);
27 return [4 /*yield*/, resolvePromise];
28 case 1:
29 resolved = _a.sent();
30 return [2 /*return*/, resolved];
31 }
32 });
33 }); },
34 };
35}
36exports.createResolver = createResolver;
37function resolve(load) {
38 return tslib_1.__awaiter(this, void 0, void 0, function () {
39 var resolved;
40 return tslib_1.__generator(this, function (_a) {
41 switch (_a.label) {
42 case 0: return [4 /*yield*/, load()];
43 case 1:
44 resolved = _a.sent();
45 return [2 /*return*/, normalize(resolved)];
46 }
47 });
48 });
49}
50function normalize(module) {
51 if (module == null) {
52 return null;
53 }
54 var value = typeof module === 'object' && 'default' in module ? module.default : module;
55 return value == null ? null : value;
56}
57/* eslint-enable babel/camelcase */
58// Webpack does not like seeing an explicit require(someVariable) in code
59// because that is a dynamic require that it can’t resolve. This code
60// obfuscates `require()` for the purpose of fooling Webpack, which is fine
61// because we only want to use the `require()` in cases where Webpack
62// is not the module bundler.
63//
64// If we ever reference `require` directly, Webpack complains. So, we first
65// check global["require"], which works in Node. However, this doesn’t work
66// in Jest when the test is set to simulate a browser, as global in that case
67// in a Window object. There, we can only rely on module.require, which is
68// actually supposed to be something different but in Jest is the same as
69// the global require function.
70var requireKey = 'require';
71var nodeRequire = (typeof global === 'object' &&
72 typeof global[requireKey] === 'function' &&
73 global[requireKey]) ||
74 (typeof module === 'object' &&
75 typeof module[requireKey] === 'function' &&
76 module[requireKey]) ||
77 undefined;
78// If we have an ID, we try to first use Webpack’s internal stuff
79// to resolve the module. If those don’t exist, we know we aren’t
80// inside of a Webpack bundle, so we try to use Node’s native resolution
81// (which will work in environments like Jest’s test runner).
82function tryRequire(id) {
83 if (
84 /* eslint-disable babel/camelcase */
85 typeof __webpack_require__ === 'function' &&
86 typeof __webpack_modules__ === 'object' &&
87 __webpack_modules__[id]
88 /* eslint-enable babel/camelcase */
89 ) {
90 try {
91 return normalize(__webpack_require__(id));
92 }
93 catch (_a) {
94 // Just ignore failures
95 }
96 }
97 else if (typeof nodeRequire === 'function') {
98 try {
99 return normalize(nodeRequire(id));
100 }
101 catch (_b) {
102 // Just ignore failures
103 }
104 }
105 return undefined;
106}
107function trySynchronousResolve(id) {
108 return tryRequire(id) || null;
109}