Skip to content
This repository was archived by the owner on Apr 8, 2020. It is now read-only.

Commit 6f684ef

Browse files
Set NODE_ENV automatically based on ASP.NET environment (or allow configuration when using outside ASP.NET hosting env). Fixes #230
1 parent 56cb898 commit 6f684ef

8 files changed

Lines changed: 42 additions & 22 deletions

File tree

‎samples/misc/LatencyTest/Program.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ private static async Task MeasureLatency(INodeServices nodeServices) {
3737

3838
private static INodeServices CreateNodeServices(NodeHostingModel hostingModel) {
3939
return Configuration.CreateNodeServices(new NodeServicesOptions {
40+
IsDevelopmentMode = true,
4041
HostingModel = hostingModel,
4142
ProjectPath = Directory.GetCurrentDirectory(),
4243
WatchFileExtensions = new string[] {} // Don't watch anything

‎src/Microsoft.AspNetCore.NodeServices/Configuration/Configuration.cs‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,18 @@ public static void AddNodeServices(this IServiceCollection serviceCollection, No
2020
{
2121
// Since this instance is being created through DI, we can access the IHostingEnvironment
2222
// to populate options.ProjectPath if it wasn't explicitly specified.
23+
var hostEnv = serviceProvider.GetRequiredService<IHostingEnvironment>();
2324
if (string.IsNullOrEmpty(options.ProjectPath))
2425
{
25-
var hostEnv = serviceProvider.GetRequiredService<IHostingEnvironment>();
2626
options.ProjectPath = hostEnv.ContentRootPath;
2727
}
2828

29+
// Similarly, we can take a default value for IsDevelopmentMode from the IHostingEnvironment
30+
if (!options.IsDevelopmentMode.HasValue)
31+
{
32+
options.IsDevelopmentMode = hostEnv.IsDevelopment();
33+
}
34+
2935
// Likewise, if no logger was specified explicitly, we should use the one from DI.
3036
// If it doesn't provide one, CreateNodeInstance will set up a default.
3137
if (options.NodeInstanceOutputLogger == null)
@@ -65,15 +71,16 @@ private static INodeInstance CreateNodeInstance(NodeServicesOptions options)
6571
{
6672
// Otherwise we'll construct the type of INodeInstance specified by the HostingModel property,
6773
// which itself has a useful default value.
74+
var isDevelopmentMode = options.IsDevelopmentMode.GetValueOrDefault(false);
6875
switch (options.HostingModel)
6976
{
7077
case NodeHostingModel.Http:
7178
return new HttpNodeInstance(options.ProjectPath, options.WatchFileExtensions, logger,
72-
options.LaunchWithDebugging, options.DebuggingPort, /* port */ 0);
79+
isDevelopmentMode, options.LaunchWithDebugging, options.DebuggingPort, /* port */ 0);
7380
case NodeHostingModel.Socket:
7481
var pipeName = "pni-" + Guid.NewGuid().ToString("D"); // Arbitrary non-clashing string
7582
return new SocketNodeInstance(options.ProjectPath, options.WatchFileExtensions, pipeName, logger,
76-
options.LaunchWithDebugging, options.DebuggingPort);
83+
isDevelopmentMode, options.LaunchWithDebugging, options.DebuggingPort);
7784
default:
7885
throw new ArgumentException("Unknown hosting model: " + options.HostingModel);
7986
}

‎src/Microsoft.AspNetCore.NodeServices/Configuration/NodeServicesOptions.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public NodeServicesOptions()
2222
public string[] WatchFileExtensions { get; set; }
2323
public ILogger NodeInstanceOutputLogger { get; set; }
2424
public bool LaunchWithDebugging { get; set; }
25+
public bool? IsDevelopmentMode { get; set; }
2526
public int? DebuggingPort { get; set; }
2627
}
2728
}

‎src/Microsoft.AspNetCore.NodeServices/HostingModels/HttpNodeInstance.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ internal class HttpNodeInstance : OutOfProcessNodeInstance
3434
private int _portNumber;
3535

3636
public HttpNodeInstance(string projectPath, string[] watchFileExtensions, ILogger nodeInstanceOutputLogger,
37-
bool launchWithDebugging, int? debuggingPort, int port = 0)
37+
bool isDevelopmentMode, bool launchWithDebugging, int? debuggingPort, int port = 0)
3838
: base(
3939
EmbeddedResourceReader.Read(
4040
typeof(HttpNodeInstance),
@@ -43,6 +43,7 @@ public HttpNodeInstance(string projectPath, string[] watchFileExtensions, ILogge
4343
watchFileExtensions,
4444
MakeCommandLineOptions(port),
4545
nodeInstanceOutputLogger,
46+
isDevelopmentMode,
4647
launchWithDebugging,
4748
debuggingPort)
4849
{

‎src/Microsoft.AspNetCore.NodeServices/HostingModels/OutOfProcessNodeInstance.cs‎

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public OutOfProcessNodeInstance(
4646
string[] watchFileExtensions,
4747
string commandLineArguments,
4848
ILogger nodeOutputLogger,
49+
bool isDevelopmentMode,
4950
bool launchWithDebugging,
5051
int? debuggingPort)
5152
{
@@ -58,7 +59,7 @@ public OutOfProcessNodeInstance(
5859
_entryPointScript = new StringAsTempFile(entryPointScript);
5960

6061
var startInfo = PrepareNodeProcessStartInfo(_entryPointScript.FileName, projectPath, commandLineArguments,
61-
launchWithDebugging, debuggingPort);
62+
isDevelopmentMode, launchWithDebugging, debuggingPort);
6263
_nodeProcess = LaunchNodeProcess(startInfo);
6364
_watchFileExtensions = watchFileExtensions;
6465
_fileSystemWatcher = BeginFileWatcher(projectPath);
@@ -99,7 +100,7 @@ public void Dispose()
99100
// This method is virtual, as it provides a way to override the NODE_PATH or the path to node.exe
100101
protected virtual ProcessStartInfo PrepareNodeProcessStartInfo(
101102
string entryPointFilename, string projectPath, string commandLineArguments,
102-
bool launchWithDebugging, int? debuggingPort)
103+
bool isDevelopmentMode, bool launchWithDebugging, int? debuggingPort)
103104
{
104105
string debuggingArgs;
105106
if (launchWithDebugging)
@@ -130,11 +131,16 @@ protected virtual ProcessStartInfo PrepareNodeProcessStartInfo(
130131
}
131132

132133
var nodePathValue = existingNodePath + Path.Combine(projectPath, "node_modules");
133-
#if NET451
134-
startInfo.EnvironmentVariables["NODE_PATH"] = nodePathValue;
135-
#else
136-
startInfo.Environment["NODE_PATH"] = nodePathValue;
137-
#endif
134+
SetEnvironmentVariable(startInfo, "NODE_PATH", nodePathValue);
135+
136+
// If there's no NODE_ENV variable, then set one based on the ASP.NET runtime environment
137+
var existingNodeEnv = Environment.GetEnvironmentVariable("NODE_ENV");
138+
if (string.IsNullOrEmpty(existingNodeEnv))
139+
{
140+
// These strings are the values recognized by Express.js and have become a de-facto standard in Node
141+
var nodeEnvValue = isDevelopmentMode ? "development" : "production";
142+
SetEnvironmentVariable(startInfo, "NODE_ENV", nodeEnvValue);
143+
}
138144

139145
return startInfo;
140146
}
@@ -179,6 +185,15 @@ private void EnsureFileSystemWatcherIsDisposed()
179185
}
180186
}
181187

188+
private static void SetEnvironmentVariable(ProcessStartInfo startInfo, string name, string value)
189+
{
190+
#if NET451
191+
startInfo.EnvironmentVariables[name] = value;
192+
#else
193+
startInfo.Environment[name] = value;
194+
#endif
195+
}
196+
182197
private static Process LaunchNodeProcess(ProcessStartInfo startInfo)
183198
{
184199
var process = Process.Start(startInfo);

‎src/Microsoft.AspNetCore.NodeServices/HostingModels/SocketNodeInstance.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ internal class SocketNodeInstance : OutOfProcessNodeInstance
3838
private VirtualConnectionClient _virtualConnectionClient;
3939

4040
public SocketNodeInstance(string projectPath, string[] watchFileExtensions, string socketAddress,
41-
ILogger nodeInstanceOutputLogger, bool launchWithDebugging, int? debuggingPort)
41+
ILogger nodeInstanceOutputLogger, bool isDevelopmentMode, bool launchWithDebugging, int? debuggingPort)
4242
: base(
4343
EmbeddedResourceReader.Read(
4444
typeof(SocketNodeInstance),
@@ -47,6 +47,7 @@ public SocketNodeInstance(string projectPath, string[] watchFileExtensions, stri
4747
watchFileExtensions,
4848
MakeNewCommandLineOptions(socketAddress),
4949
nodeInstanceOutputLogger,
50+
isDevelopmentMode,
5051
launchWithDebugging,
5152
debuggingPort)
5253
{

‎src/Microsoft.AspNetCore.SpaServices/Prerendering/PrerenderTagHelper.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public PrerenderTagHelper(IServiceProvider serviceProvider)
3535
{
3636
_nodeServices = _fallbackNodeServices = Configuration.CreateNodeServices(new NodeServicesOptions
3737
{
38+
IsDevelopmentMode = hostEnv.IsDevelopment(),
3839
ProjectPath = _applicationBasePath
3940
});
4041
}

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,8 @@ public static void UseWebpackDevMiddleware(
3535
"To enable ReactHotModuleReplacement, you must also enable HotModuleReplacement.");
3636
}
3737

38-
string projectPath;
39-
if (options.ProjectPath == null)
40-
{
41-
var hostEnv = (IHostingEnvironment)appBuilder.ApplicationServices.GetService(typeof(IHostingEnvironment));
42-
projectPath = hostEnv.ContentRootPath;
43-
}
44-
else
45-
{
46-
projectPath = options.ProjectPath;
47-
}
38+
var hostEnv = (IHostingEnvironment)appBuilder.ApplicationServices.GetService(typeof(IHostingEnvironment));
39+
var projectPath = options.ProjectPath ?? hostEnv.ContentRootPath;
4840

4941
// Unlike other consumers of NodeServices, WebpackDevMiddleware dosen't share Node instances, nor does it
5042
// use your DI configuration. It's important for WebpackDevMiddleware to have its own private Node instance
@@ -53,6 +45,7 @@ public static void UseWebpackDevMiddleware(
5345
// as fast as some theoretical future alternative.
5446
var nodeServices = Configuration.CreateNodeServices(new NodeServicesOptions
5547
{
48+
IsDevelopmentMode = hostEnv.IsDevelopment(),
5649
ProjectPath = projectPath,
5750
WatchFileExtensions = new string[] { } // Don't watch anything
5851
});

0 commit comments

Comments
 (0)