<?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: elfatouaki khalid</title>
    <description>The latest articles on DEV Community by elfatouaki khalid (@elfakhalid).</description>
    <link>https://dev.to/elfakhalid</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%2F470461%2Ff09c52e5-6c92-49d6-8dda-caf5bee6dead.jpg</url>
      <title>DEV Community: elfatouaki khalid</title>
      <link>https://dev.to/elfakhalid</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/elfakhalid"/>
    <language>en</language>
    <item>
      <title>Your first tailwindcss setup with webpack</title>
      <dc:creator>elfatouaki khalid</dc:creator>
      <pubDate>Wed, 07 Jul 2021 13:57:17 +0000</pubDate>
      <link>https://dev.to/elfakhalid/your-first-tailwindcss-setup-with-webpack-1gfm</link>
      <guid>https://dev.to/elfakhalid/your-first-tailwindcss-setup-with-webpack-1gfm</guid>
      <description>&lt;p&gt;While ago in my first internship i was integrated into a project in which i have to use tailwindcss, i used ended loving it, all i did was just write bunch of class to make sure everything looked nice, the one thing i didn't do is to setup tailwindcss and the dependencies it need to work properly, and that's what i'm going to explain in this article.&lt;/p&gt;

&lt;h1&gt;
  
  
  Easy and quick way to use tailwind:
&lt;/h1&gt;

&lt;p&gt;the easiest and the quickest way to get going with tailwindcss is to use a CDN.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However by using a CDN you will be missing on tailwindcss great features,you wont be able to customize it, you won't be able to use directives such as &lt;code&gt;@apply&lt;/code&gt; and &lt;code&gt;@variantes&lt;/code&gt; etc, you won't be able to use plugins and more...&lt;/p&gt;

&lt;p&gt;in order to leverage all these great features you have to incorporate tailwind into you build process.&lt;/p&gt;

&lt;h1&gt;
  
  
  Install tailwindcss with webpack via npm
&lt;/h1&gt;

&lt;p&gt;In order to use npm you gotta have node and npm installed in your machine.&lt;br&gt;
First thing we need is to create a package.json file to do that open terminal and type &lt;code&gt;npm init -y&lt;/code&gt; this command will create a package.json file for you then in your terminal again type these commands to install the packages needed for tailwindcss to work&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 webpack webpack-cli postcss postcss-loader css-loader tailwindcss mini-css-extract-plugin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;if you took a look at project file you will notice a bunch of files added and a folder with node_modules name on it, in that folder there is all the code for the packages you installed including tailwind&lt;br&gt;
to set everythings up we need to create a file with the webpack.config.js and in that file we write the following :&lt;/p&gt;

&lt;p&gt;first we gonna require path we write &lt;code&gt;const path=require('path')&lt;/code&gt;&lt;br&gt;
path is node module provides utilities for working with file and directory paths&lt;br&gt;
second we gonna require &lt;code&gt;mini-css-extract-plugin&lt;/code&gt; which is a plugin we installed earlier this plugin helps us to output a standalone css file,&lt;br&gt;
so for our webpach.config.js will look like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const path=require("path");
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;after that write 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={
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;set the mode to developpement&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mode:"development",
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;then you need to create an entry point file with the .js extension the entry point is where webpack looks to start building the output files, i will call my entry point &lt;code&gt;main.js&lt;/code&gt; and i will set it in the root directory &lt;br&gt;
let's add the entry point in our webpack config file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;entry: "./main.js",
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;now create a css file i will name main styles.css and put these tailwind directives in it&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;now go to main.js file and import your css file &lt;code&gt;import "styles.css"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;on to our webpack config again&lt;br&gt;
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;output:{
        filename:"main.js",
        path: path.resolve(__dirname,"./build")
    },
    plugins: [new MiniCssExtractPlugin({
        filename:"styles.css",
    })],
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the output object will generate javascripts and css files for us it will translate tailwindcss into regulare css for you and javascript files, also it will bundle all the files for into one single file you will be able to link to in your html &lt;br&gt;
the plugins options will use &lt;code&gt;mini-css-extract-plugin&lt;/code&gt; to help us output a css file &lt;br&gt;
last thing we need to set is rules for our css for that you need to write 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:{
        rules:[
            {
                test:/\.css$/,
                use:[
                MiniCssExtractPlugin.loader,
                "css-loader",
                "postcss-loader"
            ]
            }
        ]
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;what will this do tell webpack how to process files with .css extension it specifies with loader to be used since tailwind is postcss blugin it will start by poscss then translating it to css then it will use &lt;code&gt;MiniCssExtractPlugin.loader&lt;/code&gt; to put the css in an external css file.&lt;br&gt;
this is how your webpack.config.js will look &lt;br&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%2Fwkcw00807e549a5q3g8h.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%2Fwkcw00807e549a5q3g8h.png" alt="webpack config file"&gt;&lt;/a&gt;&lt;br&gt;
Ok this is all you need for your webpack config now lets build and see what happens, to do go to package.json and add the build script, under the script object in package.json 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;"scripts": {
    "build": "webpack --config webpack.config.js"
  },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;your package.config.json file will look somthing like this &lt;br&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%2F0wdvzk3537wmu5bh5mb0.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%2F0wdvzk3537wmu5bh5mb0.png" alt="package.json config file"&gt;&lt;/a&gt;&lt;br&gt;
now open terminal and run &lt;code&gt;npm run build&lt;/code&gt; &lt;br&gt;
now this bundle all files and generate a build folder in witch you'll find two files a javascript one and css one those are the files we told webpack to generate here&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;output:{
        filename:"main.js",
        path: path.resolve(__dirname,"./build")
    },
    plugins: [new MiniCssExtractPlugin({
        filename:"styles.css",
    })],
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;no go on and include the generated css file in your html file &lt;br&gt;
through the link tag like how you do to any css file, add some tailwind classes too to test if its working and open it in the browser. you'll notice no css is being applied that's because we still need one last thing which a postcss.config.js file &lt;br&gt;
and add tailwindcss as a plugin your postcss config file will look like this &lt;br&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%2F4nz3b0y9k3tuwoxx85hv.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%2F4nz3b0y9k3tuwoxx85hv.png" alt="postcss config file"&gt;&lt;/a&gt;&lt;br&gt;
now run &lt;code&gt;npm run build&lt;/code&gt; again and congrats tailwind is working and you can start writing tailwind classes and designing your &lt;br&gt;
next i advise to read more about webpack npm and postcss i'll be writing more tutorials about these topics in the future.&lt;br&gt;
i hope you found this tutorial helpful see you in the next one.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
