简体   繁体   中英

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. I want the page will not refresh instead of hot replacement when i change dom

global webpack version 3.3.0, Mac OS 10.11.6

this is my package.json file:

{
  "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:

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.

import app from './app.css'

var divv = document.createElement('div')

divv.innerHTML = "Hello webpack"

document.body.appendChild(divv)

this is my app.css file:

body {
    background-color: yellow;
}

this is my index.html file:

<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. In css-loader hmr logic is already implemented because it's so trivial. But for your files you should implement HMR using API . I agree that it's not so trivial. But it's interesting enough.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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