lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


2008/7/31 Christian Lindig <lindig@vistabella.de>:
> I assume this is a FAQ but could not find anything with Google: I'd like to
> combine all *.lua files of an application into a single bytecode file (using
> luac) which I would pass to Lua at startup. I tried this but observing Lua
> on Linux with strace(1) revealed that all or most *.lua files were still
> loaded. I assumed that the order of the modules matters and tried the order
> as I observed it through strace(1) but this did not help. Is there a recipe
> for compiling everything into a single bytecode file?
>
> The application I am looking to compile is the Nanoki wiki which is
> implemented as several modules which are loaded using "require" plus some C
> libraries. Obviously the C libraries would still be loaded from the
> bytecode. However, my question is completely independent of Nanoki.

This thread in the mailing list archive solves a similar problem:

http://lua-users.org/lists/lua-l/2008-06/msg00326.html

You can try the following script to generate a bytecode file of all
the listed modules (see the beginning of the script). Then in your
final program, just call dofile("preload.lbc") before using (through
'require' as usual) any of your modules.

local modules = {
	"foo",
	"foo.bar",
	"foo.baz",
}

local preload = ""
for _,modname in ipairs(modules) do
	preload = preload .. 'function package.preload["'..modname..'"](...)' .. '\n'
	preload = preload .. io.open(modname:gsub("%.", "/")..".lua"):read"*a" .. '\n'
	preload = preload .. 'end' .. '\n'
end

local output = io.open("preload.lua", "w")
output:write(preload)
output:close()

os.execute("luac -o preload.lbc preload.lua")