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 | Object.defineProperty(exports, "__esModule", { value: true });
|
14 | exports.TsJestTransformer = void 0;
|
15 | var config_set_1 = require("./config/config-set");
|
16 | var constants_1 = require("./constants");
|
17 | var json_1 = require("./utils/json");
|
18 | var jsonable_value_1 = require("./utils/jsonable-value");
|
19 | var logger_1 = require("./utils/logger");
|
20 | var messages_1 = require("./utils/messages");
|
21 | var sha1_1 = require("./utils/sha1");
|
22 | var TsJestTransformer = (function () {
|
23 | function TsJestTransformer() {
|
24 | this.logger = logger_1.rootLogger.child({ namespace: 'ts-jest-transformer' });
|
25 | this.logger.debug('created new transformer');
|
26 | }
|
27 | TsJestTransformer.prototype.configsFor = function (jestConfig) {
|
28 | var ccs = TsJestTransformer._cachedConfigSets.find(function (cs) { return cs.jestConfig.value === jestConfig; });
|
29 | var configSet;
|
30 | if (ccs) {
|
31 | this._transformCfgStr = ccs.transformerCfgStr;
|
32 | configSet = ccs.configSet;
|
33 | }
|
34 | else {
|
35 | var serializedJestCfg_1 = json_1.stringify(jestConfig);
|
36 | var serializedCcs = TsJestTransformer._cachedConfigSets.find(function (cs) { return cs.jestConfig.serialized === serializedJestCfg_1; });
|
37 | if (serializedCcs) {
|
38 | serializedCcs.jestConfig.value = jestConfig;
|
39 | this._transformCfgStr = serializedCcs.transformerCfgStr;
|
40 | configSet = serializedCcs.configSet;
|
41 | }
|
42 | else {
|
43 | this.logger.info('no matching config-set found, creating a new one');
|
44 | configSet = new config_set_1.ConfigSet(jestConfig);
|
45 | var jest_1 = __assign({}, jestConfig);
|
46 | var globals = (jest_1.globals = __assign({}, jest_1.globals));
|
47 | jest_1.name = undefined;
|
48 | jest_1.cacheDirectory = undefined;
|
49 | delete globals['ts-jest'];
|
50 | this._transformCfgStr = new jsonable_value_1.JsonableValue(__assign(__assign({ digest: configSet.tsJestDigest, babel: configSet.babelConfig }, jest_1), { tsconfig: {
|
51 | options: configSet.parsedTsConfig.options,
|
52 | raw: configSet.parsedTsConfig.raw,
|
53 | } })).serialized;
|
54 | TsJestTransformer._cachedConfigSets.push({
|
55 | jestConfig: new jsonable_value_1.JsonableValue(jestConfig),
|
56 | configSet: configSet,
|
57 | transformerCfgStr: this._transformCfgStr,
|
58 | });
|
59 | }
|
60 | }
|
61 | return configSet;
|
62 | };
|
63 | TsJestTransformer.prototype.process = function (input, filePath, jestConfig, transformOptions) {
|
64 | this.logger.debug({ fileName: filePath, transformOptions: transformOptions }, 'processing', filePath);
|
65 | var result;
|
66 | var source = input;
|
67 | var configs = this.configsFor(jestConfig);
|
68 | var hooks = configs.hooks;
|
69 | var shouldStringifyContent = configs.shouldStringifyContent(filePath);
|
70 | var babelJest = shouldStringifyContent ? undefined : configs.babelJestTransformer;
|
71 | var isDefinitionFile = filePath.endsWith(constants_1.DECLARATION_TYPE_EXT);
|
72 | var isJsFile = constants_1.JS_JSX_REGEX.test(filePath);
|
73 | var isTsFile = !isDefinitionFile && constants_1.TS_TSX_REGEX.test(filePath);
|
74 | if (shouldStringifyContent) {
|
75 | result = "module.exports=" + json_1.stringify(source);
|
76 | }
|
77 | else if (isDefinitionFile) {
|
78 | result = '';
|
79 | }
|
80 | else if (!configs.parsedTsConfig.options.allowJs && isJsFile) {
|
81 | this.logger.warn({ fileName: filePath }, messages_1.interpolate("Got a `.js` file to compile while `allowJs` option is not set to `true` (file: {{path}}). To fix this:\n - if you want TypeScript to process JS files, set `allowJs` to `true` in your TypeScript config (usually tsconfig.json)\n - if you do not want TypeScript to process your `.js` files, in your Jest config change the `transform` key which value is `ts-jest` so that it does not match `.js` files anymore", { path: filePath }));
|
82 | result = source;
|
83 | }
|
84 | else if (isJsFile || isTsFile) {
|
85 | result = configs.tsCompiler.compile(source, filePath);
|
86 | }
|
87 | else {
|
88 | var message = babelJest ? "Got a unknown file type to compile (file: {{path}}). To fix this, in your Jest config change the `transform` key which value is `ts-jest` so that it does not match this kind of files anymore. If you still want Babel to process it, add another entry to the `transform` option with value `babel-jest` which key matches this type of files." : "Got a unknown file type to compile (file: {{path}}). To fix this, in your Jest config change the `transform` key which value is `ts-jest` so that it does not match this kind of files anymore.";
|
89 | this.logger.warn({ fileName: filePath }, messages_1.interpolate(message, { path: filePath }));
|
90 | result = source;
|
91 | }
|
92 | if (babelJest) {
|
93 | this.logger.debug({ fileName: filePath }, 'calling babel-jest processor');
|
94 | result = babelJest.process(result, filePath, jestConfig, __assign(__assign({}, transformOptions), { instrument: false }));
|
95 | }
|
96 | if (hooks.afterProcess) {
|
97 | this.logger.debug({ fileName: filePath, hookName: 'afterProcess' }, 'calling afterProcess hook');
|
98 | var newResult = hooks.afterProcess([input, filePath, jestConfig, transformOptions], result);
|
99 | if (newResult !== undefined) {
|
100 | return newResult;
|
101 | }
|
102 | }
|
103 | return result;
|
104 | };
|
105 | TsJestTransformer.prototype.getCacheKey = function (fileContent, filePath, _jestConfigStr, transformOptions) {
|
106 | var configs = this.configsFor(transformOptions.config);
|
107 | this.logger.debug({ fileName: filePath, transformOptions: transformOptions }, 'computing cache key for', filePath);
|
108 | var _a = transformOptions.instrument, instrument = _a === void 0 ? false : _a, _b = transformOptions.rootDir, rootDir = _b === void 0 ? configs.rootDir : _b;
|
109 | return sha1_1.sha1(this._transformCfgStr, '\x00', rootDir, '\x00', "instrument:" + (instrument ? 'on' : 'off'), '\x00', fileContent, '\x00', filePath);
|
110 | };
|
111 | TsJestTransformer._cachedConfigSets = [];
|
112 | return TsJestTransformer;
|
113 | }());
|
114 | exports.TsJestTransformer = TsJestTransformer;
|