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
Simplify AddViteApp port configuration by using vite.config.ts
Co-authored-by: davidfowl <[email protected]>
  • Loading branch information
Copilot and davidfowl committed Jun 10, 2025
commit 6040d2dd5a094067e9236647e480a02be283ecd0
3 changes: 3 additions & 0 deletions examples/nodejs-ext/pnpm-demo/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ import react from '@vitejs/plugin-react'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
server: {
port: parseInt(process.env.PORT ?? '5173')
}
})
3 changes: 3 additions & 0 deletions examples/nodejs-ext/vite-demo/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ import react from '@vitejs/plugin-react'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
server: {
port: parseInt(process.env.PORT ?? '5173')
}
})
3 changes: 3 additions & 0 deletions examples/nodejs-ext/yarn-demo/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ import react from '@vitejs/plugin-react'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
server: {
port: parseInt(process.env.PORT ?? '5173')
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -37,34 +37,9 @@ public static IResourceBuilder<NodeAppResource> AddViteApp(this IDistributedAppl
_ => builder.AddNpmApp(name, wd, "dev")
};

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

builder.Eventing.Subscribe<ResourceEndpointsAllocatedEvent>((@event, ct) =>
{
if (@event.Resource.Name != name)
{
return Task.CompletedTask;
}

endpointBuilder.WithArgs(ctx =>
{
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
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,5 @@ public void ViteAppConfiguresPortFromEnvironment()
var httpEndpoint = endpoints.FirstOrDefault(e => e.UriScheme == "http");
Assert.NotNull(httpEndpoint);
Assert.Equal("PORT", httpEndpoint.EnvVar);

// Verify that command line arguments callback is configured
Assert.True(resource.TryGetAnnotationsOfType<CommandLineArgsCallbackAnnotation>(out var argsCallbacks));
Assert.NotEmpty(argsCallbacks);
}
}