參考文章javascript
依賴webpack-dev-serverjava
$ npm i --save-dev webpack-dev-server $ npm i --save-dev react-hot-loader
新建server.jsnode
var webpack = require('webpack'); var WebpackDevServer = require('webpack-dev-server'); var config = require('./webpack.config'); new WebpackDevServer(webpack(config), { publicPath: config.output.publicPath, hot: true, historyApiFallback: true }).listen(3000, 'localhost', function (err, result) { if (err) { return console.log(err); } console.log('Listening at http://localhost:3000/') });
配置webpack.config.jsreact
var path = require('path'); var webpack = require('webpack'); module.exports = { devtool: 'source-map', entry: [ 'react-hot-loader/patch', 'webpack-dev-server/client?http://0.0.0.0:3000', // WebpackDevServer host and port 'webpack/hot/only-dev-server', // "only" prevents reload on syntax errors './index.js' // Your appʼs entry point ], output: { path: __dirname, filename: 'bundle.js', publicPath: '/static/' }, plugins: [ new webpack.HotModuleReplacementPlugin() ], module: { loaders: [ { test: /.jsx?$/, loader: 'babel-loader', exclude: /node_modules/ } ] } };
package.jsonwebpack
"scripts": { "start": "node server.js" },
.babelrcgit
{ "presets": ["react", "es2015"], "plugins": ["react-hot-loader/babel"] }
$ npm run start
經過http://localhost:3000來訪問github
控制檯顯示以下所示:web
修改組件中的文本,觀察控制檯以下所示:npm
證實局部熱更新已搭建完畢。json
$ git clone https://github.com/doudounannan/react-redux.git $ cd react-redux $ git checkout react-hot-loader-demo $ cd demo/react-hot-loader-demo $ npm run start