javascript Webpack:Tailwind CSS + PurgeCSS示例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript Webpack:Tailwind CSS + PurgeCSS示例相关的知识,希望对你有一定的参考价值。

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"]
        }
      ]
    })
  ]
};

以上是关于javascript Webpack:Tailwind CSS + PurgeCSS示例的主要内容,如果未能解决你的问题,请参考以下文章

javascript 用于Javascript和CSS文件的Webpack

如何使用动态目录中的 webpack 要求 JavaScript

JavaScript Webpack安装及文件打包

javascript 在Pluralsight上开发Webpack配置“构建JavaScript开发环境”

Webpack 编译的 CSS 文件包含 Javascript 变量和函数

公开通过 webpack 捆绑的 javascript 全局变量