【发布时间】:2018-07-12 07:36:46
【问题描述】:
我正在使用 webpack 启动一个 React 项目并尝试让热加载工作。控制台输出显示App updated,但没有显示保存的更改(只是App.js 中div 标记之间的文本)。
例如,我将Hey sup 更改为Hey sup dude,但浏览器页面中的文本仍为Hey sup,尽管控制台说App is up to date. 我可以在手动刷新时看到更改页面。
我已尝试跟随 this React boilerplate,但无法让页面显示已保存的更改。
客户端/App.js
import React from 'react';
export default class App extends React.Component {
render() {
return (
<div>
Hey sup
</div>
);
}
}
webpack.dev.js
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const webpack = require('webpack');
module.exports = {
entry: [
'babel-polyfill',
'react-hot-loader/patch',
'webpack/hot/only-dev-server',
'./client/index.js'
],
devtool: 'inline-source-map',
devServer: {
contentBase: path.resolve(__dirname, 'dist'),
historyApiFallback: true,
hot: true
},
plugins: [
new CleanWebpackPlugin(['dist']),
new HtmlWebpackPlugin({
template: 'public/index.html'
}),
new webpack.NamedModulesPlugin(),
new webpack.HotModuleReplacementPlugin()
],
output: {
filename: '[name].bundle.js',
path: path.resolve(__dirname, 'dist'),
publicPath: '/'
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader',
options: {
cacheDirectory: true,
presets: [
'env',
'react'
],
plugins: [
'react-hot-loader/babel'
]
}
},
{
test: /\.s?css$/,
use: [
'style-loader',
'css-loader'
]
},
{
test: /\.(png|svg|jpg|gif)$/,
use: [
'file-loader'
]
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/,
use: [
'file-loader'
]
}
]
}
};
.babelrc
{
"presets": [
"env",
"react"
],
"plugins": [
"react-hot-loader/babel"
]
}
客户端/index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { AppContainer } from 'react-hot-loader';
import App from './App';
import './style.css';
const render = (Component) => {
ReactDOM.render(
<AppContainer>
<Component />
</AppContainer>,
document.getElementById('root')
);
};
render(App);
// Webpack Hot Module Replacement API
if (module.hot) {
module.hot.accept('./App', () => {
render(App);
});
}
public/index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Title</title>
</head>
<body>
<div id="root">
</div>
</body>
</html>
【问题讨论】:
-
您找到解决方案了吗?请添加它或选择一个答案,因为我有同样的问题。
-
我也面临同样的问题,如果有人找到解决方案,请帮助我。谢谢!!
标签: reactjs webpack react-hot-loader