def compressJS_jsmin(self, js, description): self.log("compressing " + description + " code") original = StringIO.StringIO(js) output = StringIO.StringIO() jsMinifier = jsmin.JavascriptMinify() jsMinifier.minify(original, output) result = output.getvalue() original.close() output.close() return result
def regex_recognise(self, js): if not jsmin.is_3: if jsmin.cStringIO and not isinstance(js, str): # strings can use cStringIO for a 3x performance # improvement, but unicode (in python2) cannot klass = jsmin.cStringIO.StringIO else: klass = jsmin.StringIO.StringIO else: klass = jsmin.io.StringIO ins = klass(js[2:]) outs = klass() jsmin.JavascriptMinify(ins, outs).regex_literal(js[0], js[1]) return outs.getvalue()
def run(self): try: import jsmin except: pass djanalytics_js_in = open('djanalytics/templates/djanalytics.js') djanalytics_js_out = open('djanalytics/templates/djanalytics.js.min', 'w') try: jsmin.JavascriptMinify(djanalytics_js_in, djanalytics_js_out).minify() finally: djanalytics_js_in.close() djanalytics_js_out.close()
def process_js(): jsm = jsmin.JavascriptMinify() try: os.remove(PATH + OUTJS) except: print "delete nop" out = open(PATH + OUTJS, 'wb') for t in file_js: print "Processing " + t fd1 = open(PATH + t, 'r') jsm.minify(fd1, out) fd1.close() out.close() print "Written in " + PATH + OUTJS
def testInputStream(self): try: from StringIO import StringIO except ImportError: from io import StringIO ins = StringIO(r''' function foo('') { } ''') outs = StringIO() m = jsmin.JavascriptMinify() m.minify(ins, outs) output = outs.getvalue() assert output == "function foo(''){}"
def regex_recognise(self, js): klass = jsmin.io.StringIO ins = klass(js[2:]) outs = klass() jsmin.JavascriptMinify(ins, outs).regex_literal(js[0], js[1]) return outs.getvalue()
# Templates reference: # http://www.djangoproject.com/documentation/0.96/templates/ from cefbase import * import os import StringIO from google.appengine.ext import webapp from google.appengine.ext.webapp.util import run_wsgi_app from google.appengine.ext.webapp import template from google.appengine.api import memcache import jsmin jsm = jsmin.JavascriptMinify() class NavbarScript(webapp.RequestHandler): # code correspond au nom du fichier .js (exemple: cef, clermont...) def get(self, code): if 'Host' in self.request.headers.keys(): host = self.request.headers['Host'] else: raise NameError('MissingHost') # Systeme de cache js_response = memcache.get( NEW_VALUE_WHEN_DEPLOYED + "_js_response_" + code) # Le cache est desactive en local if host == "localhost:8080": js_response = None
def build(self, input_files, output_file, packaging_module, include_version=True, src_prefix_path=""): """ Build distributable version of x3dom @param src_prefix_path: Optional path that is used as prefix for all source files @type src_prefix_path: String """ print "output file:", output_file print "input_files:", input_files version_out = "" if include_version == True: # find the VERSION file if os.path.isfile("VERSION"): version_file_name = "VERSION" elif os.path.isfile("src/VERSION"): version_file_name = "src/VERSION" else: print "FATAL: Cannot find any VERSION file" sys.exit(0) # parse file & generate version.js version_out = self.generate_version_file(version_file_name) # Add the version.js to the list of input files input_files.append((version_out, [version_out])) concatenated_file = "" in_len = 0 out_len = 0 # Merging files print "Packing Files" for (_, files) in input_files: for f in files: if f == version_out: concatenated_file = self._mergeFile(concatenated_file, f) else: concatenated_file = self._mergeFile( concatenated_file, self._prefixFilePath(f, src_prefix_path)) """ #Single file? if filename[-3:] == ".js": #Merge directly concatenated_file = self._mergeFile(concatenated_file, filename) #Otherwise (folder) else: #Open all files in folder and merge individually print "Folder: ", filename node_files = [f for f in os.listdir(filename) if isfile(join(filename,f)) and f[-3:]==".js"] print ";".join(node_files) for node_file in node_files: concatenated_file = self._mergeFile(concatenated_file, join(filename,node_file)) """ print "" outpath = os.path.dirname(os.path.abspath(output_file)) if not os.access(outpath, os.F_OK): print "Create Dir ", outpath os.mkdir(outpath) # Packaging print "Packaging" print self.VERSION_STRING print " Algo " + packaging_module print " Output " + os.path.abspath(output_file) # JSMIN if packaging_module == "jsmin": # Minifiy the concatenated files out_stream = StringIO() jsm = jsmin.JavascriptMinify() jsm.minify(StringIO(concatenated_file), out_stream) out_len = len(out_stream.getvalue()) # Write the minified output file outfile = open(output_file, 'w') outfile.write(self.VERSION_STRING) outfile.write(out_stream.getvalue()) outfile.close() # JSPACKER elif packaging_module == "jspacker": p = JavaScriptPacker() result = p.pack(concatenated_file, compaction=True, encoding=62, fastDecode=False) out_len = len(result) outfile = open(output_file, 'w') outfile.write(self.VERSION_STRING) outfile.write(result) outfile.close() # ClosureCompiler elif packaging_module == "closure": # collect files files = [] for (_, filesForComponent) in input_files: for f in filesForComponent: if f == version_out: files += ["--js=" + f] else: files += [ "--js=" + self._prefixFilePath(f, src_prefix_path) ] #concatenated_file = self._mergeFile(concatenated_file, _prefixFilePath(f, src_prefix_path)) Popen([ "java", "-jar", "tools/compiler.jar", "--js_output_file=" + output_file, "--summary_detail_level=3", "--warning_level=VERBOSE" ] + files) # Popen(["java", "-jar", "tools/compiler.jar", "--js_output_file=" + output_file] + files) # NONE elif packaging_module == 'none': outfile = open(output_file, 'w') outfile.write(self.VERSION_STRING) outfile.write(concatenated_file) outfile.close() # Output some stats in_len = len(concatenated_file) ratio = float(out_len) / float(in_len) print " Packed %s -> %s" % (in_len, out_len) print " Ratio %s" % (ratio)
def output(self, _in, out, **kw): jsmin.JavascriptMinify().minify(_in, out)