build.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 startMS = Date.now();
  19. var sourcemap = '';
  20. var sourcemapping = '';
  21. if ( args.sourcemaps ){
  22. sourcemap = output + '.map';
  23. sourcemapping = '\n//# sourceMappingURL=three.min.js.map';
  24. }
  25. var buffer = [];
  26. var sources = []; // used for source maps with minification
  27. if ( args.amd ){
  28. 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');
  29. };
  30. console.log( ' Collecting source files.' );
  31. for ( var i = 0; i < args.include.length; i ++ ){
  32. var contents = fs.readFileSync( './includes/' + args.include[i] + '.json', 'utf8' );
  33. var files = JSON.parse( contents );
  34. for ( var j = 0; j < files.length; j ++ ){
  35. var file = '../../' + files[ j ];
  36. buffer.push('// File:' + files[ j ]);
  37. buffer.push('\n\n');
  38. contents = fs.readFileSync( file, 'utf8' );
  39. if( file.indexOf( '.glsl') >= 0 ) {
  40. contents = 'THREE.ShaderChunk[ \'' +
  41. path.basename( file, '.glsl' ) + '\' ] =' +
  42. JSON.stringify( contents ) + ';\n';
  43. }
  44. sources.push( { file: file, contents: contents } );
  45. buffer.push( contents );
  46. buffer.push( '\n' );
  47. }
  48. }
  49. if ( args.amd ){
  50. buffer.push('exports.THREE = THREE;\n\n} ) );');
  51. };
  52. var temp = buffer.join( '' );
  53. if ( !args.minify ){
  54. console.log( ' Writing output.' );
  55. fs.writeFileSync( output, temp, 'utf8' );
  56. } else {
  57. console.log( ' Uglyifying.' );
  58. var LICENSE = "threejs.org/license";
  59. // Parsing
  60. var toplevel = null;
  61. toplevel = uglify.parse( '// ' + LICENSE + '\n' );
  62. sources.forEach( function( source ) {
  63. toplevel = uglify.parse( source.contents, {
  64. filename: source.file,
  65. toplevel: toplevel
  66. } );
  67. } );
  68. // Compression
  69. toplevel.figure_out_scope();
  70. var compressor = uglify.Compressor( {} );
  71. var compressed_ast = toplevel.transform( compressor );
  72. // Mangling
  73. compressed_ast.figure_out_scope();
  74. compressed_ast.compute_char_frequency();
  75. compressed_ast.mangle_names();
  76. // Output
  77. var source_map_options = {
  78. file: 'three.min.js',
  79. root: 'src'
  80. };
  81. var source_map = uglify.SourceMap( source_map_options )
  82. var stream = uglify.OutputStream( {
  83. source_map: source_map,
  84. comments: new RegExp( LICENSE )
  85. } );
  86. compressed_ast.print( stream );
  87. var code = stream.toString();
  88. console.log( ' Writing output.' );
  89. fs.writeFileSync( output, code + sourcemapping, 'utf8' );
  90. if ( args.sourcemaps ) {
  91. console.log( ' Writing source map.' );
  92. fs.writeFileSync( sourcemap, source_map.toString(), 'utf8' );
  93. }
  94. }
  95. var deltaMS = Date.now() - startMS;
  96. console.log( " --- Build time: " + ( deltaMS / 1000 ) + "s" );
  97. }
  98. main();