mirror of
https://github.com/aspnet/JavaScriptServices.git
synced 2025-12-24 10:40:23 +00:00
WebpackDevMiddleware should run in a separate Node instance that doesn't restart when files change (otherwise there's no point in running it at all)
This commit is contained in:
@@ -3,24 +3,40 @@ using Microsoft.Extensions.PlatformAbstractions;
|
||||
|
||||
namespace Microsoft.AspNet.NodeServices {
|
||||
public static class Configuration {
|
||||
public static void AddNodeServices(this IServiceCollection serviceCollection, NodeHostingModel hostingModel = NodeHostingModel.Http) {
|
||||
private static string[] defaultWatchFileExtensions = new[] { ".js", ".jsx", ".ts", ".tsx", ".json", ".html" };
|
||||
|
||||
public static void AddNodeServices(this IServiceCollection serviceCollection, NodeServicesOptions options) {
|
||||
serviceCollection.AddSingleton(typeof(INodeServices), (serviceProvider) => {
|
||||
var appEnv = serviceProvider.GetRequiredService<IApplicationEnvironment>();
|
||||
return CreateNodeServices(hostingModel, appEnv.ApplicationBasePath);
|
||||
if (string.IsNullOrEmpty(options.ProjectPath)) {
|
||||
options.ProjectPath = appEnv.ApplicationBasePath;
|
||||
}
|
||||
return CreateNodeServices(options);
|
||||
});
|
||||
}
|
||||
|
||||
public static INodeServices CreateNodeServices(NodeHostingModel hostingModel, string projectPath)
|
||||
public static INodeServices CreateNodeServices(NodeServicesOptions options)
|
||||
{
|
||||
switch (hostingModel)
|
||||
var watchFileExtensions = options.WatchFileExtensions ?? defaultWatchFileExtensions;
|
||||
switch (options.HostingModel)
|
||||
{
|
||||
case NodeHostingModel.Http:
|
||||
return new HttpNodeInstance(projectPath);
|
||||
return new HttpNodeInstance(options.ProjectPath, /* port */ 0, watchFileExtensions);
|
||||
case NodeHostingModel.InputOutputStream:
|
||||
return new InputOutputStreamNodeInstance(projectPath);
|
||||
return new InputOutputStreamNodeInstance(options.ProjectPath);
|
||||
default:
|
||||
throw new System.ArgumentException("Unknown hosting model: " + hostingModel.ToString());
|
||||
throw new System.ArgumentException("Unknown hosting model: " + options.HostingModel.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class NodeServicesOptions {
|
||||
public NodeHostingModel HostingModel { get; set; }
|
||||
public string ProjectPath { get; set; }
|
||||
public string[] WatchFileExtensions { get; set; }
|
||||
|
||||
public NodeServicesOptions() {
|
||||
this.HostingModel = NodeHostingModel.Http;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,9 +2,12 @@
|
||||
// but simplifies things for the consumer of this module.
|
||||
var http = require('http');
|
||||
var path = require('path');
|
||||
var requestedPortOrZero = parseInt(process.argv[2]) || 0; // 0 means 'let the OS decide'
|
||||
var parsedArgs = parseArgs(process.argv);
|
||||
var requestedPortOrZero = parsedArgs.port || 0; // 0 means 'let the OS decide'
|
||||
|
||||
autoQuitOnFileChange(process.cwd(), ['.js', '.jsx', '.ts', '.tsx', '.json', '.html']);
|
||||
if (parsedArgs.watch) {
|
||||
autoQuitOnFileChange(process.cwd(), parsedArgs.watch.split(','));
|
||||
}
|
||||
|
||||
var server = http.createServer(function(req, res) {
|
||||
readRequestBodyAsJson(req, function(bodyJson) {
|
||||
@@ -75,3 +78,22 @@ function autoQuitOnFileChange(rootDir, extensions) {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function parseArgs(args) {
|
||||
// Very simplistic parsing which is sufficient for the cases needed. We don't want to bring in any external
|
||||
// dependencies (such as an args-parsing library) to this file.
|
||||
var result = {};
|
||||
var currentKey = null;
|
||||
args.forEach(function(arg) {
|
||||
if (arg.indexOf('--') === 0) {
|
||||
var argName = arg.substring(2);
|
||||
result[argName] = undefined;
|
||||
currentKey = argName;
|
||||
} else if (currentKey) {
|
||||
result[currentKey] = arg;
|
||||
currentKey = null;
|
||||
}
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -17,11 +17,19 @@ namespace Microsoft.AspNet.NodeServices {
|
||||
|
||||
private int _portNumber;
|
||||
|
||||
public HttpNodeInstance(string projectPath, int port = 0)
|
||||
: base(EmbeddedResourceReader.Read(typeof(HttpNodeInstance), "/Content/Node/entrypoint-http.js"), projectPath, port.ToString())
|
||||
public HttpNodeInstance(string projectPath, int port = 0, string[] watchFileExtensions = null)
|
||||
: base(EmbeddedResourceReader.Read(typeof(HttpNodeInstance), "/Content/Node/entrypoint-http.js"), projectPath, MakeCommandLineOptions(port, watchFileExtensions))
|
||||
{
|
||||
}
|
||||
|
||||
private static string MakeCommandLineOptions(int port, string[] watchFileExtensions) {
|
||||
var result = "--port " + port.ToString();
|
||||
if (watchFileExtensions != null && watchFileExtensions.Length > 0) {
|
||||
result += " --watch " + string.Join(",", watchFileExtensions);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public override async Task<T> Invoke<T>(NodeInvocationInfo invocationInfo) {
|
||||
await this.EnsureReady();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user