1

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!

2 Answers 2

1

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
  }
Sign up to request clarification or add additional context in comments.

3 Comments

nope, it does not help. In description I added picture what i get in the console.
try to replace path.join(__dirname, './application/dist') by path.join(__dirname, 'application/dist') both for contentBase and output keys
does not help either.
0

Try this, writeToDisk is important:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.