因此,我将UseWebpackDevMiddleware
HMR 用于更顺畅的开发过程 - 最后我恢复使用webpack-dev-server
脚步:
1) 将包添加到 package.json: "webpack-dev-server": "3.8.2",
2) 添加webpack.development.js
const merge = require('webpack-merge');
const common = require('./webpack.config.js');
const ExtractCssPlugin = require('mini-css-extract-plugin');
const webpackDevServerPort = 8083;
const proxyTarget = "http://localhost:8492";
module.exports = merge(common(), {
output: {
filename: "[name].js",
publicPath: '/dist/'
},
mode: 'development',
devtool: 'inline-source-map',
devServer: {
compress: true,
proxy: {
'*': {
target: proxyTarget
}
},
port: webpackDevServerPort
},
plugins: [
new ExtractCssPlugin({
filename: "[name].css",
chunkFilename: "[id].css"
})
]
});
请注意,此处的代理设置将用于代理到 ASP.Net 核心以进行 API 调用
修改launchSettings.json
为指向 webpack-dev-server:
"profiles": {
"VisualStudio: Connect to HotDev proxy": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "http://localhost:8083/",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:8492/"
}
}
(我在 webpack 中配置正确位置时也遇到了一些问题,发现这很有用
此外,还需要启动 webpack-dev-server,这可以通过 npm 脚本完成:
"scripts": {
"build:hotdev": "webpack-dev-server --config webpack.development.js --hot --inline",
然后这是自举的
app.UseSpa(spa =>
{
spa.Options.SourcePath = "ClientApp";
if (env.IsDevelopment())
{
spa.UseReactDevelopmentServer(npmScript: "build:hotdev");
}
});
(或者您可以安装 npm 任务运行器扩展和:
"-vs-binding": {
"ProjectOpened": [
"build:hotdev"
]
}
或者,我意识到您可以使用以下其他方式代理 - 这样,“dist”下的任何请求都将被推送到代理 webpack-dev-server
app.UseSpa(spa =>
{
spa.Options.SourcePath = "dist";
if (env.IsDevelopment())
{
// Ensure that you start webpack-dev-server - run "build:hotdev" npm script
// Also if you install the npm task runner extension then the webpack-dev-server script will run when the solution loads
spa.UseProxyToSpaDevelopmentServer("http://localhost:8083");
}
});
然后你不需要再从那个背面代理,可以只提供 /dist/ 内容 - 热和供应商预编译使用 web.config.js 这样的:
module.exports = merge(common(), {
output: {
filename: "[name].js",
publicPath: '/dist/',
},
mode: 'development',
devtool: 'inline-source-map',
devServer: {
compress: true,
port: 8083,
contentBase: path.resolve(__dirname,"wwwroot"),
},
plugins: [
new ExtractCssPlugin({
filename: "[name].css",
chunkFilename: "[id].css"
})
]
});