Skip to content

Instantly share code, notes, and snippets.

@andrewdelprete
Last active April 12, 2023 01:55
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save andrewdelprete/d2f44d0c7f120aae1b8bd87cbf0e3bc8 to your computer and use it in GitHub Desktop.
Save andrewdelprete/d2f44d0c7f120aae1b8bd87cbf0e3bc8 to your computer and use it in GitHub Desktop.
Webpack: Tailwind CSS + PurgeCSS Example
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const path = require("path");
const glob = require("glob-all");
const PurgecssPlugin = require("purgecss-webpack-plugin");
/**
* Custom PurgeCSS Extractor
* https://github.com/FullHuman/purgecss
* https://github.com/FullHuman/purgecss-webpack-plugin
*/
class TailwindExtractor {
static extract(content) {
return content.match(/[A-z0-9-:\/]+/g);
}
}
module.exports = {
entry: "./index.js",
output: {
path: path.resolve(__dirname, "dist"),
filename: "styles.css"
},
module: {
rules: [
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: [{ loader: "css-loader", options: { importLoaders: 1 } }, "postcss-loader"]
})
}
]
},
plugins: [
new ExtractTextPlugin("styles.css"),
new PurgecssPlugin({
paths: glob.sync([
path.join(__dirname, "resources/views/**/*.blade.php"),
path.join(__dirname, "resources/assets/js/**/*.vue")
]),
extractors: [
{
extractor: TailwindExtractor,
extensions: ["html", "js", "php", "vue"]
}
]
})
]
};
@pxwee5
Copy link

pxwee5 commented Nov 28, 2017

Oh, how did I miss that bit? Working example.

  plugins: [
    new ExtractTextPlugin("styles.css"),
    new PurgecssPlugin({
      whitelist: ['body', '.whitelisted-class'],
      paths: glob.sync([
        path.join(__dirname, "resources/views/**/*.blade.php"),
        path.join(__dirname, "resources/assets/js/**/*.vue")
      ]),
      extractors: [
        {
          extractor: TailwindExtractor,
          extensions: ["html", "js", "php", "vue"]
        }
      ]
    })
  ]

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