Skip to content

Commit 6040d2d

Browse files
Copilotdavidfowl
andcommitted
Simplify AddViteApp port configuration by using vite.config.ts
Co-authored-by: davidfowl <[email protected]>
1 parent 8ed59ff commit 6040d2d

File tree

5 files changed

+10
-30
lines changed

5 files changed

+10
-30
lines changed

examples/nodejs-ext/pnpm-demo/vite.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ import react from '@vitejs/plugin-react'
44
// https://vitejs.dev/config/
55
export default defineConfig({
66
plugins: [react()],
7+
server: {
8+
port: parseInt(process.env.PORT ?? '5173')
9+
}
710
})

examples/nodejs-ext/vite-demo/vite.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ import react from '@vitejs/plugin-react'
44
// https://vitejs.dev/config/
55
export default defineConfig({
66
plugins: [react()],
7+
server: {
8+
port: parseInt(process.env.PORT ?? '5173')
9+
}
710
})

examples/nodejs-ext/yarn-demo/vite.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ import react from '@vitejs/plugin-react'
44
// https://vitejs.dev/config/
55
export default defineConfig({
66
plugins: [react()],
7+
server: {
8+
port: parseInt(process.env.PORT ?? '5173')
9+
}
710
})

src/CommunityToolkit.Aspire.Hosting.NodeJS.Extensions/NodeJSHostingExtensions.cs

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -37,34 +37,9 @@ public static IResourceBuilder<NodeAppResource> AddViteApp(this IDistributedAppl
3737
_ => builder.AddNpmApp(name, wd, "dev")
3838
};
3939

40-
var endpointBuilder = useHttps
40+
return useHttps
4141
? resource.WithHttpsEndpoint(env: "PORT").WithExternalHttpEndpoints()
4242
: resource.WithHttpEndpoint(env: "PORT").WithExternalHttpEndpoints();
43-
44-
builder.Eventing.Subscribe<ResourceEndpointsAllocatedEvent>((@event, ct) =>
45-
{
46-
if (@event.Resource.Name != name)
47-
{
48-
return Task.CompletedTask;
49-
}
50-
51-
endpointBuilder.WithArgs(ctx =>
52-
{
53-
if (@event.Resource.TryGetEndpoints(out var endpoints))
54-
{
55-
// Set the PORT environment variable to the first endpoint's port
56-
var firstEndpoint = endpoints.FirstOrDefault();
57-
58-
ctx.Args.Add("--");
59-
ctx.Args.Add("--port");
60-
ctx.Args.Add(firstEndpoint?.AllocatedEndpoint?.Port.ToString() ?? "5173");
61-
}
62-
});
63-
64-
return Task.CompletedTask;
65-
});
66-
67-
return resource;
6843
}
6944

7045
/// <summary>

tests/CommunityToolkit.Aspire.Hosting.NodeJS.Extensions.Tests/ResourceCreationTests.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,5 @@ public void ViteAppConfiguresPortFromEnvironment()
217217
var httpEndpoint = endpoints.FirstOrDefault(e => e.UriScheme == "http");
218218
Assert.NotNull(httpEndpoint);
219219
Assert.Equal("PORT", httpEndpoint.EnvVar);
220-
221-
// Verify that command line arguments callback is configured
222-
Assert.True(resource.TryGetAnnotationsOfType<CommandLineArgsCallbackAnnotation>(out var argsCallbacks));
223-
Assert.NotEmpty(argsCallbacks);
224220
}
225221
}

0 commit comments

Comments
 (0)