What is the Minifier?
The minifier reduces Lua code size by:- Removing unnecessary whitespace and comments (trivia)
- Renaming variables to shorter names while preserving program behavior
- Respecting scope rules to avoid naming conflicts
- Reduce file size for distribution
- Decrease bandwidth usage
- Provide basic code obfuscation
- Optimize code for embedded systems
Basic Usage
Use theMinify extension method on any SyntaxTree:
Minifier API
TheLuaExtensions class provides three overloads of the Minify method:
Simple Minification
Uses default settings (alphabetical naming strategy with sorted slot allocation):Custom Naming Strategy
Specify how variables should be renamed:Full Control
Customize both naming and slot allocation:Naming Strategies
Naming strategies determine how variable slots are converted to names. TheNamingStrategies class provides several built-in options:
Alphabetical (Default)
Uses lowercase letters a-z with underscore prefix for conflicts:a, b, c, …, z, aa, ab, ac, …
If a name conflicts with an existing variable or keyword, it adds an underscore prefix: _a, _b, etc.
Numerical
Uses digits 0-9 with underscore prefix:_0, _1, _2, …, _9, _00, _01, …
Numbers cannot start Lua identifiers, so numerical names always have at least one underscore prefix.
ZeroWidth
Uses zero-width Unicode characters for maximum size reduction:- Prefix: Zero Width Space (
\u200B) - Alphabet: Zero Width Non-Joiner (
\u200C), Zero Width Joiner (\u200D), Zero Width No-Break Space (\uFEFF)
Custom Naming Strategy
Create your own naming strategy using theSequential factory method:
NamingStrategy delegate directly:
Slot Allocators
Slot allocators determine how variable slots are assigned and reused. The experimental package provides two implementations:SortedSlotAllocator (Default)
Reuses variable names when possible by tracking which slots are available:- Maximum name reuse
- Smallest possible variable names
- Better minification ratio
- Slightly slower due to slot tracking
SequentialSlotAllocator
Assigns slots sequentially without reuse:- Faster allocation
- Simpler logic
- No name reuse
- May produce longer names
Custom Slot Allocator
Implement theISlotAllocator interface:
Transformations Performed
The minifier performs two main transformations:1. Trivia Removal
Removes all unnecessary whitespace and comments while preserving required separators:A space is inserted between tokens when required by Lua syntax. For example,
return1 would be invalid, so it becomes return 1.2. Variable Renaming
Renames local variables and function parameters to shorter names:- Local variables
- Function parameters
- Local function names
- Global variables (preserved to maintain external API)
- Table field names (preserved to maintain data structure)
- String literals (preserved as data)
Complete Example
Trade-offs and Considerations
Advantages
- Smaller file size: Typically 40-70% reduction
- Faster downloads: Less bandwidth usage
- Basic obfuscation: Makes code harder to read
- Performance: Minimal impact on runtime (slightly faster parsing)
Disadvantages
- Loss of readability: Debugging becomes very difficult
- No source maps: Hard to map errors back to original code
- Irreversible: Cannot recover original variable names
- Limited obfuscation: Not a security measure; can be reverse-engineered
Best Practices
- Minify only for distribution: Keep development code readable
- Version control: Commit source code, not minified code
- Test thoroughly: Ensure minified code behaves identically
- Document external APIs: Global variables and table structures should be documented
- Consider source maps: For production debugging (requires custom implementation)
Use Cases
Good Use Cases
- Lua scripts in games: Reduce size of embedded scripts
- Web-delivered Lua: Minimize bandwidth for browser-based Lua engines
- ROM-constrained devices: Save space on embedded systems
- Release builds: Optimize production code
- Basic IP protection: Make code harder to read (not secure!)
When to Avoid
- Development: Keep code readable during development
- Open source: If you want others to learn from your code
- Debugging: When you need stack traces and readable errors
- Collaborative projects: Team members need to understand the code
Combining with Constant Folding
For maximum optimization, combine constant folding with minification:See Also
- Constant Folding - Optimize constant expressions
- Experimental Package Overview - Learn about other experimental features