我设置了代理来实现此目的:
您有一个常规的快速网络服务器,可以在任何路由上为index.html提供服务,除非它是资产路由。如果是资产,则请求将被代理到web-dev-server
您的反应热入口点仍将直接指向webpack开发服务器,因此热重装仍然有效。
假设您在8081上运行webpack-dev-server,在8080上运行代理。您的server.js文件将如下所示:
"use strict";
var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server');
var config = require('./make-webpack-config')('dev');
var express = require('express');
var proxy = require('proxy-middleware');
var url = require('url');
## --------your proxy----------------------
var app = express();
## proxy the request for static assets
app.use('/assets', proxy(url.parse('http://localhost:8081/assets')));
app.get('/*', function(req, res) {
res.sendFile(__dirname + '/index.html');
});
# -----your-webpack-dev-server------------------
var server = new WebpackDevServer(webpack(config), {
contentBase: __dirname,
hot: true,
quiet: false,
noInfo: false,
publicPath: "/assets/",
stats: { colors: true }
});
## run the two servers
server.listen(8081, "localhost", function() {});
app.listen(8080);
现在在webpack配置中设置入口点,如下所示:
entry: [
'./src/main.js',
'webpack/hot/dev-server',
'webpack-dev-server/client?http://localhost:8081'
]
请注意直接致电8081进行hotreload
还要确保将绝对网址传递给该output.publicPath
选项:
output: {
publicPath: "http://localhost:8081/assets/",
// ...
}