Skip to content

Commit e03c0c4

Browse files
committed
fix: reuse existing hmr clients
1 parent ff3a6b9 commit e03c0c4

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

packages/dev-server-hmr/src/HmrPlugin.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {
44
Logger,
55
WebSocketData,
66
ServerStartParams,
7+
DevServerCoreConfig,
78
} from '@web/dev-server-core';
89
import WebSocket from 'ws';
910
import type { Context } from 'koa';
@@ -47,9 +48,10 @@ export class HmrPlugin implements Plugin {
4748
protected _dependencyTree: Map<string, HmrModule> = new Map();
4849
protected _webSockets?: WebSocketsManager;
4950
protected _logger?: Logger;
51+
protected _config?: DevServerCoreConfig;
5052

5153
/** @inheritDoc */
52-
async serverStart({ webSockets, fileWatcher, logger }: ServerStartParams) {
54+
async serverStart({ webSockets, fileWatcher, logger, config }: ServerStartParams) {
5355
if (!fileWatcher) {
5456
throw new Error('Cannot use HMR when watch mode is disabled.');
5557
}
@@ -58,6 +60,7 @@ export class HmrPlugin implements Plugin {
5860
throw new Error('Cannot use HMR when web sockets are disabled.');
5961
}
6062

63+
this._config = config;
6164
this._webSockets = webSockets;
6265
this._logger = logger;
6366

@@ -159,8 +162,14 @@ export class HmrPlugin implements Plugin {
159162
* Fired when a file has changed.
160163
* @param path Module path which has changed
161164
*/
162-
protected _onFileChanged(path: string): void {
163-
this._triggerUpdate(path);
165+
protected _onFileChanged(filePath: string): void {
166+
if (!this._config?.rootDir) {
167+
return;
168+
}
169+
170+
const relativePath = pathUtil.relative(this._config.rootDir, filePath);
171+
const browserPath = relativePath.split(pathUtil.sep).join('/');
172+
this._triggerUpdate(`/${browserPath}`);
164173
}
165174

166175
/**

packages/dev-server-hmr/src/hmrClientScript.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ export class HotModule {
2525
}
2626
2727
accept(deps, callback) {
28-
if (this[moduleState] !== HmrState.Accepted) {
29-
sendMessage({ type: 'hmr:accept', id: this.id });
30-
this[moduleState] = HmrState.Accepted;
28+
if (this[moduleState] === HmrState.Accepted) {
29+
return;
3130
}
3231
32+
sendMessage({ type: 'hmr:accept', id: this.id });
33+
this[moduleState] = HmrState.Accepted;
34+
3335
if (!callback) {
3436
callback = deps;
3537
deps = [];
@@ -93,6 +95,7 @@ export function create(url) {
9395
9496
if (existing) {
9597
existing[disposeTrigger]();
98+
return existing;
9699
}
97100
98101
const instance = new HotModule(path);

0 commit comments

Comments
 (0)