Using Tailwind with SASS, Vue-CLI and Webpack


I recently decided to revamp my personal home page as a Vue SPA. I am not much of a designer, so I am going to use a free design from HTML5 Up! as a starting point and riff from there. Most of those templates make heavy use of Sass - of which I am a big fan. Theoretically you can use PostCSS plugins to replicate everything that SASS does, but I am not ready to make that leap yet. Perhaps someday. So, my plan is to use the Vue CLI webpack starter template along with an HTML5 Up design as the foundation for my new site.

Just to mix things up, I thought I would also try taking Tailwind CSS for a spin as well. I may have to re-work quite a bit of the design template to integrate it with the Tailwind framework, but if it works it will be worth the effort.

Here are the steps I took to get my dev setup up and running:

Start by installing the Vue CLI tool and then using it to generate a new site:

$ vue init webpack homepage

I used all the default answers to the prompts, and I had Vue CLI run npm install for me. Next, after moving my terminal session into the new project directory, I installed Sass and Tailwind CSS:

$ npm install node-sass sass-loader tailwindcss --save-dev

and then I initialized my new tailwind.js configuration file:

$ ./node_modules/.bin/tailwind init tailwind.js

We also need to update the .postcssrc.js file to have Post CSS trigger the tailwind compliation step:

1module.exports = {
2 plugins: [require("tailwindcss")("tailwind.js"), require("autoprefixer")()]

Now everything has been installed, we just need to get it all working together. Start by creating an app.scss file in a new /src/assets/scss/ directory, and then pasting in the tailwind base directives. In the example below I have removed the comments to save space.

1@tailwind preflight;
3/* Custom Sass goes here... */
5@tailwind utilities;

Next, locate the "style" section in the App.vue file and replace it with this:

1<style lang="sass">
2 @import "./assets/scss/app.scss"

Note that when using Sass with the Vue Style loader (which allows for using Sass in Vue Component files) you have to be specific here about which type of sass files you will be using. If you specify lang="scss" the loader will assume you are using the "indentation" style of sass files, whereas lang="sass" will indicate the use of bracket style sass files.

At this point, you should be able to run npm run dev to compile the assets and launch a dev server.

To verify that everything is working, try updating your app.scss file to look like this:

1@tailwind preflight;
3#app {
4 @apply .text-center .text-grey-darker .mt-8;
5 font-family: 'Avenir', Helvetica, Arial, sans-serif;
6 -webkit-font-smoothing: antialiased;
7 -moz-osx-font-smoothing: grayscale;
10@tailwind utilities;

If everything is working correctly, wepback will automatically recompile that file and refresh the dev server content, and you should see that we are now successfully using Tailwind CSS directives to manipulate the content on the home page.

About the Author

Ryan C. Durham is a software developer who lives in southern Washington with his wife and daughter. His areas of interest include PHP, Laravel, Rust and PostgreSQL, as well as organizational efficiency and communications strategies.

You can find him on GitHub and LinkedIn.