gpt4 book ai didi

javascript - 如何解决 "The following modules couldn'不热更新: (They would need a full reload!)”

转载 作者:行者123 更新时间:2023-12-02 23:27:58 25 4
gpt4 key购买 nike

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

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

The following modules couldn't be hot updated: (They would need a full reload!)

log.js:26 Ignored an update to unaccepted module ./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"]
}

应用程序.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。

最佳答案

过了一段时间,我明白了这一点。

我原来的 webpackconfig 看起来与问题中的情况相当不同,但我无法让它工作,所以我尝试了一些其他尝试。

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

Enables Hot Module Replacement (see devServer.hot) without page refresh as fallback in case of build failures.

事实证明,这具有误导性,因为当您删除 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

关于javascript - 如何解决 "The following modules couldn'不热更新: (They would need a full reload!)”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56648279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com