fix: Prevent changing dev server port when reloading config (#1241)

This commit is contained in:
Aaron
2024-12-05 11:48:49 -06:00
committed by GitHub
parent fd436a0d63
commit 17af05acc8
2 changed files with 32 additions and 0 deletions
+24
View File
@@ -0,0 +1,24 @@
import { describe, it, expect } from 'vitest';
import { TestProject } from '../utils';
describe('Dev Mode', () => {
it('should not change ports when restarting the server', async () => {
const project = new TestProject();
project.addFile(
'entrypoints/background.ts',
'export default defineBackground(() => {})',
);
const server = await project.startServer({
runner: {
disabled: true,
},
});
const initialPort = server.port;
await server.restart();
const finalPort = server.port;
await server.stop();
expect(finalPort).toBe(initialPort);
});
});
+8
View File
@@ -36,6 +36,14 @@ export async function registerWxt(
return config.logger;
},
async reloadConfig() {
// Prevent changing the server port when resolving config multiple times
// get-port-please doesn't always return the same port if it was recently closed.
if (wxt.config.dev.server?.port) {
inlineConfig.dev ??= {};
inlineConfig.dev.server ??= {};
inlineConfig.dev.server.port = wxt.config.dev.server.port;
}
wxt.config = await resolveConfig(inlineConfig, command);
await wxt.hooks.callHook('config:resolved', wxt);
},