Skip to content

Commit

Permalink
feat: Add dev.server.port config (#577)
Browse files Browse the repository at this point in the history
Co-authored-by: Aaron <[email protected]>
  • Loading branch information
zizhengtai and aklinker1 authored Mar 29, 2024
1 parent c9c5d72 commit 5a3b42a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 8 deletions.
14 changes: 14 additions & 0 deletions src/cli/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
9 changes: 9 additions & 0 deletions src/cli/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ cli
.option('-c, --config <file>', 'use specified config file')
.option('-m, --mode <mode>', 'set env mode')
.option('-b, --browser <browser>', 'specify a browser')
.option('-p, --port <port>', 'specify a port for the dev server')
.option(
'-e, --filter-entrypoint <entrypoint>',
'only build specific entrypoints',
Expand All @@ -35,6 +36,14 @@ cli
configFile: flags.config,
debug: flags.debug,
filterEntrypoints: getArrayFromFlags(flags, 'filterEntrypoint'),
dev:
flags.port == null
? undefined
: {
server: {
port: parseInt(flags.port),
},
},
});
await server.start();
return { isOngoing: true };
Expand Down
10 changes: 2 additions & 8 deletions src/core/create-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ import { mapWxtOptionsToRegisteredContentScript } from './utils/content-scripts'
export async function createServer(
inlineConfig?: InlineConfig,
): Promise<WxtDevServer> {
await registerWxt('serve', inlineConfig, async (_config) => {
const port = await getPort();
const hostname = 'localhost';
await registerWxt('serve', inlineConfig, async (config) => {
const { port, hostname } = config.dev.server!;
const serverInfo: ServerInfo = {
port,
hostname,
Expand Down Expand Up @@ -138,11 +137,6 @@ export async function createServer(
return server;
}

async function getPort(): Promise<number> {
const { default: getPort, portNumbers } = await import('get-port');
return await getPort({ port: portNumbers(3000, 3010) });
}

/**
* Returns a function responsible for reloading different parts of the extension when a file
* changes.
Expand Down
14 changes: 14 additions & 0 deletions src/core/utils/building/resolve-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,19 @@ export async function resolveConfig(
}).map(([key, value]) => [key, path.resolve(root, value)]),
);

let devServerConfig: ResolvedConfig['dev']['server'];
if (command === 'serve') {
let port = mergedConfig.dev?.server?.port;
if (port == null || !isFinite(port)) {
const { default: getPort, portNumbers } = await import('get-port');
port = await getPort({ port: portNumbers(3000, 3010) });
}
devServerConfig = {
port,
hostname: 'localhost',
};
}

return {
browser,
command,
Expand Down Expand Up @@ -141,6 +154,7 @@ export async function resolveConfig(
includeBrowserPolyfill: true,
}),
dev: {
server: devServerConfig,
reloadCommand,
},
hooks: mergedConfig.hooks ?? {},
Expand Down
11 changes: 11 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,12 @@ export interface InlineConfig {
* Config effecting dev mode only.
*/
dev?: {
server?: {
/**
* Port to run the dev server on. Defaults to the first open port from 3000 to 3010.
*/
port?: number;
};
/**
* Controls whether a custom keyboard shortcut command, `Alt+R`, is added during dev mode to
* quickly reload the extension.
Expand Down Expand Up @@ -1077,6 +1083,11 @@ export interface ResolvedConfig {
includeBrowserPolyfill: boolean;
};
dev: {
/** Only defined during dev command */
server?: {
port: number;
hostname: string;
};
reloadCommand: string | false;
};
hooks: NestedHooks<WxtHooks>;
Expand Down

0 comments on commit 5a3b42a

Please sign in to comment.