简体   繁体   English

使用PurgeCSS,TailwindCSS和Webpack

[英]Using PurgeCSS, TailwindCSS, and Webpack

I am trying to set up webpack for a static site built with TailwindCSS. 我正在尝试为使用TailwindCSS构建的静态站点设置webpack。 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. 我正在尝试使用PurgeCSS来制作我的css文件,并清除所有未使用的东西,但我认为它没有用。 It will compile with no errors but the css file is 16kb and Google lighthouse audit says there is unused css. 它将编译没有错误,但css文件为16kb,并且Google灯塔审核说有未使用的css。

webpack.config.js 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 的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!! 任何帮助将不胜感激!

Are you trying to compile your code with npm build or yarn build ? 您是否要使用npm buildyarn 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. 如果转到“使用Tailwind处理CSS”下的TailwindCSS文档,则存在“使用PostCSS使用Tailwind”部分,并且链接有模板存储库,您可以在其中看到如何完成此操作。 First thing that I see is that your entry point is your index.js and in the template repo its ./src/styles.css . 我首先看到的是您的入口点是index.js,在模板./src/styles.css它是./src/styles.css

如果您编译的CSS仅为16Kb,则PurgeCSS肯定可以正常工作,如果没有它,Tailwind将输出〜300Kb。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM