【发布时间】:2019-02-05 20:05:41
【问题描述】:
我正在运行一个带有用 TypeScript 编写的 React 前端的 ASP.NET Core 网站。我已经在后端中间件中设置了 HMR:
app.UseWebpackDevMiddleware(new Microsoft.AspNetCore.SpaServices.Webpack.WebpackDevMiddlewareOptions
{
HotModuleReplacement = true
});
和我的webpack.config.js 文件是这样的:
const path = require('path');
module.exports = {
mode: 'development',
entry: { main: './scripts/app.tsx' },
output: {
path: path.resolve(__dirname, './wwwroot/js/dist'),
filename: 'bundle.js',
publicPath: '/dist/'
},
resolve: {
extensions: ['*', '.js', '.jsx', '.tsx']
},
module: {
rules: [
{
test: /\.ts|\.tsx$/, include: /scripts/,
use: [
{
loader: 'babel-loader',
options: {
"plugins" : ['react-hot-loader/babel'],
"presets": ["@babel/preset-env", "@babel/preset-react", "@babel/preset-typescript"]
}
}
]
}
]
}
};
webpack 指向这个文件,app.tsx:
import * as React from "react";
import * as ReactDOM from "react-dom";
import { Application } from "./Application";
ReactDOM.render(
<div><Application /></div>,
document.getElementById("example")
)
if (module.hot) {
module.hot.accept()
}
我在网页中使用了bundle.js 文件。当我运行 Web 服务器并浏览到该页面时,我将 [HMR] connected 登录到控制台。当我编辑并保存 app.tsx 文件时,我得到了预期的输出:
client.js:234 [HMR] bundle rebuilding
client.js:242 [HMR] bundle rebuilt in 69ms
process-update.js:39 [HMR] Checking for updates on the server...
process-update.js:112 [HMR] Updated modules:
process-update.js:114 [HMR] - ./scripts/app.tsx
process-update.js:119 [HMR] App is up to date.
但是,当我编辑并保存 application.tsx 时,(其中包括
module.hot.accept() 在底部)我什么也没得到 - 网络服务器和浏览器都没有任何输出。
我认为这可能是因为 HMR 配置为仅查看 app.tsx 文件:
[0] multi webpack-hot-middleware/client?path=__webpack_hmr&dynamicPublicPath=true ./scripts/app.tsx 40 bytes {main}
有人知道这里可能出现什么问题吗?它适用于在 webpack 配置文件中明确声明的文件,但不适用于它正在导入的模块 - 不应该自动包含其他文件吗?根据https://webpack.js.org/guides/hot-module-replacement/,我认为我已经正确设置了所有内容,但我无法完全使用它,因为使用 ASP.NET Core 中间件的配置不同。
提前致谢。
【问题讨论】:
标签: reactjs typescript asp.net-core webpack webpack-hmr