Selaa lähdekoodia

build.js ux improvements (#9237)

* build.js ux improvements

- script can be run from any directory, uses script dir to determine
  relative paths to other needed files
- by default, determine minified file name from `output` file name (e.g.
  three.js -> three.min.js). minified file name can be explicitly set
  directly via a new option `minifiedoutput`
- if `minify` is true, still build un-minified file
- ensure sourcemap url refers to actual minified file name, (before,
  three.min.js was hard-coded)

* updating npm scripts with new build script usage
Abel Allison 9 vuotta sitten
vanhempi
commit
cb71bd01e0
2 muutettua tiedostoa jossa 69 lisäystä ja 32 poistoa
  1. 3 2
      package.json
  2. 66 30
      utils/build/build.js

+ 3 - 2
package.json

@@ -20,7 +20,8 @@
     "test": "test"
   },
   "scripts": {
-    "build": "cd ./utils/build && node build.js --include common --include extras",
+    "build": "node utils/build/build.js --include common --include extras",
+    "build-min": "node utils/build/build.js --include common --include extras --minify",
     "dev": "chokidar \"./src/**/*.*\" \"./utils/build/includes/*.*\" --initial -c \"npm run build\"",
     "test": "echo \"Error: no test specified\" && exit 1"
   },
@@ -45,5 +46,5 @@
     "chokidar-cli": "1.2.0",
     "jscs": "^1.13.1",
     "uglify-js": "^2.6.0"
-    }
+  }
 }

+ 66 - 30
utils/build/build.js

@@ -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' );
 
 		}