build.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 shutil
  11. import tempfile
  12. def main(argv=None):
  13. parser = argparse.ArgumentParser()
  14. parser.add_argument('--include', action='append', required=True)
  15. parser.add_argument('--externs', action='append', default=['externs/common.js'])
  16. parser.add_argument('--amd', action='store_true', default=False)
  17. parser.add_argument('--minify', action='store_true', default=False)
  18. parser.add_argument('--output', default='../../build/three.js')
  19. parser.add_argument('--sourcemaps', action='store_true', default=False)
  20. args = parser.parse_args()
  21. output = args.output
  22. # merge
  23. print(' * Building ' + output)
  24. # enable sourcemaps support
  25. if args.sourcemaps:
  26. sourcemap = output + '.map'
  27. sourcemapping = '\n//@ sourceMappingURL=' + sourcemap
  28. sourcemapargs = ' --create_source_map ' + sourcemap + ' --source_map_format=V3'
  29. else:
  30. sourcemap = sourcemapping = sourcemapargs = ''
  31. fd, path = tempfile.mkstemp()
  32. tmp = open(path, 'w')
  33. sources = []
  34. if args.amd:
  35. tmp.write('( 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')
  36. for include in args.include:
  37. with open('includes/' + include + '.json','r') as f:
  38. files = json.load(f)
  39. for filename in files:
  40. tmp.write('// File:' + filename)
  41. tmp.write('\n\n')
  42. filename = '../../' + filename
  43. sources.append(filename)
  44. with open(filename, 'r') as f:
  45. if filename.endswith(".glsl"):
  46. tmp.write('THREE.ShaderChunk[ \'' + os.path.splitext(os.path.basename(filename))[0] + '\'] = "')
  47. tmp.write(f.read().replace('\n','\\n'))
  48. tmp.write('";\n\n')
  49. else:
  50. tmp.write(f.read())
  51. tmp.write('\n')
  52. if args.amd:
  53. tmp.write('exports.THREE = THREE;\n\n} ) );')
  54. tmp.close()
  55. # save
  56. if args.minify is False:
  57. shutil.copy(path, output)
  58. os.chmod(output, 0o664) # temp files would usually get 0600
  59. else:
  60. backup = ''
  61. if os.path.exists(output):
  62. with open(output,'r') as f: backup = f.read()
  63. os.remove(output)
  64. externs = ' --externs '.join(args.externs)
  65. source = ' '.join(sources)
  66. 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)
  67. os.system(cmd)
  68. # header
  69. if os.path.exists(output):
  70. with open(output,'r') as f: text = f.read()
  71. with open(output,'w') as f: f.write('// threejs.org/license\n' + text + sourcemapping)
  72. else:
  73. print("Minification with Closure compiler failed. Check your Java runtime version.")
  74. with open(output,'w') as f: f.write(backup)
  75. os.close(fd)
  76. os.remove(path)
  77. if __name__ == "__main__":
  78. script_dir = os.path.dirname(os.path.abspath(__file__))
  79. os.chdir(script_dir)
  80. main()