@@ -103,6 +103,16 @@ function M.disable()
103
103
M .enabled = false
104
104
end
105
105
106
+ function M .check_loaded (modname )
107
+ --- @diagnostic disable-next-line : no-unknown
108
+ local mod = package.loaded [modname ]
109
+ if type (mod ) == " table" then
110
+ return function ()
111
+ return mod
112
+ end
113
+ end
114
+ end
115
+
106
116
--- @param modname string
107
117
--- @return any
108
118
function M .loader (modname )
@@ -111,12 +121,6 @@ function M.loader(modname)
111
121
local chunk , err
112
122
if entry and M .check_path (modname , entry .modpath ) then
113
123
M .stats .find .total = M .stats .find .total + 1
114
- local mod = package.loaded [modname ]
115
- if type (mod ) == " table" then
116
- return function ()
117
- return mod
118
- end
119
- end
120
124
chunk , err = M .load (modname , entry .modpath )
121
125
end
122
126
if not chunk then
@@ -127,7 +131,10 @@ function M.loader(modname)
127
131
if M .enabled then
128
132
chunk , err = M .load (modname , modpath )
129
133
else
130
- chunk , err = M ._loadfile (modpath )
134
+ chunk = M .check_loaded (modname )
135
+ if not chunk then
136
+ chunk , err = M ._loadfile (modpath )
137
+ end
131
138
end
132
139
end
133
140
end
145
152
--- @param modpath string
146
153
--- @return function ?, string ? error_message
147
154
function M .load (modkey , modpath )
155
+ local chunk , err
156
+ chunk = M .check_loaded (modkey )
157
+ if chunk then
158
+ return chunk
159
+ end
148
160
modpath = modpath :gsub (" \\ " , " /" )
149
161
local hash = M .hash (modpath )
150
162
if not hash then
@@ -158,7 +170,7 @@ function M.load(modkey, modpath)
158
170
entry .used = os.time ()
159
171
if M .eq (entry .hash , hash ) then
160
172
-- found in cache and up to date
161
- local chunk , err = loadstring (entry .chunk --[[ @as string]] , " @" .. entry .modpath )
173
+ chunk , err = loadstring (entry .chunk --[[ @as string]] , " @" .. entry .modpath )
162
174
if not (err and err :find (" cannot load incompatible bytecode" , 1 , true )) then
163
175
return chunk , err
164
176
end
@@ -175,7 +187,7 @@ function M.load(modkey, modpath)
175
187
end )
176
188
end
177
189
178
- local chunk , err = M ._loadfile (entry .modpath )
190
+ chunk , err = M ._loadfile (entry .modpath )
179
191
if chunk then
180
192
M .dirty = true
181
193
entry .chunk = string.dump (chunk )
@@ -184,7 +196,9 @@ function M.load(modkey, modpath)
184
196
end
185
197
186
198
function M .require (modname )
199
+ --- @diagnostic disable-next-line : no-unknown
187
200
local mod = M .loader (modname )()
201
+ --- @diagnostic disable-next-line : no-unknown
188
202
package.loaded [modname ] = mod
189
203
return mod
190
204
end
0 commit comments