- React Typescript Template From Scratch
yarn build
cd build
npx serve
- mkdir src build
- npm init --y
- create src/index.html
- install react dependencies
yarn add react react-dom
- install typescript dev dependencies
yarn add -D typescript @types/react @types/react-dom
- create tsconfig.json
- create src/App.tsx
- create src/index.tsx
- install babel dev dependencies
yarn add -D @babel/core @babel/preset-env @babel/preset-react @babel/preset-typescript @babel/plugin-transform-runtime
- create .babelrc in project root directory
{
"presets": [
"@babel/preset-env",
[
"@babel/preset-react",
{
"runtime": "automatic"
}
],
"@babel/preset-typescript"
],
"plugins": [
[
"@babel/plugin-transform-runtime",
{
"regenerator": true
}
]
]
}
- install webpack dev dependencies
yarn add -D webpack webpack-cli webpack-dev-server html-webpack-plugin
- install babel-loader package to transpiling source code.
yarn add -D babel-loader
- create webpack directory
- create webpack/webpack.config.js
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
module.exports = {
entry: path.resolve(__dirname, "..", "./src/index.tsx"),
resolve: {
extensions: [".tsx", ".ts", ".js"],
},
module: {
rules: [
{
test: /\.(ts|js)x?$/,
exclude: /node_modules/,
use: [
{
loader: "babel-loader",
},
],
},
{
test: /\.css$/,
use: ["style-loader", "css-loader"],
},
{
test: /\.(?:ico|gif|png|jpg|jpeg)$/i,
type: "asset/resource",
},
{
test: /\.(woff(2)?|eot|ttf|otf|svg|)$/,
type: "asset/inline",
},
],
},
output: {
path: path.resolve(__dirname, "..", "./build"),
filename: "bundle.js",
},
mode: "development",
plugins: [
new HtmlWebpackPlugin({
template: path.resolve(__dirname, "..", "./src/index.html"),
}),
],
stats: "errors-only",
};
- add start scripts in package.json
"scripts": {
"start": "webpack serve --config webpack/webpack.config.js --open"
}
- install dev dependencies.
yarn add -D css-loader style-loader
- add configuration in webpack.config.js
{
test: /\.css$/,
use: ["style-loader", "css-loader"],
},
- create
declarations.d.ts
then add declare
declare module "*.png"
- add configuration for images in webpack.config.js
{
test: /\.(?:ico|gif|png|jpg|jpeg)$/i,
type: "asset/resource",
},
- add asset/inline configuration to serve inline images like svg.
{
test: /\.(woff(2)?|eot|ttf|otf|svg|)$/,
type: "asset/inline",
},
- create webpack.dev.js, webpack.prod.js, webpack.common.js
webpack.dev.js
const webpack = require("webpack");
module.exports = {
mode: "development",
devtool: "cheap-module-source-map",
plugins: [
new webpack.DefinePlugin({
"process.env.name": JSON.stringify("Vishwas"),
}),
],
};
webpack.prod.js
const webpack = require("webpack");
module.exports = {
mode: "production",
devtool: "source-map",
plugins: [
new webpack.DefinePlugin({
"process.env.name": JSON.stringify("Codevolution"),
}),
],
};
webpack.common.js
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
module.exports = {
entry: path.resolve(__dirname, "..", "./src/index.tsx"),
resolve: {
extensions: [".tsx", ".ts", ".js"],
},
module: {
rules: [
{
test: /\.(ts|js)x?$/,
exclude: /node_modules/,
use: [
{
loader: "babel-loader",
},
],
},
{
test: /\.css$/,
use: ["style-loader", "css-loader"],
},
{
test: /\.(?:ico|gif|png|jpg|jpeg)$/i,
type: "asset/resource",
},
{
test: /\.(woff(2)?|eot|ttf|otf|svg|)$/,
type: "asset/inline",
},
],
},
output: {
path: path.resolve(__dirname, "..", "./build"),
filename: "bundle.js",
},
plugins: [
new HtmlWebpackPlugin({
template: path.resolve(__dirname, "..", "./src/index.html"),
}),
],
stats: "errors-only",
};
webpack.config.js
const { merge } = require("webpack-merge");
const commonConfig = require("./webpack.common.js");
module.exports = (envVars) => {
const { env } = envVars;
const envConfig = require(`./webpack.${env}.js`);
const config = merge(commonConfig, envConfig);
return config;
};
- add scripts
"scripts": {
"start": "webpack serve --config webpack/webpack.config.js --env env=dev --open",
"build": "webpack --config webpack/webpack.config.js --env env=prod",
},
- Change App.tsx to check environment.
import "./styles.css";
import IMAGE from "./react.png";
import LOGO from "./logo.svg";
export const App = () => {
return (
<>
<h1>
React Typescript Webpack Starter Template - {process.env.NODE_ENV} {process.env.name}
</h1>
<img src={IMAGE} alt="React Logo" width="300" height="300" style={{ objectFit: "cover" }} />
<img src={LOGO} alt="React Logo" width="300" height="300" style={{ objectFit: "cover" }} />
</>
);
};
yarn add -D eslint
yarn add -D eslint-plugin-react-hooks
yarn add -D @typescript-eslint/parser @typescript-eslint/eslint
yarn add -D eslint-plugin-import eslint-plugin-jsx-a11y
module.exports = {
parser: "@typescript-eslint/parser",
parserOptions: {
ecmaVersion: 2020,
sourceType: "module",
},
settings: {
react: {
version: "detect",
},
},
extends: [
"plugin:react/recommended",
"plugin:react-hooks/recommended",
"plugin:@typescript-eslint/recommended",
"plugin:import/errors",
"plugin:import/warnings",
"plugin:import/typescript",
"plugin:jsx-a11y/recommended",
],
rules: {
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": ["error"],
"@typescript-eslint/no-var-requires": "off",
"react/prop-types": "off",
"react/jsx-uses-react": "off",
"react/react-in-jsx-scope": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
},
};
"scripts": {
"lint": "eslint --fix \"./src/**/*.{js,jsx,ts,tsx,json}\""
},
yarn add -D prettier eslint-config-prettier eslint-plugin-prettier
module.exports = {
semi: false,
trailingComma: "es5",
singleQuote: true,
printWidth: 80,
tabWidth: 2,
endOfLine: "auto",
};
"scripts": {
"format": "prettier --write \"./src/**/*.{js,jsx,ts,tsx,json,css,scss,md}\""
},
yarn add -D husky@4 lint-staged
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"src/**/*.{js,jsx,ts,tsx,json}": [
"eslint --fix"
],
"src/**/*.{js,jsx,ts,tsx,json,css,scss,md}": [
"prettier --write"
]
}
Install
yarn add -D @babel/runtime @babel/plugin-transform-runtime
.babelrc
{
"presets": [
"@babel/preset-env",
[
"@babel/preset-react",
{
"runtime": "automatic"
}
],
"@babel/preset-typescript"
],
"plugins": [
[
"@babel/plugin-transform-runtime",
{
"regenerator": true
}
]
]
}
webpack-plugin
yarn add -D copy-webpack-plugin
webpack-bundle-analyzer
yarn add -D webpack-bundle-analyzer