|
import subprocess, os, time |
|
|
|
# When this trigger is executed, we get some variables, which are usefull for us, and they includes: |
|
# folder -> absolute path to project root folder |
|
# filename -> absolute path to file for which this trigger is processed |
|
# __file__ -> absolute path to this file |
|
# ftp -> ftp class from simple-ftp-deploy file (https://github.com/HexRx/simple-ftp-deploy/blob/master/main.py) |
|
# config -> dictionary containing configuration from simple-ftp-deploy.json |
|
# sublime -> see https://www.sublimetext.com/docs/api_reference.html#sublime |
|
|
|
# and some function used by simple-ftp-deploy (also see https://github.com/HexRx/simple-ftp-deploy/blob/master/main.py for usage) |
|
|
|
# msg -> function used by simple-ftp-deploy to display messages |
|
# error -> function used by simple-ftp-deploy to display errors |
|
# aks -> function used by simple-ftp-deploy to show ok/cancel dialog |
|
|
|
# Get filename without extension, so we can change the extension to .min.css / .min.js |
|
filenameWithoutExtension, extension = os.path.splitext(filename) |
|
|
|
# Minify on if not already minified |
|
if not '.min' in filenameWithoutExtension: |
|
|
|
minFilename = filenameWithoutExtension + '.min' + extension |
|
|
|
# Measure time to print statistics |
|
start = time.time() |
|
|
|
# Call css-html-js-minify/css-html-js-minify.py located in same folder as this file, with filename and wait for it to finish. New file should be generated with .min.js / .min.css extension |
|
subprocess.Popen([os.path.join(os.path.dirname(__file__), 'css-html-js-minify', 'css-html-js-minify.py'), filename], bufsize=-1, stderr=subprocess.STDOUT).wait() |
|
# Or alternativelly, use some CLI minifier, like https://github.com/tdewolff/minify/tree/master/cmd/minify |
|
# If you want to use other minifier, edit arguments below |
|
# subprocess.Popen(['minify', '-o', minFilename, filename], bufsize=-1, stderr=subprocess.STDOUT).wait() |
|
|
|
# Measure time to print statistics |
|
end = time.time() |
|
|
|
# Print message that we have minified that file (will print message in console and show in status bar) |
|
msg('[Minified {0}]: {1} to {2} ({3}ms)', filename.replace(folder, ''), minFilename.replace(folder, ''), str(round((end - start) * 1000))) |
|
|
|
# And finally upload our minified file |
|
ftp.upload(folder, minFilename) |