Skip to content

Commit 2f588df

Browse files
author
Matt Cole
committed
Add environment variables to webpack dev middleware options
1 parent d76b013 commit 2f588df

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

src/Microsoft.AspNetCore.SpaServices/Webpack/WebpackDevMiddleware.cs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,7 @@ public static void UseWebpackDevMiddleware(
4040
// because it must *not* restart when files change (if it did, you'd lose all the benefits of Webpack
4141
// middleware). And since this is a dev-time-only feature, it doesn't matter if the default transport isn't
4242
// as fast as some theoretical future alternative.
43-
var nodeServicesOptions = new NodeServicesOptions(appBuilder.ApplicationServices);
44-
nodeServicesOptions.WatchFileExtensions = new string[] {}; // Don't watch anything
45-
if (!string.IsNullOrEmpty(options.ProjectPath))
46-
{
47-
nodeServicesOptions.ProjectPath = options.ProjectPath;
48-
}
49-
43+
var nodeServicesOptions = CreateNodeServicesOptions(appBuilder.ApplicationServices, options);
5044
var nodeServices = NodeServicesFactory.CreateNodeServices(nodeServicesOptions);
5145

5246
// Get a filename matching the middleware Node script
@@ -91,6 +85,26 @@ public static void UseWebpackDevMiddleware(
9185
});
9286
}
9387

88+
private static NodeServicesOptions CreateNodeServicesOptions(
89+
IServiceProvider serviceProvider, WebpackDevMiddlewareOptions options)
90+
{
91+
var nodeServicesOptions = new NodeServicesOptions(serviceProvider)
92+
{
93+
WatchFileExtensions = new string[] {} // Don't watch anything
94+
};
95+
96+
if (!string.IsNullOrEmpty(options.ProjectPath))
97+
{
98+
nodeServicesOptions.ProjectPath = options.ProjectPath;
99+
}
100+
if (options.EnvironmentVariables != null)
101+
{
102+
nodeServicesOptions.EnvironmentVariables = options.EnvironmentVariables;
103+
}
104+
105+
return nodeServicesOptions;
106+
}
107+
94108
#pragma warning disable CS0649
95109
class WebpackDevServerInfo
96110
{

src/Microsoft.AspNetCore.SpaServices/Webpack/WebpackDevMiddlewareOptions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Collections.Generic;
2+
13
namespace Microsoft.AspNetCore.SpaServices.Webpack
24
{
35
public class WebpackDevMiddlewareOptions
@@ -7,5 +9,6 @@ public class WebpackDevMiddlewareOptions
79
public bool ReactHotModuleReplacement { get; set; }
810
public string ConfigFile { get; set; }
911
public string ProjectPath { get; set; }
12+
public IDictionary<string, string> EnvironmentVariables { get; set; }
1013
}
1114
}

0 commit comments

Comments
 (0)