<?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>Forem: ˗ˏˋ Lee ˎˊ˗ ☄️</title>
    <description>The latest articles on Forem by ˗ˏˋ Lee ˎˊ˗ ☄️ (@yodasmydad).</description>
    <link>https://forem.com/yodasmydad</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%2F588980%2Fd580b15e-2a4d-45e9-b3fd-96395054ebc4.jpg</url>
      <title>Forem: ˗ˏˋ Lee ˎˊ˗ ☄️</title>
      <link>https://forem.com/yodasmydad</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/yodasmydad"/>
    <language>en</language>
    <item>
      <title>Step By Step Guide To Setting Up TailwindCss In A .Net Core Web App</title>
      <dc:creator>˗ˏˋ Lee ˎˊ˗ ☄️</dc:creator>
      <pubDate>Fri, 05 Mar 2021 14:17:42 +0000</pubDate>
      <link>https://forem.com/yodasmydad/step-by-step-guide-to-setting-up-tailwindcss-in-a-net-core-web-app-2m9m</link>
      <guid>https://forem.com/yodasmydad/step-by-step-guide-to-setting-up-tailwindcss-in-a-net-core-web-app-2m9m</guid>
      <description>&lt;p&gt;I've recently started to learn TailwindCss. I'm predominantly a .Net developer and really dislike all the front end tooling. Maybe because I haven't spent enough time doing it, but I keep forgetting all the millions of commands and NPM plugins I'm supposed to be using.&lt;/p&gt;

&lt;p&gt;So this is step by step guide (Mainly for my own reference 😂) on how to get it setup. This guide is assuming you have a .Net Core Web App setup already and have the 'Developer Powershell' window open in VS or Terminal open in VS Code. Make sure you have the path set to your web project and not the root of the solution.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Open up a terminal at the root of your website project&lt;/strong&gt; and then run the following to get npm initialised.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm init -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Making sure you are still in the root of your web project. Install the latest versions of TailwindCss, PostCss, PostCss CLI, Autoprefixer &amp;amp; PurgeCss via npm&lt;br&gt;
&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@latest autoprefixer@latest postcss-cli@latest @fullhuman/postcss-purgecss
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the command below and it's creates a TailwindCss config file in the root of your web project called &lt;code&gt;tailwind.config.js&lt;/code&gt;&lt;br&gt;
&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 is optional, but I found I needed a couple of extra &lt;a href="https://tailwindcss.com/docs/plugins"&gt;TailwindCss plugins&lt;/a&gt;. One for default form element styling and one for Typography. So open up your newly created &lt;code&gt;tailwind.config.js&lt;/code&gt; file and under the &lt;code&gt;plugins:&lt;/code&gt; section add the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;plugins: [
   require('@tailwindcss/forms'),
   require('@tailwindcss/typography')
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install the plugins via NPM (&lt;em&gt;You never had to do this part, but recently you get an error if you don't manually install them&lt;/em&gt;)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install -D @tailwindcss/forms @tailwindcss/typography
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Manually create a &lt;code&gt;postcss.config.js&lt;/code&gt; file in the root of your project and add the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
    plugins: [
        require('tailwindcss'),
        require('autoprefixer')
    ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create two CSS files, one for TailwindCss to use and one for the website to use. Where you put these is up to you, however further in this step by step you will need to update the paths if you don't use the same. I'm going to create &lt;code&gt;/css/tailwind.css&lt;/code&gt; and &lt;code&gt;/wwwroot/css/styles.css&lt;/code&gt;. Styles.css is where the final styles will be saved.&lt;/p&gt;

&lt;p&gt;In your &lt;code&gt;tailwind.css&lt;/code&gt; file add the specific tailwind directives shown below and save the file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@tailwind base;
@tailwind components;
@tailwind utilities;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Setup the NPM build script in your &lt;code&gt;package.json&lt;/code&gt; under the &lt;code&gt;scripts&lt;/code&gt; section add the following (Remember to update the file paths/names if your CSS files are in a different location or named differently)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
   "build-tailwind": "postcss css/tailwind.css -o wwwroot/css/styles.css",
   "watch-tailwind": "postcss css/tailwind.css -o wwwroot/css/styles.css --watch",
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can now run the build tailwind script and get all the TailwindCss styles into the &lt;code&gt;styles.css&lt;/code&gt; file.  This will literally put all the styles from TailwindCss in. So it will make a HUGE CSS file. This is fine for development, but I'll explain further down how to trim this down to only the CSS TailwindCss classes you are using. This makes the file MUCH smaller in production.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run build-tailwind
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also set a watch on tailwind.css so if you make any changes it will auto build&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run watch-tailwind
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As mentioned above, this next part is for when you are ready for production. We want to strip out all unused TailwindCss classes. &lt;/p&gt;

&lt;p&gt;To setup PurgeCss (We installed the npm package at the beginning) you need to add it to your PostCss config file, add another &lt;code&gt;require(‘’)&lt;/code&gt; under your existing ones. You need to set the path for the templates\files you want it to check for classes. Here is what the whole file should look like. REMEMBER, you need to update the path(s) to where it should be looking for all YOUR views/pages/html files etc...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
    plugins: [
        require('tailwindcss'),
        require('autoprefixer'),
        require('@fullhuman/postcss-purgecss')({
            content: [
                './Src/MyProject.Web/Pages/**/*.cshtml',
                //'./Src/MyProject.Web/Pages/**/.html', // This is how you add other files
            ],
            defaultExtractor: content =&amp;gt; content.match(/[A-Za-z0-9-_:/]+/g) || []
        })
    ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you run &lt;code&gt;npm run build-tailwind&lt;/code&gt; now, that should only save the classes you've used. Personally I leave this whole thing commented out until I am finished and ready to push to production.&lt;/p&gt;

&lt;p&gt;Pretty sure there must be a way to dictate a build with or without PurgeCss but as I said at the beginning of this post. I'm not very knowledgeable about all the front end wizardry. &lt;/p&gt;

</description>
      <category>tailwindcss</category>
      <category>aspnetcore</category>
    </item>
  </channel>
</rss>
