Skip to content

Commit 28160c8

Browse files
Webpack template: Spawn webpack dev server in background
1 parent 02e3616 commit 28160c8

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/React.Template/reactnet-webpack/Program.cs

+32
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
using System.Diagnostics;
2+
using System.Threading;
3+
using System.Threading.Tasks;
14
using Microsoft.AspNetCore;
25
using Microsoft.AspNetCore.Hosting;
6+
using Microsoft.Extensions.Hosting;
7+
using Microsoft.Extensions.DependencyInjection;
8+
using System;
39

410
namespace React.Sample.Webpack.CoreMvc
511
{
@@ -13,6 +19,32 @@ public static void Main(string[] args)
1319
public static IWebHost BuildWebHost(string[] args) =>
1420
WebHost.CreateDefaultBuilder(args)
1521
.UseStartup<Startup>()
22+
.ConfigureServices((hostContext, service) =>
23+
{
24+
service.AddHostedService<WebpackDevServer>();
25+
})
1626
.Build();
27+
28+
private class WebpackDevServer : BackgroundService
29+
{
30+
protected override Task ExecuteAsync(CancellationToken token)
31+
{
32+
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
33+
{
34+
var devServer = Process.Start(new ProcessStartInfo
35+
{
36+
Arguments = "/c npm i && npm run build -- --watch",
37+
RedirectStandardError = false,
38+
RedirectStandardInput = false,
39+
RedirectStandardOutput = false,
40+
FileName = "cmd.exe"
41+
});
42+
43+
token.Register(() => devServer?.Kill(true));
44+
}
45+
46+
return Task.Delay(Timeout.Infinite, token);
47+
}
48+
}
1749
}
1850
}

0 commit comments

Comments
 (0)