From 5056a696353657b13f71d28859938b0d202a49c2 Mon Sep 17 00:00:00 2001 From: Chen Gang Date: Tue, 22 Sep 2020 14:39:32 +0800 Subject: [PATCH] configure hmr port --- snowpack/src/commands/build.ts | 4 ++-- snowpack/src/config.ts | 3 +++ snowpack/src/hmr-server-engine.ts | 21 +++++++++++++++------ snowpack/src/types/snowpack.ts | 1 + 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/snowpack/src/commands/build.ts b/snowpack/src/commands/build.ts index c97aa0a261..c2c5244ea7 100644 --- a/snowpack/src/commands/build.ts +++ b/snowpack/src/commands/build.ts @@ -315,7 +315,7 @@ export async function command(commandOptions: CommandOptions) { path.resolve(internalFilesBuildLoc, 'hmr-error-overlay.js'), HMR_OVERLAY_CODE, ); - hmrEngine = new EsmHmrEngine(); + hmrEngine = new EsmHmrEngine({ port: config.devOptions.hmrPort }); } logger.info(colors.yellow('! building sourceā€¦')); @@ -431,7 +431,7 @@ export async function command(commandOptions: CommandOptions) { // "--watch --hmr" mode - Tell users about the HMR WebSocket URL if (hmrEngine) { - logger.info(`[HMR] WebSocket URL available at ${colors.cyan(`${hmrEngine.wsUrl}`)}`); + logger.info(`[HMR] WebSocket URL available at ${colors.cyan(`ws://localhost:${config.devOptions.hmrPort}`)}`); } // "--watch" mode - Start watching the file system. diff --git a/snowpack/src/config.ts b/snowpack/src/config.ts index 0461dc5ced..b9af67fc8c 100644 --- a/snowpack/src/config.ts +++ b/snowpack/src/config.ts @@ -48,6 +48,7 @@ const DEFAULT_CONFIG: Partial = { out: 'build', fallback: 'index.html', hmrDelay: 0, + hmrPort: 12321, }, buildOptions: { baseUrl: '/', @@ -89,6 +90,8 @@ const configSchema = { bundle: {type: 'boolean'}, open: {type: 'string'}, hmr: {type: 'boolean'}, + hmrDelay: {type: 'number'}, + hmrPort: {type: 'number'}, }, }, installOptions: { diff --git a/snowpack/src/hmr-server-engine.ts b/snowpack/src/hmr-server-engine.ts index 154b122696..c8263899c6 100644 --- a/snowpack/src/hmr-server-engine.ts +++ b/snowpack/src/hmr-server-engine.ts @@ -23,9 +23,18 @@ type HMRMessage = errorStackTrace?: string; }; -const DEFAULT_PORT = 12321; const DEFAULT_CONNECT_DELAY = 2000; +interface EsmHmrEngineOptionsCommon { + delay?: number; +} + +type EsmHmrEngineOptions = ({ + server: http.Server | http2.Http2Server; +} | { + port: number; +}) & EsmHmrEngineOptionsCommon; + export class EsmHmrEngine { clients: Set = new Set(); dependencyTree = new Map(); @@ -34,16 +43,16 @@ export class EsmHmrEngine { private currentBatch: HMRMessage[] = []; private currentBatchTimeout: NodeJS.Timer | null = null; private cachedConnectErrors: Set = new Set(); - wsUrl = `ws://localhost:${DEFAULT_PORT}`; - constructor(options: {server?: http.Server | http2.Http2Server; delay?: number} = {}) { - const wss = options.server + constructor(options: EsmHmrEngineOptions) { + const wss = 'server' in options ? new WebSocket.Server({noServer: true}) - : new WebSocket.Server({port: DEFAULT_PORT}); + : new WebSocket.Server({port: options.port}); if (options.delay) { this.delay = options.delay; } - if (options.server) { + + if ('server' in options) { options.server.on('upgrade', (req, socket, head) => { // Only handle upgrades to ESM-HMR requests, ignore others. if (req.headers['sec-websocket-protocol'] !== 'esm-hmr') { diff --git a/snowpack/src/types/snowpack.ts b/snowpack/src/types/snowpack.ts index 192649a4fc..73671feb21 100644 --- a/snowpack/src/types/snowpack.ts +++ b/snowpack/src/types/snowpack.ts @@ -129,6 +129,7 @@ export interface SnowpackConfig { open: string; hmr?: boolean; hmrDelay: number; + hmrPort: number; }; installOptions: InstallOptions; buildOptions: {