Configuring Preact CLI with Tailwind CSS

Posted on November 23, 2019https://blog.agney.dev/preact-cli-with-typescript/

Tailwind CSS is a utility first CSS framework for building custom web designs.

The utility consists of a lot of CSS classes and is usually configured via PostCSS to include these class names and styles into the project.

Here, we will look at how to go about this configuration on a project generated with Preact CLI.

Step 1

Setup Preact CLI project

npx preact-cli create default my-project

Step 2

Installing Tailwind CSS.

npm i tailwindcss
# OR
yarn add tailwindcss

Step 3

Configuring PostCSS loader to use Tailwind.

PreactCLI default setup already uses PostCSS and to keep using the same loaders we have to modify the existing configuration. Fortunately for us, this is pretty easy with Preact CLI.

Create a preact.config.js at the root of your application.

Preact CLI exposes a bunch of helper functions to help us manipulate the configuration and here we will use getLoadersByName to access all instances of PostCSS loader in the configuration.

1module.exports = (config, env, helpers) => {
2 const postCssLoaders = helpers.getLoadersByName(config, 'postcss-loader');
3 postCssLoaders.forEach(({ loader }) => {
4 const plugins = loader.options.plugins;
5
6 // Add tailwind css at the top.
7 plugins.unshift(require('tailwindcss'));
8 });
9 return config;
10};

Now that Tailwind is added to the PostCSS configuration, you can follow the Step 2 and Step 3 on Getting Started page on Tailwind site to start using the classes.

Step 4

Tailwind by adding all utilities adds too much of size onto your project, especially when you might not be using half of it. This is where the magic with PurgeCSS comes into picture.

Adding purge css as a postcss plugin helps us get rid of styles that we are not using on our project, essentially purging them.

npm i @fullhuman/postcss-purgecss --save-dev
# OR
yarn add @fullhuman/postcss-purgecss --dev

To add to your preact.config.js:

1module.exports = (config, env, helpers, params = defaultParams) => {
2 const purgecss = require('@fullhuman/postcss-purgecss')({
3 // Specify the paths to all of the template files in your project
4 content: ['./src/**/*.js'],
5
6 // Include any special characters you're using in this regular expression
7 defaultExtractor: content => content.match(params.regex) || [],
8 });
9
10 const postCssLoaders = helpers.getLoadersByName(config, 'postcss-loader');
11 postCssLoaders.forEach(({ loader }) => {
12 const plugins = loader.options.plugins;
13
14 // Add tailwind css at the top.
15 plugins.unshift(require('tailwindcss'));
16
17 // Add PurgeCSS only in production.
18 if (env.production) {
19 plugins.push(purgecss);
20 }
21 });
22 return config;
23};

PurgeCSS plugin code and explanation is provided on the docs for Tailwind.

And we are done. 🥳

Wait.. What if you can skip all the steps and use a plugin instead?

npm i preact-cli-tailwind --save-dev
# OR
yarn add preact-cli-tailwind --dev

In your preact.config.js:

const tailwind = require('preact-cli-tailwind');
module.exports = (config, env, helpers) => {
config = tailwind(config, env, helpers);
return config;
};

You can find the docs and plugin on Github .

WebMentions

0 Likes
0 Reposts
Buy me a coffeeBuy me a coffee