From dddd524c08cf43d4e1316126d6f190dd27c36cb7 Mon Sep 17 00:00:00 2001 From: Zizheng Tai Date: Thu, 28 Mar 2024 17:02:02 -0700 Subject: [PATCH 1/3] Add dev server port option --- src/core/create-server.ts | 2 +- src/types/index.ts | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/core/create-server.ts b/src/core/create-server.ts index 973a6122b..e95f53eff 100644 --- a/src/core/create-server.ts +++ b/src/core/create-server.ts @@ -40,7 +40,7 @@ import { mapWxtOptionsToRegisteredContentScript } from './utils/content-scripts' export async function createServer( inlineConfig?: InlineConfig, ): Promise { - const port = await getPort(); + const port = inlineConfig?.dev?.port ?? (await getPort()); const hostname = 'localhost'; const origin = `http://${hostname}:${port}`; const serverInfo: ServerInfo = { diff --git a/src/types/index.ts b/src/types/index.ts index 5f3cb2cbc..f36a9d6fe 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -309,6 +309,12 @@ export interface InlineConfig { * Config effecting dev mode only. */ dev?: { + /** + * Port to run the dev server on. + * + * @default "Any available port from 3000 to 3010, otherwise fall back to a random port" + */ + port?: number; /** * Controls whether a custom keyboard shortcut command, `Alt+R`, is added during dev mode to * quickly reload the extension. From c8d63a6c6a2e9b7bfd1a279f9a0dd2503091b03f Mon Sep 17 00:00:00 2001 From: Aaron Date: Fri, 29 Mar 2024 00:41:22 -0500 Subject: [PATCH 2/3] Fix tests --- src/cli/commands.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/cli/commands.ts b/src/cli/commands.ts index 0c5999d6d..c17a39d05 100644 --- a/src/cli/commands.ts +++ b/src/cli/commands.ts @@ -36,11 +36,14 @@ cli configFile: flags.config, debug: flags.debug, filterEntrypoints: getArrayFromFlags(flags, 'filterEntrypoint'), - dev: { - server: { - port: flags.port != null ? parseInt(flags.port) : undefined, - }, - }, + dev: + flags.port == null + ? undefined + : { + server: { + port: parseInt(flags.port), + }, + }, }); await server.start(); return { isOngoing: true }; From d8ffc30219130585ae8eb0931609396d0324730c Mon Sep 17 00:00:00 2001 From: Aaron Date: Fri, 29 Mar 2024 00:46:05 -0500 Subject: [PATCH 3/3] Update CLI tests --- src/cli/__tests__/index.test.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/cli/__tests__/index.test.ts b/src/cli/__tests__/index.test.ts index 458b5fa5f..14a1a3c09 100644 --- a/src/cli/__tests__/index.test.ts +++ b/src/cli/__tests__/index.test.ts @@ -116,6 +116,20 @@ describe('CLI', () => { }); }); + it('should respect passing --port', async () => { + const expectedPort = 3100; + mockArgv('--port', String(expectedPort)); + await importCli(); + + expect(createServerMock).toBeCalledWith({ + dev: { + server: { + port: expectedPort, + }, + }, + }); + }); + it('should respect passing --debug', async () => { mockArgv('--debug'); await importCli();