@@ -17,7 +17,9 @@ function getWebpack() {
17
17
}
18
18
19
19
function getAppName ( webpackConfig ) {
20
- var appName = webpackConfig . name || webpackConfig . output . filename ;
20
+ var appName = webpackConfig . name
21
+ || webpackConfig . output && webpackConfig . output . filename
22
+ || String ( process . pid ) ;
21
23
if ( ~ appName . indexOf ( '[name]' ) && typeof webpackConfig . entry === 'object' ) {
22
24
var entryNames = Object . keys ( webpackConfig . entry ) ;
23
25
if ( entryNames . length === 1 ) {
@@ -92,78 +94,23 @@ module.exports = function(configuratorFileName, options, index, expectedConfigLe
92
94
} else {
93
95
webpackConfig = config
94
96
}
95
- var watcher ,
96
- webpack = getWebpack ( ) ,
97
- hasCompletedOneCompile = false ,
98
- outputOptions = getOutputOptions ( webpackConfig , options ) ,
99
- shutdownCallback = function ( ) {
100
- if ( watcher ) {
101
- watcher . close ( done ) ;
102
- }
103
- done ( {
104
- message : chalk . red ( '[WEBPACK]' ) + ' Forcefully shut down ' + chalk . yellow ( getAppName ( webpackConfig ) )
105
- } ) ;
106
- process . exit ( 0 ) ;
107
- } ,
108
- exitCallback = function ( code ) {
109
- if ( code === 0 ) {
110
- return ;
111
- }
112
- if ( watcher ) {
113
- watcher . close ( done ) ;
114
- }
115
- done ( {
116
- message : chalk . red ( '[WEBPACK]' )
117
- + ' Exit ' + chalk . yellow ( getAppName ( webpackConfig ) )
118
- + ' with code ' + code
119
- } ) ;
120
- } ,
121
- finishedCallback = function ( err , stats ) {
122
- if ( err ) {
123
- console . error ( '%s fatal error occured' , chalk . red ( '[WEBPACK]' ) ) ;
124
- console . error ( err ) ;
125
- process . removeListener ( 'SIGINT' , shutdownCallback ) ;
126
- process . removeListener ( 'exit' , exitCallback ) ;
127
- return done ( err ) ;
128
- }
129
- if ( stats . compilation . errors && stats . compilation . errors . length ) {
130
- var message = chalk . red ( '[WEBPACK]' ) + ' Errors building ' + chalk . yellow ( getAppName ( webpackConfig ) ) + "\n"
131
- + stats . compilation . errors . map ( function ( error ) {
132
- return error . message ;
133
- } ) . join ( "\n" ) ;
134
- if ( watch ) {
135
- console . log ( message ) ;
136
- } else {
137
- process . removeListener ( 'SIGINT' , shutdownCallback ) ;
138
- process . removeListener ( 'exit' , exitCallback ) ;
139
- return done ( {
140
- message : message ,
141
- stats : JSON . stringify ( stats . toJson ( outputOptions ) , null , 2 )
142
- } ) ;
143
- }
144
- }
145
- if ( ! silent ) {
146
- if ( options . stats ) {
147
- console . log ( stats . toString ( outputOptions ) ) ;
148
- }
149
- var timeStamp = watch
150
- ? ' ' + chalk . yellow ( new Date ( ) . toTimeString ( ) . split ( / + / ) [ 0 ] )
151
- : '' ;
152
- console . log ( '%s Finished building %s within %s seconds' , chalk . blue ( '[WEBPACK' + timeStamp + ']' ) , chalk . yellow ( getAppName ( webpackConfig ) ) , chalk . blue ( ( stats . endTime - stats . startTime ) / 1000 ) ) ;
153
- }
154
- if ( ! watch ) {
155
- process . removeListener ( 'SIGINT' , shutdownCallback ) ;
156
- process . removeListener ( 'exit' , exitCallback ) ;
157
- done ( null , options . stats ? JSON . stringify ( stats . toJson ( outputOptions ) , null , 2 ) : '' ) ;
158
- } else if ( ! hasCompletedOneCompile ) {
159
- notifyIPCWatchCompileDone ( index ) ;
160
- hasCompletedOneCompile = true ;
161
- }
162
- } ;
97
+
98
+ var MSG_ERROR = chalk . red ( '[WEBPACK]' ) ;
99
+ var MSG_SUCCESS = chalk . blue ( '[WEBPACK]' ) ;
100
+ var MSG_APP = chalk . yellow ( getAppName ( webpackConfig ) ) ;
101
+
102
+ var watcher ;
103
+ var webpack = getWebpack ( ) ;
104
+ var hasCompletedOneCompile = false ;
105
+ var outputOptions = getOutputOptions ( webpackConfig , options ) ;
106
+ var disconnected = false ;
107
+
163
108
if ( ! silent ) {
164
- console . log ( '%s Started %s %s' , chalk . blue ( '[WEBPACK]' ) , watch ? 'watching' : 'building' , chalk . yellow ( getAppName ( webpackConfig ) ) ) ;
109
+ console . log ( '%s Started %s %s' , MSG_SUCCESS , watch ? 'watching' : 'building' , MSG_APP ) ;
165
110
}
111
+
166
112
var compiler = webpack ( webpackConfig ) ;
113
+
167
114
if ( watch || webpack . watch ) {
168
115
watcher = compiler . watch ( webpackConfig . watchOptions , finishedCallback ) ;
169
116
} else {
@@ -172,5 +119,94 @@ module.exports = function(configuratorFileName, options, index, expectedConfigLe
172
119
173
120
process . on ( 'SIGINT' , shutdownCallback ) ;
174
121
process . on ( 'exit' , exitCallback ) ;
122
+ process . on ( 'unhandledRejection' , unhandledRejectionCallback ) ;
123
+ process . on ( 'disconnect' , disconnectCallback ) ;
124
+
125
+ function cleanup ( ) {
126
+ process . removeListener ( 'SIGINT' , shutdownCallback ) ;
127
+ process . removeListener ( 'exit' , exitCallback ) ;
128
+ process . removeListener ( 'unhandledRejection' , unhandledRejectionCallback ) ;
129
+ process . removeListener ( 'disconnect' , disconnectCallback ) ;
130
+ }
131
+
132
+ function shutdownCallback ( ) {
133
+ if ( watcher ) {
134
+ watcher . close ( done ) ;
135
+ }
136
+ done ( {
137
+ message : MSG_ERROR + ' Forcefully shut down ' + MSG_APP
138
+ } ) ;
139
+ process . exit ( 0 ) ;
140
+ }
141
+
142
+ function unhandledRejectionCallback ( error ) {
143
+ console . log ( MSG_ERROR + 'Build child process error:' , error ) ;
144
+ process . exit ( 1 ) ;
145
+ }
146
+
147
+ function exitCallback ( code ) {
148
+ cleanup ( ) ;
149
+ if ( code === 0 ) {
150
+ return ;
151
+ }
152
+ if ( watcher ) {
153
+ watcher . close ( done ) ;
154
+ }
155
+ done ( {
156
+ message : MSG_ERROR + ' Exit ' + MSG_APP + ' with code ' + code
157
+ } ) ;
158
+ }
159
+
160
+ function disconnectCallback ( ) {
161
+ disconnected = true ;
162
+ console . log ( '%s Parent process terminated, exit building %s' , MSG_ERROR , MSG_APP ) ;
163
+ process . exit ( 1 ) ;
164
+ }
165
+
166
+ function finishedCallback ( err , stats ) {
167
+ if ( err ) {
168
+ console . error ( '%s fatal error occured' , MSG_ERROR ) ;
169
+ console . error ( err ) ;
170
+ cleanup ( ) ;
171
+ return done ( err ) ;
172
+ }
173
+ if ( stats . compilation . errors && stats . compilation . errors . length ) {
174
+ var message = MSG_ERROR + ' Errors building ' + MSG_APP + "\n"
175
+ + stats . compilation . errors . map ( function ( error ) {
176
+ return error . message ;
177
+ } ) . join ( "\n" ) ;
178
+ if ( watch ) {
179
+ console . log ( message ) ;
180
+ } else {
181
+ cleanup ( ) ;
182
+ if ( disconnected ) {
183
+ return ;
184
+ }
185
+ return done ( {
186
+ message : message ,
187
+ stats : JSON . stringify ( stats . toJson ( outputOptions ) , null , 2 )
188
+ } ) ;
189
+ }
190
+ }
191
+ if ( ! silent ) {
192
+ if ( options . stats ) {
193
+ console . log ( stats . toString ( outputOptions ) ) ;
194
+ }
195
+ var timeStamp = watch
196
+ ? ' ' + chalk . yellow ( new Date ( ) . toTimeString ( ) . split ( / + / ) [ 0 ] )
197
+ : '' ;
198
+ console . log ( '%s Finished building %s within %s seconds' , chalk . blue ( '[WEBPACK' + timeStamp + ']' ) , MSG_APP , chalk . blue ( ( stats . endTime - stats . startTime ) / 1000 ) ) ;
199
+ }
200
+ if ( ! watch ) {
201
+ cleanup ( ) ;
202
+ if ( disconnected ) {
203
+ return ;
204
+ }
205
+ done ( null , options . stats ? JSON . stringify ( stats . toJson ( outputOptions ) , null , 2 ) : '' ) ;
206
+ } else if ( ! hasCompletedOneCompile ) {
207
+ notifyIPCWatchCompileDone ( index ) ;
208
+ hasCompletedOneCompile = true ;
209
+ }
210
+ }
175
211
} ) ;
176
212
} ;
0 commit comments