Skip to content

Instantly share code, notes, and snippets.

@xpharsh
Last active August 3, 2020 06:18
Show Gist options
  • Save xpharsh/10db7d71b045796224ec3b48e0a6a671 to your computer and use it in GitHub Desktop.
Save xpharsh/10db7d71b045796224ec3b48e0a6a671 to your computer and use it in GitHub Desktop.
Install Tailwindcss in Laravel with Easy steps.

Setting up Tailwind and PostCSS - cssnano & Purgecss

  1. Install & Setting up - Tailwind and PostCSS
  2. Install - cssnano for CSS compression
  3. Install - purgecss to Optimizing css for Production

The framework source code can be found here: cakephp/cakephp.

Installation

  1. php artisan preset none
  2. npm install
  3. npm install tailwindcss postcss-cli autoprefixer

Then, generate a tailwind.config.js file:

  1. npx tailwind init

Next, create a postcss.config.js file:

module.exports = {
    plugins: [
        require('tailwindcss'),
        require('autoprefixer'),
    ]
}

Now create a CSS file { public/build/style.css } and add the special @tailwind directives:

@tailwind base;
@tailwind components;
@tailwind utilities;

Add a simple build script to your package.json file to compile your CSS:

{
    // ...
    "scripts": {
        "build": "postcss public/build/style.css -o public/css/myapp.min.css",
        "watch-css": "postcss public/build/style.css -o public/css/myapp.min.css --watch",
        "build-production": "cross-env NODE_ENV=build-production postcss public/build/style.css -o public/css/myapp.min.css"
    },
    // ...
}

Run your build script to generate your CSS:

npm run build

Create a simple HTML file that includes your compiled CSS:

<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="/css/myapp.min.css">
</head>

<body>
    <h1 class="text-4xl font-bold text-center text-blue-500">Hello world!</h1>
</body>

</html>

Installation of cssnano

npm install cssnano

Once you have done this, you will need to configure cssnano by creating a postcss.config.js file in the root of your project

module.exports = {
    plugins: [
        //.....

        require('cssnano')({
            preset: 'default',
        }),
        require('autoprefixer'),
    ],
};

Installation of purgecss

npm install @fullhuman/postcss-purgecss

Then we add this to our PostCSS configuration file postcss.config.js:

module.exports = {
    plugins: [
        //.....

        process.env.NODE_ENV === 'build-production' && require('@fullhuman/postcss-purgecss')({
            content: ['./resources/views/**/*.php'],
            defaultExtractor: content => content.match(/[A-Za-z0-9-_:/]+/g) || []
        }),
        require('autoprefixer'),
    ]
}

content: ['./layouts/**/*.html'] => Path to Root Template or Html file folder

For Non Laravel Template Development Please install cross-env (Optional)

npm install cross-env --save-dev

To Compile Optimizing CSS System Please Add in package.json file (Added above Line No 46)

"scripts": {
    "build-production": "cross-env NODE_ENV=build-production postcss public/build/style.css -o public/css/myapp.min.css"
},

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment