New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lua Local Require #76
Comments
My concept here is to make Any thoughts @xyproto ? For now I will start with implementing this myself as an in-Lua thing. |
This sounds like a good plan, as long as performance and security are kept in mind. 👍 🙂 |
Alright, I can get most of the way there using this: -- loadscriptdir.lua
local r = require
local pp = package.path
require = function(m)
package.path = scriptdir() .. "/?.lua;" .. pp
return r(m)
end
-- serverconf.lua
OnReady(function()
require("loadscriptdir")
end) That's perfectly acceptable, loads modules from the directory of the running script, which is what I really wanted. Trick is that gopher-lua's module caching system means once it's required, I can't edit the module. I'll think about that one. |
Is this different now that |
Well, esbuild is for JS, whereas this is related to Lua modules. My little script (to import other Lua files relative to the active script dir rather than Algernon's base dir) did the job but as I said it gets cached even in "development mode". Ideally it would only cache in production mode, but I didn't go down that rabbit hole thoroughly. |
Stepping back:
I think this becomes more of a philosophical question rather than a purely technical one.
Penny for your thoughts @xyproto |
added one line package.path = serverdir() .. "/lib/?.lua;" .. pp |
To avoid caching modules: -- loadscriptdir.lua
local r = require
local pp = package.path
require = function(m)
package.loaded[m] = nil -- Added this
package.path = scriptdir() .. "/?.lua;" .. pp
return r(m)
end
-- serverconf.lua
OnReady(function()
require("loadscriptdir")
end) Setting Also, |
...Actually, requiring that script in I'm wondering if there's an existing hook for running Lua when a new Lua State is created, or if one could be added. |
I noted that
require()
is relative to Algernon's root run location, even when the currently active Lua file is somewhere else, such as adata.lua
orindex.lua
inmy/webapp/path/
.Because of this, there's currently no good way to do ad-hoc code modularization. Using the code lib feature, or
require()
, both necessitate installing these things as globals within the system. If I want to just modularize some Lua code within a particular folder/Algernon app, I'm not sure how to do that.@xyproto what do you think of the concept of making
require()
relative to the location of the file of Lua code that's being executed? I suppose there would be some scenarios where you want to pull in a global module that you installed, as well...The text was updated successfully, but these errors were encountered: