Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
453 views
in Technique[技术] by (71.8m points)

webpack-dev-server开启 hot 没有效果?

webpack-dev-server开启 hot 没有效果, 不会自动刷新页面

具体情况:

webpack.config.js 中的 devServer 里面配置了 hottrue, 运行起来并没有刷新页面,但是实际上已经生成 .hot-update.js并且在 html 中引用了(看浏览器控制台看出来的),想知道为什么没有热更新页面。

相关代码

包版本

"clean-webpack-plugin": "^3.0.0",
"css-loader": "^3.6.0",
"file-loader": "^6.0.0",
"html-webpack-plugin": "^4.3.0",
"style-loader": "^1.2.1",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.12",
"webpack-dev-server": "^3.11.0"

webpack.config.js ( 重点看 devServer 就好 ) ~~~~

const path = require('path')

const HtmlWebpackPlugin = require('html-webpack-plugin');
const {CleanWebpackPlugin} = require('clean-webpack-plugin');
const webpack = require('webpack');

module.exports = {
  entry: {
    app: './src/index.js',
  },
  output: {
    filename: "[name].bundle.js",
    path: path.resolve(__dirname, 'dist'),
  },
  plugins: [
    new HtmlWebpackPlugin({
      title: 'webpack demo',
      filename: "index.html",
      template: "src/index.html"
    }),
    new CleanWebpackPlugin({
      cleanAfterEveryBuildPatterns: ["dist"]
    }),
    new webpack.HotModuleReplacementPlugin()
  ],
  module: {
    rules: [
      {
      
        test: /.css$/,
       
        use: ["style-loader", 'css-loader']
      }, {
        test: /.(png|svg|jpg|gif)$/,
        use: [
          {
            loader: 'file-loader',
            options: {
              name: "[name].[ext]",
              outputPath: 'imgs/'
            }
          }
        ]
      },
      {
        test: /.(woff|woff2|eot|ttf|oth)$/,
        use: ["file-loader"]
      }
    ]
  },
  devServer: {
    contentBase: path.join(__dirname, 'dist'), 
    port: 8000,
    openPage: 'index.html',
    open: true, 
    hot: true, 
    hotOnly: true, 
  }
};

运行的时候控制台的情况
控制台情况实例


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

// 更新
今天重新看了一下这个问题,发现不是 hotOnly 的原因,是因为没有在入口文件配置:

if (module.hot) {
    module.hot.accept('./print.js', function() {
        console.log('Accepting the updated printMe  module!');
        printMe();
    })
}   

具体文档查看:HMR
)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...