github-pages-deploy-action/node_modules/ts-jest/dist/compiler/instance.js

97 lines
4.1 KiB
JavaScript
Raw Normal View History

2020-03-31 20:42:07 +08:00
"use strict";
2020-05-15 05:33:08 +08:00
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
2020-03-31 20:42:07 +08:00
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
Object.defineProperty(exports, "__esModule", { value: true });
var fs_1 = require("fs");
var mkdirp = require("mkdirp");
var path_1 = require("path");
2020-04-30 20:40:07 +08:00
var compiler_utils_1 = require("./compiler-utils");
2020-03-31 20:42:07 +08:00
var language_service_1 = require("./language-service");
2020-05-15 05:33:08 +08:00
var transpiler_1 = require("./transpiler");
2020-03-31 20:42:07 +08:00
function updateOutput(outputText, normalizedFileName, sourceMap, getExtension) {
var base = path_1.basename(normalizedFileName);
var base64Map = Buffer.from(updateSourceMap(sourceMap, normalizedFileName), 'utf8').toString('base64');
var sourceMapContent = "data:application/json;charset=utf-8;base64," + base64Map;
var sourceMapLength = (base + ".map").length + (getExtension(normalizedFileName).length - path_1.extname(normalizedFileName).length);
return outputText.slice(0, -sourceMapLength) + sourceMapContent;
}
var updateSourceMap = function (sourceMapText, normalizedFileName) {
var sourceMap = JSON.parse(sourceMapText);
sourceMap.file = normalizedFileName;
sourceMap.sources = [normalizedFileName];
delete sourceMap.sourceRoot;
return JSON.stringify(sourceMap);
};
2020-05-15 05:33:08 +08:00
var compileAndCacheResult = function (memoryCache, compileFn, getExtension, logger) { return function (code, fileName, lineOffset) {
logger.debug({ fileName: fileName }, 'compileAndCacheResult(): get compile output');
var _a = __read(compileFn(code, fileName, lineOffset), 2), value = _a[0], sourceMap = _a[1];
var output = updateOutput(value, fileName, sourceMap, getExtension);
memoryCache.files.set(fileName, __assign(__assign({}, memoryCache.files.get(fileName)), { output: output }));
return output;
}; };
exports.createCompilerInstance = function (configs) {
2020-03-31 20:42:07 +08:00
var logger = configs.logger.child({ namespace: 'ts-compiler' });
2020-05-15 05:33:08 +08:00
var compilerOptions = configs.parsedTsConfig.options, tsJest = configs.tsJest;
var cacheDir = configs.tsCacheDir;
2020-04-30 20:40:07 +08:00
var ts = configs.compilerModule;
var extensions = ['.ts', '.tsx'];
var memoryCache = {
2020-05-15 05:33:08 +08:00
files: new Map(),
2020-04-30 20:40:07 +08:00
resolvedModules: Object.create(null),
2020-03-31 20:42:07 +08:00
};
if (compilerOptions.allowJs) {
extensions.push('.js');
extensions.push('.jsx');
}
2020-05-15 05:33:08 +08:00
if (cacheDir) {
mkdirp.sync(cacheDir);
2020-03-31 20:42:07 +08:00
try {
2020-05-15 05:33:08 +08:00
var fsMemoryCache = fs_1.readFileSync(compiler_utils_1.getResolvedModulesCache(cacheDir), 'utf-8');
memoryCache.resolvedModules = JSON.parse(fsMemoryCache);
2020-03-31 20:42:07 +08:00
}
2020-05-15 05:33:08 +08:00
catch (e) { }
2020-03-31 20:42:07 +08:00
}
2020-05-15 05:33:08 +08:00
configs.parsedTsConfig.fileNames.forEach(function (fileName) {
memoryCache.files.set(fileName, {
version: 0,
});
});
2020-03-31 20:42:07 +08:00
var getExtension = compilerOptions.jsx === ts.JsxEmit.Preserve
? function (path) { return (/\.[tj]sx$/.test(path) ? '.jsx' : '.js'); }
: function (_) { return '.js'; };
var compilerInstance;
if (!tsJest.isolatedModules) {
2020-05-15 05:33:08 +08:00
compilerInstance = language_service_1.initializeLanguageServiceInstance(configs, memoryCache, logger);
2020-03-31 20:42:07 +08:00
}
else {
2020-05-15 05:33:08 +08:00
compilerInstance = transpiler_1.initializeTranspilerInstance(configs, memoryCache, logger);
2020-03-31 20:42:07 +08:00
}
2020-05-15 05:33:08 +08:00
var compile = compileAndCacheResult(memoryCache, compilerInstance.compileFn, getExtension, logger);
2020-04-30 20:40:07 +08:00
return { cwd: configs.cwd, compile: compile, program: compilerInstance.program };
2020-03-31 20:42:07 +08:00
};