webpack5配置dev-server无法热更新的问题

来源:3-9 Hot Module Replacement 热模块更新(1)

幽辰

2021-12-06

在使用webpack5的时候,使用webpack-dev-server后修改代码无法实现效果变化,如从 新增x 变为 新增xxx时,页面还是显示 新增x

代码

// webpack.config.js
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const webpack = require('webpack')

module.exports = {
    target: "web",
    mode: 'development',
    devtool: 'inline-source-map',
    entry: {
        main: './src/index.js'
    },
    devServer: {
        static: {
            directory: path.join(__dirname, 'dist')
        },
        host: "localhost",
        port: 8080,
        hot: true,
        open: true
    },
    module: {
        rules: [
            {
                test: /\.jpg$/,
                use: {
                    loader: 'url-loader',
                    options: {
                        name: '[name].[ext]',
                        outputPath: 'images/',
                        limit: 2048
                    }
                }
            },
            {
                test: /\.(eot|ttf|svg)$/,
                use: {
                    loader: 'file-loader'
                }
            },
            {
                test: /\.css$/,
                use: ['style-loader', 'css-loader']
            },
            {
                test: /\.scss$/,
                use: [
                    'style-loader',
                    'css-loader'
                    'sass-loader'
                ]
            }
        ]
    },
    output: {
        publicPath: './',
        filename: '[name].js',
        path: path.resolve(__dirname, 'dist'),
        clean: true
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: 'src/index.html'
        },
        new webpack.HotModuleReplacementPlugin()
    )]
}
// package.json
{
  "...",
  "scripts": {
    "webpack": "webpack",
    "start": "webpack serve",
    "dev": "webpack-dev-server --hot --open --port 8080 "
  },
  "devDependencies": {
    "autoprefixer": "^10.4.0",
    "clean-webpack-plugin": "^4.0.0",
    "css-loader": "^6.5.1",
    "express": "^4.17.1",
    "file-loader": "^6.2.0",
    "html-webpack-plugin": "^5.5.0",
    "postcss-loader": "^6.2.1",
    "sass-loader": "^12.3.0",
    "style-loader": "^3.3.1",
    "url-loader": "^4.1.1",
    "webpack": "^5.64.4",
    "webpack-cli": "^4.9.1",
    "webpack-dev-middleware": "^5.2.2",
    "webpack-dev-server": "^4.6.0"
  },
  "dependencies": {
    "node-sass": "^6.0.1"
  }
}
// src/index.js
import './style.css'

var btn = document.createElement('button')
btn.innerHTML = '新增xxx'
document.body.appendChild(btn)
btn.onclick = function() {
    var div = document.createElement('div');
    div.innerHTML = 'item'
    document.body.appendChild(div)
}

目录

图片描述

效果

  1. 执行 npm start
  2. 发现页面仍然为 新增x
  3. 经检查 network 中并无 ws 连接
    图片描述

尝试过的方法

  • 因为之前使用过 browserlist,将其移除后仍然不行
  • 添加 target: 'web' 仍然不行

老师知道这是为啥吗,我也翻了官方教程,issue和网上文档,没有看到有效的解决办法

写回答

1回答

幽辰

提问者

2021-12-06

找到原因了,因为output设置了publicPath,去掉就好了。但是不知道这个的原理唉,是因为devServer是直接打包到内存的,而devSever会默认读取output的publicPath,才会导致指向的物理硬盘而非打包的内存地址吗?希望老师解答一下

2
1
Dell
是的,因为内存,你如果设置了 publicPath,在内存里匹配不上
2021-12-06
共1条回复

从基础到实战 手把手带你掌握新版Webpack4.0

知识点+项目实例+原理讲解 全方位解析Webpack4新版本

3627 学习 · 1291 问题

查看课程