|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import ts, { CompilerOptions } from "typescript"; |
| 4 | +import { |
| 5 | + createSystem, |
| 6 | + createVirtualTypeScriptEnvironment, |
| 7 | + knownLibFilesForCompilerOptions, |
| 8 | + VirtualTypeScriptEnvironment, |
| 9 | +} from "@typescript/vfs"; |
| 10 | +import { |
| 11 | + createContext, |
| 12 | + ReactNode, |
| 13 | + useCallback, |
| 14 | + useContext, |
| 15 | + useEffect, |
| 16 | + useRef, |
| 17 | + useState, |
| 18 | +} from "react"; |
| 19 | +import { useEmbedContext } from "../embedContext"; |
| 20 | +import { ReplOutput } from "../repl"; |
| 21 | +import { RuntimeContext } from "../runtime"; |
| 22 | + |
| 23 | +export const compilerOptions: CompilerOptions = {}; |
| 24 | + |
| 25 | +const TypeScriptContext = createContext<VirtualTypeScriptEnvironment | null>( |
| 26 | + null |
| 27 | +); |
| 28 | +export function TypeScriptProvider({ children }: { children: ReactNode }) { |
| 29 | + const [tsEnv, setTSEnv] = useState<VirtualTypeScriptEnvironment | null>(null); |
| 30 | + useEffect(() => { |
| 31 | + if (tsEnv === null) { |
| 32 | + const abortController = new AbortController(); |
| 33 | + (async () => { |
| 34 | + const system = createSystem(new Map()); |
| 35 | + const libFiles = knownLibFilesForCompilerOptions(compilerOptions, ts); |
| 36 | + const libFileContents = await Promise.all( |
| 37 | + libFiles.map(async (libFile) => { |
| 38 | + const response = await fetch( |
| 39 | + `/typescript/${ts.version}/${libFile}`, |
| 40 | + { signal: abortController.signal } |
| 41 | + ); |
| 42 | + if (response.ok) { |
| 43 | + return response.text(); |
| 44 | + } else { |
| 45 | + return undefined; |
| 46 | + } |
| 47 | + }) |
| 48 | + ); |
| 49 | + libFiles.forEach((libFile, index) => { |
| 50 | + const content = libFileContents[index]; |
| 51 | + if (content !== undefined) { |
| 52 | + system.writeFile(`/${libFile}`, content); |
| 53 | + } |
| 54 | + }); |
| 55 | + const env = createVirtualTypeScriptEnvironment( |
| 56 | + system, |
| 57 | + [], |
| 58 | + ts, |
| 59 | + compilerOptions |
| 60 | + ); |
| 61 | + setTSEnv(env); |
| 62 | + })(); |
| 63 | + return () => { |
| 64 | + abortController.abort(); |
| 65 | + }; |
| 66 | + } |
| 67 | + }, [tsEnv]); |
| 68 | + return ( |
| 69 | + <TypeScriptContext.Provider value={tsEnv}> |
| 70 | + {children} |
| 71 | + </TypeScriptContext.Provider> |
| 72 | + ); |
| 73 | +} |
| 74 | + |
| 75 | +export function useTypeScript(jsEval: RuntimeContext): RuntimeContext { |
| 76 | + const tsEnv = useContext(TypeScriptContext); |
| 77 | + |
| 78 | + const { writeFile } = useEmbedContext(); |
| 79 | + const runFiles = useCallback( |
| 80 | + async (filenames: string[], files: Record<string, string>) => { |
| 81 | + if (tsEnv === null) { |
| 82 | + return [ |
| 83 | + { type: "error" as const, message: "TypeScript is not ready yet." }, |
| 84 | + ]; |
| 85 | + } |
| 86 | + |
| 87 | + for (const [filename, content] of Object.entries(files)) { |
| 88 | + tsEnv.createFile(filename, content); |
| 89 | + } |
| 90 | + |
| 91 | + const outputs: ReplOutput[] = []; |
| 92 | + |
| 93 | + for (const diagnostic of tsEnv.languageService.getSyntacticDiagnostics( |
| 94 | + filenames[0] |
| 95 | + )) { |
| 96 | + outputs.push({ |
| 97 | + type: "error", |
| 98 | + message: ts.formatDiagnosticsWithColorAndContext([diagnostic], { |
| 99 | + getCurrentDirectory: () => "", |
| 100 | + getCanonicalFileName: (f) => f, |
| 101 | + getNewLine: () => "\n", |
| 102 | + }), |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + for (const diagnostic of tsEnv.languageService.getSemanticDiagnostics( |
| 107 | + filenames[0] |
| 108 | + )) { |
| 109 | + outputs.push({ |
| 110 | + type: "error", |
| 111 | + message: ts.formatDiagnosticsWithColorAndContext([diagnostic], { |
| 112 | + getCurrentDirectory: () => "", |
| 113 | + getCanonicalFileName: (f) => f, |
| 114 | + getNewLine: () => "\n", |
| 115 | + }), |
| 116 | + }); |
| 117 | + } |
| 118 | + |
| 119 | + const emitOutput = tsEnv.languageService.getEmitOutput(filenames[0]); |
| 120 | + files = await writeFile(Object.fromEntries(emitOutput.outputFiles.map((of) => [of.name, of.text]))); |
| 121 | + |
| 122 | + console.log(emitOutput) |
| 123 | + const jsOutputs = jsEval.runFiles([emitOutput.outputFiles[0].name], files); |
| 124 | + |
| 125 | + return outputs.concat(await jsOutputs); |
| 126 | + }, |
| 127 | + [tsEnv, writeFile, jsEval] |
| 128 | + ); |
| 129 | + return { |
| 130 | + ready: tsEnv !== null, |
| 131 | + runFiles, |
| 132 | + getCommandlineStr, |
| 133 | + }; |
| 134 | +} |
| 135 | + |
| 136 | +function getCommandlineStr(filenames: string[]) { |
| 137 | + return `tsc ${filenames.join(" ")}`; |
| 138 | +} |
0 commit comments