Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

webpack-dev-server hot reload not working

My file structure is:

dist
  css
    style.css
  index.html
  js
    bundle.js
src
  css
    style.css
  index.html
  js
    main.js
node_modules
webpack.config.js
package.json

My webpack.config.js looks like:

module.exports = {
    entry: './src/js/main.js',
    output: {
        path: __dirname,
        filename: './dist/js/bundle.js'
    },
    module: {
        loaders: [
            {
                test: /\.js$/,
                exclude: /(node_modules)/,
                loader: 'babel',
                query: {
                    presets: ['es2015']
                }
            },
            {
                test: /\.vue$/,
                loader: 'vue'
            }
        ]
    }
};

I run:

webpack-dev-server --content-base dist --hot

And it builds and seems like it's working. localhost:8080 shows the expected result but hot-reload does just not work. When I change a file I can see in terminal that a rebuild is happening but nothing happens in the browser. Am I missing something in the config?

like image 672
riccardolardi Avatar asked Aug 21 '16 16:08

riccardolardi


People also ask

How does webpack hot reload work?

When a hot update failed to replace the code in the browser, the HMR runtime will let webpack-dev-server know. The webpack-dev-server will then refresh the browser to download a new bundle. js file. This behavior can be disabled by adding hotOnly: true to your Webpack configuration.

How do I enable HMR?

Enabling HMR All we need to do is update our webpack-dev-server configuration, and use webpack's built-in HMR plugin. We'll also remove the entry point for print. js as it will now be consumed by the index. js module.

What is WDS Live reloading enabled?

Live Reload is a new feature in Web Connection that can detect when you make changes to any of these types of files: Static HTML files. Static CSS and JavaScript Files. Web Connection Scripts and Templates. FoxPro Code in your Web Connection Server.


3 Answers

What worked for me is to write <script src="bundle.js"> and not <script src="dist/bundle.js"> in my index.html file.

// index.html <script src="bundle.js"></script>      // works <script src="dist/bundle.js"></script> // doesn't work! 

Keeping dist/bundle.js as the output file works perfectly fine if you just build it using webpack. But when using webpack-dev-server, the static file already in the file system continues to be served, and not the latest hot replacement. It seems webpack-dev-server gets confused when it sees dist/bundle.js in the html file and doesn't hot replace it, even though webpack.config.js is configured to that path.

like image 196
fafaro Avatar answered Oct 04 '22 12:10

fafaro


When using webpack-dev-server, it builds all files internally and does not spit them out into your output path. Running webpack alone, without the dev server, does the actual compilation to disk. The dev server does everything in memory which speeds up re-compilation by a lot.

To fix your hot reload issue, set the content base to your source directory and enable inline-mode

Like so:

webpack-dev-server --content-base src --hot --inline
like image 33
Mario Tacke Avatar answered Oct 04 '22 12:10

Mario Tacke


None of the options on this page worked for me. After changing the devServer section to:

devServer: {
    port: 8080,
    contentBase: ['./src', './public'], // both src and output dirs
    inline: true,
    hot: true
},

it worked.

like image 29
Nelson Teixeira Avatar answered Oct 04 '22 14:10

Nelson Teixeira