我正在创建一个使用node/express后端和react前端的web应用程序。我已经(我认为)完成了大部分工作,但是浏览器执行热刷新的最后一步并没有按预期工作。我将尝试在此处发布所有相关设置。如果您需要其他信息来查找我犯了什么错误,请告诉我:
我使用
在控制台中,启动时会列出以下行:
我使用
node ./server/index.js
启动我的应用程序。
webpack.config.js
var path = require('path');
var webpack = require('webpack);let webpackConfig = {
name: 'server',
output: {
path: path.join(__dirname, 'dist'),
filename: 'bundle.js',
publicPath: '/dist/',
},
resolve: {
extensions: [
'', '.js', '.jsx', '.json'
]
},
module: {
loaders: [
{
test: /\.(js|jsx)$/,
loader: 'babel-loader',
exclude: /node_modules/,
query:{
presets: ['es2015', 'react', 'stage-2']
}
},
{
test: /\.json$/,
loader: 'json-loader'
}
]
},
entry: [
'webpack-hot-middleware/client',
'./app/client/client.jsx'
],
plugins: [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production')
}
}),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin()
]
};
export default webpackConfig;
index.js 只包括 'babel-register' 和 'server.js'
server/server.js 导入 webpack 模块和 webpack 配置文件 '../webpack.config'; 导入 webpack-dev-middleware 中间件模块; 导入 webpack-hot-middleware 中间件模块;
import express from 'express';
const app = express();
const renderPage = () => {
return `
<!doctype html>
<html>
<head>
<title>Brewing Day</title>
<meta charset='utf-8'>
</head>
<body>
<h1>Hello from server.js!!</h1>
<div id='root'></div>
<script src='/dist/bundle.js'></script>
</body>
</html>
`;
};
const compiler = webpack(webpackConfig);
app.use(webpackDevMiddleware(compiler, {
noInfo: true,
publicPath: webpackConfig.output.publicPath })
);
app.use(webpackHotMiddleware(compiler));
app.use((req, res, next) => {
res.status(200).end(renderPage());
});
const server = app.listen(3005, () => {
const host = server.address().address;
const port = server.address().port;
console.log(`Listening at http://${host}:${port}`);
})
export default server;
这里需要翻译的是Webpack配置中的入口文件 app/client/client.jsx :
import React from 'react';
import ReactDOM from 'react-dom';
import Application from '../components/application.jsx';
window.addEventListener('load', () => {
ReactDOM.render(<Application />, document.getElementById('root')
);
});
在控制台中,启动时会列出以下行:
webpack built cc1194a11614a3ba54a3 in 730ms
当我对包含React组件的client.jsx
或application.jsx
进行更改时,我的控制台会出现两行新代码:
webpack building...
webpack built 8d340a8853d3cfe9478d in 195ms
目前为止,一切顺利!
但是,在浏览器中,它没有更新,并在控制台中给出以下警告:
[HMR] The following modules couldn't be hot updated: (Full reload needed)
This is usually because the modules which have changed (and their parents) do not know how to hot reload themselves. See http://webpack.github.io/docs/hot-module-replacement-with-webpack.html for more details.
[HMR] - ./app/components/application.jsx
我尝试随机添加 module.hot.accept()
到 application.jsx
。这样可以消除警告,但是没有更新,需要按 F5 刷新浏览器。
你有什么想法吗?我看到另一个设置几乎与我的相同,没有任何 module.hot.accept()
的调用,在那里这个函数可以正常工作,但我不知道我的设置与另一个设置有何差异。
非常感谢您的任何帮助。
application.jsx
导出了你的组件? - booleanhunterapplication.jsx
文件的最后一行是export default Application;
。 - Øyvind Bråthen