Skip to content

Commit c0d5078

Browse files
Copilotaaronpowelldavidfowl
authored
Fix AddViteApp port configuration to use Aspire-assigned port (#724)
* Initial plan for issue * Implement AddViteApp port configuration with command line arguments Co-authored-by: aaronpowell <434140+aaronpowell@users.noreply.github.com> * Finalize AddViteApp port configuration and update example Co-authored-by: aaronpowell <434140+aaronpowell@users.noreply.github.com> * Improving on Copilots take * Keeping PORT on env vars * Simplify AddViteApp port configuration by using vite.config.ts Co-authored-by: davidfowl <95136+davidfowl@users.noreply.github.com> * Simplifying the code * Fixing test * Reverting a copilot change * Using an endpoint reference expression which is a better approach for getting the port * Think we only need -- for npm, not other package managers * Cleaning up using statements * Remove automatic WithExternalHttpEndpoints from AddViteApp to let users decide Co-authored-by: davidfowl <95136+davidfowl@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: aaronpowell <434140+aaronpowell@users.noreply.github.com> Co-authored-by: Aaron Powell <me@aaron-powell.com> Co-authored-by: davidfowl <95136+davidfowl@users.noreply.github.com>
1 parent a19aff3 commit c0d5078

File tree

5 files changed

+56
-18
lines changed

5 files changed

+56
-18
lines changed

examples/nodejs-ext/pnpm-demo/package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
"version": "0.0.0",
55
"type": "module",
66
"scripts": {
7-
"dev": "run-script-os",
8-
"dev:windows": "vite dev --port %PORT%",
9-
"dev:default": "vite dev --port $PORT",
7+
"dev": "vite dev",
108
"build": "tsc -b && vite build",
119
"lint": "eslint .",
1210
"preview": "vite preview"

examples/nodejs-ext/vite-demo/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
"version": "0.0.0",
55
"type": "module",
66
"scripts": {
7-
"dev": "run-script-os",
8-
"dev:windows": "vite dev --port %PORT%",
9-
"dev:default": "vite dev --port $PORT",
7+
"dev": "vite dev",
108
"build": "tsc -b && vite build",
119
"lint": "eslint .",
1210
"preview": "vite preview"
@@ -30,3 +28,4 @@
3028
"vite": "^6.3.4"
3129
}
3230
}
31+

examples/nodejs-ext/yarn-demo/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
"version": "0.0.0",
55
"type": "module",
66
"scripts": {
7-
"dev": "run-script-os",
8-
"dev:windows": "vite dev --port %PORT%",
9-
"dev:default": "vite dev --port $PORT",
7+
"dev": "vite dev",
108
"build": "tsc -b && vite build",
119
"lint": "eslint .",
1210
"preview": "vite preview"
@@ -30,3 +28,4 @@
3028
"vite": "^6.3.4"
3129
}
3230
}
31+

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
using Aspire.Hosting.ApplicationModel;
22
using Aspire.Hosting.Lifecycle;
33
using CommunityToolkit.Aspire.Hosting.NodeJS.Extensions;
4-
using Microsoft.Extensions.Hosting;
5-
using Microsoft.Extensions.DependencyInjection;
64
using CommunityToolkit.Aspire.Utils;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using Microsoft.Extensions.Hosting;
77

88
namespace Aspire.Hosting;
99

@@ -37,9 +37,21 @@ public static IResourceBuilder<NodeAppResource> AddViteApp(this IDistributedAppl
3737
_ => builder.AddNpmApp(name, wd, "dev")
3838
};
3939

40-
return useHttps
41-
? resource.WithHttpsEndpoint(env: "PORT").WithExternalHttpEndpoints()
42-
: resource.WithHttpEndpoint(env: "PORT").WithExternalHttpEndpoints();
40+
_ = useHttps
41+
? resource.WithHttpsEndpoint(env: "PORT")
42+
: resource.WithHttpEndpoint(env: "PORT");
43+
44+
return resource.WithArgs(ctx =>
45+
{
46+
if (packageManager == "npm")
47+
{
48+
ctx.Args.Add("--");
49+
}
50+
51+
var targetEndpoint = resource.Resource.GetEndpoint(useHttps ? "https" : "http");
52+
ctx.Args.Add("--port");
53+
ctx.Args.Add(targetEndpoint.Property(EndpointProperty.TargetPort));
54+
});
4355
}
4456

4557
/// <summary>

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

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using Aspire.Hosting;
2-
using Microsoft.AspNetCore.Http;
3-
using System.Diagnostics;
42

53
namespace CommunityToolkit.Aspire.Hosting.NodeJS.Extensions.Tests;
64

@@ -141,7 +139,7 @@ public void ViteAppHasExposedHttpsEndpoints()
141139

142140

143141
[Fact]
144-
public void ViteAppHasExposedExternalHttpEndpoints()
142+
public void ViteAppDoesNotExposeExternalHttpEndpointsByDefault()
145143
{
146144
var builder = DistributedApplication.CreateBuilder();
147145

@@ -157,7 +155,7 @@ public void ViteAppHasExposedExternalHttpEndpoints()
157155

158156
Assert.True(resource.TryGetAnnotationsOfType<EndpointAnnotation>(out var endpoints));
159157

160-
Assert.Contains(endpoints, e => e.IsExternal);
158+
Assert.DoesNotContain(endpoints, e => e.IsExternal);
161159
}
162160

163161
[Fact]
@@ -195,4 +193,36 @@ public void WithNpmPackageInstallationCanUseCICommand()
195193
var resource = Assert.Single(appModel.Resources.OfType<NodeAppResource>());
196194
Assert.Equal("npm", resource.Command);
197195
}
196+
197+
[Fact]
198+
public void ViteAppConfiguresPortFromEnvironment()
199+
{
200+
var builder = DistributedApplication.CreateBuilder();
201+
202+
builder.AddViteApp("vite");
203+
204+
using var app = builder.Build();
205+
206+
var appModel = app.Services.GetRequiredService<DistributedApplicationModel>();
207+
208+
var resource = Assert.Single(appModel.Resources.OfType<NodeAppResource>());
209+
210+
// Verify that command line arguments callback is configured
211+
Assert.True(resource.TryGetAnnotationsOfType<CommandLineArgsCallbackAnnotation>(out var argsCallbackAnnotations));
212+
List<object> args = [];
213+
var ctx = new CommandLineArgsCallbackContext(args);
214+
215+
foreach (var annotation in argsCallbackAnnotations)
216+
{
217+
annotation.Callback(ctx);
218+
}
219+
220+
Assert.Collection(args,
221+
arg => Assert.Equal("run", arg),
222+
arg => Assert.Equal("dev", arg),
223+
arg => Assert.Equal("--", arg),
224+
arg => Assert.Equal("--port", arg),
225+
arg => Assert.IsType<EndpointReferenceExpression>(arg)
226+
);
227+
}
198228
}

0 commit comments

Comments
 (0)