Procházet zdrojové kódy

Merge pull request #6226 from zz85/buildjs_sourcemaps

Make sourcemaps work with minified js builds with build.js
Mr.doob před 10 roky
rodič
revize
f1e96657af
1 změnil soubory, kde provedl 56 přidání a 17 odebrání
  1. 56 17
      utils/build/build.js

+ 56 - 17
utils/build/build.js

@@ -14,7 +14,7 @@ function main() {
 	parser.addArgument( ['--amd'], { action: 'storeTrue', defaultValue: false } );
 	parser.addArgument( ['--amd'], { action: 'storeTrue', defaultValue: false } );
 	parser.addArgument( ['--minify'], { action: 'storeTrue', defaultValue: false } );
 	parser.addArgument( ['--minify'], { action: 'storeTrue', defaultValue: false } );
 	parser.addArgument( ['--output'], { defaultValue: '../../build/three.js' } );
 	parser.addArgument( ['--output'], { defaultValue: '../../build/three.js' } );
-	parser.addArgument( ['--sourcemaps'], { action: 'storeTrue', defaultValue: false } );
+	parser.addArgument( ['--sourcemaps'], { action: 'storeTrue', defaultValue: true } );
 
 
 	
 	
 	var args = parser.parseArgs();
 	var args = parser.parseArgs();
@@ -28,13 +28,12 @@ function main() {
 	if ( args.sourcemaps ){
 	if ( args.sourcemaps ){
 
 
 		sourcemap = output + '.map';
 		sourcemap = output + '.map';
-		sourcemapping = '\n//@ sourceMappingURL=' + sourcemap;
+		sourcemapping = '\n//# sourceMappingURL=three.min.js.map';
 
 
 	}
 	}
 
 
 	var buffer = [];
 	var buffer = [];
-	var sources = [];
-	// TODO - sources are not being used. should remove and make sourcemaps work with uglify
+	var sources = []; // used for source maps with minification
 
 
 	if ( args.amd ){
 	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('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');
@@ -56,18 +55,15 @@ function main() {
 
 
 			if( file.indexOf( '.glsl') >= 0 ) {
 			if( file.indexOf( '.glsl') >= 0 ) {
 
 
-				buffer.push( 'THREE.ShaderChunk[ \'' + path.basename( file, '.glsl' ) + '\' ] =')
-				buffer.push( JSON.stringify( contents ) );
-				buffer.push( ';\n\n' );
-
-			} else {
-
-				sources.push( file );
-				buffer.push( contents );
-				buffer.push( '\n' );
+				contents = 'THREE.ShaderChunk[ \'' +
+					path.basename( file, '.glsl' ) + '\' ] =' +
+					JSON.stringify( contents ) + ';\n';
 
 
 			}
 			}
 
 
+			sources.push( { file: file, contents: contents } );
+			buffer.push( contents );
+			buffer.push( '\n' );
 		}
 		}
 
 
 	}
 	}
@@ -84,13 +80,56 @@ function main() {
 
 
 	} else {
 	} else {
 
 
-		var result = uglify.minify( temp, { outSourceMap: sourcemap, fromString: true } );
-		
-		fs.writeFileSync( output, '// threejs.org/license\n' + result.code + sourcemapping, 'utf8' );
+		var LICENSE = "threejs.org/license";
+
+		// Parsing
+
+		var toplevel = null;
+
+		toplevel = uglify.parse( '// ' + LICENSE + '\n' );
+
+		sources.forEach( function( source ) {
+
+			toplevel = uglify.parse( source.contents, {
+				filename: source.file,
+				toplevel: toplevel
+			} );
+
+		} );
+
+		// Compression
+
+		toplevel.figure_out_scope();
+		var compressor = uglify.Compressor( {} );
+		var compressed_ast = toplevel.transform( compressor );
+
+		// Mangling
+
+		compressed_ast.figure_out_scope();
+		compressed_ast.compute_char_frequency();
+		compressed_ast.mangle_names();
+
+		// Output
+
+		var source_map_options = {
+			file: 'three.min.js',
+			root: 'src'
+		};
+
+		var source_map = uglify.SourceMap( source_map_options )
+		var stream = uglify.OutputStream( {
+			source_map: source_map,
+			comments: new RegExp( LICENSE )
+		} );
+
+		compressed_ast.print( stream );
+		var code = stream.toString();
+
+		fs.writeFileSync( output, code + sourcemapping, 'utf8' );
 
 
 		if ( args.sourcemaps ) {
 		if ( args.sourcemaps ) {
 
 
-			fs.writeFileSync( sourcemap, result.map, 'utf8' );
+			fs.writeFileSync( sourcemap, source_map.toString(), 'utf8' );
 
 
 		}
 		}