我正在使用带有 Typescript/React/SpaServices/Webpack/HMR 的 ASP.Net Core。更改 tsx 文件时,HMR 会替换浏览器中的代码。
我的问题是什么功能/程序正在监视文件的更改然后触发重建?webpack 是否使用节点在后台运行?如果是这样,我可以看到该进程正在运行吗?日志等?
我正在使用带有 Typescript/React/SpaServices/Webpack/HMR 的 ASP.Net Core。更改 tsx 文件时,HMR 会替换浏览器中的代码。
我的问题是什么功能/程序正在监视文件的更改然后触发重建?webpack 是否使用节点在后台运行?如果是这样,我可以看到该进程正在运行吗?日志等?
webpack 是否使用节点在后台运行?
是的,一点没错。这里发生了很多事情,但最终双方都webpack
在webpack-dev-middleware
处理这个问题。
它从调用 开始UseWebpackDevMiddleware
。例如:
app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions
{
HotModuleReplacement = true
});
正如我已经说过的,这里的幕后发生了很多事情,所以我将简要介绍一下其中的主要部分。UseWebpackDevMiddleware
这是来自( source )的重要代码行 :
nodeServices.InvokeExportAsync<WebpackDevServerInfo>(
nodeScript.FileName,
"createWebpackDevServer",
JsonConvert.SerializeObject(devServerOptions)).Result;
该InvokeExortAsync
函数是 ASP.NET Core 和 NodeJs 之间发生通信的地方。该nodeServices
变量是 的一个实例HttpNodeInstance
,它是 的子级OutOfProcessNodeInstance
。该类OutOfProcessNodeInstance
在构建时会生成一个 NodeJs 服务器,如下所示(source):
_nodeProcess = LaunchNodeProcess(startInfo);
This ends up running a NodeJs server using the entrypoint-http.js script file (source). This has a lot of code, but it ends up creating a HTTP server that listens for requests that come out of calls to InvokeExportAsync
.
The JavaScript function that is being invoked here, createWebpackDevServer
(source), looks like this, with exception-handling removed for brevity:
export function createWebpackDevServer(callback) {
let aspNetWebpack = require('aspnet-webpack');
return aspNetWebpack.createWebpackDevServer.apply(this, arguments);
}
There's a lot of code for the createWebpackDevServer
that is being invoked (source), so I won't include it here, but suffice to say it ends up running a connect server (source) that uses the webpack-dev-middleware (source).
I hope that gives enough of an explanation and a starting point for your own exploration.