Skip to content

Commit 191ebc1

Browse files
fix: make sure users can change inspector port when running vite dev (#8786)
* [vite-plugin] fix: make sure users can change inspector port when running vite dev currently when a user starts a dev server with `vite dev` the inspector port that the Cloudflare plugin will use will always be the initial one, even if the user sets a specific port in the plugin's options inside the vite config file, the changes here make sure that such config updates are instead actually reflected * Update .changeset/cuddly-news-double.md Co-authored-by: James Opstad <[email protected]> --------- Co-authored-by: James Opstad <[email protected]>
1 parent f4c37bb commit 191ebc1

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

.changeset/cuddly-news-double.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@cloudflare/vite-plugin": patch
3+
---
4+
5+
fix: make sure users can change inspector port when running vite dev
6+
7+
Ensure that the inspector port is updated if the user modifies it in the Vite config while the dev server is running.

packages/vite-plugin-cloudflare/src/index.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -334,12 +334,12 @@ export function cloudflare(pluginConfig: PluginConfig = {}): vite.Plugin[] {
334334
"Unexpected error: No Vite HTTP server"
335335
);
336336

337-
if (!miniflare) {
338-
const inputInspectorPort = await getInputInspectorPortOption(
339-
pluginConfig,
340-
viteDevServer
341-
);
337+
const inputInspectorPort = await getInputInspectorPortOption(
338+
pluginConfig,
339+
viteDevServer
340+
);
342341

342+
if (!miniflare) {
343343
miniflare = new Miniflare(
344344
getDevMiniflareOptions(
345345
resolvedPluginConfig,
@@ -348,14 +348,11 @@ export function cloudflare(pluginConfig: PluginConfig = {}): vite.Plugin[] {
348348
)
349349
);
350350
} else {
351-
const resolvedInspectorPort =
352-
await getResolvedInspectorPort(pluginConfig);
353-
354351
await miniflare.setOptions(
355352
getDevMiniflareOptions(
356353
resolvedPluginConfig,
357354
viteDevServer,
358-
resolvedInspectorPort ?? false
355+
inputInspectorPort
359356
)
360357
);
361358
}
@@ -842,6 +839,19 @@ async function getInputInspectorPortOption(
842839
pluginConfig: PluginConfig,
843840
viteServer: vite.ViteDevServer | vite.PreviewServer
844841
) {
842+
if (
843+
pluginConfig.inspectorPort === undefined ||
844+
pluginConfig.inspectorPort === 0
845+
) {
846+
const resolvedInspectorPort = await getResolvedInspectorPort(pluginConfig);
847+
848+
if (resolvedInspectorPort !== null) {
849+
// the user is not specifying an inspector port to use and we're already
850+
// using one (this is a server restart) so let's just reuse that
851+
return resolvedInspectorPort;
852+
}
853+
}
854+
845855
const inputInspectorPort =
846856
pluginConfig.inspectorPort ??
847857
(await getFirstAvailablePort(DEFAULT_INSPECTOR_PORT));

0 commit comments

Comments
 (0)