# Lua Minifier

Paste Lua source and strip comments and whitespace, preserve license banners, keep or strip line breaks, and optionally rename locals.

## Run it

- **CLI:** `gizza tool lua-minifier '--! @license MIT
local function greet(name)
  -- build a greeting
  local message = "hello, " .. name
  print(message)
end

greet("Ada")'`
- **Web:** https://gizza.ai/tools/lua-minifier/
- **Agents:** machine-readable descriptor (parameters JSON Schema) at https://gizza.ai/tools/lua-minifier/tool.json

## Inputs

- `code` — Lua source _(field)_
- `remove_comments` — Strip Lua comments _(field)_
- `keep_license` — Preserve license / copyright banners _(field)_
- `rename_locals` — Rename locals and parameters to short aliases _(field)_
- `line_breaks` — Line breaks _(field)_

## Output

- Minified Lua (text)

## Query parameters

Open the tool pre-filled and auto-run via URL:

- `code` — Lua source
- `remove_comments` — Strip Lua comments
- `keep_license` — Preserve license / copyright banners
- `rename_locals` — Rename locals and parameters to short aliases
- `line_breaks` — Line breaks

Example: `https://gizza.ai/tools/lua-minifier/?code=--%21%20%40license%20MIT%0Alocal%20function%20greet%28name%29%0A%20%20--%20build%20a%20greeting%0A%20%20local%20message%20%3D%20%22hello%2C%20%22%20..%20name%0A%20%20print%28message%29%0Aend%0A%0Agreet%28%22Ada%22%29&remove_comments=true&keep_license=true&rename_locals=true&line_breaks=strip`

---

## About this tool

Lua is flexible about whitespace, which makes it a good fit for minification: comments, blank lines
and indentation can usually disappear without changing how the chunk runs. This tool scans Lua as
Lua tokens instead of doing a blind regular-expression replace, so quoted strings, long strings,
long-bracket levels, shebangs and tricky token boundaries survive.

Use it when you need a smaller plugin, game script, embedded config chunk or LuaJIT helper. The
default output strips comments and joins the script onto one line. Turn on local renaming when you
want a smaller payload and the script does not inspect its own local names with debug APIs.

### Worked example

Input:

```lua
--! @license MIT
local function greet(name)
  -- build a greeting
  local message = "hello, " .. name
  print(message)
end

greet("Ada")
```

With **Strip Lua comments** and **Preserve license / copyright banners** enabled, the output keeps
the banner and removes the internal comment and spacing:

```lua
--! @license MIT
local function greet(name)local message="hello, "..name print(message)end greet("Ada")
```

If **Rename locals and parameters to short aliases** is enabled, local variables and parameters are
shortened while globals, fields, method names and string contents are left alone.

### Limits and edge cases

- The transform is token-aware, not a full Lua AST optimizer. It does not fold constants, reorder
  code, remove dead branches or encode strings.
- Local renaming is opt-in and refuses when block structure does not balance. Minification without
  renaming is still forgiving for incomplete snippets.
- `--!`, `@license`, `@preserve` and `@copyright` comments are kept by default; disable the banner
  option to strip them too.
- `line_breaks = keep` keeps one output line per non-empty source line, useful when runtime error
  line numbers matter more than the smallest possible result.
- Code that reflects over local names with `debug.getlocal`, builds code strings that mention local
  names, or depends on exact source text should not use local renaming.

## FAQ

<details>
<summary>Is this an obfuscator?</summary>

No. It is a size-reducing minifier. It removes comments and unnecessary whitespace, and it can
rename locals to shorter names, but it does not encode strings, insert control-flow traps or try to
hide what the code does. The output is still Lua source intended to run the same way as the input.

</details>

<details>
<summary>What names are safe to rename?</summary>

Only locals, `local function` names and function parameters are candidates. Globals, table fields,
method names, table-constructor keys, `goto` labels and anything inside a string are never renamed.
Aliases are unique across the file and avoid global names already used by the script.

</details>

<details>
<summary>Why preserve license comments by default?</summary>

Minification often runs right before publishing a file, and it is easy to strip required attribution
accidentally. The default keeps comments marked with `--!`, `@license`, `@preserve` or `@copyright`.
If you own the file and want the smallest possible output, turn that option off.

</details>

<details>
<summary>Will it change strings or long brackets?</summary>

No. Short strings, long strings and long-bracket contents are emitted exactly as scanned. A comment
marker such as `--` inside a string remains part of the string, and nested long-bracket levels such
as `[=[ ... ]=]` keep their original delimiters and contents.

</details>

## Related tools

- [Apply a Unified Diff to a File](https://gizza.ai/tools/apply-patch/): Paste a file and a unified diff to get the patched text in your browser, with reverse apply, fuzz matching, and per-hunk conflict reports.
- [Autocomplete Trie](https://gizza.ai/tools/autocomplete-trie/): Build a prefix trie from a pasted wordlist and get ranked autocomplete suggestions for any typed prefix. Weights, typo tolerance, trie stats, JSON. Runs locally.
- [Code Chunker](https://gizza.ai/tools/code-chunker/): Split Python, Rust, JavaScript, TypeScript, Go, Java, C/C++, C#, PHP, or Swift into line-ranged chunks that keep functions and classes intact.
- [Code Formatter](https://gizza.ai/tools/code-formatter/): Beautify and re-indent minified or messy HTML, CSS, JavaScript, or JSON. Auto-detect the language, choose spaces or tabs, and format locally in your browser.
- [Code language detector](https://gizza.ai/tools/code-language-detect/): Detect the likely programming language of a pasted code snippet with ranked alternatives, confidence and explainable signals.
