ts-jest 29.3.2 → 29.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.ts-jest-digest +1 -1
- package/CHANGELOG.md +10 -0
- package/dist/cli/config/init.js +103 -128
- package/dist/cli/config/migrate.js +113 -156
- package/dist/cli/help.js +14 -42
- package/dist/cli/helpers/presets.js +7 -8
- package/dist/cli/index.js +47 -101
- package/dist/config/paths-to-module-name-mapper.js +37 -61
- package/dist/constants.js +1 -5
- package/dist/index.js +2 -2
- package/dist/legacy/compiler/compiler-utils.js +5 -5
- package/dist/legacy/compiler/ts-compiler.js +194 -213
- package/dist/legacy/compiler/ts-jest-compiler.js +9 -9
- package/dist/legacy/config/config-set.js +281 -292
- package/dist/legacy/index.js +2 -4
- package/dist/legacy/ts-jest-transformer.js +134 -201
- package/dist/presets/all-presets.js +2 -2
- package/dist/presets/create-jest-preset.js +101 -139
- package/dist/transformers/hoist-jest.js +30 -43
- package/dist/transpilers/typescript/transpile-module.js +69 -98
- package/dist/types.js +1 -1
- package/dist/utils/backports.js +26 -28
- package/dist/utils/get-package-version.js +2 -2
- package/dist/utils/importer.js +53 -108
- package/dist/utils/json.js +2 -2
- package/dist/utils/jsonable-value.js +20 -27
- package/dist/utils/logger.js +11 -15
- package/dist/utils/memoize.js +11 -17
- package/dist/utils/messages.js +2 -3
- package/dist/utils/sha1.js +7 -11
- package/dist/utils/ts-error.js +19 -35
- package/package.json +18 -18
- package/tsconfig.base.json +20 -0
- package/sonar-project.properties +0 -16
|
@@ -1,53 +1,36 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __values = (this && this.__values) || function(o) {
|
|
3
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
4
|
-
if (m) return m.call(o);
|
|
5
|
-
if (o && typeof o.length === "number") return {
|
|
6
|
-
next: function () {
|
|
7
|
-
if (o && i >= o.length) o = void 0;
|
|
8
|
-
return { value: o && o[i++], done: !o };
|
|
9
|
-
}
|
|
10
|
-
};
|
|
11
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
12
|
-
};
|
|
13
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
14
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
15
|
-
if (!m) return o;
|
|
16
|
-
var i = m.call(o), r, ar = [], e;
|
|
17
|
-
try {
|
|
18
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
19
|
-
}
|
|
20
|
-
catch (error) { e = { error: error }; }
|
|
21
|
-
finally {
|
|
22
|
-
try {
|
|
23
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
24
|
-
}
|
|
25
|
-
finally { if (e) throw e.error; }
|
|
26
|
-
}
|
|
27
|
-
return ar;
|
|
28
|
-
};
|
|
29
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
30
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
31
|
-
if (ar || !(i in from)) {
|
|
32
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
33
|
-
ar[i] = from[i];
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
37
|
-
};
|
|
38
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
4
|
};
|
|
41
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
6
|
exports.tsTranspileModule = exports.isModernNodeModuleKind = void 0;
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
7
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
8
|
+
const typescript_1 = __importDefault(require("typescript"));
|
|
9
|
+
const messages_1 = require("../../utils/messages");
|
|
10
|
+
const barebonesLibContent = `/// <reference no-default-lib="true"/>
|
|
11
|
+
interface Boolean {}
|
|
12
|
+
interface Function {}
|
|
13
|
+
interface CallableFunction {}
|
|
14
|
+
interface NewableFunction {}
|
|
15
|
+
interface IArguments {}
|
|
16
|
+
interface Number {}
|
|
17
|
+
interface Object {}
|
|
18
|
+
interface RegExp {}
|
|
19
|
+
interface String {}
|
|
20
|
+
interface Array<T> { length: number; [n: number]: T; }
|
|
21
|
+
interface SymbolConstructor {
|
|
22
|
+
(desc?: string | number): symbol;
|
|
23
|
+
for(name: string): symbol;
|
|
24
|
+
readonly toStringTag: symbol;
|
|
25
|
+
}
|
|
26
|
+
declare var Symbol: SymbolConstructor;
|
|
27
|
+
interface Symbol {
|
|
28
|
+
readonly [Symbol.toStringTag]: string;
|
|
29
|
+
}`;
|
|
30
|
+
const barebonesLibName = 'lib.d.ts';
|
|
31
|
+
let barebonesLibSourceFile;
|
|
32
|
+
const carriageReturnLineFeed = '\r\n';
|
|
33
|
+
const lineFeed = '\n';
|
|
51
34
|
function getNewLineCharacter(options) {
|
|
52
35
|
switch (options.newLine) {
|
|
53
36
|
case typescript_1.default.NewLineKind.CarriageReturnLineFeed:
|
|
@@ -57,11 +40,11 @@ function getNewLineCharacter(options) {
|
|
|
57
40
|
return lineFeed;
|
|
58
41
|
}
|
|
59
42
|
}
|
|
60
|
-
|
|
43
|
+
const isModernNodeModuleKind = (module) => {
|
|
61
44
|
return module ? [typescript_1.default.ModuleKind.Node16, /* ModuleKind.Node18 */ 101, typescript_1.default.ModuleKind.NodeNext].includes(module) : false;
|
|
62
45
|
};
|
|
63
46
|
exports.isModernNodeModuleKind = isModernNodeModuleKind;
|
|
64
|
-
|
|
47
|
+
const shouldCheckProjectPkgJsonContent = (fileName, moduleKind) => {
|
|
65
48
|
return fileName.endsWith('package.json') && (0, exports.isModernNodeModuleKind)(moduleKind);
|
|
66
49
|
};
|
|
67
50
|
/**
|
|
@@ -70,41 +53,29 @@ var shouldCheckProjectPkgJsonContent = function (fileName, moduleKind) {
|
|
|
70
53
|
* - Remove generation of declaration files
|
|
71
54
|
* - Allow using custom AST transformers with the internal created {@link Program}
|
|
72
55
|
*/
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
var _b, _c, _d, _e, _f;
|
|
76
|
-
barebonesLibSourceFile !== null && barebonesLibSourceFile !== void 0 ? barebonesLibSourceFile : (barebonesLibSourceFile = typescript_1.default.createSourceFile(barebonesLibName, barebonesLibContent, {
|
|
56
|
+
const transpileWorker = (input, transpileOptions) => {
|
|
57
|
+
barebonesLibSourceFile ??= typescript_1.default.createSourceFile(barebonesLibName, barebonesLibContent, {
|
|
77
58
|
languageVersion: typescript_1.default.ScriptTarget.Latest,
|
|
78
|
-
})
|
|
79
|
-
|
|
80
|
-
|
|
59
|
+
});
|
|
60
|
+
const diagnostics = [];
|
|
61
|
+
const options = transpileOptions.compilerOptions
|
|
81
62
|
? // @ts-expect-error internal TypeScript API
|
|
82
63
|
typescript_1.default.fixupCompilerOptions(transpileOptions.compilerOptions, diagnostics)
|
|
83
64
|
: {};
|
|
84
65
|
// mix in default options
|
|
85
|
-
|
|
86
|
-
for (
|
|
66
|
+
const defaultOptions = typescript_1.default.getDefaultCompilerOptions();
|
|
67
|
+
for (const key in defaultOptions) {
|
|
87
68
|
if (Object.hasOwn(defaultOptions, key) && options[key] === undefined) {
|
|
88
69
|
options[key] = defaultOptions[key];
|
|
89
70
|
}
|
|
90
71
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
if (options.verbatimModuleSyntax && new Set(['isolatedModules']).has(option.name)) {
|
|
97
|
-
continue;
|
|
98
|
-
}
|
|
99
|
-
options[option.name] = option.transpileOptionValue;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
103
|
-
finally {
|
|
104
|
-
try {
|
|
105
|
-
if (_h && !_h.done && (_a = _g.return)) _a.call(_g);
|
|
72
|
+
// @ts-expect-error internal TypeScript API
|
|
73
|
+
for (const option of typescript_1.default.transpileOptionValueCompilerOptions) {
|
|
74
|
+
// Do not set redundant config options if `verbatimModuleSyntax` was supplied.
|
|
75
|
+
if (options.verbatimModuleSyntax && new Set(['isolatedModules']).has(option.name)) {
|
|
76
|
+
continue;
|
|
106
77
|
}
|
|
107
|
-
|
|
78
|
+
options[option.name] = option.transpileOptionValue;
|
|
108
79
|
}
|
|
109
80
|
// transpileModule does not write anything to disk so there is no need to verify that there are no conflicts between input and output paths.
|
|
110
81
|
options.suppressOutputPathCheck = true;
|
|
@@ -112,12 +83,12 @@ var transpileWorker = function (input, transpileOptions) {
|
|
|
112
83
|
options.allowNonTsExtensions = true;
|
|
113
84
|
options.declaration = false;
|
|
114
85
|
options.declarationMap = false;
|
|
115
|
-
|
|
86
|
+
const newLine = getNewLineCharacter(options);
|
|
116
87
|
// if jsx is specified then treat file as .tsx
|
|
117
|
-
|
|
88
|
+
const inputFileName = transpileOptions.fileName ?? (transpileOptions.compilerOptions?.jsx ? 'module.tsx' : 'module.ts');
|
|
118
89
|
// Create a compilerHost object to allow the compiler to read and write files
|
|
119
|
-
|
|
120
|
-
getSourceFile:
|
|
90
|
+
const compilerHost = {
|
|
91
|
+
getSourceFile: (fileName) => {
|
|
121
92
|
// @ts-expect-error internal TypeScript API
|
|
122
93
|
if (fileName === typescript_1.default.normalizePath(inputFileName)) {
|
|
123
94
|
return sourceFile;
|
|
@@ -125,7 +96,7 @@ var transpileWorker = function (input, transpileOptions) {
|
|
|
125
96
|
// @ts-expect-error internal TypeScript API
|
|
126
97
|
return fileName === typescript_1.default.normalizePath(barebonesLibName) ? barebonesLibSourceFile : undefined;
|
|
127
98
|
},
|
|
128
|
-
writeFile:
|
|
99
|
+
writeFile: (name, text) => {
|
|
129
100
|
if (node_path_1.default.extname(name) === '.map') {
|
|
130
101
|
sourceMapText = text;
|
|
131
102
|
}
|
|
@@ -133,33 +104,33 @@ var transpileWorker = function (input, transpileOptions) {
|
|
|
133
104
|
outputText = text;
|
|
134
105
|
}
|
|
135
106
|
},
|
|
136
|
-
getDefaultLibFileName:
|
|
137
|
-
useCaseSensitiveFileNames:
|
|
138
|
-
getCanonicalFileName:
|
|
139
|
-
getCurrentDirectory:
|
|
140
|
-
getNewLine:
|
|
141
|
-
fileExists:
|
|
107
|
+
getDefaultLibFileName: () => barebonesLibName,
|
|
108
|
+
useCaseSensitiveFileNames: () => false,
|
|
109
|
+
getCanonicalFileName: (fileName) => fileName,
|
|
110
|
+
getCurrentDirectory: () => '',
|
|
111
|
+
getNewLine: () => newLine,
|
|
112
|
+
fileExists: (fileName) => {
|
|
142
113
|
if (shouldCheckProjectPkgJsonContent(fileName, options.module)) {
|
|
143
114
|
return typescript_1.default.sys.fileExists(fileName);
|
|
144
115
|
}
|
|
145
116
|
return fileName === inputFileName;
|
|
146
117
|
},
|
|
147
|
-
readFile:
|
|
118
|
+
readFile: (fileName) => {
|
|
148
119
|
if (shouldCheckProjectPkgJsonContent(fileName, options.module)) {
|
|
149
120
|
return typescript_1.default.sys.readFile(fileName);
|
|
150
121
|
}
|
|
151
122
|
return '';
|
|
152
123
|
},
|
|
153
|
-
directoryExists:
|
|
154
|
-
getDirectories:
|
|
124
|
+
directoryExists: () => true,
|
|
125
|
+
getDirectories: () => [],
|
|
155
126
|
};
|
|
156
|
-
|
|
157
|
-
languageVersion:
|
|
127
|
+
const sourceFile = typescript_1.default.createSourceFile(inputFileName, input, {
|
|
128
|
+
languageVersion: options.target ?? typescript_1.default.ScriptTarget.ESNext,
|
|
158
129
|
impliedNodeFormat: typescript_1.default.getImpliedNodeFormatForFile(inputFileName,
|
|
159
130
|
/*packageJsonInfoCache*/ undefined, compilerHost, options),
|
|
160
131
|
// @ts-expect-error internal TypeScript API
|
|
161
132
|
setExternalModuleIndicator: typescript_1.default.getSetExternalModuleIndicator(options),
|
|
162
|
-
jsDocParsingMode:
|
|
133
|
+
jsDocParsingMode: transpileOptions.jsDocParsingMode ?? typescript_1.default.JSDocParsingMode.ParseAll,
|
|
163
134
|
});
|
|
164
135
|
if (transpileOptions.moduleName) {
|
|
165
136
|
sourceFile.moduleName = transpileOptions.moduleName;
|
|
@@ -169,21 +140,21 @@ var transpileWorker = function (input, transpileOptions) {
|
|
|
169
140
|
sourceFile.renamedDependencies = new Map(Object.entries(transpileOptions.renamedDependencies));
|
|
170
141
|
}
|
|
171
142
|
// Output
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
143
|
+
let outputText;
|
|
144
|
+
let sourceMapText;
|
|
145
|
+
const inputs = [inputFileName];
|
|
146
|
+
const program = typescript_1.default.createProgram(inputs, options, compilerHost);
|
|
176
147
|
if (transpileOptions.reportDiagnostics) {
|
|
177
|
-
diagnostics.push
|
|
148
|
+
diagnostics.push(...program.getSyntacticDiagnostics(sourceFile));
|
|
178
149
|
}
|
|
179
|
-
diagnostics.push
|
|
150
|
+
diagnostics.push(...program.getOptionsDiagnostics());
|
|
180
151
|
// Emit
|
|
181
|
-
|
|
152
|
+
const result = program.emit(
|
|
182
153
|
/*targetSourceFile*/ undefined,
|
|
183
154
|
/*writeFile*/ undefined,
|
|
184
155
|
/*cancellationToken*/ undefined,
|
|
185
|
-
/*emitOnlyDtsFiles*/ undefined,
|
|
186
|
-
diagnostics.push
|
|
156
|
+
/*emitOnlyDtsFiles*/ undefined, transpileOptions.transformers?.(program));
|
|
157
|
+
diagnostics.push(...result.diagnostics);
|
|
187
158
|
if (outputText === undefined) {
|
|
188
159
|
diagnostics.push({
|
|
189
160
|
category: typescript_1.default.DiagnosticCategory.Error,
|
|
@@ -194,6 +165,6 @@ var transpileWorker = function (input, transpileOptions) {
|
|
|
194
165
|
length: 0,
|
|
195
166
|
});
|
|
196
167
|
}
|
|
197
|
-
return { outputText: outputText
|
|
168
|
+
return { outputText: outputText ?? '', diagnostics, sourceMapText };
|
|
198
169
|
};
|
|
199
170
|
exports.tsTranspileModule = transpileWorker;
|
package/dist/types.js
CHANGED
package/dist/utils/backports.js
CHANGED
|
@@ -1,38 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
|
-
var _a;
|
|
14
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
3
|
exports.backportTsJestDebugEnvVar = exports.backportJestConfig = void 0;
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
4
|
+
const bs_logger_1 = require("bs-logger");
|
|
5
|
+
const messages_1 = require("./messages");
|
|
6
|
+
const context = { [bs_logger_1.LogContexts.namespace]: 'backports' };
|
|
19
7
|
/**
|
|
20
8
|
* @internal
|
|
21
9
|
*/
|
|
22
|
-
|
|
23
|
-
logger.debug(
|
|
10
|
+
const backportJestConfig = (logger, config) => {
|
|
11
|
+
logger.debug({ ...context, config }, 'backporting config');
|
|
24
12
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
25
|
-
|
|
26
|
-
|
|
13
|
+
const { globals = {} } = (config || {});
|
|
14
|
+
const { 'ts-jest': tsJest = {} } = globals;
|
|
27
15
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
16
|
+
const mergeTsJest = {};
|
|
17
|
+
let hadWarnings = false;
|
|
18
|
+
const warnConfig = (oldPath, newPath, note) => {
|
|
31
19
|
hadWarnings = true;
|
|
32
20
|
logger.warn(context, (0, messages_1.interpolate)(note ? "\"[jest-config].{{oldPath}}\" is deprecated, use \"[jest-config].{{newPath}}\" instead.\n \u21B3 {{note}}" /* Deprecations.ConfigOptionWithNote */ : "\"[jest-config].{{oldPath}}\" is deprecated, use \"[jest-config].{{newPath}}\" instead." /* Deprecations.ConfigOption */, {
|
|
33
|
-
oldPath
|
|
34
|
-
newPath
|
|
35
|
-
note
|
|
21
|
+
oldPath,
|
|
22
|
+
newPath,
|
|
23
|
+
note,
|
|
36
24
|
}));
|
|
37
25
|
};
|
|
38
26
|
if ('__TS_CONFIG__' in globals) {
|
|
@@ -98,15 +86,25 @@ var backportJestConfig = function (logger, config) {
|
|
|
98
86
|
if (hadWarnings) {
|
|
99
87
|
logger.warn(context, "Your Jest configuration is outdated. Use the CLI to help migrating it: ts-jest config:migrate <config-file>." /* Helps.MigrateConfigUsingCLI */);
|
|
100
88
|
}
|
|
101
|
-
return
|
|
89
|
+
return {
|
|
90
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
91
|
+
...config,
|
|
92
|
+
globals: {
|
|
93
|
+
...globals,
|
|
94
|
+
'ts-jest': {
|
|
95
|
+
...mergeTsJest,
|
|
96
|
+
...tsJest,
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
};
|
|
102
100
|
};
|
|
103
101
|
exports.backportJestConfig = backportJestConfig;
|
|
104
102
|
/**
|
|
105
103
|
* @internal
|
|
106
104
|
*/
|
|
107
|
-
|
|
105
|
+
const backportTsJestDebugEnvVar = (logger) => {
|
|
108
106
|
if ('TS_JEST_DEBUG' in process.env) {
|
|
109
|
-
|
|
107
|
+
const shouldLog = !/^\s*(?:0|f(?:alse)?|no?|disabled?|off|)\s*$/i.test(process.env.TS_JEST_DEBUG || '');
|
|
110
108
|
delete process.env.TS_JEST_DEBUG;
|
|
111
109
|
if (shouldLog) {
|
|
112
110
|
process.env.TS_JEST_LOG = 'ts-jest.log,stderr:warn';
|
|
@@ -6,9 +6,9 @@ exports.getPackageVersion = getPackageVersion;
|
|
|
6
6
|
*/
|
|
7
7
|
function getPackageVersion(moduleName) {
|
|
8
8
|
try {
|
|
9
|
-
return require(
|
|
9
|
+
return require(`${moduleName}/package.json`).version;
|
|
10
10
|
}
|
|
11
|
-
catch
|
|
11
|
+
catch {
|
|
12
12
|
return undefined;
|
|
13
13
|
}
|
|
14
14
|
}
|
package/dist/utils/importer.js
CHANGED
|
@@ -1,98 +1,52 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
2
|
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
14
3
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
15
4
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
16
5
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
17
6
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
18
7
|
};
|
|
19
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
20
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
21
|
-
if (!m) return o;
|
|
22
|
-
var i = m.call(o), r, ar = [], e;
|
|
23
|
-
try {
|
|
24
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
25
|
-
}
|
|
26
|
-
catch (error) { e = { error: error }; }
|
|
27
|
-
finally {
|
|
28
|
-
try {
|
|
29
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
30
|
-
}
|
|
31
|
-
finally { if (e) throw e.error; }
|
|
32
|
-
}
|
|
33
|
-
return ar;
|
|
34
|
-
};
|
|
35
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
36
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
37
|
-
if (ar || !(i in from)) {
|
|
38
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
39
|
-
ar[i] = from[i];
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
43
|
-
};
|
|
44
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
45
9
|
exports.importer = exports.Importer = void 0;
|
|
46
10
|
exports.__requireModule = __requireModule;
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
11
|
+
const logger_1 = require("./logger");
|
|
12
|
+
const memoize_1 = require("./memoize");
|
|
13
|
+
const messages_1 = require("./messages");
|
|
14
|
+
const logger = logger_1.rootLogger.child({ namespace: 'Importer' });
|
|
51
15
|
/**
|
|
52
16
|
* @internal
|
|
53
17
|
*/
|
|
54
|
-
|
|
55
|
-
|
|
18
|
+
class Importer {
|
|
19
|
+
static get instance() {
|
|
20
|
+
logger.debug('creating Importer singleton');
|
|
21
|
+
return new Importer();
|
|
56
22
|
}
|
|
57
|
-
|
|
58
|
-
get: function () {
|
|
59
|
-
logger.debug('creating Importer singleton');
|
|
60
|
-
return new Importer();
|
|
61
|
-
},
|
|
62
|
-
enumerable: false,
|
|
63
|
-
configurable: true
|
|
64
|
-
});
|
|
65
|
-
Importer.prototype.babelJest = function (why) {
|
|
23
|
+
babelJest(why) {
|
|
66
24
|
return this._import(why, 'babel-jest');
|
|
67
|
-
}
|
|
68
|
-
|
|
25
|
+
}
|
|
26
|
+
babelCore(why) {
|
|
69
27
|
return this._import(why, '@babel/core');
|
|
70
|
-
}
|
|
71
|
-
|
|
28
|
+
}
|
|
29
|
+
typescript(why, which) {
|
|
72
30
|
return this._import(why, which);
|
|
73
|
-
}
|
|
74
|
-
|
|
31
|
+
}
|
|
32
|
+
esBuild(why) {
|
|
75
33
|
return this._import(why, 'esbuild');
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
var name;
|
|
83
|
-
var loaded;
|
|
84
|
-
var tries = __spreadArray([moduleName], __read(fallbacks), false);
|
|
34
|
+
}
|
|
35
|
+
tryThese(moduleName, ...fallbacks) {
|
|
36
|
+
let name;
|
|
37
|
+
let loaded;
|
|
38
|
+
const tries = [moduleName, ...fallbacks];
|
|
85
39
|
while ((name = tries.shift()) !== undefined) {
|
|
86
|
-
|
|
40
|
+
const req = requireWrapper(name);
|
|
87
41
|
// remove exports from what we're going to log
|
|
88
42
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
89
|
-
|
|
43
|
+
const contextReq = { ...req };
|
|
90
44
|
delete contextReq.exports;
|
|
91
45
|
if (req.exists) {
|
|
92
46
|
// module exists
|
|
93
47
|
loaded = req;
|
|
94
48
|
if (req.error) {
|
|
95
|
-
logger.error({ requireResult: contextReq },
|
|
49
|
+
logger.error({ requireResult: contextReq }, `failed loading module '${name}'`, req.error.message);
|
|
96
50
|
}
|
|
97
51
|
else {
|
|
98
52
|
logger.debug({ requireResult: contextReq }, 'loaded module', name);
|
|
@@ -101,17 +55,16 @@ var Importer = /** @class */ (function () {
|
|
|
101
55
|
}
|
|
102
56
|
else {
|
|
103
57
|
// module does not exists in the path
|
|
104
|
-
logger.debug({ requireResult: contextReq },
|
|
58
|
+
logger.debug({ requireResult: contextReq }, `module '${name}' not found`);
|
|
105
59
|
}
|
|
106
60
|
}
|
|
107
61
|
// return the loaded one, could be one that has been loaded, or one which has failed during load
|
|
108
62
|
// but not one which does not exists
|
|
109
63
|
return loaded;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
var result = this.tryThese.apply(this, __spreadArray([], __read(args), false));
|
|
64
|
+
}
|
|
65
|
+
tryTheseOr(moduleNames, missingResult, allowLoadError = false) {
|
|
66
|
+
const args = Array.isArray(moduleNames) ? moduleNames : [moduleNames];
|
|
67
|
+
const result = this.tryThese(...args);
|
|
115
68
|
if (!result)
|
|
116
69
|
return missingResult;
|
|
117
70
|
if (!result.error)
|
|
@@ -119,67 +72,59 @@ var Importer = /** @class */ (function () {
|
|
|
119
72
|
if (allowLoadError)
|
|
120
73
|
return missingResult;
|
|
121
74
|
throw result.error;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
var _b = _a === void 0 ? {} : _a, _c = _b.alternatives, alternatives = _c === void 0 ? [] : _c, _d = _b.installTip, installTip = _d === void 0 ? moduleName : _d;
|
|
75
|
+
}
|
|
76
|
+
_import(why, moduleName, { alternatives = [], installTip = moduleName } = {}) {
|
|
125
77
|
// try to load any of the alternative after trying main one
|
|
126
|
-
|
|
78
|
+
const res = this.tryThese(moduleName, ...alternatives);
|
|
127
79
|
// if we could load one, return it
|
|
128
|
-
if (res
|
|
80
|
+
if (res?.exists) {
|
|
129
81
|
if (!res.error)
|
|
130
82
|
return res.exports;
|
|
131
83
|
// it could not load because of a failure while importing, but it exists
|
|
132
84
|
throw new Error((0, messages_1.interpolate)("Loading module {{module}} failed with error: {{error}}" /* Errors.LoadingModuleFailed */, { module: res.given, error: res.error.message }));
|
|
133
85
|
}
|
|
134
86
|
// if it couldn't load, build a nice error message so the user can fix it by himself
|
|
135
|
-
|
|
136
|
-
|
|
87
|
+
const msg = alternatives.length ? "Unable to load any of these modules: {{module}}. {{reason}}. To fix it:\n{{fix}}" /* Errors.UnableToLoadAnyModule */ : "Unable to load the module {{module}}. {{reason}} To fix it:\n{{fix}}" /* Errors.UnableToLoadOneModule */;
|
|
88
|
+
const loadModule = [moduleName, ...alternatives].map((m) => `"${m}"`).join(', ');
|
|
137
89
|
if (typeof installTip === 'string') {
|
|
138
|
-
installTip = [{ module: installTip, label:
|
|
90
|
+
installTip = [{ module: installTip, label: `install "${installTip}"` }];
|
|
139
91
|
}
|
|
140
|
-
|
|
141
|
-
.map(
|
|
92
|
+
const fix = installTip
|
|
93
|
+
.map((tip) => ` ${installTip.length === 1 ? '↳' : '•'} ${(0, messages_1.interpolate)("{{label}}: `npm i -D {{module}}` (or `yarn add --dev {{module}}`)" /* Helps.FixMissingModule */, tip)}`)
|
|
142
94
|
.join('\n');
|
|
143
95
|
throw new Error((0, messages_1.interpolate)(msg, {
|
|
144
96
|
module: loadModule,
|
|
145
97
|
reason: why,
|
|
146
|
-
fix
|
|
98
|
+
fix,
|
|
147
99
|
}));
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
(0, memoize_1.Memoize)(function () {
|
|
151
|
-
var args = [];
|
|
152
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
153
|
-
args[_i] = arguments[_i];
|
|
154
|
-
}
|
|
155
|
-
return args.join(':');
|
|
156
|
-
})
|
|
157
|
-
], Importer.prototype, "tryThese", null);
|
|
158
|
-
__decorate([
|
|
159
|
-
(0, memoize_1.Memoize)()
|
|
160
|
-
], Importer, "instance", null);
|
|
161
|
-
return Importer;
|
|
162
|
-
}());
|
|
100
|
+
}
|
|
101
|
+
}
|
|
163
102
|
exports.Importer = Importer;
|
|
103
|
+
__decorate([
|
|
104
|
+
(0, memoize_1.Memoize)((...args) => args.join(':'))
|
|
105
|
+
], Importer.prototype, "tryThese", null);
|
|
106
|
+
__decorate([
|
|
107
|
+
(0, memoize_1.Memoize)()
|
|
108
|
+
], Importer, "instance", null);
|
|
164
109
|
/**
|
|
165
110
|
* @internal
|
|
166
111
|
*/
|
|
167
112
|
exports.importer = Importer.instance;
|
|
168
113
|
function requireWrapper(moduleName) {
|
|
169
|
-
|
|
170
|
-
|
|
114
|
+
let path;
|
|
115
|
+
let exists = false;
|
|
171
116
|
try {
|
|
172
117
|
path = resolveModule(moduleName);
|
|
173
118
|
exists = true;
|
|
174
119
|
}
|
|
175
120
|
catch (error) {
|
|
176
|
-
return { error: error, exists
|
|
121
|
+
return { error: error, exists, given: moduleName };
|
|
177
122
|
}
|
|
178
|
-
|
|
123
|
+
const result = { exists, path, given: moduleName };
|
|
179
124
|
try {
|
|
180
125
|
result.exports = requireModule(path);
|
|
181
126
|
}
|
|
182
|
-
catch
|
|
127
|
+
catch {
|
|
183
128
|
try {
|
|
184
129
|
result.exports = requireModule(moduleName);
|
|
185
130
|
}
|
|
@@ -189,8 +134,8 @@ function requireWrapper(moduleName) {
|
|
|
189
134
|
}
|
|
190
135
|
return result;
|
|
191
136
|
}
|
|
192
|
-
|
|
193
|
-
|
|
137
|
+
let requireModule = (mod) => require(mod);
|
|
138
|
+
let resolveModule = (mod) => require.resolve(mod, { paths: [process.cwd(), __dirname] });
|
|
194
139
|
/**
|
|
195
140
|
* @internal
|
|
196
141
|
*/
|
package/dist/utils/json.js
CHANGED
|
@@ -5,8 +5,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.stringify = stringify;
|
|
7
7
|
exports.parse = parse;
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
const fast_json_stable_stringify_1 = __importDefault(require("fast-json-stable-stringify"));
|
|
9
|
+
const UNDEFINED = 'undefined';
|
|
10
10
|
function stringify(input) {
|
|
11
11
|
return input === undefined ? UNDEFINED : (0, fast_json_stable_stringify_1.default)(input);
|
|
12
12
|
}
|