本文介绍了使用webpack-dev实现Hot loader 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的项目中使用hot-react-loader。
所以我改变了一些文件来处理这个问题,但是当我编辑一个组件时出现错误。

I'm trying to use hot-react-loader in my project.So I changed some files to work with this but I get an error when I edit a component.

[HMR]无法应用更新。需要完全重新加载!

[HMR] Cannot apply update. Need to do a full reload!

我做错了什么?

var webpack               = require('webpack');
var path                  = require('path');
// Questo il plugin di webpack che mi genera  il file index.html in dist
var HtmlwebpackPlugin     = require('html-webpack-plugin');


const PATHS = {
    app: path.join(__dirname, 'app/index'),
    build: path.join(__dirname, 'dist')
};

module.exports = {
  devtool: 'eval',
    entry: [
    'webpack-dev-server/client?http://localhost:3500',
    'webpack/hot/only-dev-server',
    'react-hot-loader/patch',
    PATHS.app
  ],
    output: {
        path: PATHS.build,
        filename: 'js/bundle.js'
    },
    // Questo serve a non specificare le estensioni
    resolve: {
        extensions: ['', '.js']
    },
    module: {
        loaders: [
            {
                test: /\.js$/,
                loader: 'babel',
                exclude: /node_modules/
            },
            { test: /\.(png|jpg)$/, loader: "file?limit=1000&name=images/[hash].[ext]" },
            { test: /\.scss$/, loaders: [ 'style','css?modules&importLoaders=1&localIdentName=[path]___[name]__[local]___[hash:base64:5]' ] }
        ]
    },
    // Porta utilizzata da webpack-dev-server
    devServer: {
        port: 3500
    },
    plugins: [
    new webpack.HotModuleReplacementPlugin(),
        new HtmlwebpackPlugin({
            title: 'React Starter Kit',
            hash: true,
            inject: false,
            appMountId: 'app',
            template: 'jade!./app/assets/index.jade'
        })
    ]
};



server.js



server.js

var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server');
var config = require('./webpack.config.dev');

new WebpackDevServer(webpack(config), {
  hot: true,
  historyApiFallback: true,
  stats: {
    colors: true
  }
}).listen(3500, 'localhost', function (err) {
  if (err) {
    console.log(err);
  }
  console.log('Listening at localhost:3500');
});



index.js



index.js

import ReactDOM from 'react-dom';
import React from 'react';
import App from './app';
import { AppContainer } from 'react-hot-loader';

const root = document.getElementById('app');

ReactDOM.render(
  <AppContainer>
    <App />
  </AppContainer>
  , root);

if (module.hot) {
  module.hot.accept('./app', () => {
    const App = require('./app');

    ReactDOM.render(
      <AppContainer>
        <App />
      </AppContainer>
    , root);
  });
}



app.js



app.js

import React from 'react';
import { Router, browserHistory } from 'react-router';
import routes from './routes/routes';

const App = () => (
    <Router history={browserHistory} routes={routes} />
);

export default App;


推荐答案

您必须修改路由器组件的原型链本身强制渲染新东西。

You have to modify the prototype chain of the Router component itself to force render the new stuff.

// Router
import { Router, browserHistory } from 'react-router'

if (process.env.NODE_ENV === 'production') {
  // ...
} else {
  // the hacky workaround 
  Router.prototype.componentWillReceiveProps = function (nextProps) {
    let components = [];
    function grabComponents(routes) {
      routes.forEach((route) => {
        if (route.component) {
          components.push(route.component)
        }
        if (route.indexRoute && route.indexRoute.component) {
          components.push(route.indexRoute.component)
        }
        if (route.childRoutes) {
          grabComponents(route.childRoutes)
        }
      })
    }
    grabComponents(nextProps.routes)
    components.forEach(React.createElement) // force patching
  }
}

您在此处看到的代码是改编

The code you see here is an adaptation of https://github.com/gaearon/react-hot-boilerplate/pull/61#issuecomment-211504531

这篇关于使用webpack-dev实现Hot loader 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 06:59