What the Lua minifier does
This Lua minifier tokenizes your script and prints the tokens back with as little whitespace as Lua's grammar allows. Line comments (--) and block comments (--[[ ... ]], including the --[==[ forms) are removed. Spaces are kept only where two tokens would otherwise merge, for example between local and a name, or between end and the next keyword. Lua does not need newlines or semicolons between statements, so a whole program can become one line.
Example
-- Fibonacci with memoization
local cache = {}
--[[ multi-line
comment ]]
local function fib(n)
if n < 2 then return n end
if cache[n] then return cache[n] end
local value = fib(n - 1) + fib(n - 2)
cache[n] = value
return value
end
print("fib(30) = " .. fib(30), [[long string kept]])
local cache={}local function fib(n)if n<2 then return n end if cache[n]then return cache[n]end local value=fib(n-1)+fib(n-2)cache[n]=value return value end print("fib(30) = "..fib(30),[[long string kept]])
The status bar reports 301 → 207 bytes (31% smaller). The long string [[long string kept]] keeps its double spaces, and so does every quoted string.
What is preserved
- Single- and double-quoted strings with their escapes, and long strings in
[[ ]]or[=[ ]=]form. - A
#!shebang line at the top of the file, needed to run scripts directly on Unix systems. - Number formats, including hexadecimal and exponents, and the separation needed so that
1 ..anda - -bkeep their meaning.
What it does not do
Local variable names are not shortened, so the savings come from comments and whitespace. That keeps the output predictable and makes error messages still mention real names. It also means the minifier never changes behavior that depends on names, such as debug.getlocal. The minifier does not check that your code is valid Lua beyond tokenization; format it with the Lua Beautifier first if you want a full syntax check.
When to minify Lua
Minified Lua is useful when scripts are embedded in other files or sent over a network: Redis EVAL scripts, game mods with size limits, scripts stored in configuration or databases, and code for microcontrollers running NodeMCU or similar firmware where flash space is tight. Keep the commented original for development. For plain text rather than code, the Text Minifier collapses whitespace in the same way. Processing happens in your browser.
Checking the minified script
Because the minifier only tokenizes, it is worth confirming that the output still parses before you ship it. The standard Lua distribution includes a compiler that can check syntax without running anything:
luac -p script.min.lua # prints nothing when the file is valid
Two side effects are easy to forget. Error messages and debug.getinfo now report line 1 for almost everything, so keep the original file for debugging. And comments are gone completely, including type annotations such as ---@param used by editors and language servers, so minify a copy rather than your working source.