【发布时间】: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