热模块更换不起作用 总是发生重新加载
我似乎不知道如何让热模块更换工作。每次我对 html 文件或 CSS 文件进行更改时,webpack 总是会刷新以显示更改。
webpack.config.js
const path = require('path')
const postCSSPlugins = [
require('postcss-simple-vars'),
require('postcss-nested'),
require('autoprefixer'),
require('postcss-import')
]
module.exports = {
entry: './app/assets/scripts/App.js',
output: {
filename: 'bundled.js',
path: path.resolve(__dirname, 'app')
},
devServer: {
watchFiles: ('./app/**/*.html'),
static: path.join(__dirname, 'app'),
hot: true,
port: 3000,
host: '0.0.0.0'
},
mode: 'development',
module: {
rules: [
{
test: /\.css$/i,
use: ['style-loader','css-loader', {loader: 'postcss-loader', options: {postcssOptions: {plugins: postCSSPlugins}}}]
}
]
}
}
package.json
"scripts": {
"dev": "webpack serve --hot",
"test": "echo \"Error: no test specified\" && exit 1"
},
"devDependencies": {
"autoprefixer": "^10.4.2",
"css-loader": "^6.6.0",
"postcss-import": "^14.0.2",
"postcss-loader": "^6.2.1",
"postcss-nested": "^5.0.6",
"postcss-simple-vars": "^6.0.3",
"style-loader": "^3.3.1",
"webpack": "^5.69.1",
"webpack-cli": "^4.9.2",
"webpack-dev-server": "^4.7.4"
}
App.js
import '../styles/styles.css'
if(module.hot) {
module.hot.accept(function() {
console.log("Accepting the updated modules...")
})
}
我尝试过的..
- 我尝试过使用
hotOnly< /code> 选项,但它已被删除
- 我尝试在我的
package.json
文件中的 CLI 中添加选项标签,
但似乎没有任何效果。每当我进行更改时,整个页面都会刷新。
I can't seem to figure out how to get hot module replacement to work. Every time I make a change to my html file or my CSS files the webpack always does a refresh to show the changes.
webpack.config.js
const path = require('path')
const postCSSPlugins = [
require('postcss-simple-vars'),
require('postcss-nested'),
require('autoprefixer'),
require('postcss-import')
]
module.exports = {
entry: './app/assets/scripts/App.js',
output: {
filename: 'bundled.js',
path: path.resolve(__dirname, 'app')
},
devServer: {
watchFiles: ('./app/**/*.html'),
static: path.join(__dirname, 'app'),
hot: true,
port: 3000,
host: '0.0.0.0'
},
mode: 'development',
module: {
rules: [
{
test: /\.css$/i,
use: ['style-loader','css-loader', {loader: 'postcss-loader', options: {postcssOptions: {plugins: postCSSPlugins}}}]
}
]
}
}
package.json
"scripts": {
"dev": "webpack serve --hot",
"test": "echo \"Error: no test specified\" && exit 1"
},
"devDependencies": {
"autoprefixer": "^10.4.2",
"css-loader": "^6.6.0",
"postcss-import": "^14.0.2",
"postcss-loader": "^6.2.1",
"postcss-nested": "^5.0.6",
"postcss-simple-vars": "^6.0.3",
"style-loader": "^3.3.1",
"webpack": "^5.69.1",
"webpack-cli": "^4.9.2",
"webpack-dev-server": "^4.7.4"
}
App.js
import '../styles/styles.css'
if(module.hot) {
module.hot.accept(function() {
console.log("Accepting the updated modules...")
})
}
What I've tried..
- I've tried using the
hotOnly
option but its been removed - I've tried add an option tag in the CLI in my
package.json
file
Nothing seems to be working. Anytime I make a change the whole page refreshes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要了解热模块重新加载的工作原理和设置,您可以查看我的帖子 此处。如果您不将
--host 0.0.0.0
与 Docker 一起使用,请删除它。To figure out how Hot Module Reload works and is set up you may take a look at my post here. Remove
--host 0.0.0.0
if you don't use it with Docker.