Connect with us

Publications

React App Configuration with TypeScript and Webpack

Published

on

A step-by-step guide on how to create a React project from scratch, with TypeScript and Webpack.

You can find the full source code here.

Setup

Prerequisites:

Create the project’s folder:

mkdir react-app
cd react-app

Generate a default package.json file with yarn:

yarn init -y

Install React, TypeScript and Webpack:

yarn add react react-dom yarn add --dev @types/react @types/react-dom awesome-typescript-loader css-loader html-webpack-plugin node-sass sass-loader style-loader typescript webpack webpack-cli webpack-dev-server

Add build, dev & clean scripts in the package.json file:

 .... }, "scripts": { "clean": "rm -rf dist/*", "build": "webpack", "dev": "webpack serve" }

Configure TypeScript by creating the file tsconfig.json with:

{ "compilerOptions": { "incremental": true, "target": "es5", "module": "commonjs", "lib": ["dom", "dom.iterable", "es6"], "allowJs": true, "jsx": "react", "sourceMap": true, "outDir": "./dist/", "rootDir": ".", "removeComments": true, "strict": true, "moduleResolution": "node", "allowSyntheticDefaultImports": true, "esModuleInterop": true, "experimentalDecorators": true }, "include": [ "client" ], "exclude": [ "node_modules", "build", "dist" ]
}

To configure Webpack, make a file webpack.config.js containing:

const path = require("path"); const app_dir = __dirname + '/client'; const HtmlWebpackPlugin = require('html-webpack-plugin');
const HTMLWebpackPluginConfig = new HtmlWebpackPlugin({ template: app_dir + '/index.html', filename: 'index.html', inject: 'body'
}); const config = { mode: 'development', entry: app_dir + '/app.tsx', output: { path: __dirname + '/dist', filename: 'app.js', publicPath: '/' }, module: { rules: [{ test: /.s?css$/, use: [ 'style-loader', 'css-loader', 'sass-loader' ] }, { test: /.tsx?$/, loader: "awesome-typescript-loader", exclude: /(node_modules|bower_components)/ }, { test: /.(woff|woff2|ttf|eot)(?v=[0-9].[0-9].[0-9])?$/, exclude: [/node_modules/], loader: "file-loader" }, { test: /.(jpe?g|png|gif|svg)$/i, exclude: [/node_modules/], loader: "file-loader" }, { test: /.(pdf)$/i, exclude: [/node_modules/], loader: "file-loader", options: { name: '[name].[ext]', }, }, ] }, plugins: [HTMLWebpackPluginConfig], resolve: { extensions: [".ts", ".tsx", ".js", ".jsx"] }, optimization: { removeAvailableModules: false, removeEmptyChunks: false, splitChunks: false, }, devServer: { port: 8080, // open: true, hot: true, inline: true, historyApiFallback: true, },
};
module.exports = config;

Example App

Create a folder named client (in the project’s folder):

mkdir client
cd client

Make a simple React component, in the file numbers.tsx:

import React, {useState} from 'react'; interface INumberProps { initValue: number
} const Numbers = (props: INumberProps) => { const [value, setValue] = useState(props.initValue) const onIncrement = () => { setValue(value + 1) } const onDecrement = () => { setValue(value - 1) } return ( <div> Number is {value} <div> <button onClick={onIncrement}>+</button> <button onClick={onDecrement}>-</button> </div> </div> )
}
export default Numbers

Create the main React component (the entry point), in the file app.tsx:

import * as React from 'react';
import * as ReactDOM from 'react-dom';
import Numbers from './numbers'; ReactDOM.render( <Numbers initValue={42} />, document.getElementById('app') as HTMLElement );

Next, add the index.html:

 <!DOCTYPE html>
<html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>React TypeScript</title>
</head> <body> <div id="app"></div>
</body>
</html>

Then, run yarn dev and open http://localhost:8080/ in a browser.

Use this project as a template

You can save the

Setup

steps as a shell script

#!/bin/sh rm -rf node_modules
rm package.json yarn init --yes yarn add react react-dom yarn add --dev @types/react @types/react-dom awesome-typescript-loader css-loader html-webpack-plugin node-sass sass-loader style-loader typescript webpack webpack-cli webpack-dev-server # Remove the last line
sed -i.bak '$ d' package.json && rm package.json.bak # append the scripts commads
cat <<EOT >> package.json ,"scripts": { "clean": "rm -rf dist/*", "build": "webpack", "dev": "webpack serve" }
}

Delete the node-modules folder and, when you want to start a new project, you can copy the contents of

react-app

to the new location:

mkdir new-project
cd new-project # copy the react-app folder content to the new project
rsync -rtv /path/to/../react-app/ . ./init.sh

Previously published at https://alexadam.dev/blog/create-react-project.html

Tags

Join Hacker Noon

Create your free account to unlock your custom reading experience.

Coinsmart. Beste Bitcoin-Börse in Europa
Source: https://hackernoon.com/react-app-configuration-with-typescript-and-webpack-zy1q354b?source=rss

CNBC

‘Trek to Yomi’ is a Kurosawa-inspired 2D samurai game coming in 2022

Published

on

If you played through the entirety of Ghost of Tsushima in Kurosawa mode and it left you wanting for more cinematic samurai action, you’ll want to look out for Trek to Yomi. Announced today during Devolver Digital’s E3 presentation, the 2.5D side-scroller sees you play as a young samurai charged with protecting their town and its inhabitants. You’ll face off against both human and supernatural enemies. Like Tsushima, the game attempts to emulate the look of Akira Kurosawa’s films. Judging by the trailer, it does a pretty good job of it too.          

Developed by Flying Wild Hog and Leonard Menchiari, Trek to Yomi will come out on PlayStation 5, Xbox Series X/S and PC in 2022. 

All products recommended by Engadget are selected by our editorial team, independent of our parent company. Some of our stories include affiliate links. If you buy something through one of these links, we may earn an affiliate commission.

Coinsmart. Beste Bitcoin-Börse in Europa
Source: https://www.engadget.com/trek-to-yomi-2022-211810467.html?src=rss_b2c

Continue Reading

CNBC

‘Far Cry 6′ will bring back the series’ best villains (if you pay more)

Published

on

Far Cry 6 will see the return of past villains from the franchise as playable characters in post-launch DLC included with the game’s season pass. As you can see from the trailer below, Vass Montenegro, Pagan Min and Joseph Seed from Far Cry 3, 4 and 5 are back, with Michael Mando, Troy Baker and Greg Bryk reprising their roles. If that amount of fan service wasn’t enough, the season pass will also include Far Cry 3’s excellent Blood Dragon DLC.

Ubisoft will release Far Cry 6 on PC, Xbox One, Xbox Series X/S, PlayStation 4, PlayStation 5 and Stadia on October 7th.

All products recommended by Engadget are selected by our editorial team, independent of our parent company. Some of our stories include affiliate links. If you buy something through one of these links, we may earn an affiliate commission.

Coinsmart. Beste Bitcoin-Börse in Europa
Source: https://www.engadget.com/far-cry-6-season-pass-204352211.html?src=rss_b2c

Continue Reading

CNBC

Ubisoft’s Avatar game will arrive in 2022

Published

on

Ubisoft’s Avatar game is finally starting to take shape. The game publisher has revealed that Massive Entertainment’s newly-named Avatar: Frontiers of Pandora will be available on PlayStation 5, Xbox Series X/S, PC, Luna and Stadia sometime in 2022. Yes, that’s right — unlike some of Ubi’s other recent games, there won’t be versions for last-gen consoles.

There isn’t much to say about the plot of the first-person action game at this point, but you’ll play as a Na’vi traveling across the “never-before-seen” Western Frontier of Pandora. Not surprisingly, you’ll have to deal with both the planet’s wildlife as well as the human RDA forces determined to exploit the world for all it’s worth. The Avatar title will take advantage of the newest version of Ubisoft’s Snowdrop engine, and it’s clear that will involve a lot of visual detail befitting James Cameron’s CG world.

The timing will be convenient. Cameron’s repeatedly delayed Avatar sequels should arrive starting December 16th, 2022. The game will no doubt be part of the hype campaign for that movie release. Still, it should be interesting to see how well the eco-conscious sci-fi franchise translates to a video game.

All products recommended by Engadget are selected by our editorial team, independent of our parent company. Some of our stories include affiliate links. If you buy something through one of these links, we may earn an affiliate commission.

Coinsmart. Beste Bitcoin-Börse in Europa
Source: https://www.engadget.com/avatar-frontiers-of-pandora-game-release-date-202716664.html?src=rss_b2c

Continue Reading

CNBC

‘Mario + Rabbids: Sparks of Hope’ is coming to Switch in 2022

Published

on

Nintendo and Ubisoft have teamed up once again for a sequel to Mario + Rabbids Kingdom Battle. The original was a well-received turn-based strategy game that came out in the Switch’s early days. Now, Nintendo and Ubisoft are revisiting the series with Mario + Rabbids: Sparks of Hope.

The sequel will take Mario, Luigi, Peach, Rabbid Rosalina and others beyond the Mushroom Kingdom. They’ll have to save a whole galaxy and face off against a villain named Cursa. However, they’ll have some new allies on their side called Sparks, which look like Rabbid versions of the Lumas from Super Mario Galaxy.

This time around, the developers say they’re adding new action elements to “push the boundaries of tactical games even further.”

The first cinematic and gameplay trailers were shown at the Ubisoft Forward E3 event, but details initially emerged earlier on Saturday. Nintendo appeared to jump the gun with a Sparks of Hope eShop listing that was later taken down. 

Mario + Rabbids: Sparks of Hope is a Nintendo Switch exclusive. It’ll be released in 2022.

All products recommended by Engadget are selected by our editorial team, independent of our parent company. Some of our stories include affiliate links. If you buy something through one of these links, we may earn an affiliate commission.

Coinsmart. Beste Bitcoin-Börse in Europa
Source: https://www.engadget.com/mario-rabbids-sparks-of-hope-nintendo-switch-ubisoft-2022-201632962.html?src=rss_b2c

Continue Reading
Esports3 days ago

Genshin Impact Echoing Conch Locations Guide

Esports5 days ago

All 17 character locations in Collections in Fortnite Chapter 2, season 7

Esports4 days ago

Here are all the milestones in Fortnite Chapter 2, season 7

Blockchain5 days ago

BPI No Longer Allows Crypto Transactions

AR/VR5 days ago

‘Warhammer Age of Sigmar: Tempestfall’ Gets First Look at Gameplay, Invite-only Beta

Blockchain4 days ago

Doge meme Shiba Inu dog to be auctioned off as NFT

Esports4 days ago

What Time Does Minecraft 1.17 Release?

Esports3 days ago

MLB The Show 21 Kitchen Sink 2 Pack: Base Round Revealed

Esports4 days ago

How to Fly UFOs in Fortnite

Blockchain4 days ago

World Economic Forum Releases a DeFi Policy Toolkit for Fair and Executable Regulations

Esports5 days ago

How to unlock the Superman Skin in Fortnite Chapter 2, season 7

Blockchain5 days ago

Donald Trump on Bitcoin: “Essentially It’s a Currency Competing Against the Dollar”

Esports5 days ago

How to get Rick from Rick and Morty in Fortnite Chapter 2, season 7

zephyrnet3 days ago

7th Global Blockchain Congress by Agora Group & TDeFi on June 21st and 22nd, 2021, Dubai.

Aviation3 days ago

The Story Of The Boeing 777 Family

Gaming4 days ago

MUCK: How To Get The Best Weapon | Wyvern Dagger Guide

Blockchain3 days ago

Woonkly will be the official Title Sponsor of the 7th edition Global Blockchain Congress organized by Agora Group in Dubai

Crowdfunding5 days ago

US Government Claws Back Crypto from Ransomware Scam as Feds Flex Growing Tech Muscle

Blockchain3 days ago

Death Cross is Appearing Over Bitcoin Price Chart

Cyber Security5 days ago

How to Learn Cybersecurity On Your Own

Trending