@@ -4,6 +4,7 @@ const connect = require('connect'),
4
4
fs = require ( 'fs' ) ,
5
5
http = require ( 'http' ) ,
6
6
https = require ( 'https' ) ,
7
+ socketIO = require ( 'socket.io' ) ,
7
8
serveStatic = require ( 'serve-static' ) ,
8
9
serveIndex = require ( 'serve-index' ) ,
9
10
moment = require ( 'moment' ) ,
@@ -50,6 +51,9 @@ exports.run = (options) => {
50
51
allAssetsEntry = { } ,
51
52
watchCacheNames = { } ;
52
53
54
+ let io = null ,
55
+ assetEntrys = { } ;
56
+
53
57
let usingHotServer = false ;
54
58
const dateFormat = 'HH:mm:ss' ;
55
59
@@ -260,6 +264,13 @@ exports.run = (options) => {
260
264
config . plugins . push ( require ( '../plugins/compileInfoPlugin.js' ) ) ;
261
265
262
266
nextConfig = extend ( { } , config ) ;
267
+
268
+ // 注入 sockitIO
269
+ nextConfig . module . postLoaders . push ( {
270
+ test : / \. ( j s ) $ / ,
271
+ loader : sysPath . join ( __dirname , '../modules/SocketClientLoader.js?cacheId=' + cacheId )
272
+ } ) ;
273
+
263
274
if ( shouldCompileAllEntries ) {
264
275
return nextConfig ;
265
276
} else {
@@ -364,8 +375,8 @@ exports.run = (options) => {
364
375
compiler ,
365
376
{
366
377
quiet : true , reporter : ( { state, stats} ) => {
378
+ // 打印编译完成时间(小于 100ms 不展示)
367
379
if ( ! stats . hasErrors ( ) && ! stats . hasWarnings ( ) ) {
368
- // 打印编译完成时间(过小的不展示)
369
380
const minDuration = 100 ;
370
381
if ( stats . endTime - stats . startTime > minDuration ) {
371
382
const dateLog = '[' + moment ( ) . format ( dateFormat ) + ']' ;
@@ -377,6 +388,13 @@ exports.run = (options) => {
377
388
spinner . stop ( ) ;
378
389
spinner . text = '' ;
379
390
391
+ // emit compile info by socket
392
+ const statsInfo = stats . toJson ( { errorDetails : false } ) ;
393
+ assetEntrys [ cacheId ] = {
394
+ compilationId : statsInfo . hash ,
395
+ errors : statsInfo . errors
396
+ } ;
397
+
380
398
Object . keys ( stats . compilation . assets ) . map ( ( key ) => {
381
399
const keyCacheId = sysPath . join ( projectName , key ) ;
382
400
middlewareCache [ keyCacheId ] = middleware ;
@@ -390,11 +408,13 @@ exports.run = (options) => {
390
408
}
391
409
}
392
410
) ;
411
+
393
412
if ( hot ) {
394
413
app . use ( require ( 'webpack-hot-middleware' ) ( compiler , {
395
414
log : false
396
415
} ) ) ;
397
416
}
417
+
398
418
middleware ( req , res , next ) ;
399
419
}
400
420
@@ -429,6 +449,7 @@ exports.run = (options) => {
429
449
servers . push ( extend ( https . createServer ( httpsOpts , app ) , { _port : '443' , _isHttps : true } ) ) ;
430
450
}
431
451
452
+ // setup server
432
453
servers . forEach ( ( server ) => {
433
454
server . on ( 'error' , ( e ) => {
434
455
if ( e . code === 'EACCES' ) {
@@ -438,17 +459,28 @@ exports.run = (options) => {
438
459
}
439
460
process . exit ( 1 ) ;
440
461
} ) ;
462
+
441
463
server . listen ( server . _port , ( ) => {
442
464
const serverUrl = ( server . _isHttps ? 'https' : 'http' )
443
465
+ '://127.0.0.1:'
444
466
+ server . _port ;
445
467
446
468
! server . _isHttps && log ( 'Starting up server, serving at: ' + options . cwd ) ;
447
469
logInfo ( 'Available on: ' + serverUrl . underline ) ;
470
+
471
+ // socket
472
+ if ( ! server . _isHttps ) {
473
+ io = socketIO ( server ) ;
474
+ io . on ( 'connection' , function ( socket ) {
475
+ setInterval ( function ( ) {
476
+ io && io . volatile . emit ( 'testAppID' , assetEntrys ) ;
477
+ } , 500 ) ;
478
+ } ) ;
479
+ }
448
480
} ) ;
449
481
} ) ;
450
482
451
- // 代理
483
+ // proxy
452
484
var proxyProcess ;
453
485
if ( proxy ) {
454
486
const proxyPath = sysPath . join ( requireg . resolve ( 'jerryproxy' ) , '../bin/jerry.js' ) ;
0 commit comments