@@ -16,14 +16,17 @@ export class TranspileError extends Error {
1616export class LuaTranspiler {
1717 // Transpile a source file
1818 static transpileSourceFile ( node : ts . SourceFile , checker : ts . TypeChecker , options : ts . CompilerOptions ) : string {
19- let transpiler = new LuaTranspiler ( checker , options ) ;
19+ let transpiler = new LuaTranspiler ( checker , options , node ) ;
2020 let header = options . addHeader ? "--=======================================================================================\n"
2121 + "-- Generated by TypescriptToLua transpiler https://github.com/Perryvw/TypescriptToLua \n"
2222 + "-- Date: " + new Date ( ) . toDateString ( ) + "\n"
2323 + "--=======================================================================================\n"
2424 : "" ;
25- let result = header
26- transpiler . isModule = tsEx . isFileModule ( node ) ;
25+ let result = header ;
26+ if ( ! options . dontRequireLualib ) {
27+ // require helper functions
28+ result += `require("typescript_lualib")\n` ;
29+ }
2730 if ( transpiler . isModule ) {
2831 // Shadow exports if it already exists
2932 result += "local exports = exports or {}\n" ;
@@ -43,16 +46,18 @@ export class LuaTranspiler {
4346 namespace : string [ ] ;
4447 importCount : number ;
4548 isModule : boolean ;
49+ sourceFile : ts . SourceFile ;
4650
47- constructor ( checker : ts . TypeChecker , options : ts . CompilerOptions ) {
51+ constructor ( checker : ts . TypeChecker , options : ts . CompilerOptions , sourceFile : ts . SourceFile ) {
4852 this . indent = "" ;
4953 this . checker = checker ;
5054 this . options = options ;
5155 this . genVarCounter = 0 ;
5256 this . transpilingSwitch = false ;
5357 this . namespace = [ ] ;
5458 this . importCount = 0 ;
55- this . isModule = false ;
59+ this . sourceFile = sourceFile ;
60+ this . isModule = tsEx . isFileModule ( sourceFile ) ;
5661 }
5762
5863 pushIndent ( ) : void {
@@ -83,6 +88,16 @@ export class LuaTranspiler {
8388 return result ;
8489 }
8590
91+ getImportPath ( relativePath : string ) {
92+ // Calculate absolute path to import
93+ let absolutePathToImport = path . resolve ( path . dirname ( this . sourceFile . fileName ) , relativePath ) ;
94+ if ( this . options . rootDir ) {
95+ // Calculate path realtive to project root and replace path.sep with dots (lua doesn't know paths)
96+ return `"${ absolutePathToImport . replace ( this . options . rootDir , "" ) . replace ( new RegExp ( path . sep , "g" ) , "." ) . slice ( 1 ) } "` ;
97+ }
98+ return `"${ relativePath . replace ( new RegExp ( path . sep , "g" ) , "." ) } "` ;
99+ }
100+
86101 // Transpile a block
87102 transpileBlock ( node : ts . Node ) : string {
88103 let result = "" ;
@@ -152,16 +167,17 @@ export class LuaTranspiler {
152167 }
153168
154169 transpileImport ( node : ts . ImportDeclaration ) : string {
155- const importFile = this . transpileExpression ( node . moduleSpecifier ) ;
170+ const importPath = this . transpileExpression ( node . moduleSpecifier ) ;
156171 if ( ! node . importClause || ! node . importClause . namedBindings ) {
157172 throw new TranspileError ( "Default Imports are not supported, please use named imports instead!" , node ) ;
158173 }
159174
160175 const imports = node . importClause . namedBindings ;
161176
162177 if ( ts . isNamedImports ( imports ) ) {
163- let fileImportTable = path . basename ( importFile . replace ( new RegExp ( "\"" , "g" ) , "" ) ) + this . importCount
164- let result = `local ${ fileImportTable } = require(${ importFile } )\n`
178+ let importPathWithoutQuotes = importPath . replace ( new RegExp ( "\"" , "g" ) , "" ) ;
179+ let fileImportTable = path . basename ( importPathWithoutQuotes ) + this . importCount
180+ let result = `local ${ fileImportTable } = require(${ this . getImportPath ( importPathWithoutQuotes ) } )\n`
165181 this . importCount ++ ;
166182 imports . elements . forEach ( element => {
167183 if ( element . propertyName ) {
@@ -375,9 +391,9 @@ export class LuaTranspiler {
375391 this . transpilingSwitch = false ;
376392
377393 let i = index + 1 ;
378- if ( i < clauses . length && ! tsEx . containsStatement ( clause . statements , ts . SyntaxKind . BreakStatement ) ) {
394+ if ( i < clauses . length && ! tsEx . containsStatement ( clause . statements , ts . SyntaxKind . BreakStatement ) ) {
379395 let nextClause = clauses [ i ] ;
380- while ( i < clauses . length
396+ while ( i < clauses . length
381397 && ts . isCaseClause ( nextClause )
382398 && nextClause . statements . length === 0
383399 ) {
0 commit comments