build.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/usr/bin/env python
  2. import sys
  3. if sys.version_info < (2, 7):
  4. print("This script requires at least Python 2.7.")
  5. print("Please, update to a newer version: http://www.python.org/download/releases/")
  6. # exit()
  7. import argparse
  8. import json
  9. import os
  10. import re
  11. import shutil
  12. import tempfile
  13. from io import open
  14. def main(argv=None):
  15. parser = argparse.ArgumentParser()
  16. parser.add_argument('--include', action='append', required=True)
  17. parser.add_argument('--externs', action='append', default=['externs/common.js'])
  18. parser.add_argument('--amd', action='store_true', default=False)
  19. parser.add_argument('--minify', action='store_true', default=False)
  20. parser.add_argument('--output', default='../../build/three.js')
  21. parser.add_argument('--sourcemaps', action='store_true', default=False)
  22. args = parser.parse_args()
  23. output = args.output
  24. # merge
  25. print(' * Building ' + output)
  26. # enable sourcemaps support
  27. if args.sourcemaps:
  28. sourcemap = output + '.map'
  29. sourcemapping = '\n//@ sourceMappingURL=' + sourcemap
  30. sourcemapargs = ' --create_source_map ' + sourcemap + ' --source_map_format=V3'
  31. else:
  32. sourcemap = sourcemapping = sourcemapargs = ''
  33. fd, path = tempfile.mkstemp()
  34. tmp = open(path, 'w', encoding='utf-8')
  35. sources = []
  36. if args.amd:
  37. tmp.write(u'( function ( root, factory ) {\n\n\tif ( typeof define === \'function\' && define.amd ) {\n\n\t\tdefine( [ \'exports\' ], factory );\n\n\t} else if ( typeof exports === \'object\' ) {\n\n\t\tfactory( exports );\n\n\t} else {\n\n\t\tfactory( root );\n\n\t}\n\n}( this, function ( exports ) {\n\n')
  38. for include in args.include:
  39. with open('includes/' + include + '.json','r', encoding='utf-8') as f:
  40. files = json.load(f)
  41. for filename in files:
  42. tmp.write(u'// File:' + filename)
  43. tmp.write(u'\n\n')
  44. filename = '../../' + filename
  45. sources.append(filename)
  46. with open(filename, 'r', encoding='utf-8') as f:
  47. if filename.endswith(".glsl"):
  48. tmp.write(u'THREE.ShaderChunk[ \'' + os.path.splitext(os.path.basename(filename))[0] + u'\'] = "')
  49. text = f.read()
  50. text = re.sub(r"\t*//.*\n", "", text) # strip comments
  51. text = text.replace('\n','\\n') # line breaks to \n
  52. tmp.write(text)
  53. tmp.write(u'";\n\n')
  54. else:
  55. tmp.write(f.read())
  56. tmp.write(u'\n')
  57. if args.amd:
  58. tmp.write(u'exports.THREE = THREE;\n\n} ) );')
  59. tmp.close()
  60. # save
  61. if args.minify is False:
  62. shutil.copy(path, output)
  63. os.chmod(output, 0o664) # temp files would usually get 0600
  64. else:
  65. backup = ''
  66. if os.path.exists(output):
  67. with open(output, 'r', encoding='utf-8') as f: backup = f.read()
  68. os.remove(output)
  69. externs = ' --externs '.join(args.externs)
  70. source = ' '.join(sources)
  71. cmd = 'java -jar compiler/compiler.jar --warning_level=VERBOSE --jscomp_off=globalThis --externs %s --jscomp_off=checkTypes --language_in=ECMASCRIPT5_STRICT --js %s --js_output_file %s %s' % (externs, path, output, sourcemapargs)
  72. os.system(cmd)
  73. # header
  74. if os.path.exists(output):
  75. with open(output, 'r', encoding='utf-8') as f: text = f.read()
  76. with open(output, 'w', encoding='utf-8') as f: f.write('// threejs.org/license\n' + text + sourcemapping)
  77. else:
  78. print("Minification with Closure compiler failed. Check your Java runtime version.")
  79. with open(output, 'w', encoding='utf-8') as f: f.write(backup)
  80. os.close(fd)
  81. os.remove(path)
  82. if __name__ == "__main__":
  83. script_dir = os.path.dirname(os.path.abspath(__file__))
  84. os.chdir(script_dir)
  85. main()