You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bundle.py 2.0 KiB

14 jaren geleden
14 jaren geleden
14 jaren geleden
14 jaren geleden
13 jaren geleden
14 jaren geleden
14 jaren geleden
14 jaren geleden
14 jaren geleden
14 jaren geleden
14 jaren geleden
13 jaren geleden
14 jaren geleden
14 jaren geleden
14 jaren geleden
14 jaren geleden
13 jaren geleden
14 jaren geleden
13 jaren geleden
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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):
  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. outtxt = ''
  15. for f in filelist:
  16. suffix = None
  17. if ':' in f:
  18. f, suffix = f.split(':')
  19. # print f + ' | ' + str(int(os.path.getsize(f)/1024)) + 'k'
  20. # get data
  21. with open(f, 'r') as infile:
  22. # get file type
  23. ftype = f.split('.')[-1]
  24. data = infile.read()
  25. # css -> js
  26. if out_type=='js' and ftype =='css':
  27. data = "\nwn.assets.handler.css('%s');\n" %\
  28. data.replace("'", "\\'").replace('\n', '\\\n')
  29. outtxt += ('\n/*\n *\t%s\n */' % f)
  30. # append
  31. if suffix=='concat' or out_type != 'js':
  32. outtxt += data
  33. else:
  34. jsm = JavascriptMinify()
  35. tmpin = StringIO(data)
  36. tmpout = StringIO()
  37. jsm.minify(tmpin, tmpout)
  38. tmpmin = tmpout.getvalue() or ''
  39. tmpmin.strip('\n')
  40. outtxt += tmpmin
  41. with open(outfile, 'w') as f:
  42. f.write(outtxt)
  43. print "Wrote %s - %sk" % (outfile, str(int(os.path.getsize(outfile)/1024)))
  44. def make(self, bpath):
  45. """
  46. Build (stitch + compress) the file defined in build.json
  47. """
  48. import os, sys, json
  49. from build import no_minify
  50. # open the build.json file and read
  51. # the dict
  52. print "making %s ..." % bpath
  53. with open(bpath, 'r') as bfile:
  54. bdata = json.loads(bfile.read())
  55. path = os.path.dirname(bpath)
  56. for buildfile in bdata:
  57. # build the file list relative to the main folder
  58. outfile = buildfile.keys()[0]
  59. infiles = buildfile[outfile]
  60. fl = [os.path.relpath(os.path.join(path, f), os.curdir) for f in infiles]
  61. # js files are minified by default unless explicitly
  62. # mentioned in the prefix.
  63. # some files may not work if minified (known jsmin bug)
  64. self.concat(fl, os.path.relpath(os.path.join(path, outfile), os.curdir))