Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React with Typescript hot reload IIS webserver

I would like to use hot reloading when developing my React Application in TypeScript with a C# Web Api backend. I use .Net framework and not Core so I need to use IIS or IIS Express. I can have hot reloading for the front end using webpack dev server without a problem but then I can't access the API resources. Can I achieve this?

like image 825
Ogglas Avatar asked Apr 21 '17 13:04

Ogglas


People also ask

Does react support hot reload?

React Native solves this problem for the developers. Hot reloading allows you to see the changes that you have made in the code without reloading your entire app. Whenever you make any changes, all you need to do is save your code.

What is hot reload?

The idea behind hot reloading is to keep the app running and to inject new versions of the files that you edited at runtime. This way, you don't lose any of your state which is especially useful if you are tweaking the UI. So it reloads only that page which you change more info here.


1 Answers

Found a solution for it using webpack dev server as reverse proxy to the IIS.

NPM:

npm install --save react-hot-loader@next

npm install webpack-dev-server --save-dev

webpack.config.js, proxy is where the IIS is running:

var webpack = require('webpack');
var path = require("path");
var proxy = 'localhost:61299';

module.exports = {
    entry: [
        // activate HMR for React
        'react-hot-loader/patch',

        // the entry point of our app
        './Scripts/src/index.tsx',
    ],
    //entry: "./Scripts/src/index.tsx",
    output: {
        filename: "./Scripts/dist/bundle.js",
    },

    // Enable sourcemaps for debugging webpack's output.
    devtool: "source-map",

    resolve: {
        // Add '.ts' and '.tsx' as resolvable extensions.
        extensions: [".webpack.js", ".web.js", ".ts", ".tsx", ".js"]
    },

    module: {
        loaders: [
            // All files with a '.ts' or '.tsx' extension will be handled by 'ts-loader'.
            //{ test: /\.tsx?$/, loader: "ts-loader" }
            { test: /\.tsx?$/, loader: ['react-hot-loader/webpack', 'ts-loader'] }
        ]
    },

    plugins: [
        new webpack.HotModuleReplacementPlugin(),
        // enable HMR globally

        new webpack.NamedModulesPlugin(),
        // prints more readable module names in the browser console on HMR updates
    ],

    devServer: {
        proxy: {
            '*': {
                target: 'http://' + proxy,
            }
        },
        port: 8080,
        host: '0.0.0.0',
        hot: true,
    },
}

I can then start the web server by using this command from the project root folder: node_modules\.bin\webpack-dev-server. If I then access http://localhost:8080/ I have hot reloading and I can still use the C# web api since it will proxy IIS Express at "http://localhost:61299/"

like image 192
Ogglas Avatar answered Sep 22 '22 09:09

Ogglas