Tutorial

How To Set Up a React Project With Parcel

JavaScriptReact

While this tutorial has content that we believe is of great benefit to our community, we have not yet tested or edited it to ensure you have an error-free learning experience. It's on our list, and we're working on it! You can help us out by using the "report an issue" button at the bottom of the tutorial.

Introduction

When it comes to web application bundling, ParcelJS is a relatively new bundler. Parcel describes itself as a blazing fast, zero-configuration web application bundler.

Parcel is based on the premise that building apps with JavaScript bundlers could be easier. Webpack is arguably the most popular bundler out there right now, it’s an awesome tool that gives you the power of configurability but sometimes all you need is a minimal tool that helps you get started as soon as possible.

This is where Parcel comes in. It touts itself as a fast bundle tool with zero configuration—all you need to is just point it at the entry point of your application. Parcel also offers the following features:

  • Fast bundle times - Parcel has a considerably faster time than other bundling tools out there. Below is a screenshot of the Parcel page on Github. Screenshot of Parcel benchmarks
  • Assets Bundling - Parcel has out of the box support for JS, CSS, HTML, file assets.
  • Automatic transforms - All your code are automatically transformed using Babel, PostCSS, and PostHTML.
  • Code Splitting - Parcel allows you to split your output bundle by using the dynamic import() syntax.
  • Hot module replacement (HMR) - Parcel automatically updates modules in the browser as you make changes during development, no configuration needed.
  • Error Logging - Parcel prints syntax highlighted code frames when it encounters errors to help you pinpoint the problem.

One other thing to note about Parcel is that it allows developers to use an index.html file as an entry point as opposed to JS modules.

In this tutorial, you will set up a React web app with Parcel.

Step 1 — Getting Started With Parcel

To get started, create a new working directory and install Parcel by running the command below in your terminal:

  • npm install -g parcel-bundler

The next thing to do is create a package.json file in your directory. You can do this by running the command below:

  • npm init -y

That generates a working package.json for you.

JSON output showing that the file was created

Parcel can take any type of file as its entry point, but an HTML or JavaScript file is a good place to start. If you link your main JavaScript file in the HTML using a relative path, Parcel will also process it for you. Create the index.html and index.js files and add the following content to them:

index.html
<html>
<body>
  <script src="./index.js"></script>
</body>
</html>
index.js
console.log("hello world");

Now that the files are created, you’re ready to run the app. Parcel comes with a development server built in which will automatically rebuild your app as you change files and supports hot module replacement for fast development. To run the app, run the terminal command below:

  • parcel index.html

You can now open http://localhost:1234/ in your browser and check the console for the output in the console.
Console displaying "hello world" text

Step 2 — Setting up a Project with Parcel and React

Next you will set up a React project with Parcel. Before you continue, you’ll need to install some dependencies:

  • npm i react
  • npm i react-dom

The dependencies above will install React and react-dom in your application. Since React is written in ES6, we’ll need a way to transform the code. Parcel does that for you with no need for configurations. All you have to do is install the presets and Parcel does the heavy lifting for you.

  • npm i babel-preset-env babel-preset-react --save-dev

Once that’s done, create a .babelrc file and edit it with the following:

.babelrc
{
  "presets": ["env", "react"]
}

Now Babel is configured to transform ES6/ES7 JavaScript into a meaningful JavaScript bundle for browsers.

Now you can begin to create the React app and its components. Open up the index.html file and replace it with the code below.

index.html
<!DOCTYPE html>
<html>
<head>
    <title>React starter app</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.6.2/css/bulma.min.css">
</head>
<body>
<div id="app"></div>
<script src="./src/index.js"></script>
</body>
</html>

Next up, create a folder titled src and in it create an index.js file and edit it with the following:

src/index.js
import React from "react";
import ReactDOM from "react-dom";
import Header from './components/Header'

class HelloMessage extends React.Component {
    render() {
        return <div>
            <Header/>
            <div className="container">
                <h1>Hello {this.props.name}</h1>
            </div>
        </div>
    }
}

let App = document.getElementById("app");

ReactDOM.render(<HelloMessage name="Yomi" />, App);

This is a basic React starting app. The HelloMessage component will be rendered in the index.html file in the div with an ID of app.

Next, create a Header component. Create a component folder and in it, create a JS file titled Header.js and edit it with the code below.

component/Header.js
import React from 'react'
import ParcelLogo from "../img/parcel-logo.svg";

const Header = () => (
    <header>
        <nav className="navbar" role="navigation" aria-label="main navigation">
            <div className="navbar-brand">
                <a className="navbar-item" href="/">
                    <img width="120" src={ParcelLogo} alt=""/>
                </a>
            </div>
        </nav>
    </header>
)

export default Header

Parcel also supports the import of assets like images. One more thing to note is that, Parcel also ships with SCSS support. To use it, install node-sass:

  • npm i node-sass

Once you have node-sass installed, you can import SCSS files from JavaScript files. In your index.js file, add the line of code below to the top of the file to import an SCSS file:

index.js
import './scss/app.scss'
...

Let’s also create the app.scss file. Create a folder titled scss and create a file titled app.scss and edit it with the following:

scss/app.scss
body {
  background-color: #fefefe;
  text-align: center;
  .navbar {
    background: #21374B;
    color: #E7DACB;
    height: 50px;
  }
  h1 {
    font-size: 40px;
    margin-top: 30px;
  }
}

Now that you’re done setting up the React app, run it and see if it works. To do that, you’ll need to start a dev server and configure the package.json. Add the line of code below to the package.json file:

package.json
"scripts": {
    "start": "parcel index.html"
  },

This means that whenever you run the npm start command, you are telling Parcel to start a development server using the index.html file as a entry file. Run the npm start command and you can now view the React app at http://localhost:1234 with support for hot module reloading.

Web page displaying "Hello World" page

Step 3 — Building for Production

To prepare your app for production with Parcel, you need to run the parcel build index.html command. Add that to the scripts object in the package.json file.

package.json
"scripts": {
    "start": "parcel index.html",
    "build": "parcel build index.html"
  },

Once you run the npm run build command, Parcel disables watch mode and hot module replacement so it will only build once. It also enables the minifier for all output bundles to reduce file size. The build command also enables production mode which also sets the NODE_ENV=production environment variable.

Parcel also gives you some options in which you can use to choose how your app is prepared for production mode.

If you’d prefer another directory for Parcel to put the production files in (dist is the default directory), you can specify that by appending --out-dir directory-name at the end of the build key in package.json so it looks like this:

package.json
"scripts": {
    "start": "parcel index.html",
    "build": "parcel build index.html --out-dir directory-name"
  },

If you’d like to disable minification which is the default setting, you can do that by appending --no-minify at the end of the build key in package.json so it looks like this:

package.json
"scripts": {
    "start": "parcel index.html",
    "build": "parcel build index.html --no-minify"
  },

If you’d like to disable filesystem caching, you can do that by appending --no-cache at the end of the build key in package.json so it looks like this:

package.json
"scripts": {
    "start": "parcel index.html",
    "build": "parcel build index.html --no-cache"
  },

You can read more about the different options here.

Conclusion

In this tutorial, you bundled a web app with Parcel. Parcel is still relatively new and its support keeps growing in numbers every day. Its use case might be different from Webpack, but it’s a bundle tool that can be considered in your everyday web development.

You can check out the Parcel repo here to see issues and submit PRs.

The codebase for this tutorial can be found on GitHub.

0 Comments

Creative Commons License