refactor: apply default vs transform to xproj

refactor(spa-services): clean code

refactor(node-services): clean code, extract classes nto separate files

refactor(angular-services): prime cache cleanup
This commit is contained in:
Andrei Tserakhau
2016-05-23 11:28:42 +03:00
parent 2c35945562
commit 95cba7f5dd
32 changed files with 621 additions and 448 deletions

View File

@@ -9,22 +9,31 @@ using Microsoft.Extensions.PlatformAbstractions;
using Newtonsoft.Json;
// Putting in this namespace so it's always available whenever MapRoute is
namespace Microsoft.AspNetCore.Builder
{
public static class WebpackDevMiddleware
{
const string WebpackDevMiddlewareScheme = "http";
const string WebpackDevMiddlewareHostname = "localhost";
const string WebpackHotMiddlewareEndpoint = "/__webpack_hmr";
const string DefaultConfigFile = "webpack.config.js";
private const string WebpackDevMiddlewareScheme = "http";
private const string WebpackDevMiddlewareHostname = "localhost";
private const string WebpackHotMiddlewareEndpoint = "/__webpack_hmr";
private const string DefaultConfigFile = "webpack.config.js";
public static void UseWebpackDevMiddleware(this IApplicationBuilder appBuilder, WebpackDevMiddlewareOptions options = null) {
// Validate options
if (options == null) {
public static void UseWebpackDevMiddleware(
this IApplicationBuilder appBuilder,
WebpackDevMiddlewareOptions options = null)
{
// Prepare options
if (options == null)
{
options = new WebpackDevMiddlewareOptions();
}
if (options.ReactHotModuleReplacement && !options.HotModuleReplacement) {
throw new ArgumentException("To enable ReactHotModuleReplacement, you must also enable HotModuleReplacement.");
// Validate options
if (options.ReactHotModuleReplacement && !options.HotModuleReplacement)
{
throw new ArgumentException(
"To enable ReactHotModuleReplacement, you must also enable HotModuleReplacement.");
}
// Unlike other consumers of NodeServices, WebpackDevMiddleware dosen't share Node instances, nor does it
@@ -32,44 +41,55 @@ namespace Microsoft.AspNetCore.Builder
// because it must *not* restart when files change (if it did, you'd lose all the benefits of Webpack
// middleware). And since this is a dev-time-only feature, it doesn't matter if the default transport isn't
// as fast as some theoretical future alternative.
var hostEnv = (IHostingEnvironment)appBuilder.ApplicationServices.GetService(typeof (IHostingEnvironment));
var nodeServices = Configuration.CreateNodeServices(new NodeServicesOptions {
var hostEnv = (IHostingEnvironment)appBuilder.ApplicationServices.GetService(typeof(IHostingEnvironment));
var nodeServices = Configuration.CreateNodeServices(new NodeServicesOptions
{
HostingModel = NodeHostingModel.Http,
ProjectPath = hostEnv.ContentRootPath,
WatchFileExtensions = new string[] {} // Don't watch anything
WatchFileExtensions = new string[] { } // Don't watch anything
});
// Get a filename matching the middleware Node script
var script = EmbeddedResourceReader.Read(typeof (WebpackDevMiddleware), "/Content/Node/webpack-dev-middleware.js");
var script = EmbeddedResourceReader.Read(typeof(WebpackDevMiddleware),
"/Content/Node/webpack-dev-middleware.js");
var nodeScript = new StringAsTempFile(script); // Will be cleaned up on process exit
// Tell Node to start the server hosting webpack-dev-middleware
var devServerOptions = new {
var devServerOptions = new
{
webpackConfigPath = Path.Combine(hostEnv.ContentRootPath, options.ConfigFile ?? DefaultConfigFile),
suppliedOptions = options
};
var devServerInfo = nodeServices.InvokeExport<WebpackDevServerInfo>(nodeScript.FileName, "createWebpackDevServer", JsonConvert.SerializeObject(devServerOptions)).Result;
var devServerInfo =
nodeServices.InvokeExport<WebpackDevServerInfo>(nodeScript.FileName, "createWebpackDevServer",
JsonConvert.SerializeObject(devServerOptions)).Result;
// Proxy the corresponding requests through ASP.NET and into the Node listener
var proxyOptions = new ConditionalProxyMiddlewareOptions(WebpackDevMiddlewareScheme, WebpackDevMiddlewareHostname, devServerInfo.Port.ToString());
var proxyOptions = new ConditionalProxyMiddlewareOptions(WebpackDevMiddlewareScheme,
WebpackDevMiddlewareHostname, devServerInfo.Port.ToString());
appBuilder.UseMiddleware<ConditionalProxyMiddleware>(devServerInfo.PublicPath, proxyOptions);
// While it would be nice to proxy the /__webpack_hmr requests too, these return an EventStream,
// and the Microsoft.AspNetCore.Proxy code doesn't handle that entirely - it throws an exception after
// a while. So, just serve a 302 for those.
appBuilder.Map(WebpackHotMiddlewareEndpoint, builder => {
builder.Use(next => async ctx => {
ctx.Response.Redirect($"{ WebpackDevMiddlewareScheme }://{ WebpackDevMiddlewareHostname }:{ devServerInfo.Port.ToString() }{ WebpackHotMiddlewareEndpoint }");
appBuilder.Map(WebpackHotMiddlewareEndpoint, builder =>
{
builder.Use(next => async ctx =>
{
ctx.Response.Redirect(
$"{WebpackDevMiddlewareScheme}://{WebpackDevMiddlewareHostname}:{devServerInfo.Port.ToString()}{WebpackHotMiddlewareEndpoint}");
await Task.Yield();
});
});
}
#pragma warning disable CS0649
class WebpackDevServerInfo {
public int Port;
public string PublicPath;
#pragma warning disable CS0649
class WebpackDevServerInfo
{
public int Port { get; set; }
public string PublicPath { get; set; }
}
#pragma warning restore CS0649
}
#pragma warning restore CS0649
}
}