<?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: Ako</title>
    <description>The latest articles on DEV Community by Ako (@akov).</description>
    <link>https://dev.to/akov</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%2F654794%2F2a089962-481f-4b66-a8a7-e675b8b5b102.jpg</url>
      <title>DEV Community: Ako</title>
      <link>https://dev.to/akov</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/akov"/>
    <language>en</language>
    <item>
      <title>How to set up tailwindcss with create-react-app + jit feature</title>
      <dc:creator>Ako</dc:creator>
      <pubDate>Sun, 27 Jun 2021 08:19:13 +0000</pubDate>
      <link>https://dev.to/akov/how-to-set-up-tailwindcss-with-create-react-app-jit-feature-1m20</link>
      <guid>https://dev.to/akov/how-to-set-up-tailwindcss-with-create-react-app-jit-feature-1m20</guid>
      <description>&lt;p&gt;If you used create react app + tailwindcss before, probably you noticed that website is slow to load in development mode or when you want to use inspect feature and debug things, Css takes time to load,or in worst scenario if you want to add some custom color pallets and also use dark mode, maybe browser unable to load the CSS file!whaaaat??? why??? Because tailwindcss creates a huge Css file under the hood in the size of megabytes, and max size the browser supports is about 20MB. Above that browser gives up to load the file. But there is a solution.&lt;br&gt;
Recently tailwind introduced "jit" mode 😍. Just in time Css build, that generates CSS on demand and there will be no need to that huge Css file for your website to look good. But jit mode needs Postcss 8. As you know CRA 4 (that is the current version), uses Postcss 7 😞 .&lt;br&gt;
Buuuuut! there is a workaround! lets see how to set up tailwindcss jit feature with CRA.&lt;/p&gt;

&lt;h1&gt;
  
  
  Create App
&lt;/h1&gt;

&lt;p&gt;first create your app using CRA:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

npx create-react-app my-app


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h1&gt;
  
  
  Set up tailwindcss
&lt;/h1&gt;

&lt;p&gt;Go to the my-app folder (or whatever you named it) and install tailwindcss and its peer-dependencies.&lt;br&gt;
NOTE: postcss-cli version 9.0.1 is the current last version and have some problems and does not work correctly so use version 8.3.1 for now.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

npm install -D tailwindcss@latest postcss-cli@8.3.1 autoprefixer@latest


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Initialize tailwindcss:
&lt;/h3&gt;

&lt;p&gt;Create talwindcss config file:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

npx tailwindcss init


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;this creates a file named tailwind.config.js in the root of your project.to be able to use jit mode, you have to enable it inside your config file (&lt;a href="https://tailwindcss.com/docs/just-in-time-mode" rel="noopener noreferrer"&gt;here&lt;/a&gt; you can find more about jit feature and how to set it up in tailwind css). so your tailwind config file should look like this now, if its not, just copy and paste it:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

// tailwind.config.js
module.exports = {
  mode: "jit",
  purge: ["./src/**/*.{js,jsx,ts,tsx}", "./public/index.html"],
  darkMode: false, // or 'media' or 'class'
  theme: {
    extend: {},
  },
  variants: {},
  plugins: [],
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Create &lt;em&gt;tailwind.css&lt;/em&gt; file inside &lt;em&gt;src&lt;/em&gt; folder and paste this lines:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

/* ./my-app/src/tailwind.css */
@tailwind base;
@tailwind components;
@tailwind utilities;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;we are done with tailwindcss part.&lt;/p&gt;

&lt;h1&gt;
  
  
  Postcss config:
&lt;/h1&gt;

&lt;p&gt;Create a file named &lt;em&gt;postcss.config.js&lt;/em&gt; in the root of your project and add tailwindcss and autoprefixer to your PostCSS configuration.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

// postcss.config.js
module.exports = {
  plugins: { tailwindcss: {}, autoprefixer: {} }
};


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h1&gt;
  
  
  Automate CSS rebuild:
&lt;/h1&gt;

&lt;p&gt;To automate css creation on file changes we need a package named &lt;em&gt;chokidar&lt;/em&gt;. We also need &lt;em&gt;concurrently&lt;/em&gt; to run two scripts at a time;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

npm i -D chokidar-cli concurrently


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Then edit &lt;em&gt;package.json&lt;/em&gt; scripts as below:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

  "scripts": {
    "start": "react-scripts start",
    "build": "npm run watch:css &amp;amp;&amp;amp; react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "watch:css": "postcss src/tailwind.css -o src/index.css",
    "watch": "chokidar \"./src/**/*.js\" -c \"npm run watch:css\"",
    "dev": "concurrently \"npm run watch\" \"npm run start\""
  },


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We must use &lt;code&gt;npm run dev&lt;/code&gt; to run our app and also automate css build.&lt;/p&gt;

&lt;h1&gt;
  
  
  Test it
&lt;/h1&gt;

&lt;p&gt;Paste below lines in your &lt;em&gt;App.js&lt;/em&gt; file for the test:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

import "./index.css";

function App() {
  return (
    &amp;lt;div className="bg-blue-400 h-screen grid"&amp;gt;
      &amp;lt;div className="w-3/4 my-auto ml-20"&amp;gt;
        &amp;lt;h1 className="text-5xl font-bold mb-10 text-white"&amp;gt;JIT mode is cool&amp;lt;/h1&amp;gt;
        &amp;lt;p className="text-white"&amp;gt;
          Lorem ipsum dolor sit amet, consectetur adipisicing elit. Explicabo officia earum ducimus neque obcaecati consequuntur ratione accusamus at officiis tempore,
          magnam non debitis fugit unde alias id quidem necessitatibus.
        &amp;lt;/p&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default App;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;now if you run your app using &lt;code&gt;npm run dev&lt;/code&gt; you must see somehting like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy17na8pzw3hjw695il70.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy17na8pzw3hjw695il70.png" alt="result"&gt;&lt;/a&gt;&lt;br&gt;
Hooorayyy! 😃 👯👯 we made it. now we can use tailwindcss utility classes in our files and no more worried about large css files and loading issues.&lt;br&gt;
here is a ready made project that you can clone and use it without doing all the steps:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/ako-v/cra-tailwind-jit" rel="noopener noreferrer"&gt;https://github.com/ako-v/cra-tailwind-jit&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

</description>
      <category>react</category>
      <category>tailwindcss</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
