@@ -79,7 +79,10 @@ class ResolutionContext {
7979 return ;
8080 }
8181
82- const dependencyPath = this . resolveDependencyPathsWithPlugins ( file , required . requirePath ) ?? this . resolveDependencyPath ( file , required . requirePath ) ;
82+ const dependencyPath =
83+ this . resolveDependencyPathsWithPlugins ( file , required . requirePath ) ??
84+ this . resolveDependencyPath ( file , required . requirePath ) ;
85+
8386 if ( ! dependencyPath ) return this . couldNotResolveImport ( required , file ) ;
8487
8588 if ( this . options . tstlVerbose ) {
@@ -99,28 +102,50 @@ class ResolutionContext {
99102 const requiredFromLuaFile = required . fileName . endsWith ( ".lua" ) ;
100103 const dependencyPath = requiredFromLuaFile ? luaRequireToPath ( dependency ) : dependency ;
101104
102- for ( const p of this . plugins ) {
103- if ( p . moduleResolution != null ) {
104- const pluginResolvedPath = p . moduleResolution ( dependency , dependencyPath , this . options , this . emitHost )
105+ for ( const plugin of this . plugins ) {
106+ if ( plugin . moduleResolution != null ) {
107+ const pluginResolvedPath = plugin . moduleResolution (
108+ dependency ,
109+ dependencyPath ,
110+ this . options ,
111+ this . emitHost
112+ ) ;
105113 if ( pluginResolvedPath !== undefined ) {
106-
107114 // If lua file is in node_module
108115 if ( requiredFromLuaFile && isNodeModulesFile ( required . fileName ) ) {
109116 // If requiring file is in lua module, try to resolve sibling in that file first
110- const resolvedNodeModulesFile = this . resolveLuaDependencyPathFromNodeModules ( required , pluginResolvedPath ) ;
117+ const resolvedNodeModulesFile = this . resolveLuaDependencyPathFromNodeModules (
118+ required ,
119+ pluginResolvedPath
120+ ) ;
111121 if ( resolvedNodeModulesFile ) {
112122 if ( this . options . tstlVerbose ) {
113- console . log ( `Resolved file path for module ${ dependency } to path ${ dependencyPath } using plugin.` )
123+ console . log (
124+ `Resolved file path for module ${ dependency } to path ${ dependencyPath } using plugin.`
125+ ) ;
114126 }
115- return resolvedNodeModulesFile
127+ return resolvedNodeModulesFile ;
116128 }
117129 }
118130
119- if ( this . getFileFromPath ( pluginResolvedPath ) ) {
131+ const isRelative = [ "/" , "./" , "../" ] . some ( p => pluginResolvedPath . startsWith ( p ) ) ;
132+
133+ // // If the import is relative, always resolve it relative to the requiring file
134+ // // If the import is not relative, resolve it relative to options.baseUrl if it is set
135+ const fileDirectory = path . dirname ( required . fileName ) ;
136+ const relativeTo = isRelative ? fileDirectory : this . options . baseUrl ?? fileDirectory ;
137+
138+ // // Check if file is a file in the project
139+ const resolvedPath = path . join ( relativeTo , pluginResolvedPath ) ;
140+ const fileFromPath = this . getFileFromPath ( resolvedPath ) ;
141+
142+ if ( fileFromPath ) {
120143 if ( this . options . tstlVerbose ) {
121- console . log ( `Resolved file path for module ${ dependency } to path ${ dependencyPath } using plugin.` )
144+ console . log (
145+ `Resolved file path for module ${ dependency } to path ${ resolvedPath } using plugin.`
146+ ) ;
122147 }
123- return pluginResolvedPath ;
148+ return fileFromPath ;
124149 }
125150 }
126151 }
@@ -312,7 +337,12 @@ class ResolutionContext {
312337 }
313338}
314339
315- export function resolveDependencies ( program : ts . Program , files : ProcessedFile [ ] , emitHost : EmitHost , plugins : Plugin [ ] ) : ResolutionResult {
340+ export function resolveDependencies (
341+ program : ts . Program ,
342+ files : ProcessedFile [ ] ,
343+ emitHost : EmitHost ,
344+ plugins : Plugin [ ]
345+ ) : ResolutionResult {
316346 const options = program . getCompilerOptions ( ) as CompilerOptions ;
317347
318348 const resolutionContext = new ResolutionContext ( program , options , emitHost , plugins ) ;
0 commit comments