Created
May 3, 2022 12:12
-
-
Save gphg/d51f0ea1064aefa0491fbbf4b25ccbd4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Based on this: http://lua-users.org/wiki/LuaModulesLoader | |
module(..., package.seeall) | |
local function loader(modulename) | |
local errmsg = "" | |
local modulepath = string.gsub(modulename, "%.", "/") | |
local dirname = modulepath:match("(.*[/\\])") | |
local basename = modulepath:match("^.+/(.+)$") | |
for path in string.gmatch(package.path, "([^;]+)") do | |
local filename = string.gsub(path, "%?", table.concat({dirname, basename, "/", basename})) | |
local file = io.open(filename, "rb") | |
if file then | |
return assert(loadstring(assert(file:read("*a")), filename)) | |
end | |
errmsg = errmsg.."\n\tno file '"..filename.."' (checked with custom loader)" | |
end | |
return errmsg | |
end | |
table.insert(package.loaders, 3, loader) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Most modules are singleton and has the same name as its directory. This let you load module *sorter*.
Before:
After