This is a guide for setting up a React app to be bundled with Parcel 📦. We'll also configure ESLint and Jest.
If you want a more detailed explanation of what Parcel does then have a look at the Parcel document.
npm init -yto initialise your reponpm i -D parcel-bundler @babel/preset-env @babel/preset-react @babel/plugin-proposal-class-propertiesto install dev dependenciesnpm i react react-domto install dependencies- Create
.babelrcfile containing:
{
"presets": ["@babel/preset-env", "@babel/preset-react"],
"plugins": ["@babel/plugin-proposal-class-properties"]
}Parcel will automatically use Babel to transpile all ES6 and React syntax out of the box, but we want to use a new feature (class properties), so we need to tell Parcel to use an extra plugin.
- Create an
index.htmlfile containing:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>My App</title>
</head>
<body>
<div id="root"></div>
<script src="index.js"></script>
</body>
</html>Parcel will use this as an 'entrypoint' to your app. It'll find the index.js script in there and follow all the imports to bundle everything. Then it'll replace the index.js with the final JS bundle when it outputs the dist folder.
- Create
index.jswith your react setup:
import React from "react";
import { render } from "react-dom";
const App = () => <h1>Hello World</h1>;
render(<App />, document.getElementById("root"));- Setup your build scripts in
package.json:
{
"scripts": {
"dev": "parcel index.html",
"build": "parcel build index.html"
}
}- Run
npm run devto start the development server.
We need a few extra things to get Jest working with our ES6+ and React code.
npm i -D jestto install testing dependencies- Add
"test": "jest --watch"to your npm scripts inpackage.json
Jest will read the .babelrc file to know what presets/plugins it should use.
Parcel allows us to import non-JS files like CSS or images. Unfortunately Jest doesn't use Parcel, so we need to help it understand these imports.
Create a directory called __mocks__, with two files inside:
fileMock.js:
export default "whatever-string";styleMock.js:
export default {};Finally we need to tell Jest to use these mocks for the file types we are importing. Add this to the top level of your package.json:
"jest": {
"moduleNameMapper": {
"\\.(css|less|sass|scss)$": "<rootDir>/__mocks__/styleMock.js",
"\\.(gif|ttf|eot|svg)$": "<rootDir>/__mocks__/fileMock.js"
}
}Note: make sure you don't have ESLint installed globally (npm rm -g eslint), otherwise you can get really weird errors. If you still get errors after uninstalling try restarting your editor.
npm install -D eslint babel-eslint eslint-plugin-import eslint-plugin-react eslint-config-react-minimalto install dev dependencies- create
.eslintrcfile with:
{
"extends": ["eslint:recommended", "react-minimal"]
}I've packaged up all the necessary config into eslint-config-react-minimal. Check out the readme if you want more information.
Run npx gitignore node to generate a JS-focused .gitignore file. You may want to add the dist/ directory to it, as Parcel will re-generate this on every build from your source files.