@@ -4,40 +4,44 @@ import { TypeScriptWorker } from "monaco-editor/esm/vs/language/typescript/tsWor
44import * as ts from "typescript" ;
55import * as tstl from "typescript-to-lua" ;
66
7+ const libContext = require . context ( `raw-loader!typescript-to-lua/dist/lualib` , true , / ( .+ ) (?< ! l u a l i b _ b u n d l e ) \. l u a $ / ) ;
78const emitHost : tstl . EmitHost = {
8- getCurrentDirectory : ( ) => ". " ,
9+ getCurrentDirectory : ( ) => "" ,
910 readFile : ( fileName : string ) => {
10- const featureName = fileName . replace ( "/dist/lualib/" , "" ) . replace ( ".lua" , "" ) ;
11- return require ( `raw-loader!typescript-to-lua/dist/lualib/${ featureName } .lua` ) . default ;
11+ const [ , featureName ] = fileName . match ( / \/ d i s t \/ l u a l i b \/ ( .+ ) \. l u a $ / ) ?? [ ] ;
12+ if ( featureName === undefined ) {
13+ throw new Error ( `Unexpected file to read: ${ fileName } ` ) ;
14+ }
15+
16+ return libContext ( `./${ featureName } .lua` ) . default ;
1217 } ,
1318} ;
1419
1520// TODO: In latest monaco-typescript it returns `ts.Diagnostic[]`
1621const clearDiagnostics = ( TypeScriptWorker as any ) . clearFiles ;
1722
1823export class CustomTypeScriptWorker extends TypeScriptWorker {
19- public async getTranspileOutput ( ) {
20- const { transpiledFiles } = this . transpileLua ( ) ;
24+ public async getTranspileOutput ( fileName : string ) {
25+ const { transpiledFiles } = this . transpileLua ( fileName ) ;
2126 const [ transpiledFile ] = transpiledFiles ;
2227 return { code : transpiledFile . lua ! , ast : transpiledFile . luaAst ! } ;
2328 }
2429
2530 public async getSemanticDiagnostics ( fileName : string ) {
2631 const diagnostics = await super . getSemanticDiagnostics ( fileName ) ;
27- const { diagnostics : transpileDiagnostics } = this . transpileLua ( ) ;
32+ const { diagnostics : transpileDiagnostics } = this . transpileLua ( fileName ) ;
2833 clearDiagnostics ( transpileDiagnostics ) ;
2934 return [ ...diagnostics , ...transpileDiagnostics ] ;
3035 }
3136
32- private transpileLua ( ) {
37+ private transpileLua ( fileName : string ) {
3338 const program = ( ( this as any ) . _languageService as ts . LanguageService ) . getProgram ( ) ! ;
3439
3540 const compilerOptions = program . getCompilerOptions ( ) ;
3641 compilerOptions . luaLibImport = tstl . LuaLibImportKind . Inline ;
3742 compilerOptions . luaTarget = tstl . LuaTarget . Lua53 ;
3843
39- const sourceFiles = program . getRootFileNames ( ) . map ( n => program . getSourceFile ( n ) ! ) ;
40- return tstl . transpile ( { program, emitHost, sourceFiles } ) ;
44+ return tstl . transpile ( { program, emitHost, sourceFiles : [ program . getSourceFile ( fileName ) ! ] } ) ;
4145 }
4246}
4347
0 commit comments