asp.net core mvc 中间件之WebpackDevMiddleware

  • WebpackDevMiddleware中间件主要用于开发SPA应用,启用Webpack,增强网页开发体验。好吧,你想用来干嘛就干嘛,这次主要是通过学习该中间件,学习如何在core中启用Webpack支持
  • 通过上上篇asp.net core mvc 管道之中间件,大致可以了解中间件是什么东西,现在就以中间件为单位,一个一个点学习各种中间件,了解并掌握,最后学会自己写中间件
  • 该中间件源码

说明

WebpackDevMiddleware

  • Enables Webpack dev middleware support. This hosts an instance of the Webpack compiler in memory

    in your application so that you can always serve up-to-date Webpack-built resources without having

    to run the compiler manually. Since the Webpack compiler instance is retained in memory, incremental

    compilation is vastly faster that re-running the compiler from scratch.

Incoming requests that match Webpack-built files will be handled by returning the Webpack compiler

output directly, regardless of files on disk. If compilation is in progress when the request arrives,

the response will pause until updated compiler output is ready.

  • 大概意思是Webpack编译器实例存在于内存,始终提供最新编译的资源,增量编译比重新编译速度要快得多。任何请求Webpack编译后的文件,都原样返回,如果请求到达时编译没完成,响应将暂停,直到编译完成,输出准备就绪

NodeServices

  • Unlike other consumers of NodeServices, WebpackDevMiddleware dosen't share Node instances, nor does it

    use your DI configuration. It's important for WebpackDevMiddleware to have its own private Node instance

    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.

  • WebpackDevMiddleware不共享Node实例,也不共享使用的DI配置。因为文件改变时Node服务不能重启。这是个开发时使用的功能,所以传输速度可能不会很快

分析

  • 创建Node实例
var nodeServicesOptions = new NodeServicesOptions(appBuilder.ApplicationServices);
var nodeServices = NodeServicesFactory.CreateNodeServices(nodeServicesOptions);
  • 创建devServerOptions,包含设置webpack.config.js的路径以及整合在Stratup.cs的设置、模块热加载断点等。这些设置需要传到Nodeaspnet-webpack模块,如果是自定义的模块,那么参数也是自己定义啦
var devServerOptions = new
{
webpackConfigPath = Path.Combine(nodeServicesOptions.ProjectPath, options.ConfigFile ?? DefaultConfigFile),
suppliedOptions = options,
understandsMultiplePublicPaths = true,
hotModuleReplacementEndpointUrl = hmrEndpoint
};
  • 下面是通过nodeServices,执行指定aspnet-webpack模块里面的方法并得到结果。参数分别是模块文件路径、要调用的方法、传递的参数。传递给js模块的参数先序列成json字符串,模块接收参数后再反序列化成对象
var devServerInfo =
nodeServices.InvokeExportAsync<WebpackDevServerInfo>(nodeScript.FileName, "createWebpackDevServer",
JsonConvert.SerializeObject(devServerOptions, jsonSerializerSettings)).Result;
  • 返回结果是Webpack编译之后的输出目录,循环输出目录,添加请求代理,代理到所有输出目录。超时时间100s, /__webpack_hmr无限超时
  • 代理源码
foreach (var publicPath in devServerInfo.PublicPaths)
{
appBuilder.UseProxyToLocalWebpackDevMiddleware(publicPath + hmrEndpoint, devServerInfo.Port, Timeout.InfiniteTimeSpan);
appBuilder.UseProxyToLocalWebpackDevMiddleware(publicPath, devServerInfo.Port, TimeSpan.FromSeconds(100));
} // Note that this is hardcoded to make requests to "localhost" regardless of the hostname of the
// server as far as the client is concerned. This is because ConditionalProxyMiddlewareOptions is
// the one making the internal HTTP requests, and it's going to be to some port on this machine
// because aspnet-webpack hosts the dev server there. We can't use the hostname that the client
// sees, because that could be anything (e.g., some upstream load balancer) and we might not be
// able to make outbound requests to it from here.
// Also note that the webpack HMR service always uses HTTP, even if your app server uses HTTPS,
// because the HMR service has no need for HTTPS (the client doesn't see it directly - all traffic
// to it is proxied), and the HMR service couldn't use HTTPS anyway (in general it wouldn't have
// the necessary certificate).
var proxyOptions = new ConditionalProxyMiddlewareOptions(
"http", "localhost", proxyToPort.ToString(), requestTimeout);
appBuilder.UseMiddleware<ConditionalProxyMiddleware>(publicPath, proxyOptions);

结果

  • 创建自己的中间件,自定义配置,运行Webpack服务,只需要创建Node实例,调用自己写的模块即可。模块根据传过来的配置运行服务即可
  • 关键方法
var nodeServicesOptions = new NodeServicesOptions(appBuilder.ApplicationServices); // node配置

var nodeServices = NodeServicesFactory.CreateNodeServices(nodeServicesOptions); // 创建node实例 

// dev服务配置
var devServerOptions = new
{
webpackConfigPath = Path.Combine(nodeServicesOptions.ProjectPath, options.ConfigFile ?? DefaultConfigFile),
suppliedOptions = options,
understandsMultiplePublicPaths = true,
hotModuleReplacementEndpointUrl = hmrEndpoint
}; // 调用js模块,运行dev服务,返回输出目录
var devServerInfo =
nodeServices.InvokeExportAsync<WebpackDevServerInfo>(nodeScript.FileName, "createWebpackDevServer",
JsonConvert.SerializeObject(devServerOptions, jsonSerializerSettings)).Result; // 添加输出目录到代理
foreach (var publicPath in devServerInfo.PublicPaths)
{
appBuilder.UseProxyToLocalWebpackDevMiddleware(publicPath + hmrEndpoint, devServerInfo.Port, Timeout.InfiniteTimeSpan);
appBuilder.UseProxyToLocalWebpackDevMiddleware(publicPath, devServerInfo.Port, TimeSpan.FromSeconds(100));
} private static void UseProxyToLocalWebpackDevMiddleware(this IApplicationBuilder appBuilder, string publicPath, int proxyToPort, TimeSpan requestTimeout)
{
var proxyOptions = new ConditionalProxyMiddlewareOptions(
"http", "localhost", proxyToPort.ToString(), requestTimeout);
appBuilder.UseMiddleware<ConditionalProxyMiddleware>(publicPath, proxyOptions);
}

示例

  • 待更新...

asp.net core mvc 中间件之WebpackDevMiddleware的更多相关文章

  1. asp.net core mvc 中间件之路由

    asp.net core mvc 中间件之路由 路由中间件 首先看路由中间件的源码 先用httpContext实例化一个路由上下文,然后把中间件接收到的路由添加到路由上下文的路由集合 然后把路由上下文 ...

  2. asp.net core mvc 管道之中间件

    asp.net core mvc 管道之中间件 http请求处理管道通过注册中间件来实现各种功能,松耦合并且很灵活 此文简单介绍asp.net core mvc中间件的注册以及运行过程 通过理解中间件 ...

  3. ASP.NET Core MVC/WebAPi如何构建路由?

    前言 本节我们来讲讲ASP.NET Core中的路由,在讲路由之前我们首先回顾下之前所讲在ASP.NET Core中的模型绑定这其中有一个问题是我在项目当中遇见的,我们下面首先来看看这个问题. 回顾A ...

  4. asp.net core mvc权限控制:权限控制介绍

    在进行业务软件开发的时候,都会涉及到权限控制的问题,asp.net core mvc提供了相关特性. 在具体介绍使用方法前,我们需要先了解几个概念: 1,claim:英文翻译过来是声明的意思,一个cl ...

  5. ASP.NET Core MVC 中设置全局异常处理方式

    在asp.net core mvc中,如果有未处理的异常发生后,会返回http500错误,对于最终用户来说,显然不是特别友好.那如何对于这些未处理的异常显示统一的错误提示页面呢? 在asp.net c ...

  6. asp.net core mvc剖析:启动流程

    asp.net core mvc是微软开源的跨平台的mvc框架,首先它跟原有的MVC相比,最大的不同就是跨平台,然后又增加了一些非常实用的新功能,比如taghelper,viewcomponent,D ...

  7. ASP.NET Core MVC 源码学习:Routing 路由

    前言 最近打算抽时间看一下 ASP.NET Core MVC 的源码,特此把自己学习到的内容记录下来,也算是做个笔记吧. 路由作为 MVC 的基本部分,所以在学习 MVC 的其他源码之前还是先学习一下 ...

  8. ASP.NET Core MVC 源码学习:MVC 启动流程详解

    前言 在 上一篇 文章中,我们学习了 ASP.NET Core MVC 的路由模块,那么在本篇文章中,主要是对 ASP.NET Core MVC 启动流程的一个学习. ASP.NET Core 是新一 ...

  9. ASP.NET Core MVC 控制器创建与依赖注入

    本文翻译自<Controller activation and dependency injection in ASP.NET Core MVC>,由于水平有限,故无法保证翻译完全准确,欢 ...

随机推荐

  1. List<? extends T>和List<? super T>之间有什么区别?

    List<? extends T>表示类型的上界为T,即参数化的类型可能是T也可能是T的子类.<? extends T>被设计用来读数据的泛型,只能读取类型为T的元素. Lis ...

  2. 在HashTable上下文中,同步指的是什么?

    同步意味着在一个时间点只能有一个线程可以修改hash表,任何线程在执行HashTable的更新操作前都需要获取对象锁,其他线程需要等带锁的释放.

  3. PHP使用swoole来实现实时异步任务队列

    转载来自第七星尘的技术博客的<PHP使用swoole来实现实时异步任务队列> 关于异步任务队列 用户打开了我们的网站.他要做的就是勾选需要发邮件的代理商列表,然后把结算邮件发出去.假如我们 ...

  4. redis 和 kookeeper 连用 构建 redis集群

    转载地址:https://www.zhihu.com/question/62598701

  5. js中实现 复制到剪切板 功能

    一:引包 <script type="text/javascript" src="jquery.js"></script> <sc ...

  6. 哈希与字典树与KMP

    hash讲解 主要记录hash的公式: ; i<=len; i++) { Hash[i]=(Hash[i-]*)%mod)%mod; } 求hash的公式是这个,怎么求一小段的hash值呢? ; ...

  7. UGUI控制UI的显示层级

    1.调用transform.SetAsLastSibling();将该UI的显示层级调到最上面. 调用transform.SetAsFirstSibling();将该UI的显示层级调到最下面. 2. ...

  8. KBMMW 4.84.00 发布

    kbmMW is a portable, highly scalable, high end application server and enterprise architecture integr ...

  9. ubuntu安装vmare tools

    在vm中安装vm tools, 点击安装 vmware tools cp  VMwareTools-10.0.10-4301679.tar.gz  /home/YOURNAME/    //因为cd ...

  10. Codeforces 1111 简要题解

    文章目录 A题 B题 C题 D题 E题 传送门 A题 传送门 题意简述:把262626个英文字母分成两类A,BA,BA,B,AAA类字符可以转成AAA类字符,BBB类字符可以转成BBB类字符,问给出的 ...