zrna
zrna

Reputation: 594

Webpack 4 hot reload not working with React TypeScript

I'm setting up React application using Typescript using custom Webpack config but I have problem with hot reloading.

Changes are only shown if I restart the app.

I looked for answers and examples, but I could not find a solution that would help me.

Below are my code and configuration.

package.json

{
  "name": "frontend-bootstrap-project",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "webpack-dev-server --mode development  --open --hot",
    "build": "webpack --mode production"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "awesome-typescript-loader": "^5.2.1",
    "eslint": "^5.16.0",
    "html-webpack-plugin": "^3.2.0",
    "redux": "^4.0.1",
    "redux-saga": "^1.0.2",
    "typescript": "^3.4.5",
    "webpack": "^4.32.2",
    "webpack-cli": "^3.3.2",
    "webpack-dev-server": "^3.4.1"
  },
  "dependencies": {
    "@types/react": "^16.8.18",
    "@types/react-dom": "^16.8.4",
    "@typescript-eslint/parser": "^1.9.0",
    "eslint-plugin-react": "^7.13.0",
    "react": "^16.8.6",
    "react-dom": "^16.8.6",
    "ts-loader": "^6.0.1"
  }
}

webpack.config.json

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  entry: ["./application/src/index.tsx"],
  resolve: {
    extensions: [".ts", ".tsx", ".js"]
  },
  output: {
    path: path.join(__dirname, './application/dist'),
    filename: 'bundle.min.js'
  },
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        loader: 'awesome-typescript-loader'
      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './application/src/index.html'
    })
  ]
};

App.tsx

import * as React from 'react';

class App extends React.Component {
  render() {
    return (
      <div>
        <h1>Welcome to React with Typescript</h1>
      </div>
    );
  }
}

export default App;

This is what I get in console: enter image description here

EDIT (SOLVED):

My code above is working. I found the typo in my index.tsx file.

It was: import App from './components/App';

instead: import App from './Components/App';

Thanks for helping guys!

Upvotes: 1

Views: 6094

Answers (2)

Try this, writeToDisk is important:

    devServer: {
    contentBase: path.resolve(__dirname, './application/dist'),
    host: 'localhost',
    hot: true,
    writeToDisk: true
}

Upvotes: 0

Timofey Goncharov
Timofey Goncharov

Reputation: 1148

For resolve this problem you need use devServer.

In your case add this in your webpack config:

 devServer: {
    contentBase: path.join(__dirname, './application/dist'),
    compress: true,
    port: 9000
  }

Upvotes: 1

Related Questions