博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
asp.net core mvc 中间件之WebpackDevMiddleware
阅读量:6442 次
发布时间:2019-06-23

本文共 5246 字,大约阅读时间需要 17 分钟。

asp.net core mvc 中间件之WebpackDevMiddleware

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

说明

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
(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
(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
(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
(publicPath, proxyOptions);}

示例

  • 待更新...
用心做好每一件事,结果会给你最大的惊喜!

转载地址:http://zscwo.baihongyu.com/

你可能感兴趣的文章
工商业务畅通保障 新华三安全设备“出彩”中原
查看>>
区块链并不存在泡沫,但事实比我们想象得要复杂
查看>>
Java中String/StringBuffer/StringBuilder区别(转)
查看>>
北达软微服务架构设计与实践圆满结束
查看>>
leetcode算法题解(Java版)-4-动态规划(杨辉三角问题)
查看>>
(C#)Windows Shell 外壳编程系列7 - ContextMenu 注册文件右键菜单
查看>>
vuejs6-v-else指令
查看>>
求一个表中某个分类前几条的记录(这样做效率其实不高,只是做记录一下)
查看>>
Hibernate5-一对多双向关联-迫切左外连接-HQL
查看>>
SVN删除tags旧版本失败
查看>>
浅谈TCP/IP网络编程中socket的行为
查看>>
Android基础之——startActivityForResult启动界面并返回数据,上传头像
查看>>
mysql 主主配置
查看>>
SVN中对repo中的某一目录设置钩子
查看>>
nginx带宽资源调度脚本
查看>>
nginx中location配置
查看>>
Java Thread.join()详解
查看>>
MySQL 记一次 Bug发现过程
查看>>
.bash_profile和.bashrc的区别(如何设置生效)
查看>>
图像滤镜艺术---保留细节的磨皮滤镜之PS实现
查看>>