gpt4 book ai didi

javascript - 使用 PurgeCSS、TailwindCSS 和 Webpack

转载 作者:行者123 更新时间:2023-11-28 01:19:19 31 4
gpt4 key购买 nike

我正在尝试为使用 TailwindCSS 构建的静态站点设置 webpack。我正在尝试使用 PurgeCSS 制作我的 css 文件并删除任何未使用的内容,但我认为它不起作用。它将编译没有错误,但 css 文件是 16kb,Google lighthouse 审计说有未使用的 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"]
}
]
})
]
};

包.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"
}
}

任何帮助将不胜感激!!

最佳答案

您是否正在尝试使用 npm buildyarn build 编译您的代码?

如果您转到“使用 Tailwind 处理您的 CSS”下的 TailwindCSS 文档,那里有“将 Tailwind 与 PostCSS 结合使用”部分,并且有一个链接的模板存储库,您可以在其中查看如何完成。我看到的第一件事是你的入口点是你的 index.js 并且在模板 repo 中它的 ./src/styles.css.

关于javascript - 使用 PurgeCSS、TailwindCSS 和 Webpack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51603507/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com