Generated Loader

Lux replaces hand-written GMod loader code with a generated loader tree. The goal is to remove repeated if SERVER then AddCSLuaFile(...) end boilerplate and make load order a compiler-owned property. autorun is only an optional thin forwarder into that loader tree.

Hand-written loader is replaced by generated loader
if SERVER then AddCSLuaFile("cl_hud.lua") end
if SERVER then AddCSLuaFile("cl_menu.lua") end
include("sh_items.lua")
if SERVER then include("sv_store.lua") end
if CLIENT then include("cl_hud.lua") end

Generated files are still ordinary Lua, but developers should treat them as build output.

Loader Files

Loader files live under runtime_base:

lux/my-addon/loader_shared.lua
lux/my-addon/loader_client.lua
lux/my-addon/loader_server.lua

When autorun = true, Lux also writes a small addon-style forwarder:

autorun/my_addon.lua

Set autorun = false when an existing gamemode, framework, or hand-written entry point should include the generated loaders itself. This does not disable the Lux loader.

AddCSLuaFile Batching

The generator batches client-send files instead of repeating the same if SERVER guard on every line.

AddCSLuaFile batching
if SERVER then AddCSLuaFile("lux/my-addon/client/runtime/lux/std.lua") end
if SERVER then AddCSLuaFile("lux/my-addon/client/my_addon/hud.lua") end
if SERVER then AddCSLuaFile("lux/my-addon/client/my_addon/ui.lua") end

This keeps generated loaders readable even when a project has many client artifacts.

Module Factories

Modules are emitted as factories. The loader provides __lux_import; modules return their export table.

Module factory
-- conceptual module output
local secret = 1
export { secret as value }

The global environment does not get a public Lux import function. Import stays inside generated loader scope.

Private Registry

The loader stores module exports in a bundle-private registry.

Private registry
-- Each bundle owns its generated module table.
-- Other addons do not share it accidentally.

Using a local registry avoids cross-addon collisions while keeping generated Lua simple.

Registering Modules

Each emitted module file returns a factory. The loader calls it once and stores the exports.

Register module
-- load generated module file
-- call the returned factory
-- store exports by canonical module id

Import order is therefore deterministic: dependencies are registered before modules that import them.

Load Order

Lux computes load order from:

  • package dependency graph;
  • module import graph;
  • realm artifacts;
  • module entry part order;
  • local before and after part constraints.

Developers should not encode load order through artificial 00_, 10_, or 20_ file prefixes. Use part order in the entry part when initialization order matters.

Do Not Edit Generated Files

Generated files are marked clearly.

Generated file marker
-- Generated files may be deleted and regenerated.
-- Put fixes in Lux source, not in generated Lua.

If generated Lua looks wrong, the fix belongs in source lowering, module graph resolution, or loader generation.