GGio
GGio

Reputation: 7643

PurgeCSS removes third party library css - Laravel Mix / TailwindCSS

I installed TailwindCSS & PurgeCSS in an existing application that uses custom CSS as well as some third party libraries. I configured PurgeCSS to only purge a single tailwind file but it for some reason removes css selectors from third party libraries, maybe my config is wrong:

webpack.mix.js

const mix         = require("laravel-mix");
const purgeCss    = require("@fullhuman/postcss-purgecss")
const tailwindCss = require("tailwindcss")

mix.webpackConfig(require("./webpack.parts"));

// Don't want to purge anything under app.scss, it contains custom css &
// current app related stuff
mix.sass("resources/sass/app.scss", "public/css").version();

// I simply want to add tailwind so I can use it in addition to whatever
// css the app currently uses
mix.postCss("resources/sass/tailwind.pcss", "public/css", [
    tailwindCss,
    ...process.env.NODE_ENV === "production" ? [purgeCss(
        {
            content: [
                "./resources/**/*.js",
                "./resources/**/*.blade.php",
            ],

            css: [
                "./public/css/tailwind.css"
            ]
        }
    )] : []
])

Tried changing css path from ./public/css/tailwind.css to ./resources/sass/tailwind.pcss but did not help

Edit: Seems like I had to move out some of the dependency imports from the app.scss to get it to work, though I am not sure why:

Added this snippet right before mix.sass(app.scss)

mix.styles(
    [
        "./node_modules/datatables.net-bs4/css/dataTables.bootstrap4.css",
        "./node_modules/bootstrap-datepicker/dist/css/bootstrap-datepicker.standalone.css",
        "./node_modules/animate.css/animate.css",
        "./node_modules/react-datepicker/dist/react-datepicker.css",
    ],
    "public/css/vendors.css"
).version()

Upvotes: 1

Views: 2241

Answers (1)

Andy Song
Andy Song

Reputation: 4684

I can not reproduce your bug, this code works perfectly for me.

const mix = require('laravel-mix');

const purgecss = require('@fullhuman/postcss-purgecss')({
    content: [
      'resources/**/*.blade.php',
    ],
    defaultExtractor: content => content.match(/[\w-/:]+(?<!:)/g) || []
  })

mix.sass('resources/sass/app.scss', 'public/css').version();


mix.postCss('resources/sass/tailwind.pcss', 'public/css', [
    require('tailwindcss'),
    ...process.env.NODE_ENV === 'production'
    ? [purgecss]
    : []
  ])

Upvotes: 2

Related Questions