Skip to content

Commit

Permalink
lua minifier (wowie,,,.... python.)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuglioIsStupid committed Nov 13, 2024
1 parent a87715f commit 223c5d8
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
build/
build/
mini/
58 changes: 58 additions & 0 deletions tools/minifier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# lua code minifier
# removes all comments, and minifies variable names
# usage: python minifier.py <input.lua> <output.lua>
# if output is not specified, it will write to mini/<input.lua>
import sys, re, os

ignoreFiles = [
# Breaks when minified for some reason
"Yaml.lua"
]

globalVars = {
'love': 'love',
'jit': 'jit',
'math': 'math',
'table': 'table',
'string': 'string',
'io': 'io',
'os': 'os',
'debug': 'debug',
'coroutine': 'coroutine',
}
currentChar = "a" # a-z, then A-Z, then aa-az, then aA-aZ, then ba-bz, etc

def minify(input, output):
with open(input, 'r') as f:
lua = f.read()

lua = re.sub(r'--\[\[.*?\]\]', '', lua, flags=re.DOTALL)
lua = re.sub(r'--\[=*\[.*?\]=*\]', '', lua, flags=re.DOTALL)
lua = re.sub(r'--.*', '', lua)

# remove whitespace
lua = re.sub(r'\s+', ' ', lua)

# TODO: minify variable names

os.makedirs(os.path.dirname(output), exist_ok=True)
with open(output, 'w') as f:
f.write(lua)

def checkFolder(folder):
for root, dirs, files in os.walk(folder):
for file in files:
if file.endswith('.lua') and file not in ignoreFiles:
minify(os.path.join(root, file), os.path.join('mini', root, file))
elif (not file.endswith('.lua') or file in ignoreFiles) and not os.path.exists(os.path.join('mini', root, file)):
os.makedirs(os.path.join('mini', root), exist_ok=True)
os.system('cp "{}" "{}"'.format(os.path.join(root, file), os.path.join('mini', root, file)))

if __name__ == '__main__':
if len(sys.argv) < 2:
print('usage: python minifier.py <folder>')
sys.exit(1)

folder = sys.argv[1]

checkFolder(folder)

0 comments on commit 223c5d8

Please sign in to comment.