Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improving on Copilots take
  • Loading branch information
aaronpowell committed Jun 10, 2025
commit c2d7e818f868eddb44d102b343ecf14e2d0dc993
4 changes: 1 addition & 3 deletions examples/nodejs-ext/pnpm-demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "run-script-os",
"dev:windows": "vite dev --port %PORT%",
"dev:default": "vite dev --port $PORT",
"dev": "vite dev",
"build": "tsc -b && vite build",
"lint": "eslint .",
"preview": "vite preview"
Expand Down
5 changes: 2 additions & 3 deletions examples/nodejs-ext/vite-demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "run-script-os",
"dev:windows": "vite dev --port %PORT%",
"dev:default": "vite dev --port $PORT",
"dev": "vite dev",
"build": "tsc -b && vite build",
"lint": "eslint .",
"preview": "vite preview"
Expand All @@ -30,3 +28,4 @@
"vite": "^6.3.4"
}
}

3 changes: 0 additions & 3 deletions examples/nodejs-ext/vite-demo/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,4 @@ import react from '@vitejs/plugin-react'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
server: {
port: parseInt(process.env.PORT || '5173')
}
})
5 changes: 2 additions & 3 deletions examples/nodejs-ext/yarn-demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "run-script-os",
"dev:windows": "vite dev --port %PORT%",
"dev:default": "vite dev --port $PORT",
"dev": "vite dev",
"build": "tsc -b && vite build",
"lint": "eslint .",
"preview": "vite preview"
Expand All @@ -30,3 +28,4 @@
"vite": "^6.3.4"
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,33 @@ public static IResourceBuilder<NodeAppResource> AddViteApp(this IDistributedAppl
};

var endpointBuilder = useHttps
? resource.WithHttpsEndpoint(env: "PORT").WithExternalHttpEndpoints()
: resource.WithHttpEndpoint(env: "PORT").WithExternalHttpEndpoints();
? resource.WithHttpsEndpoint().WithExternalHttpEndpoints()
: resource.WithHttpEndpoint().WithExternalHttpEndpoints();

// Configure Vite to use the assigned port from the endpoint
return endpointBuilder.WithArgs(ctx =>
builder.Eventing.Subscribe<ResourceEndpointsAllocatedEvent>((@event, ct) =>
{
// Add -- separator to pass arguments to the underlying command (vite dev)
ctx.Args.Add("--");
ctx.Args.Add("--port");

// Try to get the port from the environment variable set by the endpoint
if (ctx.EnvironmentVariables.TryGetValue("PORT", out var portValue))
if (@event.Resource.Name != name)
{
ctx.Args.Add(portValue);
return Task.CompletedTask;
}
else

endpointBuilder.WithArgs(ctx =>
{
// Fallback to Vite's default port
ctx.Args.Add("5173");
}
if (@event.Resource.TryGetEndpoints(out var endpoints))
{
// Set the PORT environment variable to the first endpoint's port
var firstEndpoint = endpoints.FirstOrDefault();

ctx.Args.Add("--");
ctx.Args.Add("--port");
ctx.Args.Add(firstEndpoint?.AllocatedEndpoint?.Port.ToString() ?? "5173");
}
});

return Task.CompletedTask;
});

return resource;
}

/// <summary>
Expand Down