|
1 | | -import * as fs from "fs"; |
2 | | -import * as path from "path"; |
3 | | -import * as ts from "typescript"; |
4 | | -import { parseConfigFileWithSystem } from "../cli/tsconfig"; |
5 | | -import { CompilerOptions } from "../CompilerOptions"; |
6 | | -import { createEmitOutputCollector, TranspiledFile } from "./output-collector"; |
7 | | -import { EmitResult, Transpiler } from "./transpiler"; |
8 | | - |
| 1 | +export * from "./managed"; |
9 | 2 | export * from "./transpile"; |
10 | 3 | export * from "./transpiler"; |
11 | 4 | export { EmitHost } from "./utils"; |
12 | | -export { TranspiledFile }; |
13 | | - |
14 | | -export function transpileFiles( |
15 | | - rootNames: string[], |
16 | | - options: CompilerOptions = {}, |
17 | | - writeFile?: ts.WriteFileCallback |
18 | | -): EmitResult { |
19 | | - const program = ts.createProgram(rootNames, options); |
20 | | - const { diagnostics: transpileDiagnostics, emitSkipped } = new Transpiler().emit({ program, writeFile }); |
21 | | - const diagnostics = ts.sortAndDeduplicateDiagnostics([ |
22 | | - ...ts.getPreEmitDiagnostics(program), |
23 | | - ...transpileDiagnostics, |
24 | | - ]); |
25 | | - |
26 | | - return { diagnostics: [...diagnostics], emitSkipped }; |
27 | | -} |
28 | | - |
29 | | -export function transpileProject( |
30 | | - configFileName: string, |
31 | | - optionsToExtend?: CompilerOptions, |
32 | | - writeFile?: ts.WriteFileCallback |
33 | | -): EmitResult { |
34 | | - const parseResult = parseConfigFileWithSystem(configFileName, optionsToExtend); |
35 | | - if (parseResult.errors.length > 0) { |
36 | | - return { diagnostics: parseResult.errors, emitSkipped: true }; |
37 | | - } |
38 | | - |
39 | | - return transpileFiles(parseResult.fileNames, parseResult.options, writeFile); |
40 | | -} |
41 | | - |
42 | | -const libCache: { [key: string]: ts.SourceFile } = {}; |
43 | | - |
44 | | -/** @internal */ |
45 | | -export function createVirtualProgram(input: Record<string, string>, options: CompilerOptions = {}): ts.Program { |
46 | | - function notImplemented(): never { |
47 | | - throw new Error("Not implemented"); |
48 | | - } |
49 | | - |
50 | | - const getFileFromInput = (fileName: string) => |
51 | | - input[fileName] ?? (fileName.startsWith("/") ? input[fileName.slice(1)] : undefined); |
52 | | - |
53 | | - const compilerHost: ts.CompilerHost = { |
54 | | - useCaseSensitiveFileNames: () => false, |
55 | | - getCanonicalFileName: fileName => fileName, |
56 | | - getCurrentDirectory: () => "/", |
57 | | - fileExists: fileName => fileName.startsWith("lib.") || getFileFromInput(fileName) !== undefined, |
58 | | - readFile: notImplemented, |
59 | | - writeFile: notImplemented, |
60 | | - getDefaultLibFileName: ts.getDefaultLibFileName, |
61 | | - getNewLine: () => "\n", |
62 | | - |
63 | | - getSourceFile(fileName) { |
64 | | - const fileFromInput = getFileFromInput(fileName); |
65 | | - if (fileFromInput !== undefined) { |
66 | | - return ts.createSourceFile(fileName, fileFromInput, ts.ScriptTarget.Latest, false); |
67 | | - } |
68 | | - |
69 | | - if (fileName.startsWith("lib.")) { |
70 | | - if (libCache[fileName]) return libCache[fileName]; |
71 | | - const typeScriptDir = path.dirname(require.resolve("typescript")); |
72 | | - const filePath = path.join(typeScriptDir, fileName); |
73 | | - const content = fs.readFileSync(filePath, "utf8"); |
74 | | - |
75 | | - libCache[fileName] = ts.createSourceFile(fileName, content, ts.ScriptTarget.Latest, false); |
76 | | - |
77 | | - return libCache[fileName]; |
78 | | - } |
79 | | - }, |
80 | | - }; |
81 | | - |
82 | | - return ts.createProgram(Object.keys(input), options, compilerHost); |
83 | | -} |
84 | | - |
85 | | -export interface TranspileVirtualProjectResult { |
86 | | - diagnostics: ts.Diagnostic[]; |
87 | | - transpiledFiles: TranspiledFile[]; |
88 | | -} |
89 | | - |
90 | | -export function transpileVirtualProject( |
91 | | - files: Record<string, string>, |
92 | | - options: CompilerOptions = {} |
93 | | -): TranspileVirtualProjectResult { |
94 | | - const program = createVirtualProgram(files, options); |
95 | | - const collector = createEmitOutputCollector(); |
96 | | - const { diagnostics: transpileDiagnostics } = new Transpiler().emit({ program, writeFile: collector.writeFile }); |
97 | | - const diagnostics = ts.sortAndDeduplicateDiagnostics([ |
98 | | - ...ts.getPreEmitDiagnostics(program), |
99 | | - ...transpileDiagnostics, |
100 | | - ]); |
101 | | - |
102 | | - return { diagnostics: [...diagnostics], transpiledFiles: collector.files }; |
103 | | -} |
104 | | - |
105 | | -export interface TranspileStringResult { |
106 | | - diagnostics: ts.Diagnostic[]; |
107 | | - file?: TranspiledFile; |
108 | | -} |
109 | | - |
110 | | -export function transpileString(main: string, options: CompilerOptions = {}): TranspileStringResult { |
111 | | - const { diagnostics, transpiledFiles } = transpileVirtualProject({ "main.ts": main }, options); |
112 | | - return { |
113 | | - diagnostics, |
114 | | - file: transpiledFiles.find(({ sourceFiles }) => sourceFiles.some(f => f.fileName === "main.ts")), |
115 | | - }; |
116 | | -} |
0 commit comments