How to setup React Typescript Project from scratch with Webpack

Create React App is no doubt a very good toolkit to build a basic react application. You simply run a single command (npx create-react-app your-project-name)and it will setup the tools you need to start your React project. It is really helpful to start building react app for Beginners but as a Senior or Mid-Senior level developer you should have ability to create your own boilerplate. This article will teach you how to create your own boilerplate from base in react with typescript by using webpack.

Getting Started

Let's start by creating a new directory for our project

Now initialize the project with npm

This command will create a package.json file with some default values.

Adding React and Typescript

Add the following commands in order to install React and Typescript

Typescript is configured by a file which is called tsconfig.json. Let's add this file in the root directory of our project.

Let's also look and understand the different options we added in this file.

  • compilerOptions Represents the different compiler options.
  • jsx:react Adds support for JSX in .tsx files.
  • module Generates module code.
  • noImplicitAny Raises errors for declarations with an implied any type.
  • outDir Represents the output directory.
  • sourceMap Generates a .map file, which can be very useful for debugging the app
  • target Represents the target ECMAScript version to transpile our code down to specified version(we can add a version based on our specific browser requirements).
  • include Used to specify the file list to be included even we can specify folder here as well.

Adding Webpack

Webpack is a bundler which bundles all your javascript code and related dependencies into one bundle file (bundle.js). Let's install webpack related dependencies.

  • webpack is a module bundler.
  • webpack-cli is a command line interface to run webpack related commands.
  • webpack-dev-server is a web server which watches our changes and refresh the web page when any changes is made to our component.

Now we need to add some loaders.

  • ts-loader loads any file which has extension .tsx or .ts
  • css-loader loads any file which has extension .css
  • source-map-loader loads any file which has extension .js

You may learn more about loaders and use them if you need any according to your project requirement . We also need one more dependency html-webpack-plugin which creates a template file that is rendered to the browser from index.html

Now it's time to configure webpack in our project. Let's create a file webpack.config.js in the root directory of our project

Let's look at different options we have added in webpack.config.js

  • entry This field specifies the entry point of our application.
  • output This field tells webpack where to bundle our code. The path represents the output directory of the code and filename represent the filename where all the code will be bundled usually we call it bundle.js
  • resolve.extensions This field specifies which type of files to look during module resolution.
  • module This field directed Webpack how different modules will be treated. For example, we are using ts-loader to load .tsx and.ts types of file.
  • plugins This field usually work at the end of bundle generation process.

Adding index.html

Create src folder in the root directory and add index.html

Adding Components

Lets create a component App.tsx in src folder

Now let's add index.tsx in the same folder which will be the entry point of our app.

Adding npm scripts

Add the following scripts in package.json file.

If you are using webpack 4 or above it is recommended to use webpack serve otherwise you can use webpack-dev-server

Run Application

Now let's run the script npm start and your application will be ready.

If you want to build the project you can run npm run build and you will see the folder dist in the root directory and in this folder you will have bundle.js and index.html.

This repo has all the code which is used in this blog post. If you want you can clone the project and try to run it. If you found any difficulty you may write to me. I'd be very happy to help and please don't forget to star :)

I am a Frontend Engineer having expertise in React.js. I learn concepts first and then try practical implementation on it.