| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| import { isMiddlewareFilename } from '../../build/utils'; |
| import { HMR_ACTIONS_SENT_TO_BROWSER } from './hot-reloader-types'; |
| import { devIndicatorServerState } from './dev-indicator-server-state'; |
| function isMiddlewareStats(stats) { |
| for (const key of stats.compilation.entrypoints.keys()){ |
| if (isMiddlewareFilename(key)) { |
| return true; |
| } |
| } |
| return false; |
| } |
| function statsToJson(stats) { |
| if (!stats) return {}; |
| return stats.toJson({ |
| all: false, |
| errors: true, |
| hash: true, |
| warnings: true |
| }); |
| } |
| function getStatsForSyncEvent(clientStats, serverStats) { |
| if (!clientStats) return serverStats == null ? void 0 : serverStats.stats; |
| if (!serverStats) return clientStats == null ? void 0 : clientStats.stats; |
| |
| |
| |
| if (serverStats.stats.hasErrors()) { |
| return serverStats.stats; |
| } |
| |
| return serverStats.ts > clientStats.ts ? serverStats.stats : clientStats.stats; |
| } |
| class EventStream { |
| constructor(){ |
| this.clients = new Set(); |
| } |
| close() { |
| for (const wsClient of this.clients){ |
| |
| wsClient.terminate(); |
| } |
| this.clients.clear(); |
| } |
| handler(client) { |
| this.clients.add(client); |
| client.addEventListener('close', ()=>{ |
| this.clients.delete(client); |
| }); |
| } |
| publish(payload) { |
| for (const wsClient of this.clients){ |
| wsClient.send(JSON.stringify(payload)); |
| } |
| } |
| } |
| export class WebpackHotMiddleware { |
| constructor(compilers, versionInfo, devtoolsFrontendUrl, devToolsConfig){ |
| this.onClientInvalid = ()=>{ |
| var _this_serverLatestStats; |
| if (this.closed || ((_this_serverLatestStats = this.serverLatestStats) == null ? void 0 : _this_serverLatestStats.stats.hasErrors())) return; |
| this.publish({ |
| action: HMR_ACTIONS_SENT_TO_BROWSER.BUILDING |
| }); |
| }; |
| this.onClientDone = (statsResult)=>{ |
| var _this_serverLatestStats; |
| this.clientLatestStats = { |
| ts: Date.now(), |
| stats: statsResult |
| }; |
| if (this.closed || ((_this_serverLatestStats = this.serverLatestStats) == null ? void 0 : _this_serverLatestStats.stats.hasErrors())) return; |
| this.publishStats(statsResult); |
| }; |
| this.onServerInvalid = ()=>{ |
| var _this_serverLatestStats, _this_clientLatestStats; |
| if (!((_this_serverLatestStats = this.serverLatestStats) == null ? void 0 : _this_serverLatestStats.stats.hasErrors())) return; |
| this.serverLatestStats = null; |
| if ((_this_clientLatestStats = this.clientLatestStats) == null ? void 0 : _this_clientLatestStats.stats) { |
| this.publishStats(this.clientLatestStats.stats); |
| } |
| }; |
| this.onServerDone = (statsResult)=>{ |
| if (this.closed) return; |
| if (statsResult.hasErrors()) { |
| this.serverLatestStats = { |
| ts: Date.now(), |
| stats: statsResult |
| }; |
| this.publishStats(statsResult); |
| } |
| }; |
| this.onEdgeServerInvalid = ()=>{ |
| var _this_middlewareLatestStats, _this_clientLatestStats; |
| if (!((_this_middlewareLatestStats = this.middlewareLatestStats) == null ? void 0 : _this_middlewareLatestStats.stats.hasErrors())) return; |
| this.middlewareLatestStats = null; |
| if ((_this_clientLatestStats = this.clientLatestStats) == null ? void 0 : _this_clientLatestStats.stats) { |
| this.publishStats(this.clientLatestStats.stats); |
| } |
| }; |
| this.onEdgeServerDone = (statsResult)=>{ |
| if (this.closed) return; |
| if (!isMiddlewareStats(statsResult)) { |
| this.onServerInvalid(); |
| this.onServerDone(statsResult); |
| } |
| if (statsResult.hasErrors()) { |
| this.middlewareLatestStats = { |
| ts: Date.now(), |
| stats: statsResult |
| }; |
| this.publishStats(statsResult); |
| } |
| }; |
| |
| |
| |
| |
| |
| this.onHMR = (client)=>{ |
| if (this.closed) return; |
| this.eventStream.handler(client); |
| const syncStats = getStatsForSyncEvent(this.clientLatestStats, this.serverLatestStats); |
| if (syncStats) { |
| var _this_middlewareLatestStats; |
| const stats = statsToJson(syncStats); |
| const middlewareStats = statsToJson((_this_middlewareLatestStats = this.middlewareLatestStats) == null ? void 0 : _this_middlewareLatestStats.stats); |
| if (devIndicatorServerState.disabledUntil < Date.now()) { |
| devIndicatorServerState.disabledUntil = 0; |
| } |
| this.publish({ |
| action: HMR_ACTIONS_SENT_TO_BROWSER.SYNC, |
| hash: stats.hash, |
| errors: [ |
| ...stats.errors || [], |
| ...middlewareStats.errors || [] |
| ], |
| warnings: [ |
| ...stats.warnings || [], |
| ...middlewareStats.warnings || [] |
| ], |
| versionInfo: this.versionInfo, |
| debug: { |
| devtoolsFrontendUrl: this.devtoolsFrontendUrl |
| }, |
| devIndicator: devIndicatorServerState, |
| devToolsConfig: this.devToolsConfig |
| }); |
| } |
| }; |
| this.publishStats = (statsResult)=>{ |
| const stats = statsResult.toJson({ |
| all: false, |
| hash: true, |
| warnings: true, |
| errors: true, |
| moduleTrace: true |
| }); |
| this.publish({ |
| action: HMR_ACTIONS_SENT_TO_BROWSER.BUILT, |
| 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; |
| |
| |
| this.closed = true; |
| this.eventStream.close(); |
| }; |
| this.eventStream = new EventStream(); |
| this.clientLatestStats = null; |
| this.middlewareLatestStats = null; |
| this.serverLatestStats = null; |
| this.closed = false; |
| this.versionInfo = versionInfo; |
| this.devtoolsFrontendUrl = devtoolsFrontendUrl; |
| this.devToolsConfig = devToolsConfig || {}; |
| 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); |
| compilers[2].hooks.done.tap('webpack-hot-middleware', this.onEdgeServerDone); |
| compilers[2].hooks.invalid.tap('webpack-hot-middleware', this.onEdgeServerInvalid); |
| } |
| updateDevToolsConfig(newConfig) { |
| this.devToolsConfig = newConfig; |
| } |
| } |
|
|
| |