|
| 1 | +import type typescript from "typescript" |
| 2 | +import type tsvfs from "@typescript/vfs" |
| 3 | +type TS = typeof typescript |
| 4 | +type TSVFS = typeof tsvfs |
| 5 | + |
| 6 | +/** Create Program */ |
| 7 | +export function createProgram( |
| 8 | + { |
| 9 | + ts, |
| 10 | + compilerOptions, |
| 11 | + compilerHost, |
| 12 | + }: { |
| 13 | + ts: TS |
| 14 | + compilerOptions: typescript.CompilerOptions |
| 15 | + compilerHost: typescript.CompilerHost |
| 16 | + }, |
| 17 | + options: { filePath: string }, |
| 18 | +): typescript.Program { |
| 19 | + try { |
| 20 | + const program = ts.createProgram({ |
| 21 | + rootNames: [options.filePath], |
| 22 | + options: compilerOptions, |
| 23 | + host: compilerHost, |
| 24 | + }) |
| 25 | + return program |
| 26 | + } catch (e) { |
| 27 | + // eslint-disable-next-line no-console -- Demo debug |
| 28 | + console.error(e) |
| 29 | + throw e |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +export function createCompilerOptions(ts: TS): typescript.CompilerOptions { |
| 34 | + const compilerOptions: typescript.CompilerOptions = { |
| 35 | + target: ts.ScriptTarget.ESNext, |
| 36 | + module: ts.ModuleKind.ESNext, |
| 37 | + jsx: ts.JsxEmit.Preserve, |
| 38 | + strict: true, |
| 39 | + } |
| 40 | + compilerOptions.lib = [ts.getDefaultLibFileName(compilerOptions)] |
| 41 | + return compilerOptions |
| 42 | +} |
| 43 | + |
| 44 | +export async function createVirtualCompilerHost( |
| 45 | + { |
| 46 | + ts, |
| 47 | + tsvfs, |
| 48 | + compilerOptions, |
| 49 | + }: { |
| 50 | + ts: TS |
| 51 | + tsvfs: TSVFS |
| 52 | + compilerOptions: typescript.CompilerOptions |
| 53 | + }, |
| 54 | + { filePath: targetFilePath }: { filePath: string }, |
| 55 | +): Promise<{ |
| 56 | + compilerHost: typescript.CompilerHost |
| 57 | + updateFile: (sourceFile: typescript.SourceFile) => boolean |
| 58 | + fsMap: Map<string, string> |
| 59 | +}> { |
| 60 | + const fsMap = await tsvfs.createDefaultMapFromCDN( |
| 61 | + { |
| 62 | + lib: Array.from( |
| 63 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any -- use internal |
| 64 | + (ts as any).libMap.keys(), |
| 65 | + ), |
| 66 | + }, |
| 67 | + ts.version, |
| 68 | + true, |
| 69 | + ts, |
| 70 | + ) |
| 71 | + const system = tsvfs.createSystem(fsMap) |
| 72 | + const host = tsvfs.createVirtualCompilerHost(system, compilerOptions, ts) |
| 73 | + // eslint-disable-next-line @typescript-eslint/unbound-method -- backup original |
| 74 | + const original = { getSourceFile: host.compilerHost.getSourceFile } |
| 75 | + host.compilerHost.getSourceFile = function ( |
| 76 | + fileName, |
| 77 | + languageVersionOrOptions, |
| 78 | + ...args |
| 79 | + ) { |
| 80 | + if (targetFilePath === fileName) { |
| 81 | + // Exclude the target file from caching as it will be modified. |
| 82 | + const file = this.readFile(fileName) ?? "" |
| 83 | + return ts.createSourceFile(fileName, file, languageVersionOrOptions, true) |
| 84 | + } |
| 85 | + if (this.fileExists(fileName)) { |
| 86 | + return original.getSourceFile.apply(this, [ |
| 87 | + fileName, |
| 88 | + languageVersionOrOptions, |
| 89 | + ...args, |
| 90 | + ]) |
| 91 | + } |
| 92 | + // Avoid error |
| 93 | + // eslint-disable-next-line no-console -- Demo debug |
| 94 | + console.log(`Not exists: ${fileName}`) |
| 95 | + return undefined |
| 96 | + } |
| 97 | + return { |
| 98 | + ...host, |
| 99 | + fsMap, |
| 100 | + } |
| 101 | +} |
0 commit comments