简体   繁体   English

CLI热模块重新加载页面上的webpack-dev-server内联模式将刷新以更改dom

[英]webpack-dev-server Inline mode on CLI hot module reload page will refresh for change dom

When I change dom, page will refresh, but change css, hmr works, page not refresh, will hot replacement. 当我更改dom时,页面将刷新,但是更改CSS,hmr起作用时,页面不刷新,将进行热替换。 I want the page will not refresh instead of hot replacement when i change dom 我希望在更改dom时页面不会刷新而不是热替换

global webpack version 3.3.0, Mac OS 10.11.6 全局Webpack版本3.3.0,Mac OS 10.11.6

this is my package.json file: 这是我的package.json文件:

{
  "name": "demo-hmr",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "webpack-dev-server --hot --inline webpack.config.js"
  },
  "keywords": [],
  "author": "",
  "license": "MIT",
  "dependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-preset-env": "^1.6.0",
    "css-loader": "^0.28.7",
    "style-loader": "^0.18.2",
    "webpack": "^3.5.6",
    "webpack-dev-server": "^2.7.1"
  }
}

this is my webpack.config.js file: 这是我的webpack.config.js文件:

module.exports = {
  entry: './main.js',
  output: {
    filename: 'bundle.js'
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader'
        }
      },
      {
        test: /\.css$/,
        use: [
          'style-loader',
          'css-loader'
        ]
      }
    ]
  }
}

this is my main.js file, I change dom in this file, just change innerHTML of divv, then page will refresh, the result is not i want for. 这是我的main.js文件,我在此文件中更改dom,仅更改divv的innerHTML,然后页面将刷新,结果不是我想要的。

import app from './app.css'

var divv = document.createElement('div')

divv.innerHTML = "Hello webpack"

document.body.appendChild(divv)

this is my app.css file: 这是我的app.css文件:

body {
    background-color: yellow;
}

this is my index.html file: 这是我的index.html文件:

<html>
    <body>
      <h1>Hello World!</h1>
    <script type="text/javascript" src="bundle.js"></script>
    </body>
</html>

sorry about my simple question and my terrible english & format. 对我的简单问题和糟糕的英语和格式感到抱歉。

Actually, webpack is not so smart to know how you want to replace your modules. 实际上,webpack不太聪明,无法知道如何更换模块。 In css-loader hmr logic is already implemented because it's so trivial. 在css-loader中,hmr逻辑已经实现,因为它很简单。 But for your files you should implement HMR using API . 但是对于文件,您应该使用API实现HMR。 I agree that it's not so trivial. 我同意这不是那么简单。 But it's interesting enough. 但这很有趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM