25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

128 lines
3.7 KiB

  1. # Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. #
  3. # MIT License (MIT)
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a
  6. # copy of this software and associated documentation files (the "Software"),
  7. # to deal in the Software without restriction, including without limitation
  8. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. # and/or sell copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #
  22. from __future__ import unicode_literals
  23. from minify import JavascriptMinify
  24. class Bundle:
  25. """
  26. Concatenate, compress and mix (if required) js+css files from build.json
  27. """
  28. def concat(self, filelist, outfile=None):
  29. """
  30. Concat css and js files into a bundle
  31. """
  32. import os
  33. from cStringIO import StringIO
  34. from build import verbose
  35. out_type = outfile and outfile.split('.')[-1] or 'js'
  36. outtxt = ''
  37. for f in filelist:
  38. suffix = None
  39. if ':' in f:
  40. f, suffix = f.split(':')
  41. # print f + ' | ' + str(int(os.path.getsize(f)/1024)) + 'k'
  42. # get data
  43. with open(f, 'r') as infile:
  44. # get file type
  45. ftype = f.split('.')[-1]
  46. data = infile.read()
  47. # css -> js
  48. if out_type=='js' and ftype =='css':
  49. data = "\nwn.assets.handler.css('%s');\n" %\
  50. data.replace("'", "\\'").replace('\n', '\\\n')
  51. outtxt += ('\n/*\n *\t%s\n */' % f)
  52. # append
  53. if suffix=='concat' or out_type != 'js':
  54. outtxt += '\n' + data + '\n'
  55. else:
  56. jsm = JavascriptMinify()
  57. tmpin = StringIO(data)
  58. tmpout = StringIO()
  59. jsm.minify(tmpin, tmpout)
  60. tmpmin = tmpout.getvalue() or ''
  61. tmpmin.strip('\n')
  62. outtxt += tmpmin
  63. with open(outfile, 'w') as f:
  64. f.write(outtxt)
  65. print "Wrote %s - %sk" % (outfile, str(int(os.path.getsize(outfile)/1024)))
  66. def make(self):
  67. """
  68. Build (stitch + compress) the file defined in build.json
  69. """
  70. import os, sys
  71. from build import no_minify
  72. # open the build.json file and read
  73. # the dict
  74. print "Building js and css files..."
  75. # framework js and css files
  76. with open('lib/build.json', 'r') as bfile:
  77. bdata = eval(bfile.read())
  78. # app js and css files
  79. if os.path.exists('build.json'):
  80. with open('build.json', 'r') as bfile:
  81. appfiles = eval(bfile.read())
  82. else:
  83. appfiles = {}
  84. path = '.'
  85. # add additional app files in bdata
  86. buildfile_list = [buildfile.keys()[0] for buildfile in bdata]
  87. for f in appfiles:
  88. if f not in buildfile_list:
  89. bdata.append({f: appfiles[f]})
  90. for buildfile in bdata:
  91. # build the file list relative to the main folder
  92. outfile = buildfile.keys()[0]
  93. infiles = buildfile[outfile]
  94. # add app js and css to the list
  95. if outfile in appfiles:
  96. for f in appfiles[outfile]:
  97. if f not in infiles:
  98. infiles.append(f)
  99. fl = [os.path.relpath(os.path.join(path, f), os.curdir) for f in infiles]
  100. # js files are minified by default unless explicitly
  101. # mentioned in the prefix.
  102. # some files may not work if minified (known jsmin bug)
  103. self.concat(fl, os.path.relpath(os.path.join(path, outfile), os.curdir))