@@ -141,11 +141,40 @@ export class LuaTransformer {
141141 )
142142 ) ;
143143
144- // return exports
145- statements . push ( tstl . createReturnStatement ( [ this . createExportsIdentifier ( ) ] ) ) ;
144+ if ( this . options . outFile ) {
145+ // module = module or {}
146+ statements . unshift (
147+ tstl . createAssignmentStatement (
148+ this . createModulesIdentifier ( ) ,
149+ tstl . createBinaryExpression (
150+ this . createModulesIdentifier ( ) ,
151+ tstl . createTableExpression ( ) ,
152+ tstl . SyntaxKind . OrOperator
153+ )
154+ )
155+ ) ;
156+
157+ // module["sourceFile"] = exports
158+ statements . push (
159+ tstl . createAssignmentStatement (
160+ tstl . createTableIndexExpression (
161+ this . createModulesIdentifier ( ) ,
162+ tstl . createStringLiteral ( this . getExportPath ( this . currentSourceFile ) )
163+ ) ,
164+ this . createExportsIdentifier ( )
165+ )
166+ ) ;
167+ } else {
168+ // return exports
169+ statements . push ( tstl . createReturnStatement ( [ this . createExportsIdentifier ( ) ] ) ) ;
170+ }
146171 }
147172 }
148173
174+ if ( this . options . outFile ) {
175+ return [ tstl . createBlock ( [ tstl . createDoStatement ( statements ) ] , node ) , this . luaLibFeatureSet ] ;
176+ }
177+
149178 return [ tstl . createBlock ( statements , node ) , this . luaLibFeatureSet ] ;
150179 }
151180
@@ -455,12 +484,20 @@ export class LuaTransformer {
455484 }
456485 }
457486
458- protected createModuleRequire ( moduleSpecifier : ts . StringLiteral , resolveModule = true ) : tstl . CallExpression {
487+ protected createModuleRequire (
488+ moduleSpecifier : ts . StringLiteral ,
489+ resolveModule = true
490+ ) : tstl . CallExpression | tstl . TableIndexExpression {
459491 const modulePathString = resolveModule
460492 ? this . getImportPath ( moduleSpecifier . text . replace ( new RegExp ( '"' , "g" ) , "" ) , moduleSpecifier )
461493 : moduleSpecifier . text ;
462494 const modulePath = tstl . createStringLiteral ( modulePathString ) ;
463- return tstl . createCallExpression ( tstl . createIdentifier ( "require" ) , [ modulePath ] , moduleSpecifier ) ;
495+
496+ if ( this . options . outFile ) {
497+ return tstl . createTableIndexExpression ( this . createModulesIdentifier ( ) , modulePath ) ;
498+ } else {
499+ return tstl . createCallExpression ( tstl . createIdentifier ( "require" ) , [ modulePath ] , moduleSpecifier ) ;
500+ }
464501 }
465502
466503 protected validateClassElement ( element : ts . ClassElement ) : void {
@@ -4902,21 +4939,24 @@ export class LuaTransformer {
49024939 }
49034940 }
49044941
4905- protected getAbsoluteImportPath ( relativePath : string ) : string {
4942+ protected getAbsoluteImportPath ( relativePath : string , directoryPath : string ) : string {
49064943 if ( relativePath . charAt ( 0 ) !== "." && this . options . baseUrl ) {
49074944 return path . resolve ( this . options . baseUrl , relativePath ) ;
49084945 }
49094946
4910- if ( this . currentSourceFile === undefined ) {
4911- throw TSTLErrors . MissingSourceFile ( ) ;
4912- }
4913-
4914- return path . resolve ( path . dirname ( this . currentSourceFile . fileName ) , relativePath ) ;
4947+ return path . resolve ( directoryPath , relativePath ) ;
49154948 }
49164949
49174950 protected getImportPath ( relativePath : string , node : ts . Node ) : string {
49184951 const rootDir = this . options . rootDir ? path . resolve ( this . options . rootDir ) : path . resolve ( "." ) ;
4919- const absoluteImportPath = path . format ( path . parse ( this . getAbsoluteImportPath ( relativePath ) ) ) ;
4952+
4953+ if ( this . currentSourceFile === undefined ) {
4954+ throw TSTLErrors . MissingSourceFile ( ) ;
4955+ }
4956+
4957+ const absoluteImportPath = path . format (
4958+ path . parse ( this . getAbsoluteImportPath ( relativePath , path . dirname ( this . currentSourceFile . fileName ) ) )
4959+ ) ;
49204960 const absoluteRootDirPath = path . format ( path . parse ( rootDir ) ) ;
49214961 if ( absoluteImportPath . includes ( absoluteRootDirPath ) ) {
49224962 return this . formatPathToLuaPath ( absoluteImportPath . replace ( absoluteRootDirPath , "" ) . slice ( 1 ) ) ;
@@ -4929,6 +4969,18 @@ export class LuaTransformer {
49294969 }
49304970 }
49314971
4972+ protected getExportPath ( sourceFile : ts . SourceFile ) : string {
4973+ const rootDir = this . options . rootDir ? path . resolve ( this . options . rootDir ) : path . resolve ( "." ) ;
4974+
4975+ if ( this . currentSourceFile === undefined ) {
4976+ throw TSTLErrors . MissingSourceFile ( ) ;
4977+ }
4978+
4979+ const absolutePath = path . resolve ( sourceFile . fileName . replace ( / .t s $ / , "" ) ) ;
4980+ const absoluteRootDirPath = path . format ( path . parse ( rootDir ) ) ;
4981+ return this . formatPathToLuaPath ( absolutePath . replace ( absoluteRootDirPath , "" ) . slice ( 1 ) ) ;
4982+ }
4983+
49324984 protected formatPathToLuaPath ( filePath : string ) : string {
49334985 filePath = filePath . replace ( / \. j s o n $ / , "" ) ;
49344986 if ( process . platform === "win32" ) {
@@ -4946,6 +4998,10 @@ export class LuaTransformer {
49464998 return tstl . createIdentifier ( "____exports" ) ;
49474999 }
49485000
5001+ protected createModulesIdentifier ( ) : tstl . Identifier {
5002+ return tstl . createIdentifier ( "____modules" ) ;
5003+ }
5004+
49495005 protected createLocalOrExportedOrGlobalDeclaration (
49505006 lhs : tstl . Identifier | tstl . Identifier [ ] ,
49515007 rhs ?: tstl . Expression | tstl . Expression [ ] ,
0 commit comments