@@ -168,17 +168,6 @@ class ResolutionContext {
168168 const resolveResult = resolver . resolveSync ( { } , fileDirectory , dependencyPath ) ;
169169 if ( resolveResult ) return resolveResult ;
170170 } catch ( e : any ) {
171-
172- const plugins = getPlugins ( this . program ) . plugins ;
173- for ( let p of plugins ) {
174- if ( p . onImportResolutionFailure != null ) {
175- const pluginResolvedPath = p . onImportResolutionFailure ( fileDirectory , dependencyPath )
176- if ( pluginResolvedPath !== undefined ) {
177- return pluginResolvedPath ;
178- }
179- }
180- }
181-
182171 // resolveSync errors if it fails to resolve
183172 if ( this . options . tstlVerbose && e . details ) {
184173 // Output resolver log
@@ -196,17 +185,34 @@ class ResolutionContext {
196185 // We don't know for sure where the lua root is, so guess it is at package root
197186 const splitPath = path . normalize ( requiringFile . fileName ) . split ( path . sep ) ;
198187 let packageRootIndex = splitPath . lastIndexOf ( "node_modules" ) + 2 ;
199- let packageRoot = splitPath . slice ( 0 , packageRootIndex ) . join ( path . sep ) ;
188+ const packageRoot = splitPath . slice ( 0 , packageRootIndex ) . join ( path . sep ) ;
189+ let currentPackage = packageRoot ;
200190
201191 while ( packageRootIndex < splitPath . length ) {
202192 // Try to find lua file relative to currently guessed Lua root
203- const resolvedPath = path . join ( packageRoot , dependency ) ;
193+ const resolvedPath = path . join ( currentPackage , dependency ) ;
204194 const fileFromPath = this . getFileFromPath ( resolvedPath ) ;
205195 if ( fileFromPath ) {
206196 return fileFromPath ;
207197 } else {
208198 // Did not find file at current root, try again one directory deeper
209- packageRoot = path . join ( packageRoot , splitPath [ packageRootIndex ++ ] ) ;
199+ currentPackage = path . join ( packageRoot , splitPath [ packageRootIndex ++ ] ) ;
200+ }
201+ }
202+
203+ const plugins = getPlugins ( this . program ) . plugins ;
204+ for ( let p of plugins ) {
205+ if ( p . onImportResolutionFailure != null ) {
206+ const pluginResolvedPath = p . onImportResolutionFailure ( packageRoot , dependency )
207+ if ( pluginResolvedPath !== undefined ) {
208+ const fileFromPath = this . getFileFromPath ( pluginResolvedPath ) ;
209+ if ( fileFromPath ) {
210+ if ( this . options . tstlVerbose ) {
211+ console . log ( `Resolved file path for ${ dependency } to path ${ fileFromPath } using plugin.` )
212+ }
213+ return fileFromPath
214+ }
215+ }
210216 }
211217 }
212218
@@ -247,6 +253,11 @@ class ResolutionContext {
247253 path . join ( resolvedPath , "index.lua" ) , // lua index file in sources
248254 path . join ( resolvedPath , "init.lua" ) , // lua looks for <require>/init.lua if it cannot find <require>.lua
249255 ] ;
256+
257+ if ( resolvedPath . endsWith ( ".lua" ) ) {
258+ possibleLuaProjectFiles . push ( resolvedPath )
259+ }
260+
250261 for ( const possibleFile of possibleLuaProjectFiles ) {
251262 if ( this . emitHost . fileExists ( possibleFile ) ) {
252263 return possibleFile ;
0 commit comments