Ondrej Prochazka
Ondrej Prochazka

Reputation: 11

Compile lua5.2 files to one with using require and dofile

Can I compile all my files to one and then execute from C. In my lua files I am using require and dofile functions. When I try to use luac for compiling and then I want to execute the compiled file then It will not be able to find my modules which are built in the compiled file. I thought the including lua files via require and dofile function luac compiler proccess like for example javascript compiler do. In simply terms Javascript compilers adding the importing files to one.

And is it good practise when you do a release for app have the lua scripts in one file? Because I have a few directories with lua scripts and when I do a release I must have all lua files with binary in readable form.

Upvotes: 1

Views: 1125

Answers (1)

dualed
dualed

Reputation: 10512

luac is not a packaging tool, but simply a compiler. It will compile everything you feed it -- as long as it's valid Lua -- into Lua bytecode

If you need to have everything in one file and want to include your modules using require, you have to modify your source files a bit, so that require knows where to find those modules in memory.

Simply put this at the end of all Lua module files:

package.loaded["<yourmodnamehere>"] = yourmodreturnvalue

Then simply require or luaL_dofile your combined Lua right after your Lua init.

Whatever you do with dofile, it is probably easier to just rewrite your code to functions.

Upvotes: 0

Related Questions