1 | "use strict";exports.__esModule=true;exports.WebpackHotMiddleware=void 0;
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | class WebpackHotMiddleware{constructor(compilers){this.eventStream=void 0;this.latestStats=void 0;this.clientLatestStats=void 0;this.closed=void 0;this.serverError=void 0;this.onServerInvalid=()=>{if(!this.serverError)return;this.serverError=false;if(this.clientLatestStats){this.latestStats=this.clientLatestStats;this.publishStats('built',this.latestStats);}};this.onClientInvalid=()=>{if(this.closed||this.serverError)return;this.latestStats=null;this.eventStream.publish({action:'building'});};this.onServerDone=statsResult=>{if(this.closed)return;
|
21 |
|
22 |
|
23 | this.serverError=statsResult.hasErrors();if(this.serverError){this.latestStats=statsResult;this.publishStats('built',this.latestStats);}};this.onClientDone=statsResult=>{this.clientLatestStats=statsResult;if(this.closed||this.serverError)return;
|
24 | this.latestStats=statsResult;this.publishStats('built',this.latestStats);};this.middleware=(req,res,next)=>{var _req$url;if(this.closed)return next();if(!((_req$url=req.url)!=null&&_req$url.startsWith('/_next/webpack-hmr')))return next();this.eventStream.handler(req,res);if(this.latestStats){
|
25 |
|
26 | this.publishStats('sync',this.latestStats);}};this.publishStats=(action,statsResult)=>{const stats=statsResult.toJson({all:false,hash:true,warnings:true,errors:true});this.eventStream.publish({action:action,hash:stats.hash,warnings:stats.warnings||[],errors:stats.errors||[]});};this.publish=payload=>{if(this.closed)return;this.eventStream.publish(payload);};this.close=()=>{if(this.closed)return;
|
27 |
|
28 | this.closed=true;this.eventStream.close();};this.eventStream=new EventStream();this.latestStats=null;this.clientLatestStats=null;this.serverError=false;this.closed=false;compilers[0].hooks.invalid.tap('webpack-hot-middleware',this.onClientInvalid);compilers[0].hooks.done.tap('webpack-hot-middleware',this.onClientDone);compilers[1].hooks.invalid.tap('webpack-hot-middleware',this.onServerInvalid);compilers[1].hooks.done.tap('webpack-hot-middleware',this.onServerDone);}}exports.WebpackHotMiddleware=WebpackHotMiddleware;class EventStream{constructor(){this.clients=void 0;this.interval=void 0;this.heartbeatTick=()=>{this.everyClient(client=>{client.write('data: \uD83D\uDC93\n\n');});};this.clients=new Set();this.interval=setInterval(this.heartbeatTick,2500).unref();}everyClient(fn){for(const client of this.clients){fn(client);}}close(){clearInterval(this.interval);this.everyClient(client=>{if(!client.finished)client.end();});this.clients.clear();}handler(req,res){const headers={'Access-Control-Allow-Origin':'*','Content-Type':'text/event-stream;charset=utf-8','Cache-Control':'no-cache, no-transform',
|
29 |
|
30 | 'X-Accel-Buffering':'no'};const isHttp1=!(parseInt(req.httpVersion)>=2);if(isHttp1){req.socket.setKeepAlive(true);Object.assign(headers,{Connection:'keep-alive'});}res.writeHead(200,headers);res.write('\n');this.clients.add(res);req.on('close',()=>{if(!res.finished)res.end();this.clients.delete(res);});}publish(payload){this.everyClient(client=>{client.write('data: '+JSON.stringify(payload)+'\n\n');});}}
|
31 |
|
\ | No newline at end of file |