<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Tom Valorsa</title>
    <description>The latest articles on DEV Community by Tom Valorsa (@tomvalorsa).</description>
    <link>https://dev.to/tomvalorsa</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F366827%2F499d099e-f814-4ecf-8f78-484a4551fd83.jpeg</url>
      <title>DEV Community: Tom Valorsa</title>
      <link>https://dev.to/tomvalorsa</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tomvalorsa"/>
    <language>en</language>
    <item>
      <title>Custom templates with Create React App</title>
      <dc:creator>Tom Valorsa</dc:creator>
      <pubDate>Fri, 24 Jul 2020 08:01:32 +0000</pubDate>
      <link>https://dev.to/tomvalorsa/custom-templates-with-create-react-app-59bk</link>
      <guid>https://dev.to/tomvalorsa/custom-templates-with-create-react-app-59bk</guid>
      <description>&lt;p&gt;&lt;em&gt;&lt;span&gt;Cover photo by &lt;a href="https://unsplash.com/@grohsfabian?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Fabian Grohs&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/developer?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;&lt;/span&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Templates are great. They provide a useful starting point for projects and can cut out common setup and configuration work. We can also enhance them with tools that promote good habits and processes by default, making it easier to make the "right" decisions. This saves time and mental bandwidth - with these tasks out of the way there's more time to concentrate on actually writing code.&lt;/p&gt;

&lt;p&gt;Templates can be extended and refined over time. By building on what we already have we can start further and further from "zero". This, to me, sums up the process of technical progress - by packaging up something useful and making it easy to replicate we can direct our efforts to the "next" problem.&lt;/p&gt;

&lt;p&gt;So yeah, if you haven't guessed already I'm a big fan of templates. This post will walk through the process of creating a custom template for &lt;a href="https://github.com/facebook/create-react-app"&gt;Create React App (CRA)&lt;/a&gt;, using the official default template as a base.&lt;/p&gt;

&lt;h2&gt;
  
  
  Planning out the template
&lt;/h2&gt;

&lt;p&gt;Templates of any kind should have a clear purpose. This helps to avoid a situation where we end up catering for too many possibilities and face difficult decisions on what should and shouldn't be included. We're trying to make things easier for ourselves after all!&lt;/p&gt;

&lt;p&gt;We're going to look at a more general baseline use case, so it will be lightweight with some utilities that would be useful for any project. If you had a more specific use case it would make sense to create a more specialised template with features and utilities relevant to the problem area.&lt;/p&gt;

&lt;p&gt;What we'll do:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;use the official CRA default template as a starting point&lt;/li&gt;
&lt;li&gt;remove some files&lt;/li&gt;
&lt;li&gt;update some files&lt;/li&gt;
&lt;li&gt;add some common utilities (Prettier, Source Map Explorer, testing coverage reporting with Jest)&lt;/li&gt;
&lt;li&gt;test it out by generating a new project&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Anatomy of a CRA template
&lt;/h2&gt;

&lt;p&gt;A CRA template has two key elements:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/template&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;can contain anything you want and will form the basis of your newly created project&lt;/li&gt;
&lt;li&gt;must contain some common files and folders as a bare minimum for &lt;code&gt;react-scripts&lt;/code&gt; to work as expected&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;template.json&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the config file for your template&lt;/li&gt;
&lt;li&gt;currently supports a single key, &lt;code&gt;"package"&lt;/code&gt;, under which you can nest info that will be merged into the newly created project's &lt;code&gt;package.json&lt;/code&gt; file&lt;/li&gt;
&lt;li&gt;any dependencies and scripts that you specify will be merged in with the default values in &lt;code&gt;react-scripts&lt;/code&gt; (i.e. dependencies like React itself, and scripts that set your app up for development/build)&lt;/li&gt;
&lt;li&gt;other values will just be directly copied over, replacing any defaults if they already exist&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The template needs to have the following structure and files as a bare minimum, as laid out in the &lt;a href="https://create-react-app.dev/docs/custom-templates/"&gt;CRA docs&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;README.md
template.json
package.json
template/
  README.md
  gitignore (this will be renamed to .gitignore during the init process)
  public/
    index.html
  src/
    index.js
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Using the default template as a starting point
&lt;/h2&gt;

&lt;p&gt;To ensure we're meeting the minimum criteria above, we can use the &lt;a href="https://github.com/facebook/create-react-app/tree/master/packages/cra-template"&gt;official default template&lt;/a&gt; of CRA as a foundation for our own template.&lt;/p&gt;

&lt;p&gt;In your terminal, navigate to the directory where this template should live and run the following commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Clone the repo&lt;/span&gt;
git clone https://github.com/facebook/create-react-app.git

&lt;span class="c"&gt;# Copy the template into the current directory&lt;/span&gt;
&lt;span class="nb"&gt;cp&lt;/span&gt; &lt;span class="nt"&gt;-r&lt;/span&gt; create-react-app/packages/cra-template &lt;span class="nb"&gt;.&lt;/span&gt;

&lt;span class="c"&gt;# Clean up after ourselves&lt;/span&gt;
&lt;span class="nb"&gt;rm&lt;/span&gt; &lt;span class="nt"&gt;-rf&lt;/span&gt; create-react-app
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Cleaning up
&lt;/h2&gt;

&lt;p&gt;Let's get rid of some unnecessary files and edit a few of the existing ones so that we're left with only what we need.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Delete &lt;code&gt;App.css&lt;/code&gt; and &lt;code&gt;logo.svg&lt;/code&gt; from the &lt;code&gt;/template&lt;/code&gt; directory&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Update &lt;code&gt;App.js&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Hello, world&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;3. Update &lt;code&gt;App.test.js&lt;/code&gt; to reflect the change to &lt;code&gt;&amp;lt;App /&amp;gt;&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;renders test heading&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;App&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;headingElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getByText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/hello, world/i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;headingElement&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;toBeInTheDocument&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;4. The last step here is to add the following to your &lt;code&gt;package.json&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;...&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"main"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"template.json"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;N.B. this is a necessary step until CRA v4 is released. &lt;a href="https://github.com/facebook/create-react-app/pull/8734"&gt;A fix has already been made&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You may also want to update the name and info in &lt;code&gt;README.md&lt;/code&gt; and &lt;code&gt;package.json&lt;/code&gt;, and the name of the directory we're working in, but I'll leave that up to you.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding some common utilities
&lt;/h2&gt;

&lt;p&gt;There are a couple of things that I always set up on new projects - this template is the perfect place to put these things.&lt;/p&gt;

&lt;p&gt;We're going to add:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://prettier.io/"&gt;Prettier&lt;/a&gt; to ensure our code stays nicely formatted&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/danvk/source-map-explorer"&gt;Source Map Explorer&lt;/a&gt; so we can keep an eye on the state of the app's bundle&lt;/li&gt;
&lt;li&gt;an npm script for code coverage reporting using &lt;a href="https://jestjs.io/"&gt;Jest&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  A note on specifying dependencies
&lt;/h3&gt;

&lt;p&gt;We'll have to add a few dependencies in order to complete the next steps. We're not actually going to install them, we just need to list what we need in &lt;code&gt;template.json&lt;/code&gt; so that CRA knows what to install when we use this template to create a new project. The process we'll use to do this is as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;go to the &lt;a href="https://www.npmjs.com/"&gt;npm site&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;search for the package to add&lt;/li&gt;
&lt;li&gt;copy the version number, and then add the package and version number to &lt;code&gt;template.json&lt;/code&gt; with &lt;code&gt;^&lt;/code&gt; as a prefix, for example:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"package"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"dependencies"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"prettier"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^2.0.5"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;^&lt;/code&gt; symbol is a "caret", and allows us to give npm permission to install newer minor or patch versions of the package - it's like saying "feel free to install a newer version if there is one, but no breaking changes please". It does rely on the package authors following &lt;a href="https://semver.org/"&gt;semantic versioning&lt;/a&gt; but most major open source projects do so we should be fine, just be aware of this. This will mean that we can go for a longer period of time without needing to update the template's dependencies (although this is something to revisit periodically so that we're benefitting from the latest releases). If you want a specific version of a package then you can leave this off.&lt;/p&gt;

&lt;p&gt;N.B. while you'd normally add these as &lt;code&gt;devDependencies&lt;/code&gt; the current system for templates in CRA only supports listing them as regular &lt;code&gt;dependencies&lt;/code&gt;. While this &lt;a href="https://github.com/facebook/create-react-app/issues/6180"&gt;wasn't considered a problem in the past&lt;/a&gt;, it now looks like this will be &lt;a href="https://github.com/facebook/create-react-app/pull/8838"&gt;supported in a future release&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Adding Prettier
&lt;/h3&gt;

&lt;p&gt;We're going to add Prettier and run it with a pre-commit hook using &lt;a href="https://github.com/typicode/husky"&gt;Husky&lt;/a&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Add &lt;code&gt;prettier&lt;/code&gt;, &lt;code&gt;pretty-quick&lt;/code&gt;, and &lt;code&gt;husky&lt;/code&gt; to &lt;code&gt;dependencies&lt;/code&gt; in &lt;code&gt;template.json&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a file called &lt;code&gt;prettier.config.js&lt;/code&gt; in &lt;code&gt;/template&lt;/code&gt; and add config options:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Some of these are defaults, but let's be explicit for other humans&lt;/span&gt;
&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;tabWidth&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;semi&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;singleQuote&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;bracketSpacing&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;printWidth&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;3. Create a file called &lt;code&gt;.prettierignore&lt;/code&gt; in &lt;code&gt;/template&lt;/code&gt; (this can stay blank for now)&lt;/p&gt;

&lt;p&gt;4. Create a file called &lt;code&gt;husky.config.js&lt;/code&gt; in &lt;code&gt;/template&lt;/code&gt; and add the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;hooks&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;pre-commit&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;npm run pre-commit&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;5. In &lt;code&gt;template.json&lt;/code&gt; add a &lt;code&gt;"scripts"&lt;/code&gt; object to &lt;code&gt;"package"&lt;/code&gt; with the following script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"package"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;...&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"pre-commit"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"pretty-quick --staged"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;N.B. you might also want to add Prettier to the actual CRA template we're creating to ensure your template code is formatted as well.&lt;/p&gt;

&lt;h3&gt;
  
  
  Adding Source Map Explorer
&lt;/h3&gt;

&lt;p&gt;Being able to see what actually goes into your bundles is useful when trying to reduce bundle size and save your user from downloading unnecessary bytes. To get some visibility on this we're going to use Source Map Explorer.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Add &lt;code&gt;source-map-explorer&lt;/code&gt; to &lt;code&gt;dependencies&lt;/code&gt; in &lt;code&gt;template.json&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In &lt;code&gt;template.json&lt;/code&gt; add the following to the &lt;code&gt;"scripts"&lt;/code&gt; object:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"package"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;...&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="err"&gt;...&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"analyze"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"source-map-explorer 'build/static/js/*.js'"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That's it! This command will only look at built files. If you want, you can prefix the command above &lt;code&gt;npm run build &amp;amp;&amp;amp;&lt;/code&gt; so that you don't have to build as a separate step before running this.&lt;/p&gt;

&lt;h3&gt;
  
  
  Adding code coverage reporting with Jest
&lt;/h3&gt;

&lt;p&gt;This is also quite straightforward. Jest has its own built-in coverage reporting functionality, and the package itself already comes with any app created using CRA so we don't even need to add any dependencies.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;template.json&lt;/code&gt; add the following to the &lt;code&gt;"scripts"&lt;/code&gt; object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"package"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;...&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="err"&gt;...&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"coverage"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"npm test -- --coverage --watchAll=false"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Putting it all together
&lt;/h2&gt;

&lt;p&gt;Now that we've added a bunch of useful stuff we need to make sure it works as expected. CRA allows to you specify the path to a custom template when creating a new app. For convenience you might want to add something like this to your &lt;code&gt;.bash_profile&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Maybe with a catchier name...&lt;/span&gt;
&lt;span class="nb"&gt;alias &lt;/span&gt;create-react-app-custom&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"npx create-react-app --template=file:/path/to/template"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Hint: a quick way to make sure you get the correct path is to type use &lt;code&gt;pwd&lt;/code&gt; in your terminal and just copy/paste the result into the alias above.&lt;/p&gt;

&lt;p&gt;Now you can just run the following every time you want to use this template:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;create-react-app-custom &amp;lt;name&amp;gt; &lt;span class="o"&gt;[&lt;/span&gt;options]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;N.B. you'll need to open up a new terminal window for this change to your &lt;code&gt;.bash_profile&lt;/code&gt; to take effect.&lt;/p&gt;

&lt;p&gt;In a new terminal window, try running the following command and looking at the output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;create-react-app-custom custom-app
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The contents of the generated project should look familiar. This is the contents of &lt;code&gt;/template&lt;/code&gt;, and if you look at the &lt;code&gt;package.json&lt;/code&gt; for this project you will see that the dependencies and scripts we specified in &lt;code&gt;template.json&lt;/code&gt; have been included.&lt;/p&gt;

&lt;p&gt;To test that our additions have been included correctly you can do the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Prettier&lt;/strong&gt;: mess up some formatting and commit the change to see the pre-commit handler tidy up for you (e.g. remove the semi-colons in &lt;code&gt;App.js&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Source Map Explorer&lt;/strong&gt;: run &lt;code&gt;npm run build &amp;amp;&amp;amp; npm run analyze&lt;/code&gt; to see a report in your browser&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Test coverage&lt;/strong&gt;: run &lt;code&gt;npm run coverage&lt;/code&gt; to see a very basic coverage report based on the default &lt;code&gt;&amp;lt;App&amp;gt;&lt;/code&gt; test we left in

&lt;ul&gt;
&lt;li&gt;when this command runs it also generates a new folder, &lt;code&gt;/coverage&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;you can open &lt;code&gt;./coverage/lcov-report/index.html&lt;/code&gt; in your browser for a more interactive experience&lt;/li&gt;
&lt;li&gt;N.B. you may receive some errors in your terminal related to &lt;a href="https://github.com/facebook/create-react-app/issues/8689"&gt;this issue&lt;/a&gt; but the &lt;code&gt;/coverage&lt;/code&gt; folder should still be created&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And that's it for the basic template! When using this template to create new projects we no longer have to delete stock files that we don't need, and a few useful utilities are set up out of the box.&lt;/p&gt;

&lt;h2&gt;
  
  
  Next steps
&lt;/h2&gt;

&lt;p&gt;If you weren't already sold, I hope that over the course of reading this you've realised that templates can be incredibly useful. We've added some basic quality-of-life tools for new projects that use this template but there are a tonne of other things you could add depending on your use case - to name a few:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;set up your preferred styling solution with a basic theme, default global styles (&lt;a href="https://css-tricks.com/inheriting-box-sizing-probably-slightly-better-best-practice/"&gt;box-sizing anyone?&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;react-axe&lt;/code&gt; and a11y plugins&lt;/li&gt;
&lt;li&gt;change default icons and HTML in &lt;code&gt;/public&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;i18n config&lt;/li&gt;
&lt;li&gt;preferred folder structure&lt;/li&gt;
&lt;li&gt;add more npm scripts that match your common workflows&lt;/li&gt;
&lt;li&gt;common packages you always use, including your own utils&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;p&gt;We've looked at making our own custom template for Create React App by using the official starter as a base. This was as simple as removing some unwanted code and files, specifying some packages and scripts to include in new projects, and testing it out.&lt;/p&gt;

&lt;p&gt;Finally, you should remember that while templates can help us to save time and cut out repetitive tasks it's important to think about your use case and what to include. If you regularly end up in a situation where you're editing or deleting the files and utilities generated by your template, you've probably gone a bit overboard.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;This post was originally published on &lt;a href="https://tomvalorsa.com/blog/custom-templates-with-create-react-app/"&gt;tomvalorsa.com&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
