Thursday, 15 January 2015

How to minify & version js/css on Google App Engine? -


Since js / css is included on the flight, it seems that this is the best way to do the deployment.

After lots of googling I came up with the script to get it.

At first I thought that the best base.html should be left in the JavaScript path and this is just CSS / JS compress.

cssmin compresses css and overwrites the original. Although the closing does not allow the overwriting of the original and this concept fails beforehand.

The other problem is, even if I stopped overwriting the original file, caching would be a problem. For this reason, each deployment of minified css / js should come with a random number in the file name so that new versions can be picked up after a new deployment.

The only way to get this way will be to modify base.html with sd or something else.

Before I change the wheel again, is there a better way to do this? Many thanks

  import sys, os import cssmin def main (): if len (sys.argv) == 1: return appId = sys.argv [1] print "appId", appId cmd = R'java -jar compiler.jar --js = / src / application / static / f11 / f11.js --js_output_file = / src / application / static / f11 / f11.min.js' os.system (cmd) Output = cssmin.cssmin ('/ src / application / static / f11 / f11.css'). Read ()) f = open ('/ src / application / static / f11 / f11.css',' w ') F.write (output) # GAE server cmd = r' "google_appengine \ appcfg.py" os To update the system (cmd + "update." + "-A% s"% appId), please appcfp __name__ == "__main__": main ()    

You should do this once by detecting on startup startup some global wars may be set if your app is running on the developer server. You generate the necessary URL for your property on that basis, and make a list of the versions for each asset.

Python Example ():

  JQUERY_VERSION = '1.7.2' JQUERY_UI_VERSION = '1.8.20' ANGULAR_VERSION = '1.0.2' If os.environ ['SERVER_SOFTWARE' '] (' Google '): JQUERY_URL = "//ajax.googleapis.com/ajax/libs/jquery/% (version) s / jquery.min.js"% {' version ': JQUERY_VERSION} JQUERY_UI_URL = "//ajax.googleapis.com/ajax/libs/jqueryui/%(version)s/jquery-ui.min.js"% {'Version': JQUERY_UI_VERSION} JQUERY_UI_CSS_URL = "//ajax.googleapis.com/ajax /libs/jqueryui/%(version)/themes/base/jquery.ui.all.css"% {'version': JQUERY_UI_VERSION} ANGULAR_URL = "//ajax.googleapis.com/ajax/libs/angularjs/%(version Jquery_vsion = "/ static / js / jquery-% (version) s.min.js"% {'version': JQUERY_VERSION} JQUERY_UI_URL = "s / angular.min.js"% {'version'}: ANGULAR_VERSION} and: /static/js/jquery-ui-%(version)s.min.js "% {'version': JQ UERY_UI_VERSION} JQUERY_UI_CSS_URL = "/ static / CSS / jQuery-ui / jQuery-UI-% (version) s.css"% {'version'}: JQUERY_UI_VERSION} ANGULAR_URL = "/ static / js / angular -% (version) s Min.js "% {'version': ANGULAR_VERSION}   

Go for example ():

  func init () {angular_ver: =" 1.0 .5 "bootstrap_ver: =" 2.3.1 "jquery_ver: =" 1.9.1 "if Appengine.IsDevAppServer () {angular = fmt.Sprintf (" / static / js / angular-% v.js ", angular_ver) BootstrapCss = Fmt.Sprintf ("/ static / css / bootstrap-% v.css", bootstrap_ver) BootstrapJs = fmt.Sprintf ("/ static / js / bootstrap-% v.js", bootstrap_ver) Jquery = fmt.Sprintf ("/ Static / js / jquery-% v.js ", jquery_ver)} and {angular = FM.T.printf (" // ajax.googleapis.com/ajax/libs/angularjs/%v/angular.min.js ", at angular_) BootstrapCss = fmt.Spr Intf ("// netdna.bootstrapcdn.com/twitter-bootstrap/% v / css / bootstrap-combined.min.css", bootstrap_ver) BootstrapJs = fmt.Sprintf ("// netdna.bootstrapcdn.com/twitter-bootstrap/ % V / js / bootstrap.min.js ", bootstrap_ver) Jquery = Fmt.Sprintf (" //jj.googleapis.com/ajax/libs/jquery/%v/jquery.min.js ", jquery_ver)}} < / Code>  

If you have a deployment strip that installs your local (i.e., non-cdn) content, runs it here, and use the method above, but with a local url .min extension

No comments:

Post a Comment