ts-jest-transformer.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. var util_1 = require("util");
  15. var config_set_1 = require("./config/config-set");
  16. var json_1 = require("./util/json");
  17. var jsonable_value_1 = require("./util/jsonable-value");
  18. var logger_1 = require("./util/logger");
  19. var messages_1 = require("./util/messages");
  20. var sha1_1 = require("./util/sha1");
  21. exports.INSPECT_CUSTOM = util_1.inspect.custom || 'inspect';
  22. var TsJestTransformer = (function () {
  23. function TsJestTransformer(baseOptions) {
  24. if (baseOptions === void 0) { baseOptions = {}; }
  25. this.options = __assign({}, baseOptions);
  26. this.id = TsJestTransformer._nextTransformerId;
  27. this.logger = logger_1.rootLogger.child({
  28. transformerId: this.id,
  29. namespace: 'jest-transformer',
  30. });
  31. this.logger.debug({ baseOptions: baseOptions }, 'created new transformer');
  32. }
  33. Object.defineProperty(TsJestTransformer, "lastTransformerId", {
  34. get: function () {
  35. return TsJestTransformer._lastTransformerId;
  36. },
  37. enumerable: true,
  38. configurable: true
  39. });
  40. Object.defineProperty(TsJestTransformer, "_nextTransformerId", {
  41. get: function () {
  42. return ++TsJestTransformer._lastTransformerId;
  43. },
  44. enumerable: true,
  45. configurable: true
  46. });
  47. TsJestTransformer.prototype[exports.INSPECT_CUSTOM] = function () {
  48. return "[object TsJestTransformer<#" + this.id + ">]";
  49. };
  50. TsJestTransformer.prototype.configsFor = function (jestConfig) {
  51. var csi;
  52. var jestConfigObj;
  53. if (typeof jestConfig === 'string') {
  54. csi = TsJestTransformer._configSetsIndex.find(function (cs) { return cs.jestConfig.serialized === jestConfig; });
  55. if (csi)
  56. return csi.configSet;
  57. jestConfigObj = json_1.parse(jestConfig);
  58. }
  59. else {
  60. csi = TsJestTransformer._configSetsIndex.find(function (cs) { return cs.jestConfig.value === jestConfig; });
  61. if (csi)
  62. return csi.configSet;
  63. var serialized_1 = json_1.stringify(jestConfig);
  64. csi = TsJestTransformer._configSetsIndex.find(function (cs) { return cs.jestConfig.serialized === serialized_1; });
  65. if (csi) {
  66. csi.jestConfig.value = jestConfig;
  67. return csi.configSet;
  68. }
  69. jestConfigObj = jestConfig;
  70. }
  71. this.logger.info("no matching config-set found, creating a new one");
  72. var configSet = new config_set_1.ConfigSet(jestConfigObj, this.options, this.logger);
  73. TsJestTransformer._configSetsIndex.push({
  74. jestConfig: new jsonable_value_1.JsonableValue(jestConfigObj),
  75. configSet: configSet,
  76. });
  77. return configSet;
  78. };
  79. TsJestTransformer.prototype.process = function (input, filePath, jestConfig, transformOptions) {
  80. this.logger.debug({ fileName: filePath, transformOptions: transformOptions }, 'processing', filePath);
  81. var result;
  82. var source = input;
  83. var configs = this.configsFor(jestConfig);
  84. var hooks = configs.hooks;
  85. var stringify = configs.shouldStringifyContent(filePath);
  86. var babelJest = stringify ? undefined : configs.babelJestTransformer;
  87. var isDefinitionFile = filePath.endsWith('.d.ts');
  88. var isJsFile = !isDefinitionFile && /\.jsx?$/.test(filePath);
  89. var isTsFile = isDefinitionFile || /\.tsx?$/.test(filePath);
  90. if (stringify) {
  91. result = "module.exports=" + JSON.stringify(source);
  92. }
  93. else if (isDefinitionFile) {
  94. result = '';
  95. }
  96. else if (!configs.typescript.options.allowJs && isJsFile) {
  97. this.logger.warn({ fileName: filePath }, messages_1.interpolate(messages_1.Errors.GotJsFileButAllowJsFalse, { path: filePath }));
  98. result = source;
  99. }
  100. else if (isJsFile || isTsFile) {
  101. result = configs.tsCompiler.compile(source, filePath);
  102. }
  103. else {
  104. var message = babelJest ? messages_1.Errors.GotUnknownFileTypeWithBabel : messages_1.Errors.GotUnknownFileTypeWithoutBabel;
  105. this.logger.warn({ fileName: filePath }, messages_1.interpolate(message, { path: filePath }));
  106. result = source;
  107. }
  108. if (babelJest) {
  109. this.logger.debug({ fileName: filePath }, 'calling babel-jest processor');
  110. result = babelJest.process(result, filePath, jestConfig, __assign(__assign({}, transformOptions), { instrument: false }));
  111. }
  112. if (hooks.afterProcess) {
  113. this.logger.debug({ fileName: filePath, hookName: 'afterProcess' }, 'calling afterProcess hook');
  114. var newResult = hooks.afterProcess([input, filePath, jestConfig, transformOptions], result);
  115. if (newResult !== undefined) {
  116. return newResult;
  117. }
  118. }
  119. return result;
  120. };
  121. TsJestTransformer.prototype.getCacheKey = function (fileContent, filePath, jestConfigStr, transformOptions) {
  122. if (transformOptions === void 0) { transformOptions = {}; }
  123. this.logger.debug({ fileName: filePath, transformOptions: transformOptions }, 'computing cache key for', filePath);
  124. var configs = this.configsFor(jestConfigStr);
  125. var _a = transformOptions.instrument, instrument = _a === void 0 ? false : _a, _b = transformOptions.rootDir, rootDir = _b === void 0 ? configs.rootDir : _b;
  126. return sha1_1.sha1(configs.cacheKey, '\x00', rootDir, '\x00', "instrument:" + (instrument ? 'on' : 'off'), '\x00', fileContent, '\x00', filePath);
  127. };
  128. TsJestTransformer._configSetsIndex = [];
  129. TsJestTransformer._lastTransformerId = 0;
  130. return TsJestTransformer;
  131. }());
  132. exports.TsJestTransformer = TsJestTransformer;