tsc-to
Version:
TypeScript's command-line compiler `tsc` without type-checking. Benefits:
170 lines • 7.93 kB
JavaScript
;
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);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var typescript_1 = __importDefault(require("typescript"));
var ts = typescript_1.default;
var service_1 = require("@ts-tools/service");
var fs_1 = __importDefault(require("fs"));
var path_1 = __importDefault(require("path"));
function main() {
var service = new service_1.TypeScriptService();
// HACK: trigger tsconfig discovery and parsing
var result = service.transpileFile("DOES NOT EXIST" + Math.random() + ".ts", {
getCompilerOptions: function (baseHost, tsconfigOptions) {
// Hacky type assertions because byots compilerOptions and normal compilerOptions are incompatible.
return neuterCompilerOptions(__assign({}, tsconfigOptions));
}
});
var runningService = service.runningServices.get('tsconfig.json');
var baseHost = runningService.baseHost;
var languageService = runningService.languageService;
var program = languageService.getProgram();
var compilerOptions = program.getCompilerOptions();
// Simple memoized mechanism to create directories that don't exist.
var createdDirectories = new Set();
function ensureDirectoryExists(path) {
if (createdDirectories.has(path))
return true;
var parent = path_1.default.dirname(path);
if (parent !== path)
ensureDirectoryExists(parent);
try {
fs_1.default.mkdirSync(path);
}
catch (e) {
if (!(e.code === 'EEXIST') || !fs_1.default.statSync(path).isDirectory()) {
throw e;
}
}
createdDirectories.add(path);
}
var diagnostics = program.getSyntacticDiagnostics().concat(program.getOptionsDiagnostics());
console.log(ts.formatDiagnosticsWithColorAndContext(diagnostics, baseHost));
program.emit(undefined, function (path, content) {
console.log('Emitting', path);
ensureDirectoryExists(path_1.default.dirname(path));
fs_1.default.writeFileSync(path, content);
});
return diagnostics.length ? 1 : 0;
}
exports.main = main;
/**
* Copied from the source of ts.transpileModule
* https://github.com/Microsoft/TypeScript/blob/master/src/services/transpile.ts#L31-L58
*
* These modifications to compiler options preserve transpilation behavior but
* prevent wasted time on typechecking and loading other source files.
*/
function neuterCompilerOptions(options) {
options.isolatedModules = true;
// transpileModule does not write anything to disk so there is no need to verify that there are no conflicts between input and output paths.
options.suppressOutputPathCheck = true;
// Filename can be non-ts file.
options.allowNonTsExtensions = true;
// We are not returning a sourceFile for lib file when asked by the program,
// so pass --noLib to avoid reporting a file not found error.
options.noLib = true;
// Clear out other settings that would not be used in transpiling this module
options.lib = undefined;
options.types = undefined;
options.noEmit = undefined;
options.noEmitOnError = undefined;
options.paths = undefined;
options.rootDirs = undefined;
options.declaration = undefined;
options.composite = undefined;
options.declarationDir = undefined;
options.out = undefined;
options.outFile = undefined;
// We are not doing a full typecheck, we are not resolving the whole context,
// so pass --noResolve to avoid reporting missing file errors.
options.noResolve = true;
// I added these 2 to prevent the language service from loading any @types:
options.typeRoots = [];
options.types = [];
// Adding this because declaration is nulled above
options.declarationMap = undefined;
return options;
}
exports.neuterCompilerOptions = neuterCompilerOptions;
// copied from https://github.com/Microsoft/TypeScript/blob/865b3e786277233585e1586edba52bf837b61b71/src/services/transpile.ts
// function transpileModules(sourceFiles: ReadonlyArray<ts.SourceFile>, options: ts.CompilerOptions) {
// if(true) {
// options.isolatedModules = true;
// // transpileModule does not write anything to disk so there is no need to verify that there are no conflicts between input and output paths.
// options.suppressOutputPathCheck = true;
// // Filename can be non-ts file.
// options.allowNonTsExtensions = true;
// // We are not returning a sourceFile for lib file when asked by the program,
// // so pass --noLib to avoid reporting a file not found error.
// options.noLib = true;
// // Clear out other settings that would not be used in transpiling this module
// options.lib = undefined;
// options.types = undefined;
// options.noEmit = undefined;
// options.noEmitOnError = undefined;
// options.paths = undefined;
// options.rootDirs = undefined;
// options.declaration = undefined;
// options.composite = undefined;
// options.declarationDir = undefined;
// options.out = undefined;
// options.outFile = undefined;
// // We are not doing a full typecheck, we are not resolving the whole context,
// // so pass --noResolve to avoid reporting missing file errors.
// options.noResolve = true;
// }
// const inputFileNames: Array<string> = [];
// const inputs: Record<string, ts.SourceFile> = Object.create(null);
// for(const sourceFile of sourceFiles) {
// const normalized = ts.normalizePath(sourceFile.fileName);
// inputFileNames.push(normalized);
// inputs[normalized] = sourceFile;
// }
// const outputs: Record<string, string> = Object.create(null);
// const compilerHost: ts.CompilerHost = {
// getSourceFile: (fileName) => inputs[fileName],
// writeFile: (name, text) => {
// outputs[name] = text;
// // if (fileExtensionIs(name, ".map")) {
// // Debug.assertEqual(sourceMapText, undefined, "Unexpected multiple source map outputs, file:", name);
// // sourceMapText = text;
// // }
// // else {
// // Debug.assertEqual(outputText, undefined, "Unexpected multiple outputs, file:", name);
// // outputText = text;
// // }
// },
// getDefaultLibFileName: () => "lib.d.ts",
// useCaseSensitiveFileNames: () => false,
// getCanonicalFileName: fileName => fileName,
// getCurrentDirectory: () => process.cwd(),
// getNewLine: () => '\n',
// fileExists: (fileName): boolean => true,/*fileName === inputFileName,*/
// readFile: () => "",
// directoryExists: () => true,
// getDirectories: () => []
// };
// const program = ts.createProgram(inputFileNames, options, compilerHost);
// const diagnostics = program.getSyntacticDiagnostics().concat(program.getOptionsDiagnostics());
// program.emit(/*targetSourceFile*/ undefined, /*writeFile*/ undefined, /*cancellationToken*/ undefined, /*emitOnlyDtsFiles*/ undefined, undefined/*transpileOptions.transformers*/);
// return { outputs, program, diagnostics };
// }
if (require.main === module) {
process.exit(main());
}
//# sourceMappingURL=tsc-to.js.map