7

Create SPA: React + Typescript + Parcel

 2 years ago
source link: https://carlosvin.github.io/langs/en/posts/react-typescript-parcel/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Create SPA: React + Typescript + Parcel

Example project how to create a Single Page Application with React, Typescript and Parcel.

I was about to start yet another personal project, it consists of a SPA (Single Page Application) for a travel journal.

Some time ago I tried Parcel, I really loved how simple it was to create a simple project from scratch, using Typescript + React stack. I’ve decided to create this template or base project, so next time I want to create a new SPA with my favorite frontend stack, I will only have to:

Read this before: Parcel is not as mature as Webpack

If you want to create a production ready React application, use Webpack or better create-react-app which bring everything you need to develop a PWA with React and Typescript. Following you can find an example of an app I am developing using create-react-app: https://github.com/carlosvin/budget-tracker.

Parcel is a package bundler under development, not as mature as webpack. There are no go errors for me, at least in regards to Typescript support, see this issue in github #1378.

I still think it is a promising project, bringing more simplicity and speed to JS bundlers world, I will give it a try again for serious projects when Parcel 2 is ready, check Parcel 2 development status.

Quick start

Development server

Last yarn start command will:

Each time you save a file, you will see automatically the result at http://localhost:1234 without refreshing the page

Build production bundle

Parcel’s default optimizations will be applied to generated files.

Files are saved at dist folder. Inside dist folder there is also a file with information about bundle content sizes: dist/report.html.

Step by step project creation

In this section I will describe how I created this project.

Firstly, create package.json with yarn init command.

Add required dependencies

Add React dependencies.

Previous command modifies package.json file adding dependencies section and will also install React packages in node_modules folder.

Add Typescript compiler as development dependency.

We also need Parcel bundler.

I’ve added a non-required dependency, it is a plugin to generate a report of generated bundle contents (the parcel version of webpack-bundle-analyzer.

Create application source code

First we create the React application in src/index.tsx file.

Parcel can take index.html file as entry file and it figures out how to build the application, so let’s create src/index.html as follows:

We need div tag for React to inject the DOM elements. The script declaration is used by Parcel to find entry point to build.

Add commands build the project

I’ve added the commands:

  • build: Check "Build production bundle" section.

  • start: Check "Development server" section.

Then to it is really easy to:

  • run development server: yarn start

  • generate a production bundle: yarn build

There is another approach described in Parcel documentation that consists of installing Parcel globally.

I’ve opted for more isolated approach that affects only project you are working on, you just install Parcel as devDependency. There is a tiny drawback, you can’t just run parcel index.html, because it is not installed in your system, but in node_modules.

There is a simple way to run any binary installed in node_modules, you can just run npx parcel index.html.

I like more to define build steps in package.json file, so you can have well defined commands more suited to build your project. You can also use these commands as documentation how to build your project.

Configure Typescript (optional)

Create a tsconfig.json file

With this configuration, Typescript compiler will:

  • Generate files in dist folder.

  • Generate source maps.

  • Will not allow to declare any type, for example following declaration is not allowed: const elements: any;

  • Generated module code will be CommonJs.

  • Generated code will be ECMAScript 5 compliant.

  • Support JSX in .tsx files.

Full source code


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK