0

I am trying to set up webpack for a static site built with TailwindCSS. I am trying to use PurgeCSS to make my css file and get rid of anything unused and I do not believe it is working. It will compile with no errors but the css file is 16kb and Google lighthouse audit says there is unused css.

webpack.config.js

    const path = require("path");
    const glob = require("glob-all");
    const ExtractTextPlugin = require("extract-text-webpack-plugin");
    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, "src/styles.css"),
            path.join(__dirname, "index.html")
          ]),
          extractors: [
            {
              extractor: TailwindExtractor,
              extensions: ["html", "js"]
            }
          ]
        })
      ]
    };

package.json

{
    "private": true,
    "scripts": {
        "dev": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=webpack.config.js",
        "watch": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=webpack.config.js",
        "prod": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=webpack.config.js"
    },
    "devDependencies": {
        "ajv": "^6.5.2",
        "cross-env": "^5.1",
        "css-loader": "^0.28.7",
        "extract-text-webpack-plugin": "^3.0.2",
        "postcss": "^6.0.14",
        "postcss-loader": "^2.0.8",
        "purgecss-webpack-plugin": "^1.2.0",
        "style-loader": "^0.19.0",
        "tailwindcss": "^0.6.4",
        "webpack": "^3.8.1"
    },
    "dependencies": {
        "glob-all": "^3.1.0"
    }
}

Any help would be very appreciated!!

1
  • 1
    Are you're using Google Chrome? When DevTools are open, hit the ESC-Key and select "Coverage" in the dropdown menu. There you can inspect, which CSS is used/unused. Commented Oct 31, 2018 at 8:33

2 Answers 2

1

Are you trying to compile your code with npm build or yarn build ?

If you go to the TailwindCSS documentation unter "Process your CSS with Tailwind" there is section "Using Tailwind with PostCSS" and there is a template repository linked, where you can see how it can be done. First thing that I see is that your entry point is your index.js and in the template repo its ./src/styles.css.

Sign up to request clarification or add additional context in comments.

Comments

1

If your compiled CSS is just 16Kb then PurgeCSS is definitely working, without it Tailwind would be outputting something ~300Kb.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.