{"id":286449,"date":"2019-04-25T07:22:55","date_gmt":"2019-04-25T14:22:55","guid":{"rendered":"http:\/\/css-tricks.com\/?p=286449"},"modified":"2019-04-25T07:22:55","modified_gmt":"2019-04-25T14:22:55","slug":"using-parcel-as-a-bundler-for-react-applications","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/using-parcel-as-a-bundler-for-react-applications\/","title":{"rendered":"Using Parcel as a Bundler for React Applications"},"content":{"rendered":"
You may already be familiar with webpack<\/a> for asset management on projects. However, there\u2019s another cool tool out there called Parcel<\/a>, which is comparable to webpack in that it helps with hassle-free asset bundling. Where Parcel really shines is that it requires zero configuration to get up and running, where other bundlers often require writing a ton code just to get started. Plus, Parcel is super fast when it runs because it utilizes multicore processing where others work off of complex and heavy transforms.<\/p>\n So, in a nutshell, we\u2019re looking at a number of features and benefits:<\/p>\n Hopefully, you\u2019re starting to see good reasons for using Parcel. That\u2019s not to say it should be used 100% or all the time but rather that there are good cases where it makes a lot of sense.<\/p>\n <\/p>\n In this article, we\u2019re going to see how to set up a React project using Parcel. While we\u2019re at it, we\u2019ll also check out an alternative for Create React App<\/a> that we can use with Parcel to develop React applications. The goal here is see that there are other ways out there to work in React, using Parcel as an example.<\/p>\n OK, the first thing we need is a project folder to work locally. We can make a new folder and navigate to it directly from the command line:<\/p>\n Next, let\u2019s get our obligatory This gives us a We need Babel when working in React, so let\u2019s get that going:<\/p>\n Next, we install React and ReactDOM…<\/p>\n …then create a Next, we create our base We\u2019ll need an HTML file where the We will make use of the Parcel package we installed earlier. For that to work, we need to edit the Finally, let\u2019s go back to the command line and run Parcel ships with PostCSS out of the box but, if we wanted to work with something else, we can totally do that. For example, we can install node-sass<\/a> to use Sass on the project:<\/p>\n We already have autoprefixer<\/a> since it\u2019s a PostCSS plugin, so we can configure that in the We\u2019re going to want to make sure our code and assets are compiled for production use, so let\u2019s make sure we tell our build process where those will go. Again, in Running the Create React App Parcel<\/a> (CRAP<\/abbr>) is a package built by Shawn Swyz Wang<\/a> to help quickly set up React applications for Parcel. According to the documentation, we can bootstrap any application by running this:<\/p>\n That will create the files and directories we need to start working. Then, we can migrate over to the application folder and start the server.<\/p>\n Parcel is worth exploring in your next React application. The fact that there’s no required configuration and that the bundle time is super optimized makes Parcel worth considering on a future project. And, with more than 30,000 stars on GitHub, it looks like something that’s getting some traction in the community.<\/p>\n\n
Setting up a new project<\/h3>\n
mkdir csstricks-react-parcel && $_<\/code><\/pre>\n
package.json<\/code> file in there. We can either make use of npm or Yarn by running one of the following:<\/p>\n
## Using npm\r\nnpm init -y\r\n\r\n## Using Yarn, which we'll continue with throughout the article\r\nyarn init -y<\/code><\/pre>\n
package.json<\/code> file in our project folder containing the default configurations we need to work locally. Speaking of which, the parcel package can be installed globally, but for this tutorial, we\u2019ll install it locally as a dev dependency.<\/p>\n
yarn add parcel-bundler babel-preset-env babel-preset-react --dev<\/code><\/pre>\n
yarn add react react-dom<\/code><\/pre>\n
babel.rc<\/code> file and add this to it:<\/p>\n
{\r\n \"presets\": [\"env\", \"react\"]\r\n}<\/code><\/pre>\n
App<\/code> component in a new
index.js<\/code> file. Here\u2019s a quick one that simply returns a “Hello” heading:<\/p>\n
import React from 'react'\r\nimport ReactDOM from 'react-dom'\r\nclass App extends React.Component {\r\n render() {\r\n return (\r\n <React.Fragment>\r\n <h2>Hello!<\/h2>\r\n <\/React.Fragment>\r\n )\r\n }\r\n}\r\n\r\nconst rootElement = document.getElementById(\"root\");\r\nReactDOM.render(<App \/>, rootElement);<\/code><\/pre>\n
App<\/code> component will be mounted, so let\u2019s create an
index.html<\/code> file inside the
src<\/code> directory. Again, here\u2019s a pretty simple shell to work off of:<\/p>\n
<html lang=\"en\">\r\n <head>\r\n <meta charset=\"UTF-8\">\r\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\r\n <meta http-equiv=\"X-UA-Compatible\" content=\"ie=edge\">\r\n <title>Parcel React Example<\/title>\r\n <\/head>\r\n <body>\r\n <div id=\"root\"><\/div>\r\n <script src=\".\/index.js\"><\/script>\r\n <\/body>\r\n<\/html><\/code><\/pre>\n
start<\/code> script in
package.json<\/code> file so it looks like this:<\/p>\n
\"scripts\": {\r\n \"start\": \"NODE_ENV=development parcel src\/index.html --open\"\r\n}<\/code><\/pre>\n
yarn start<\/code>. The application should start and open up a fresh browser window pointing at
http:\/\/localhost:1234\/<\/code>.<\/p>\n
Working with styles<\/h3>\n
yarn add --dev node-sass autoprefixer<\/code><\/pre>\n
postcss<\/code> block of
package.json<\/code>:<\/p>\n
\/\/ ...\r\n\"postcss\": {\r\n \"modules\": false,\r\n \"plugins\": {\r\n \"autoprefixer\": {\r\n \"browsers\": [\">1%\", \"last 4 versions\", \"Firefox ESR\", \"not ie < 9\"],\r\n \"flexbox\": \"no-2009\"\r\n }\r\n }\r\n}<\/code><\/pre>\n
Setting up a production environment<\/h3>\n
package-json<\/code>:<\/p>\n
\"scripts\": {\r\n \"start\": \"NODE_ENV=development parcel src\/index.html --open\",\r\n \"build\": \"NODE_ENV=production parcel build dist\/index.html --public-url .\/\"\r\n}<\/code><\/pre>\n
yarn run build<\/code> will now build the application for production and output it in the
dist<\/code> folder. There are some additional options we can add to refine things a little further if we\u2019d like:<\/p>\n
\n
--out-dir directory-name<\/code>: This is for using another directory for the production files instead of the default
dist<\/code> directory.<\/li>\n
--no-minify<\/code>: Minification is enabled by default, but we can disable with this command.<\/li>\n
--no-cache<\/code>: This allows us to disable filesystem cache.<\/li>\n<\/ul>\n
💩 CRAP (Create React App Parcel)<\/h3>\n
npx create-react-app-parcel my-app<\/code><\/pre>\n
cd my-app\r\nyarn start<\/code><\/pre>\n
Parcel is all set up!<\/h3>\n
Additional resources<\/h4>\n