DEV Community

Sucipto
Sucipto

Posted on • Edited on

5 1

My Vue + TailwindCSS Configuration

Tailwind is "A utility-first CSS framework for rapidly building custom user interfaces", and it's not UI kit. So you need to build your own ui based on Tailwind css class.

For example, in bootstrap if we need to style button,

<button class="btn btn-primary">
    Button
</button>
Enter fullscreen mode Exit fullscreen mode

In Tailwind,

<button class="bg-blue hover:bg-blue-dark text-white font-bold py-2 px-4 rounded">
    Button
</button>
Enter fullscreen mode Exit fullscreen mode

Hmm, it's looks bootstrap win in this case. But wait, if you need customize button theme tailwind will win. Or you can see the button example in tailwind documentation site to see more case.

Vue + Tailwind + PurgeCSS

  1. npm install tailwindcss @fullhuman/postcss-purgecss --save-dev
  2. npx tailwind init tailwind.js
  3. mkdir src/assets/css
  4. touch src/assets/css/tailwind.css fill with
@tailwind preflight;
@tailwind components;
@tailwind utilities;
Enter fullscreen mode Exit fullscreen mode
  1. Edit src/main.js and import your tailwindcss
// Tailwind CSS
import '@/assets/css/tailwind.css'
Enter fullscreen mode Exit fullscreen mode

By default, tailwind file size is bigger than bootstrap, you can see the explanation here. So we need PurgeCSS to remove unused css class in our production compiled css.

  1. Edit postcss.config.js
const tailwindcss = require('tailwindcss')

const autoprefixer = require('autoprefixer')

const purgecss = require('@fullhuman/postcss-purgecss')

class TailwindExtractor {
  static extract (content) {
    return content.match(/[A-Za-z0-9-_:\/]+/g) || []
  }
}

module.exports = {
  plugins: [
    tailwindcss('./tailwind.js'),

    autoprefixer({
      add: true,
      grid: true
    }),

    purgecss({
      content: [
        './src/**/*.html',
        './src/**/*.vue',
        './src/**/*.js',
        './public/**/*.html'
      ],
      extractors: [
        {
          extractor: TailwindExtractor,
          extensions: ['html', 'vue', 'js']
        }
      ]
    })
  ]
}
Enter fullscreen mode Exit fullscreen mode

Originaly posted on: My Blog Post

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay