【问题标题】:How to solve "The following modules couldn't be hot updated: (They would need a full reload!)"如何解决“以下模块无法热更新:(它们需要完全重新加载!)”
【发布时间】:2019-06-18 11:37:56
【问题描述】:

构建一个 React 应用来学习,发现 webpack 可以帮助我处理 HMR。

但是当我更改组件(JSX)中的某些内容时,它不会更新并给我以下信息:

以下模块无法热更新:(它们需要完整的 重新加载!)

log.js:26 忽略对未接受模块 ./src/App.js 的更新 -> ./src/index.js -> 0

代码:

网页包:

const path = require("path");
const webpack = require("webpack");
var HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  entry: "./src/index.js",
  mode: "development",
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /(node_modules|bower_components)/,
        loader: "babel-loader",
        options: { presets: ["@babel/env"] }
      },
      {
        test: /\.css$/,
        use: ["style-loader", "css-loader"]
      }
    ]
  },
  resolve: { extensions: ["*", ".js", ".jsx"] },
  output: {
    path: path.resolve(__dirname, "dist/"),
    publicPath: "/dist/",
    filename: "bundle.js"
  },
  // devServer: {
  //   contentBase: path.join(__dirname, "public/"),
  //   port: 3000,
  //   publicPath: "http://localhost:3000/dist/",
  //   hotOnly: true,
  //   historyApiFallback: true
  // },
  // plugins: [new webpack.HotModuleReplacementPlugin()]
  plugins: [new HtmlWebpackPlugin({
    template: './public/index.html',
    filename: 'index.html',
    inject: 'body'
}),
new webpack.HotModuleReplacementPlugin()],
devServer: {
    contentBase: path.join(__dirname, "public/"),
    port: 3000,
    publicPath: "http://localhost:3000/dist/",
    historyApiFallback: true,
    hotOnly: true
}
};

json:

{
  "name": "reactpluralsight",
  "version": "1.0.0",
  "description": "PluralSightTutorial",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "webpack-dev-server --open"
  },
  "author": "MrCode",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.4.5",
    "@babel/preset-env": "^7.4.5",
    "@babel/preset-react": "^7.0.0",
    "babel-cli": "^6.26.0",
    "babel-loader": "^8.0.6",
    "css-loader": "^3.0.0",
    "html-webpack-plugin": "^3.2.0",
    "style-loader": "^0.23.1",
    "webpack": "^4.33.0",
    "webpack-cli": "^3.3.4",
    "webpack-dev-server": "^3.7.1",
    "webpack-hot-middleware": "^2.25.0"
  },
  "dependencies": {
    "react": "^16.8.6",
    "react-dom": "^16.8.6"
  }
}

通天塔:

{
    "presets": ["@babel/env", "@babel/preset-react"]
}

App.js:

import React, { Component} from "react";
import "./App.css";
import { Hello } from "./The basics/FirstComponent"
import { Hook } from './The basics/FirstHook'
// import { HookC } from './The basics/FirstHookChallange'

class App extends Component{
  render(){
    return(
      <div className="App">
       <Hello/>
       <Hook/>
       {/* <HookC/> */}
      </div>
    );
  }
}

export default App;

有点烦人的部分是,这在昨天有效,但今天由于某种原因停止了。我在三个 JSX 组件中工作:

Hello Hook HookC

但是当我在其中更改某些内容时,我总是在浏览器中收到相同的日志消息。

我该如何解决这个问题?

编辑: 不知道这是否重要,但我正在使用 npm 和 gitbash。

【问题讨论】:

    标签: javascript reactjs webpack babeljs


    【解决方案1】:

    一段时间后,我想通了。

    我原来的 webpackconfig 在问题中看起来完全不同,但我无法让它工作,所以我尝试了其他一些尝试。

    最大的问题是 devserver.hotOnly,根据文档:

    在没有页面的情况下启用热模块更换(参见 devServer.hot) 在构建失败的情况下刷新作为后备。

    结果证明这是一种误导,因为当您删除 hotonly 道具并进行如下配置时:

    const path = require("path");
    const webpack = require("webpack");
    
    module.exports = {
      entry: "./src/index.js",
      mode: "development",
      module: {
        rules: [
          {
            test: /\.(js|jsx)$/,
            exclude: /(node_modules|bower_components)/,
            loader: "babel-loader",
            options: { presets: ["@babel/env"],  plugins: ["transform-class-properties"] }
          },
          {
            test: /\.css$/,
            use: ["style-loader", "css-loader"]
          }
        ]
      },
      resolve: { extensions: ["*", ".js", ".jsx"] },
      output: {
        path: path.resolve(__dirname, "dist/"),
        publicPath: "/dist/",
        filename: "bundle.js"
      },
      devServer: {
        contentBase: path.join(__dirname, "public/"),
        port: 3000,
        publicPath: "http://localhost:3000/dist/",
        historyApiFallback: true
      },
      plugins: [new webpack.HotModuleReplacementPlugin()]
    };
    

    HMR 有效。

    找到相关信息here

    【讨论】:

      猜你喜欢
      • 2016-10-03
      • 1970-01-01
      • 2019-10-25
      • 2018-12-20
      • 2017-06-24
      • 2017-03-06
      • 2012-04-16
      • 2015-04-01
      • 1970-01-01
      相关资源
      最近更新 更多