LuaCC is a command line tool that allows you combine multiple Lua files into single one without any changes of your source code.
LuaCC principle is based on Lua package.loaders (or package.searchers) approach. LuaCC obtain the main file of the application and any number of additional Lua modules. The main file is used as a base of result script (it's content is copied as is) and content of each additional Lua module is saved to result script as a function to special table with a key equals to the name of module.
After that the content of the modules embedded into the the table is loading using a special internal loader function. Internal loader replaces the default package.loaders[2] which actually searches for *.lua modules using pathes from package.path. By default internal loader does not completely delete package.loaders[2], firstly internal loader tries to find out the required module among the embedded modules and if it was not found internal loader will call the standard package.loaders[2] to load the module from filesystem.
luacc -o <output> [-i <include>] [-p <position>] <main> [modules] ...
luacc ... -o /path/to/result.lua
#!
LuaCC leave it firstluacc ... -p 10
luacc ... -p "LuaCC code block"
luacc ... path.to.main.file
luacc ... path.to.main.file path.to.module1 path.to.module2 ...
luacc ... -i /one/path/to/folder/with/modules -i /another/path/to/folder/with/modules
Note: LuaCC uses includes in the same way as package.path. Firstly it tries to find out the module using the current path and if it's not found LuaCC searches the module using each include path in specified orderLet's show you example of project consists of main file main.lua
and 2 modules: module1.lua
and module2.lua
/path/to/project
|
|-main.lua
|-subfolder
|
|-module1.lua
/path/to/external/modules
|
|-module2.lua
Below you can see the source code of these files:
main.lua
print "Main module"
local module1 = require "subfolder.module1"
local module2 = require "module2"
print("Module says:", module1.name)
print("Module says:", module2.name)
module1.lua
print "Module1 was loaded"
return { name = "My name is Module1" }
module2.lua
print "Module2 was loaded"
return { name = "My name is Module2" }
To combine the files use the command below:
$ lua luacc.lua -o myapp.lua -i /path/to/project -i /path/to/external/modules main subfolder.module1 module2
And now just execute the result script:
$ lua myapp.lua
Main module
Module1 was loaded
Module2 was loaded
Module says: My name is Module1
Module says: My name is Module2
Generally, LuaCC does not use global variables and does not affect the logic of your application, but considering the fact that LuaCC override stadard package.loaders you should be careful to override it in your application.
LuaCC works with Lua 5.1 and Lua 5.2 and I think it can work with a older versions, but it can be a problem if you'r using obsolete Lua module (old version) function.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。