Webpack(使用React)热负载不起作用

时间:2017-03-25 04:18:53

标签: javascript webpack webpack-dev-server webpack-2

我已使用react-hot-loader设置热重新加载。在控制台中看起来它可以工作,但浏览器实际上并没有重新加载。为什么是这样?

enter image description here

忽略错误,我认为它们来自插件。它正确检测到我更改了App.jsx但浏览器没有更新,除非我进行完全刷新。

我的webpack.config.js如下:

const path = require("path")
const webpack = require("webpack")
const HtmlWebpackPlugin = require("html-webpack-plugin")
const ExtractTextPlugin = require("extract-text-webpack-plugin")

const context = path.resolve(__dirname, "src")
const {dependencies} = require("./package.json")

module.exports = {
  context,
  entry: {
    vendor: Object.keys(dependencies),
    app: [
      "react-hot-loader/patch",
      "./js/index.js"
    ]
  },
  output: {
    path: path.resolve(__dirname, "build"),
    filename: "[name]-[hash].js"
  },
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        loader: "babel-loader",
        exclude: /node_modules/,
        options: {
          plugins: [
            [
              "react-css-modules",
              {
                context
              }
            ]
          ]
        }
      },
      {
        test: /\.css$/,
        use: ExtractTextPlugin.extract({
          fallback: "style-loader",
          use: [
            {
              loader: "css-loader",
              options: {
                modules: true,
                importLoaders: 1,
                localIdentName: "[path]___[name]__[local]___[hash:base64:5]",
                sourceMap: true
              }
            },
            'postcss-loader'
          ]
        })
      }
    ]
  },
  plugins: [
    new webpack.optimize.CommonsChunkPlugin({
      name: "vendor"
    }),
    new HtmlWebpackPlugin({
      filename: "index.html",
      template: "index.html"
    }),
    new webpack.NamedModulesPlugin(),
    new ExtractTextPlugin("css/[name].css")
  ],
  devServer: {
    contentBase: path.resolve(__dirname, "src"),
    historyApiFallback: true
  },
  devtool: "eval"
}

index.js

import ReactDOM from "react-dom"
import React from "react"
import {AppContainer} from "react-hot-loader"
import App from "./App.jsx"

const render = Component => {
  ReactDOM.render(
    <AppContainer>
      <Component />
    </AppContainer>,
    document.getElementById("app")
  )
}

render(App)

if (module.hot) {
  module.hot.accept("./App.jsx", () => render(App))
}

0 个答案:

没有答案