finchy70's avatar

Tailwind styles broken in production

I have a Job Diary app which shows jobs across the days of the week. In development the app works fine. In development

In production all jobs show in first column. In production

The problem bit of code seems to be this.

<ul class="col-start-{{$start}} col-span-{{$end-($start-1)}} bg-white rounded-lg shadow">

It seems after purgecss runs the styles break. If I upload the full 3MB+ css file to the production server all works fine.

My tailwind.config.js looks like this

const defaultTheme = require('tailwindcss/defaultTheme')

module.exports = {
    purge: ['./resources/views/**/*.blade.php'],
    theme: {
        extend: {
            fontFamily: {
                sans: ['Inter var', ...defaultTheme.fontFamily.sans],
            },
        },
    },
  variants: {},
  plugins: [
      require('@tailwindcss/ui'),
  ],
}

My tailwind entries in my package.json look like this

"devDependencies": {
        "axios": "^0.19",
        "browser-sync": "^2.26.12",
        "browser-sync-webpack-plugin": "^2.0.1",
        "cross-env": "^7.0",
        "laravel-mix": "^5.0.1",
        "laravel-mix-purgecss": "^5.0.0",
        "lodash": "^4.17.19",
        "resolve-url-loader": "^3.1.0",
        "sass": "^1.15.2",
        "sass-loader": "^8.0.0",
        "vue-template-compiler": "^2.6.11"
    },
    "dependencies": {
        "@tailwindcss/ui": "^0.5.0",
        "autoprefixer": "^10.2.4",
        "postcss": "^8.2.6",
        "tailwindcss": "^2.0.3"
    }

Any ideas what Im doing wrong?

0 likes
12 replies
finchy70's avatar

Thank you. I now know why it doesn't work. How do I whitelist all grid classes?

Friso S's avatar

Based upon which of purgecss version you got, you can safelist or whitelist something.

https://purgecss.com/safelisting.html#patterns

You could add this in the tailwind config.

I used it like this:

  .purgeCss({
        enabled: false,
        whitelist: ['agile'],
        whitelistPatterns: [/^agile?.*/],
    }).version();

but this was done with sass, and not tailwind.

finchy70's avatar

Tried this

const defaultTheme = require('tailwindcss/defaultTheme')

module.exports = {
    purge: ['./resources/views/**/*.blade.php'],
    safelist: { greedy: ['/col-start-$/', '/col-span-$/']},
    theme: {
        extend: {
            fontFamily: {
                sans: ['Inter var', ...defaultTheme.fontFamily.sans],
            },
        },
    },
  variants: {},
  plugins: [
      require('@tailwindcss/ui'),
  ],
}

doesn't work.

finchy70's avatar

I'm on tailwindcss v 2.0.3

Also tried

const defaultTheme = require('tailwindcss/defaultTheme')

module.exports = {
    purge: {
        content: ['./resources/views/**/*.blade.php'],
        options: {
            safelist: {
                greedy: ['/col-/']
            },
        }
    },
    theme: {
        extend: {
            fontFamily: {
                sans: ['Inter var', ...defaultTheme.fontFamily.sans],
            },
        },
    },
  variants: {},
  plugins: [
      require('@tailwindcss/ui'),
  ],
}

This gave this error.

/var/www/html/node_modules/tailwindcss/node_modules/purgecss/lib/purgecss.js:1
(function (exports, require, module, __filename, __dirname) { "use ..........
finchy70's avatar

Did a dirty workaround by inserting

@if(! true)
                <div class="col-start-1 col-start-2 col-start-3 col-start-4 col-start-5 col-start-6 col-start-7 col-span-1 col-span-2 col-span-3 col-span-4 col-span-5 col-span-6 col-span-7"></div>
            @endif

before

<ul class="col-start-{{$start}} col-span-{{$end-($start-1)}} bg-white rounded-lg shadow">

Would still like to know how to properly use safelist with Laravel.

STEREOH's avatar

Hey ,

you could try with

const defaultTheme = require('tailwindcss/defaultTheme')

module.exports = {
    purge: {
        content: ['./resources/views/**/*.blade.php'],
        options: {
           safelist: {
                  standard: [
						/^col-start-/,
						/^col-span-/
				]
            }
        }
    },
    theme: {
        extend: {
            fontFamily: {
                sans: ['Inter var', ...defaultTheme.fontFamily.sans],
            },
        },
    },
  variants: {},
  plugins: [
      require('@tailwindcss/ui'),
  ],
}
finchy70's avatar

No this didn't work either. Would love to know how to safelist / whitelist for future reference.

finchy70's avatar

A regex pattern that would catch col-start-3 and col-span-2 would be

col-[a-z]*-\d

but inserting this as the pattern also doesn't work.

STEREOH's avatar

I tried on an empty laravel app with these and it works perfectly :

options: {
    safelist: {
        standard: [
            /^col-start-/,
            /^col-span-/
        ]
    }
}

note that it doesn't seem to work when there is a breakpoint such as "sm:col-start-*" I wonder if you have to specify those as well.

STEREOH's avatar
STEREOH
Best Answer
Level 18

After further tinkering with regex, I found a way to get every variants of col-start & col-span

options: {
    safelist: {
        standard: [
                    /^[\w:]*col-start-/,
                    /^[\w:]*col-span-/
        ]
    }
}
1 like

Please or to participate in this conversation.