Tailwind css generates very big files by default. With simple changes in tailwind.config.js we can make tailwind remove all the unused css classes and utilities.
            
                
                
                
            
            
On Tailwind css configuration
Tailwindcss has a lot of options to be configured. Enough to be confusing. If configured improperly, it can generate very big sizes — almost 4MB — so the wrong config can be pretty bad.
Let's go through a simple setup to avoid this problem and make sure the file sizes are not excessively big.
Install Nodejs >= v11 (if not already in the system)
# we are using Ubuntu
sudo apt-get update
# add nodesource, we need at least nodejs v11
sudo apt-get install curl
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt-get install nodejs
Check the version
node -v
Welcome to Node.js v14.17.1.
Installing Tailwind and Postcss
Tailwind uses postcss, so we will need to install it, in addition to tailwindcss and any plugins we are using.
For now feel free to not install the plugins until you need them. If you use a component that requires a plugin, the compilation step will break and give you a message of which plugin you are missing.
mkdir myproject
cd myproject
npm init
# tailwind and postcss
npm install -D tailwindcss@latest postcss@latest autoprefixer@latest postcss-cli
# also install any tailwind plugins
npm install -D @tailwindcss/typography
npm install -D @tailwindcss/forms
The package.json created by npm init now has all the dependencies we installed listed. It will look similar to the one below.
# package.json
{
  "name": "myproject",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "autoprefixer": "^10.2.6",
    "postcss": "^8.3.5",
    "postcss-cli": "^8.3.1",
    "tailwindcss": "^2.1.4"
  }
}
Tailwind.css
We need to create one more configuration file, specifying which parts of tailwind we want to use.
For this, we create a tailwind.css with the following contents.
@tailwind base;
@tailwind components;
@tailwind utilities;
The directives in this file will, on compilation, be replaced but the contents of the tailwindcss library.
The default config
We are ready to create a default tailwind.config.js file
npx tailwindcss init
We get a file with these contents
// tailwind.config.js
module.exports = {
  purge: [],
  darkMode: false, // or 'media' or 'class'
  theme: {
    extend: {},
  },
  variants: {
    extend: {},
  },
  plugins: [],
}
The default build
    To build a version from tailwind css for our project we need to pass the path to tailwind.css, and an output file — here named tailwind-styles.css.
    It is this last file that we will include in our website.
NODE_ENV=production npx tailwindcss-cli@latest build tailwind.css -o tailwind-styles.css
We can see here the produced size (3.81MB!). Including a file this big could make our website very slow (and, in mobile connections, might quickly deplete our uses mobile data plan)
npx: installed 123 in 5.888s
   tailwindcss 2.1.4
   🚀 Building: tailwind.css
warn - Tailwind is not purging unused styles because no template paths have been provided.
warn - If you have manually configured PurgeCSS outside of Tailwind or are deliberately not removing unused styles, set `purge: false` in your Tailwind config file to silence this warning.
warn - https://tailwindcss.com/docs/controlling-file-size/#removing-unused-css
   ✅ Finished in 4.27 s
   📦 Size: 3.81MB
   💾 Saved to tailwind-styles.css
The solution: purge to reduce build size
Let's go back to the tailwind.config.js. We will use the purge option to add our html, js files. We want to add any files where we are using tailwind css classes.
Tailwind example — in the documentation — asumes a src folder, but you should customize. If you only use html files on the root folder, for instance, './*.html' might be a valid option.
// tailwind.config.js
module.exports = {
  // Use one or more paths that works for your project
  // example: '*.html'
  purge: [
    './src/**/*.html',
    './src/**/*.vue',
    './src/**/*.jsx',
  ],
  theme: {},
  variants: {},
  plugins: [],
}
Let's test it
We create a test html file to test it
<!DOCTYPE html>
<head>
    <link href="tailwind-styles.css" rel="stylesheet" />
</head>
<body>
<!--
We use a tailwind class.
Only this will be included in the output.
The rest will be purged.
 -->
<div class="text-red-500">Hello Tailwind</div>
</body>
</html>
With our config
// tailwind.config.js
module.exports = {
  purge: ['*.html'],
  darkMode: false, // or 'media' or 'class'
  theme: {
    extend: {},
  },
  variants: {
    extend: {},
  },
  plugins: [],
}
And we compile it with:
NODE_ENV=production npx tailwindcss-cli@latest build tailwind.css -o tailwind-styles.css
The result is a much more (10.86KB) compressed file, only including the utility classes we used.
npx: installed 123 in 6.252s
   tailwindcss 2.1.4
   🚀 Building: tailwind.css
   ✅ Finished in 3.44 s
   📦 Size: 10.86KB
   💾 Saved to tailwind-styles.css
 
            
                Transcript
                
                    hi
                
                    we're gonna check how to to configure
                
                    tailwind
                
                    css properly to avoid a common problem
                
                    and the problem is that a lot of times
                
                    as we will see
                
                    the build that the tailwind generates by
                
                    default is very big
                
                    so i'm going to show you step by step
                
                    how how to solve that
                
                    first we need to install uh node.js
                
                    and it needs to be a version higher than
                
                    because otherwise um there's some
                
                    problems later with
                
                    with post css so basically to do this
                
                    let's do these steps first we do an
                
                    update to make sure that we have the
                
                    latest
                
                    repositories loaded okay then
                
                    i'm going to use curl and i have it
                
                    installed so i'm just going to install
                
                    curl to make
                
                    http requests
                
                    so that's going to install curl i say
                
                    yes
                
                    okay and it's installing curl and now
                
                    we're going to use curl
                
                    um to download um
                
                    node.js so as you see here uh
                
                    it's coming from this node source so you
                
                    can check in node source they have
                
                    like each version of node.js to download
                
                    and in this case i'm installing the 14th
                
                    which is the uh
                
                    lts the long term support one at the
                
                    time of this video
                
                    and but anything higher than 11 would
                
                    work so i'm just gonna download this as
                
                    you see it is not very
                
                    secure but it's like the standard
                
                    to install this because we're piping
                
                    from here to to bash right so
                
                    just keep this in mind
                
                    then this is installing node.js and it
                
                    also includes npm
                
                    and this kind of other comments that
                
                    before were separate
                
                    so what this is doing is this is having
                
                    uh
                
                    some of these reports right and then now
                
                    we can install
                
                    install node.js and then this node.js
                
                    package has everything as the npm has
                
                    everything npx
                
                    so we just install it we have to confirm
                
                    i think
                
                    no okay so we just installed it uh and
                
                    now uh
                
                    we can check it check it especially the
                
                    version
                
                    and if you already have note installed
                
                    you could just
                
                    go straight here to check the version is
                
                    version 14
                
                    so perfect we needed more than version
                
                    um let's let's go and install uh
                
                    tailwind so let's install tailwind
                
                    so uh first we create a project like
                
                    just a folder
                
                    for our project it's gonna be my project
                
                    we change directory into it okay and now
                
                    here we are
                
                    now we're gonna do an npm init to start
                
                    the package we set
                
                    all of the options by default
                
                    it's okay yes okay and then
                
                    and now we have a package right so if
                
                    you see here
                
                    you can see the contents of the package
                
                    and then here our dependencies will be
                
                    installed in this package so
                
                    this is like the documentation and also
                
                    to to install any dependencies it will
                
                    use this package
                
                    so with this we can use the npm install
                
                    and then this is to save the
                
                    dependencies it's like a short version
                
                    and then here we can use a few different
                
                    ones so basically at the very least we
                
                    need this
                
                    so that's tailwind css latest
                
                    pos css which is what's used to compile
                
                    the
                
                    the library auto prefixer to also deal
                
                    with some browser prefix and the pos css
                
                    command line interface right so we can
                
                    just install this
                
                    it's gonna take a bit uh and um in the
                
                    meantime also
                
                    uh if you are using any plugins like for
                
                    instance the typography plugins
                
                    if using the typography plugins or the
                
                    forms plugins
                
                    you can just go with the same syntax
                
                    and install it so for instance let's say
                
                    it's not necessary like if you don't
                
                    install it and you're using some of
                
                    these plugins it will blow up so you
                
                    will find out
                
                    but just for the completeness let me
                
                    just
                
                    add here a typography
                
                    tailwind css
                
                    it's actually the add sign
                
                    so this is the typography plugin you can
                
                    see this in the tailwind documentation
                
                    you can see all the packages okay so
                
                    this is the typography one
                
                    and then i'm gonna add also the forms
                
                    one
                
                    and these two i use um often okay so now
                
                    we have everything installed
                
                    um then we're gonna move on to the
                
                    tailwind css file
                
                    so basically one of the things that
                
                    tailwind asks us to do is to create a
                
                    file with the
                
                    components that we want now so it's just
                
                    a simple file
                
                    we can let me open
                
                    exactly so we're going to call it
                
                    tailwind css
                
                    [Music]
                
                    and this file has uh i mean you will see
                
                    at least it has three these three lines
                
                    which is like
                
                    this tailwind directive directives
                
                    and it's the parts of terwin that we
                
                    want to include right so this will be
                
                    compiled using post css
                
                    to generate the the library file
                
                    so we will see here so basic space
                
                    component
                
                    components
                
                    and utilities here you could add at the
                
                    end your own
                
                    styles but for now we're going to keep
                
                    it simple so this is what the
                
                    recommendation of serving
                
                    tells us to do so
                
                    basically this file is going to be used
                
                    to compile the library right
                
                    so um we have this file
                
                    and now we need to run another command
                
                    and the command
                
                    is npx
                
                    till
                
                    [Music]
                
                    then this as we can see here created
                
                    this file
                
                    tailwind config dot js right so if we
                
                    open this file
                
                    um
                
                    okay here it is
                
                    and actually as you can see i created
                
                    this in the wrong
                
                    is it inside there when css yeah
                
                    this is some old file i think
                
                    no it's correct um yeah so now we have
                
                    this file
                
                    tailwind right yes
                
                    tailwind config.js so here we have it in
                
                    the right side
                
                    and this basically tells derwent how to
                
                    compile this right
                
                    so we can leave it with the default
                
                    options uh there's a lot of different
                
                    options here we can use
                
                    but uh let's try and see how the default
                
                    build in in tailwind is
                
                    so to do this uh we need to run another
                
                    command
                
                    so the command is the following um
                
                    we're gonna write first to make sure
                
                    that this is like uh optimized we need
                
                    to pass like an environment variable
                
                    in this case it's not m production this
                
                    can have different values but exactly
                
                    ultimately for production right so we're
                
                    going to just try this one
                
                    and then we run this command using npx
                
                    tailwind css
                
                    and actually volunteer says command line
                
                    interface
                
                    latest
                
                    then this is running failure css
                
                    so all of this is like the program
                
                    and then the option is uh build
                
                    so we're building the one css build you
                
                    know basically
                
                    and then we need the previous file which
                
                    i named taylor and css
                
                    so this one this one here on the left
                
                    and we need an output file and this
                
                    output file
                
                    which we indicate with it is o flag uh
                
                    this is the one that we're gonna include
                
                    in our html
                
                    so this is i'm going to name this uh
                
                    taylorville styles
                
                    dot css you can name it however you want
                
                    uh
                
                    but yeah this this seems okay then we
                
                    run this
                
                    and this will compile a version of
                
                    tailwind for us
                
                    to include so let's see what's the
                
                    result of that
                
                    okay
                
                    um
                
                    okay so it seems uh the tailwind css
                
                    file is not in the grand folder right so
                
                    i created it in the
                
                    in the parent folder so just take it
                
                    from there
                
                    and we copy it here
                
                    and now here we can see that the file is
                
                    here it has your
                
                    contents
                
                    yeah so now it should work so let's run
                
                    this command again
                
                    and now we will see the default build of
                
                    tailwind
                
                    with the default options without doing
                
                    anything
                
                    okay okay so let's see how
                
                    how big uh this this file is
                
                    so if we go here to my project this is
                
                    the generated file right
                
                    and we can see here there is four
                
                    megabytes so
                
                    this is massive like if we include this
                
                    in our website like uh
                
                    it will go slow maybe if it's mobile it
                
                    can even like finish like a
                
                    good chunk of uh of the mobile plan of
                
                    some of our users
                
                    so this is good it's not a critic of uh
                
                    tailwind
                
                    there's a reason why this is like this
                
                    but uh just make sure
                
                    that you optimize it and now i'm gonna
                
                    show you how to optimize it before uh
                
                    putting it in production so to
                
                    illustrate this we're going to create a
                
                    a simple a simple html file we're going
                
                    to call it
                
                    index html and here we're going to
                
                    um write like a very simple uh wordpress
                
                    right html
                
                    then in the head we're gonna add a link
                
                    with our file which is this one they win
                
                    styles
                
                    and
                
                    okay so this is including uh the build
                
                    uh
                
                    file and then in the body we're just
                
                    gonna use one of the classes of taiwan
                
                    so in this case i'm just gonna use this
                
                    class that
                
                    makes the text red right right right of
                
                    the type 500
                
                    so this is a simple page that's using
                
                    tailwind which is
                
                    included and setting the text to red
                
                    so how are you gonna work so basically
                
                    this config file
                
                    has this purge option right so here we
                
                    need to pass
                
                    like an expression that will match the
                
                    source files
                
                    and then um the post css
                
                    will look into these files this one or
                
                    like
                
                    any anyone matching any other files
                
                    matching
                
                    and it will see which class we're using
                
                    and it will only include those classes
                
                    that we're using so the end file will be
                
                    like
                
                    way way smaller
                
                    so here basically you can include
                
                    different expressions
                
                    the tailwind documentation shows this as
                
                    an example
                
                    so basically it's saying like
                
                    html.view.jsx if you're using react or
                
                    gsx
                
                    and it's assuming that it's in this
                
                    source folder right
                
                    so this you can customize to your needs
                
                    in my case
                
                    or in our case here this will be enough
                
                    because it's just matching all the html
                
                    files right
                
                    you could even work like this right
                
                    index
                
                    but uh let's do it like this and then
                
                    we'll see the difference of
                
                    how this affects the build so it's going
                
                    to try to match any htmls in the current
                
                    folder and it will match this class
                
                    so the end result should be practically
                
                    this class and maybe a bit more of
                
                    boilerplate
                
                    so let's try that
                
                    this is the command we just run it again
                
                    still is taking the turbulent css and
                
                    outputting
                
                    the the wind styles
                
                    okay there's some application warnings
                
                    depending on the version here you can
                
                    see already some nice emojis with a
                
                    weight and this but
                
                    in this version it's not there it seems
                
                    um but if we come here you can see the
                
                    difference right
                
                    so here is like 11 kilobytes so
                
                    before it was 4 000 right so this could
                
                    make a huge difference for your users
                
                    instead of
                
                    especially they're in a mobile
                
                    connection and it's also just a waste
                
                    you know to include four megabytes of
                
                    files
                
                    so um just be mindful of this
                
                    optimization because it can make a big
                
                    difference and then maybe you can take a
                
                    look also at the generated
                
                    file i'm not sure if
                
                    okay uh they win stats yeah so this
                
                    determinate file
                
                    uh so it's smaller than before but yeah
                
                    basically it's a lot smaller than before
                
                    uh and yeah yes um
                
                    yeah so i hope uh this is useful and you
                
                    can use it in your projects
                
                    and to stop shipping like uh these huge
                
                    huge files