@@ -6,6 +6,7 @@ var parse = require("./parse");
66var resolve = require ( "./resolve" ) ;
77var fs = require ( "fs" ) ;
88var path = require ( "path" ) ;
9+ var assert = require ( "assert" ) ;
910
1011/**
1112 * context: current directory
@@ -31,11 +32,11 @@ module.exports = function buildDeps(context, mainModule, options, callback) {
3132 nextChunkId : 0 ,
3233 chunkModules : { } // used by checkObsolete
3334 }
35+ options . events . emit ( "task" , "build modules" ) ;
36+ options . events . emit ( "task" , "build chunks" ) ;
37+ options . events . emit ( "task" , "optimize" ) ;
38+ options . events . emit ( "task" , "cleanup" ) ;
3439 var mainModuleId ;
35- options . events . emit ( "task" ) ;
36- options . events . emit ( "task" ) ;
37- options . events . emit ( "task" ) ;
38- options . events . emit ( "task" ) ;
3940 addModule ( depTree , context , mainModule , options , { type : "main" } , function ( err , id ) {
4041 if ( err ) {
4142 callback ( err ) ;
@@ -45,18 +46,18 @@ module.exports = function buildDeps(context, mainModule, options, callback) {
4546 buildTree ( ) ;
4647 } ) ;
4748 function buildTree ( ) {
48- options . events . emit ( "task-end" ) ;
49+ options . events . emit ( "task-end" , "build modules" ) ;
4950
5051 addChunk ( depTree , depTree . modulesById [ mainModuleId ] , options ) ;
5152 createRealIds ( depTree , options ) ;
52- options . events . emit ( "task-end" ) ;
53+ options . events . emit ( "task-end" , "build chunks" ) ;
5354
5455 for ( var chunkId in depTree . chunks ) {
5556 removeParentsModules ( depTree , depTree . chunks [ chunkId ] ) ;
5657 removeChunkIfEmpty ( depTree , depTree . chunks [ chunkId ] ) ;
5758 checkObsolete ( depTree , depTree . chunks [ chunkId ] ) ;
5859 }
59- options . events . emit ( "task-end" ) ;
60+ options . events . emit ( "task-end" , "optimize" ) ;
6061
6162 // cleanup
6263 delete depTree . chunkModules ;
@@ -66,7 +67,7 @@ module.exports = function buildDeps(context, mainModule, options, callback) {
6667 delete depTree . nextModuleId ;
6768 delete depTree . nextChunkId ;
6869 // return
69- options . events . emit ( "task-end" ) ;
70+ options . events . emit ( "task-end" , "cleanup" ) ;
7071 callback ( null , depTree ) ;
7172 }
7273}
@@ -93,8 +94,9 @@ function execLoaders(request, loaders, filenames, contents, options, callback) {
9394 return ;
9495 }
9596 if ( loaderFunctions . length > 0 ) {
97+ var async = false ;
98+ var done = false ;
9699 try {
97- var async = false ;
98100 var context = {
99101 request : request ,
100102 filenames : filenames ,
@@ -114,6 +116,8 @@ function execLoaders(request, loaders, filenames, contents, options, callback) {
114116 } ,
115117 callback : function ( ) {
116118 async = true ;
119+ assert ( ! done ) ;
120+ done = true ;
117121 nextLoader . apply ( null , arguments ) ;
118122 } ,
119123 web : true ,
@@ -123,10 +127,18 @@ function execLoaders(request, loaders, filenames, contents, options, callback) {
123127 options : options
124128 } ;
125129 var retVal = loaderFunctions . pop ( ) . apply ( context , args ) ;
126- if ( ! async )
130+ if ( ! async ) {
131+ done = true ;
127132 nextLoader ( retVal === undefined ? new Error ( "loader did not return a value" ) : null , retVal ) ;
133+ }
128134 } catch ( e ) {
129- callback ( "Loader throwed exeception: " + e ) ;
135+ if ( ! done ) {
136+ done = true ;
137+ callback ( "Loader throwed exeception: " + e ) ;
138+ } else {
139+ if ( e . stack ) console . error ( e . stack ) ;
140+ else console . error ( e ) ;
141+ }
130142 return ;
131143 }
132144 } else {
@@ -270,6 +282,7 @@ function addModule(depTree, context, modu, options, reason, finalCallback) {
270282 endOne ( ) ;
271283 function endOne ( ) {
272284 count -- ;
285+ assert ( count >= 0 ) ;
273286 if ( count === 0 ) {
274287 if ( errors . length ) {
275288 callback ( errors . join ( "\n" ) ) ;
@@ -328,6 +341,7 @@ function addContextModule(depTree, context, contextModuleName, options, reason,
328341 errors . push ( err ) ;
329342 }
330343 count -- ;
344+ assert ( count >= 0 ) ;
331345 if ( count == 0 ) {
332346 if ( errors . length > 0 )
333347 done ( errors . join ( "\n" ) ) ;
0 commit comments