Skip to content

Instantly share code, notes, and snippets.

@gphg
Created May 3, 2022 12:12
Show Gist options
  • Save gphg/d51f0ea1064aefa0491fbbf4b25ccbd4 to your computer and use it in GitHub Desktop.
Save gphg/d51f0ea1064aefa0491fbbf4b25ccbd4 to your computer and use it in GitHub Desktop.
-- 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)
@gphg
Copy link
Author

gphg commented May 3, 2022

Most modules are singleton and has the same name as its directory. This let you load module *sorter*.

Before:

local inspect = require("path.to.vendor.inspect.inspect")

After

local inspect = require("path.to.vendor.inspect")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment