I am trying to make a python script that will package the contents of a directory and minify all of the JavaScript and CSS scripts.
If I used the code below (bottom of the post), and the directory structure inside of theme_files was such:
\
|-assets\
| |-css\
| | |-theme.css
| | |-stylesheet.css
| |
| |-js\
| | |-theme.js
| | |-page.js
|
|-index.html
(Is there a better way to do that?)
It would output the whole directory structure into the generated .pak file properly. However, the minified css and javascript files have no content inside of them other than their own file name.
Example: the content of the file (supposedly minified) theme.css would be "theme.css"
That's it. Nothing else. One line.
Any idea what I'm doing wrong?
import io
import os
import zipfile
import rcssmin
import rjsmin
pakName = input("Theme Name: ").replace(" ", "_").lower()
themePak = zipfile.ZipFile(pakName +".tpk", "w")
for dirname, subdirs, files in os.walk("theme_files"):
themePak.write(dirname)
for filename in files:
if not filename.endswith((".css", ".js")):
themePak.write(os.path.join(dirname, filename))
if filename.endswith(".css"):
cssMinified = io.StringIO()
cssMinified.write(rcssmin.cssmin(filename, keep_bang_comments=True))
themePak.writestr(os.path.join(dirname, filename), cssMinified.getvalue())
if filename.endswith(".js"):
jsMinified = io.StringIO()
jsMinified.write(rjsmin.jsmin(filename, keep_bang_comments=True))
themePak.writestr(os.path.join(dirname, filename), jsMinified.getvalue())
themePak.close()
rcssmin.cssmin()andrjsmin.jsmin()expect the first element to be the CSS respectively JS code to minify as string. You have to open and read the CSS and JS files by yourself.