build.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. var fs = require("fs");
  2. var path = require("path");
  3. var argparse = require( "argparse" );
  4. var uglify = require("uglify-js");
  5. var spawn = require('child_process').spawn;
  6. function main() {
  7. "use strict";
  8. var parser = new argparse.ArgumentParser();
  9. parser.addArgument( ['--include'], { action: 'append', required: true } );
  10. parser.addArgument( ['--externs'], { action: 'append', defaultValue: ['./externs/common.js'] } );
  11. parser.addArgument( ['--amd'], { action: 'storeTrue', defaultValue: false } );
  12. parser.addArgument( ['--minify'], { action: 'storeTrue', defaultValue: false } );
  13. parser.addArgument( ['--output'], { defaultValue: '../../build/three.js' } );
  14. parser.addArgument( ['--sourcemaps'], { action: 'storeTrue', defaultValue: true } );
  15. var args = parser.parseArgs();
  16. var output = args.output;
  17. console.log(' * Building ' + output);
  18. var sourcemap = '';
  19. var sourcemapping = '';
  20. if ( args.sourcemaps ){
  21. sourcemap = output + '.map';
  22. sourcemapping = '\n//# sourceMappingURL=three.min.js.map';
  23. }
  24. var buffer = [];
  25. var sources = []; // used for source maps with minification
  26. if ( args.amd ){
  27. buffer.push('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');
  28. };
  29. for ( var i = 0; i < args.include.length; i ++ ){
  30. var contents = fs.readFileSync( './includes/' + args.include[i] + '.json', 'utf8' );
  31. var files = JSON.parse( contents );
  32. for ( var j = 0; j < files.length; j ++ ){
  33. var file = '../../' + files[ j ];
  34. buffer.push('// File:' + files[ j ]);
  35. buffer.push('\n\n');
  36. contents = fs.readFileSync( file, 'utf8' );
  37. if( file.indexOf( '.glsl') >= 0 ) {
  38. contents = 'THREE.ShaderChunk[ \'' +
  39. path.basename( file, '.glsl' ) + '\' ] =' +
  40. JSON.stringify( contents ) + ';\n';
  41. }
  42. sources.push( { file: file, contents: contents } );
  43. buffer.push( contents );
  44. buffer.push( '\n' );
  45. }
  46. }
  47. if ( args.amd ){
  48. buffer.push('exports.THREE = THREE;\n\n} ) );');
  49. };
  50. var temp = buffer.join( '' );
  51. if ( !args.minify ){
  52. fs.writeFileSync( output, temp, 'utf8' );
  53. } else {
  54. var LICENSE = "threejs.org/license";
  55. // Parsing
  56. var toplevel = null;
  57. toplevel = uglify.parse( '// ' + LICENSE + '\n' );
  58. sources.forEach( function( source ) {
  59. toplevel = uglify.parse( source.contents, {
  60. filename: source.file,
  61. toplevel: toplevel
  62. } );
  63. } );
  64. // Compression
  65. toplevel.figure_out_scope();
  66. var compressor = uglify.Compressor( {} );
  67. var compressed_ast = toplevel.transform( compressor );
  68. // Mangling
  69. compressed_ast.figure_out_scope();
  70. compressed_ast.compute_char_frequency();
  71. compressed_ast.mangle_names();
  72. // Output
  73. var source_map_options = {
  74. file: 'three.min.js',
  75. root: 'src'
  76. };
  77. var source_map = uglify.SourceMap( source_map_options )
  78. var stream = uglify.OutputStream( {
  79. source_map: source_map,
  80. comments: new RegExp( LICENSE )
  81. } );
  82. compressed_ast.print( stream );
  83. var code = stream.toString();
  84. fs.writeFileSync( output, code + sourcemapping, 'utf8' );
  85. if ( args.sourcemaps ) {
  86. fs.writeFileSync( sourcemap, source_map.toString(), 'utf8' );
  87. }
  88. }
  89. }
  90. main();