|
@@ -4,34 +4,67 @@ var argparse = require( "argparse" );
|
|
|
var uglify = require("uglify-js");
|
|
|
var spawn = require('child_process').spawn;
|
|
|
|
|
|
+var scriptDir = __dirname;
|
|
|
+var baseDir = path.resolve( scriptDir, '../..' );
|
|
|
+var includesDir = path.resolve( scriptDir, 'includes' );
|
|
|
+var externsDir = path.resolve( scriptDir, 'externs' );
|
|
|
+var compilerDir = path.resolve( scriptDir, 'compiler' );
|
|
|
+var defaultBuildDir = path.resolve( baseDir, 'build' );
|
|
|
+
|
|
|
+var amdPrefix = [
|
|
|
+ 'function ( root, factory ) {',
|
|
|
+ '\tif ( typeof define === \'function\' && define.amd ) {',
|
|
|
+ '\t\tdefine( [ \'exports\' ], factory );',
|
|
|
+ '\t} else if ( typeof exports === \'object\' ) {',
|
|
|
+ '\t\tfactory( exports );',
|
|
|
+ '\t} else {',
|
|
|
+ '\t\tfactory( root );',
|
|
|
+ '\t}',
|
|
|
+ '}( this, function ( exports ) {',
|
|
|
+ ''
|
|
|
+].join('\n\n');
|
|
|
+var amdSuffix = 'exports.THREE = THREE;\n\n} ) );';
|
|
|
+
|
|
|
+function getMinifiedOutputPath( outputPath ) {
|
|
|
+
|
|
|
+ var dir = path.dirname( outputPath );
|
|
|
+ var ext = path.extname( outputPath );
|
|
|
+ var basename = path.basename( outputPath, ext );
|
|
|
+
|
|
|
+ var result = path.join( dir, basename + '.min' + ext );
|
|
|
+ return result;
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
function main() {
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
var parser = new argparse.ArgumentParser();
|
|
|
parser.addArgument( ['--include'], { action: 'append', required: true } );
|
|
|
- parser.addArgument( ['--externs'], { action: 'append', defaultValue: ['./externs/common.js'] } );
|
|
|
+ parser.addArgument( ['--externs'], { action: 'append', defaultValue: [ path.resolve( externsDir, 'common.js' ) ] } );
|
|
|
parser.addArgument( ['--amd'], { action: 'storeTrue', defaultValue: false } );
|
|
|
parser.addArgument( ['--minify'], { action: 'storeTrue', defaultValue: false } );
|
|
|
- parser.addArgument( ['--output'], { defaultValue: '../../build/three.js' } );
|
|
|
+ parser.addArgument( ['--output'], { defaultValue: path.resolve( defaultBuildDir, 'three.js' ) } );
|
|
|
+ parser.addArgument( ['--minifiedoutput'] );
|
|
|
parser.addArgument( ['--sourcemaps'], { action: 'storeTrue', defaultValue: true } );
|
|
|
|
|
|
-
|
|
|
var args = parser.parseArgs();
|
|
|
-
|
|
|
- var output = args.output;
|
|
|
|
|
|
- console.log('Building ' + output + ':');
|
|
|
-
|
|
|
+ var outputPath = args.output;
|
|
|
+ var minifiedOutputPath = args.minifiedoutput ? args.minifiedoutput : getMinifiedOutputPath( outputPath );
|
|
|
+
|
|
|
+ console.log('Building ' + outputPath + ':');
|
|
|
+
|
|
|
var startMS = Date.now();
|
|
|
|
|
|
- var sourcemap = '';
|
|
|
+ var sourcemapPath = '';
|
|
|
var sourcemapping = '';
|
|
|
|
|
|
if ( args.sourcemaps ){
|
|
|
|
|
|
- sourcemap = output + '.map';
|
|
|
- sourcemapping = '\n//# sourceMappingURL=three.min.js.map';
|
|
|
+ sourcemapPath = minifiedOutputPath + '.map';
|
|
|
+ sourcemapping = '\n//# sourceMappingURL=' + path.basename( minifiedOutputPath ) + '.map';
|
|
|
|
|
|
}
|
|
|
|
|
@@ -39,20 +72,23 @@ function main() {
|
|
|
var sources = []; // used for source maps with minification
|
|
|
|
|
|
if ( args.amd ){
|
|
|
- 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');
|
|
|
+
|
|
|
+ buffer.push( amdPrefix );
|
|
|
+
|
|
|
};
|
|
|
-
|
|
|
+
|
|
|
console.log( ' Collecting source files.' );
|
|
|
|
|
|
for ( var i = 0; i < args.include.length; i ++ ){
|
|
|
-
|
|
|
- var contents = fs.readFileSync( './includes/' + args.include[i] + '.json', 'utf8' );
|
|
|
+
|
|
|
+ var includeFile = args.include[i] + '.json';
|
|
|
+ var contents = fs.readFileSync( path.resolve( includesDir, includeFile ), 'utf8' );
|
|
|
var files = JSON.parse( contents );
|
|
|
|
|
|
for ( var j = 0; j < files.length; j ++ ){
|
|
|
|
|
|
- var file = '../../' + files[ j ];
|
|
|
-
|
|
|
+ var file = path.resolve( baseDir, files[ j ] );
|
|
|
+
|
|
|
buffer.push('// File:' + files[ j ]);
|
|
|
buffer.push('\n\n');
|
|
|
|
|
@@ -72,20 +108,20 @@ function main() {
|
|
|
}
|
|
|
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
if ( args.amd ){
|
|
|
- buffer.push('exports.THREE = THREE;\n\n} ) );');
|
|
|
+
|
|
|
+ buffer.push( amdSuffix );
|
|
|
+
|
|
|
};
|
|
|
-
|
|
|
- var temp = buffer.join( '' );
|
|
|
-
|
|
|
- if ( !args.minify ){
|
|
|
|
|
|
- console.log( ' Writing output.' );
|
|
|
+ var temp = buffer.join( '' );
|
|
|
|
|
|
- fs.writeFileSync( output, temp, 'utf8' );
|
|
|
+ // Write un-minified output
|
|
|
+ console.log( ' Writing un-minified output: ' + outputPath );
|
|
|
+ fs.writeFileSync( outputPath, temp, 'utf8' );
|
|
|
|
|
|
- } else {
|
|
|
+ if ( args.minify ) {
|
|
|
|
|
|
console.log( ' Uglyifying.' );
|
|
|
|
|
@@ -118,10 +154,10 @@ function main() {
|
|
|
compressed_ast.compute_char_frequency();
|
|
|
compressed_ast.mangle_names();
|
|
|
|
|
|
- // Output
|
|
|
+ // output file
|
|
|
|
|
|
var source_map_options = {
|
|
|
- file: 'three.min.js',
|
|
|
+ file: path.basename(minifiedOutputPath),
|
|
|
root: 'src'
|
|
|
};
|
|
|
|
|
@@ -134,15 +170,15 @@ function main() {
|
|
|
compressed_ast.print( stream );
|
|
|
var code = stream.toString();
|
|
|
|
|
|
- console.log( ' Writing output.' );
|
|
|
+ console.log( ' Writing minified output: ' + minifiedOutputPath );
|
|
|
|
|
|
- fs.writeFileSync( output, code + sourcemapping, 'utf8' );
|
|
|
+ fs.writeFileSync( minifiedOutputPath, code + sourcemapping, 'utf8' );
|
|
|
|
|
|
if ( args.sourcemaps ) {
|
|
|
|
|
|
console.log( ' Writing source map.' );
|
|
|
|
|
|
- fs.writeFileSync( sourcemap, source_map.toString(), 'utf8' );
|
|
|
+ fs.writeFileSync( sourcemapPath, source_map.toString(), 'utf8' );
|
|
|
|
|
|
}
|
|
|
|