Came up with my own temporary solution for this.
I created a simple python file that does the job of automatically cloning and pushing your code after minifying it. Your master branch will remain as it is and a new branch called minified_code will be created having the minified code. You can test the minified version and publish it if everything works well.
All your python code as well as html,css and javascript files will be minified to make your app load faster and also difficult for others to read and modify.
Update: All your yaml files will also be minimised now
Here is the python code (Run this code whenever you want to publish a new code from master to production)
import yaml
import os
import glob
import python_minifier
import minify_html
git_url="ssh://something@anvil.works:/XXX.git" #Replace with your URL
os.system(f"git clone {git_url} MyAnvilApp") #Make sure that you have added your SSH key to Anvil
print("Done Cloning")
folder="MyAnvilApp"
os.chdir(folder)
app_directory=os.getcwd()
python_files = glob.glob(app_directory + "/**/*.py", recursive=True)
#Minify all python files in the app directory
for file in python_files:
with open(file, "r",encoding="utf8") as f:
code = f.read()
minified_code = python_minifier.minify(code, remove_literal_statements=True)
with open(file, "w",encoding="utf8") as f:
f.write(minified_code)
#Minify all html/css/js files in the app directory
html_files = glob.glob(app_directory + "/**/*.html", recursive=True)
css_files = glob.glob(app_directory + "/**/*.css", recursive=True)
js_files = glob.glob(app_directory + "/**/*.js", recursive=True)
for file in html_files+css_files+js_files:
with open(file, "r",encoding="utf8") as f:
code = f.read()
minified_code = minify_html.minify(code)
with open(file, "w",encoding="utf8") as f:
f.write(minified_code)
#Minify all yaml files in the app directory
yaml_files = glob.glob(app_directory + "/**/*.yaml", recursive=True)
for file in yaml_files:
with open(file, "r",encoding="utf8") as f:
data = yaml.safe_load(f)
with open(file, "w",encoding="utf8") as f:
yaml.dump(data, f, default_flow_style=True, width=float("inf"))
#Push all changes to the repository
os.system("git add .")
os.system('git commit -am "Minified Code"')
os.system("git branch minified_code")
os.system("git push -f origin minified_code")
print("Done Minifying\n")
print(
"""-----Output-----
Check your Anvil App for a new branch called minified_code.
Test the app and if everything works fine, publish the minified_code branch.
You can keep working on the master branch with unminified code.
You can delete the MyAnvilApp folder now.
""")
The following libraries need to be installed on your system -