From d319a7503931789d8d9d17f850556aaa707f76bb Mon Sep 17 00:00:00 2001 From: 43081j <43081j@users.noreply.github.com> Date: Sun, 4 Oct 2020 21:49:14 +0100 Subject: [PATCH] add debug logging --- packages/dev-server-hmr/src/HmrPlugin.ts | 22 +++++++++++++------ .../dev-server-hmr/src/hmrPluginFactory.ts | 2 +- tsconfig.json | 19 +++++++++------- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/packages/dev-server-hmr/src/HmrPlugin.ts b/packages/dev-server-hmr/src/HmrPlugin.ts index 4ca273c21c..61fd7dbf78 100644 --- a/packages/dev-server-hmr/src/HmrPlugin.ts +++ b/packages/dev-server-hmr/src/HmrPlugin.ts @@ -1,4 +1,4 @@ -import { Plugin, WebSocketsManager } from '@web/dev-server-core'; +import type { Plugin, WebSocketsManager, Logger } from '@web/dev-server-core'; import type { ServerArgs } from '@web/dev-server-core/dist/Plugin'; import WebSocket from 'ws'; import type { Context } from 'koa'; @@ -27,8 +27,9 @@ export class HmrPlugin implements Plugin { protected _dependencyTree: Map = new Map(); protected _webSockets?: WebSocketsManager; + protected _logger?: Logger; - async serverStart({ webSockets, fileWatcher }: ServerArgs) { + async serverStart({ webSockets, fileWatcher, logger }: ServerArgs) { if (!fileWatcher) { throw new Error('Cannot use HMR when watch mode is disabled.'); } @@ -38,32 +39,38 @@ export class HmrPlugin implements Plugin { } this._webSockets = webSockets; + this._logger = logger; webSockets.on('message', ({ webSocket, data }) => this._onMessage(webSocket, data)); fileWatcher.on('change', path => this._onFileChanged(path)); fileWatcher.on('unlink', path => this._onFileChanged(path)); + + this._logger?.debug('[hmr] Listening for HMR messages'); } - async serve({ context }: { context: Context }) { + async serve(context: Context) { // We are serving a new file or it has changed, so clear all the // dependencies we previously tracked (if any). this._clearDependencies(context.path); + this._logger?.debug(`[hmr] Cleared dependency tree cache of ${context.path}`); } - async transformImport({ source, context }: { source: string, context: Context }) { + async transformImport({ source, context }: { source: string; context: Context }) { const mod = this._getOrCreateModule(context.path); const dependencyMod = this._getOrCreateModule(source); mod.dependencies.add(source); dependencyMod.dependents.add(context.path); + this._logger?.debug(`[hmr] Added dependency from ${context.path} -> ${source}`); } - async transform({ context }: { context: Context }) { + async transform(context: Context) { // If the module references import.meta.hot it can be assumed it // supports hot reloading const hmrEnabled = context.body.includes('import.meta.hot') === true; const mod = this._getOrCreateModule(context.path); mod.hmrEnabled = hmrEnabled; + this._logger?.debug(`[hmr] Setting hmrEnabled=${hmrEnabled} for ${context.path}`); } protected _clearDependencies(path: string): void { @@ -109,7 +116,7 @@ export class HmrPlugin implements Plugin { // We're not aware of this module so can't handle it if (!mod) { - this._broadcast({type: 'reload' }); + this._broadcast({ type: 'reload' }); return; } @@ -132,7 +139,7 @@ export class HmrPlugin implements Plugin { } // Nothing left to try - this._broadcast({type: 'reload' }); + this._broadcast({ type: 'reload' }); } protected _broadcast(message: HmrMessage): void { @@ -140,6 +147,7 @@ export class HmrPlugin implements Plugin { return; } + this._logger?.debug(`[hmr] emitting ${message.type} message`); this._webSockets.send(JSON.stringify(message), 'esm-hmr'); } diff --git a/packages/dev-server-hmr/src/hmrPluginFactory.ts b/packages/dev-server-hmr/src/hmrPluginFactory.ts index 8690754c39..23ce3a1524 100644 --- a/packages/dev-server-hmr/src/hmrPluginFactory.ts +++ b/packages/dev-server-hmr/src/hmrPluginFactory.ts @@ -1,4 +1,4 @@ -import { Plugin } from '@web/dev-server-core'; +import type { Plugin } from '@web/dev-server-core'; import { HmrPlugin } from './HmrPlugin'; export function hmrPlugin(): Plugin { diff --git a/tsconfig.json b/tsconfig.json index 445e509db8..9c20b2fad5 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -32,22 +32,25 @@ "path": "./packages/test-runner-playwright/tsconfig.json" }, { - "path": "./packages/test-runner-cli/tsconfig.json" + "path": "./packages/dev-server-esbuild/tsconfig.json" }, { - "path": "./packages/test-runner-commands/tsconfig.json" + "path": "./packages/test-runner-selenium/tsconfig.json" }, { "path": "./packages/dev-server-legacy/tsconfig.json" }, { - "path": "./packages/test-runner/tsconfig.json" + "path": "./packages/test-runner-cli/tsconfig.json" }, { - "path": "./packages/test-runner-selenium/tsconfig.json" + "path": "./packages/test-runner-commands/tsconfig.json" }, { - "path": "./packages/dev-server-esbuild/tsconfig.json" + "path": "./packages/test-runner-saucelabs/tsconfig.json" + }, + { + "path": "./packages/test-runner/tsconfig.json" }, { "path": "./packages/rollup-plugin-copy/tsconfig.json" @@ -61,6 +64,9 @@ { "path": "./packages/dev-server/tsconfig.json" }, + { + "path": "./packages/dev-server-hmr/tsconfig.json" + }, { "path": "./packages/dev-server-import-maps/tsconfig.json" }, @@ -73,9 +79,6 @@ { "path": "./packages/test-runner-junit-reporter/tsconfig.json" }, - { - "path": "./packages/test-runner-saucelabs/tsconfig.json" - }, { "path": "./packages/test-runner-visual-regression/tsconfig.json" }