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

328 views Asked by At

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.

1

There are 1 answers

0
Oleksiy Gontar On

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.