@@ -108,7 +108,7 @@ function createCompiler(
108
108
urls ,
109
109
useYarn ,
110
110
useTypeScript ,
111
- devSocket
111
+ devSocketWrite
112
112
) {
113
113
// "Compiler" is a low-level interface to Webpack.
114
114
// It lets us listen to some events and provide our own custom messages.
@@ -140,6 +140,9 @@ function createCompiler(
140
140
141
141
if ( useTypeScript ) {
142
142
compiler . hooks . beforeCompile . tap ( 'beforeCompile' , ( ) => {
143
+ if ( ! isFirstCompile ) {
144
+ devSocketWrite ( 'wait-for-types' , true ) ;
145
+ }
143
146
tsMessagesPromise = new Promise ( resolve => {
144
147
tsMessagesResolver = msgs => resolve ( msgs ) ;
145
148
} ) ;
@@ -164,52 +167,55 @@ function createCompiler(
164
167
// "done" event fires when Webpack has finished recompiling the bundle.
165
168
// Whether or not you have warnings or errors, you will get this event.
166
169
compiler . hooks . done . tap ( 'done' , async stats => {
167
- if ( isInteractive ) {
168
- clearConsole ( ) ;
170
+ if ( useTypeScript ) {
171
+ if ( isInteractive ) {
172
+ clearConsole ( ) ;
173
+ }
174
+
175
+ console . log (
176
+ chalk . yellow (
177
+ 'Files successfully emitted, waiting for typecheck results...'
178
+ )
179
+ ) ;
169
180
}
170
181
171
182
// We have switched off the default Webpack output in WebpackDevServer
172
183
// options so we are going to "massage" the warnings and errors and present
173
184
// them in a readable focused way.
174
185
// We only construct the warnings and errors for speed:
175
186
// https://github.com/facebook/create-react-app/issues/4492#issuecomment-421959548
176
- const statsData = stats . toJson ( {
177
- all : false ,
178
- warnings : true ,
179
- errors : true ,
180
- } ) ;
181
-
182
- if ( useTypeScript && statsData . errors . length === 0 ) {
183
- const delayedMsg = setTimeout ( ( ) => {
184
- console . log (
185
- chalk . yellow (
186
- 'Files successfully emitted, waiting for typecheck results...'
187
- )
188
- ) ;
189
- } , 100 ) ;
187
+ const messages = formatWebpackMessages (
188
+ stats . toJson ( { all : false , warnings : true , errors : true } )
189
+ ) ;
190
190
191
- const messages = await tsMessagesPromise ;
192
- clearTimeout ( delayedMsg ) ;
193
- statsData . errors . push ( ...messages . errors ) ;
194
- statsData . warnings . push ( ...messages . warnings ) ;
191
+ if ( useTypeScript ) {
192
+ const asyncMessages = await tsMessagesPromise ;
195
193
196
194
// Push errors and warnings into compilation result
197
195
// to show them after page refresh triggered by user.
198
- stats . compilation . errors . push ( ...messages . errors ) ;
199
- stats . compilation . warnings . push ( ...messages . warnings ) ;
196
+ // This is important for errors on first run in development.
197
+ stats . compilation . errors . push ( ...asyncMessages . errors ) ;
198
+ stats . compilation . warnings . push ( ...asyncMessages . warnings ) ;
200
199
201
- if ( messages . errors . length > 0 ) {
202
- devSocket . errors ( messages . errors ) ;
203
- } else if ( messages . warnings . length > 0 ) {
204
- devSocket . warnings ( messages . warnings ) ;
205
- }
200
+ messages . errors . push ( ...asyncMessages . errors ) ;
201
+ messages . warnings . push ( ...asyncMessages . warnings ) ;
206
202
207
- if ( isInteractive ) {
208
- clearConsole ( ) ;
203
+ if ( asyncMessages . errors . length > 0 ) {
204
+ devSocketWrite ( 'errors' , asyncMessages . errors ) ;
205
+ } else if ( asyncMessages . warnings . length > 0 ) {
206
+ devSocketWrite ( 'warnings' , asyncMessages . warnings ) ;
207
+ } else {
208
+ // We have to notify the hot dev client when we are waiting for types
209
+ // when `module.hot` is being used.
210
+ devSocketWrite ( 'wait-for-types' , false ) ;
211
+ devSocketWrite ( 'ok' ) ;
209
212
}
210
213
}
211
214
212
- const messages = formatWebpackMessages ( statsData ) ;
215
+ if ( useTypeScript && isInteractive ) {
216
+ clearConsole ( ) ;
217
+ }
218
+
213
219
const isSuccessful = ! messages . errors . length && ! messages . warnings . length ;
214
220
if ( isSuccessful ) {
215
221
console . log ( chalk . green ( 'Compiled successfully!' ) ) ;
0 commit comments