Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack Dev Server doesn't hot reload, requires manual webpack build

Here is some example render code in a child component of a small react app:

<React.Fragment>
  <h1>From Tab Section</h1>
  <h2>update 2</h2> // doesn't display in the browser
  <p>update 3</p> // also doesn't display
</React.Fragment>

The difference is that after writing the h1 tag I rebuilt Webpack.

Further testing confirmed that rebuilding bundle.js and refreshing the browser on the index.html page it injects into, is the only way to see updates.

I think I have hot reload setup as shown in the docs. And per another SO post I added --inline to the start command. But a manual build and dev server restart is still required.

What else is needed here?

// package.json
"scripts": {
    "test": "jest --watch",
    "watch": "webpack --watch",
    "start": "webpack-dev-server --open --inline",
    "build": "webpack"
  },
  "devDependencies": {
    "@babel/core": "^7.4.5",
    "@babel/preset-env": "^7.4.5",
    "@babel/preset-react": "^7.0.0",
    "babel-jest": "^24.8.0",
    "babel-loader": "^8.0.6",
    "jest": "^24.8.0",
    "webpack": "^4.35.0",
    "webpack-cli": "^3.3.5",
    "webpack-php-loader": "^0.5.0"
  },
  "dependencies": {
    "dotenv": "^8.0.0",
    "dotenv-webpack": "^1.7.0",
    "history": "^4.9.0",
    "lodash": "^4.17.11",
    "react": "^16.8.6",
    "react-dom": "^16.8.6",
    "react-redux": "^7.1.0",
    "react-router-dom": "^5.0.1",
    "redux": "^4.0.1",
    "save-dev": "^2.0.0",
    "webpack-dev-server": "^3.7.2"
  }

// webpack.config.js
const webpack = require("webpack");
const dotenvWebpack = require("dotenv-webpack");
const path = require("path");

module.exports = {
  entry : {
    './adminSettingsArea' :
      './adminSettingsArea/src/index.jsx'
  },
  output : {
    filename : '[name]/bundle.js',
    path : path.resolve(__dirname),
  },
  devtool: 'inline-source-map',
  devServer : {
    contentBase : './adminSettingsArea',
    hot : true
  },
  plugins : [
    new webpack.HotModuleReplacementPlugin(),
    new dotenvWebpack()
  ],
  module : {
    rules : [
      {
        test : /\.(js|jsx)$/,
        exclude : [/node_modules/, /vendor/],
        use : {
          loader : "babel-loader",
          options : {
            presets : [
              '@babel/preset-env',
              "@babel/preset-react"
            ]
          },
        }
      }
    ],
  },
};
like image 219
Sean D Avatar asked Jan 22 '26 12:01

Sean D


1 Answers

Solved by moving the index.html file into the same folder ( ./adminSettingsArea/src ) as the rest of the content webpack updates in memory.

Previously this index file was one level below as was the contentBase value. As a result, webpack was able to load the index.html file initially but not the jsx files in the /src subfolder.

Also, please see the example below for another quirk I ran into with the output path.

entry : {
    'adminArea' :
      './adminSettingsArea/src/index.jsx'
  },
  output : {
    filename : 'shared/[name].bundle.js', // for some reason I need to assign the subfolder here instead of arg 2 in the next line
    path : path.resolve(__dirname, ''),
  },
  devtool: 'inline-source-map',
  devServer : {
    contentBase : './adminSettingsArea/src',
    hot : true
  },
like image 130
Sean D Avatar answered Jan 24 '26 02:01

Sean D



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!