您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

114 行
2.6 KiB

  1. from minify import JavascriptMinify
  2. class Bundle:
  3. """
  4. Concatenate, compress and mix (if required) js+css files from build.json
  5. """
  6. def concat(self, filelist, outfile=None, is_js=False):
  7. """
  8. Concat css and js files into a bundle
  9. """
  10. import os
  11. from cStringIO import StringIO
  12. from build import verbose
  13. out_type = outfile and outfile.split('.')[-1] or 'js'
  14. temp = StringIO()
  15. for f in filelist:
  16. if verbose:
  17. print f + ' | ' + str(int(os.path.getsize(f)/1024)) + 'k'
  18. fh = open(f)
  19. # get file type
  20. ftype = f.split('.')[-1]
  21. # special concat (css inside js)
  22. if is_js and ftype =='css':
  23. css = fh.read()
  24. data = "\nwn.assets.handler.css('%s');\n" % css.replace("'", "\\'").replace('\n', '\\\n')
  25. # plain concat
  26. else:
  27. data = fh.read() + '\n'
  28. fh.close()
  29. temp.write(data)
  30. if outfile:
  31. f = open(outfile, 'w')
  32. f.write(temp.getvalue())
  33. f.close()
  34. if verbose: print 'Wrote %s' % outfile
  35. return temp
  36. def minify(self, in_files, outfile, concat=False):
  37. """
  38. Compress in_files into outfile,
  39. give some stats
  40. """
  41. from build import verbose
  42. import os
  43. # concat everything into temp
  44. outtype = outfile.split('.')[-1]
  45. temp = self.concat(in_files, is_js=True)
  46. out = open(outfile, 'w')
  47. org_size = len(temp.getvalue())
  48. temp.seek(0)
  49. # minify
  50. jsm = JavascriptMinify()
  51. jsm.minify(temp, out)
  52. out.close()
  53. new_size = os.path.getsize(outfile)
  54. if verbose:
  55. print '=> %s' % outfile
  56. print 'Original: %.2f kB' % (org_size / 1024.0)
  57. print 'Compressed: %.2f kB' % (new_size / 1024.0)
  58. print 'Reduction: %.1f%%' % (float(org_size - new_size) / org_size * 100)
  59. def make(self, bpath):
  60. """
  61. Build (stitch + compress) the file defined in build.json
  62. """
  63. import os, sys, json
  64. from build import no_minify
  65. # open the build.json file and read
  66. # the dict
  67. print "making %s ..." % bpath
  68. with open(bpath, 'r') as bfile:
  69. bdata = json.loads(bfile.read())
  70. path = os.path.dirname(bpath)
  71. for outfile in bdata:
  72. prefix, fname = False, outfile
  73. # check if there is a prefix
  74. if ':' in outfile:
  75. prefix, fname = outfile.split(':')
  76. # build the file list relative to the main folder
  77. fl = [os.path.relpath(os.path.join(path, f), os.curdir) for f in bdata[outfile]]
  78. # js files are minified by default unless explicitly
  79. # mentioned in the prefix.
  80. # some files may not work if minified (known jsmin bug)
  81. if fname.split('.')[-1]=='js' and prefix!='concat' and not no_minify:
  82. self.minify(fl, os.path.relpath(os.path.join(path, fname), os.curdir))
  83. else:
  84. self.concat(fl, os.path.relpath(os.path.join(path, fname), os.curdir))