0

我正在使用 purgeCSS 删除未使用的 CSS。我的挑战是我需要动态地做到这一点。根据正在处理的当前 .css 文件,我需要获取其路径和文件名,以便动态插入内容 HTML 路径以运行 Purge。

这是我的代码的样子:

const gulp = require("gulp"),
    appRoot = require("app-root-path"),
    sass = require("gulp-sass"),
    purgecss = require("gulp-purgecss"),
    tap = require("gulp-tap"),
    path = require("path"),
    utilities = require(appRoot + "/Tools/Utilities-Functions/utilities-functions.js");

gulp.task("sass", () => {
    let htmlContentPath = "";

    return (
        gulp
            .src("./Pages/**/*.scss")
            // Compile .scss into .css
            .pipe(sass())
            // Get path for HTML file (dynamic)
            .pipe(
                tap(function (file, t) {
                    let fileName = path.basename(file.path);
                    // This is a simple function that returns the file name without extension (homepage.css >> homepage)
                    fileName = utilities.getFileNameWithoutExtension(fileName);

                    htmlContentPath = "/fullPath/Pages/" + fileName + "/compiled/html/" + fileName + ".html";
                })
            )
            // Remove unused CSS
            .pipe(
                purgecss({
                    content: [htmlContentPath]
                })
            )
            // Set the destination folder (main css)
            .pipe(gulp.dest("./dist/css"))
    );
})

由于某种原因,Purge 的“htmlContentPath”为空。即使我希望“点击”插件总是为它设置一个值。结果,这会在 purgecss 上引发错误:

在此处输入图像描述

如上所述,此错误是由于“htmlContentPath”为空。

我尝试的另一个尝试是在 Tap 插件中执行 Purge,如下所示:

const gulp = require("gulp"),
    appRoot = require("app-root-path"),
    sass = require("gulp-sass"),
    purgecss = require("gulp-purgecss"),
    tap = require("gulp-tap"),
    path = require("path"),
    utilities = require(appRoot + "/Tools/Utilities-Functions/utilities-functions.js");

gulp.task("sass", () => {
    return (
        gulp
            .src("./Pages/**/*.scss")
            // Compile .scss into .css
            .pipe(sass())
            // Get path for HTML file (dynamic)
            .pipe(
                tap(function (file, t) {
                    let fileName = path.basename(file.path);
                    // This is a simple function that returns the file name without extension (homepage.css >> homepage)
                    fileName = utilities.getFileNameWithoutExtension(fileName);

                    let htmlContentPath = "/fullPath/Pages/" + fileName + "/compiled/html/" + fileName + ".html";

                    // Remove unused CSS
                    purgecss({
                        content: [htmlContentPath]
                    })
                })
            )
            // Set the destination folder (main css)
            .pipe(gulp.dest("./dist/css"))
    );
})

这次它没有给出错误,但是 Purge 完全被忽略了......

关于如何解决这个问题的任何解决方案?

4

1 回答 1

0

在尝试了数十种方法之后,这是对我有用的方法,并且认为值得与可能面临类似挑战的其他人分享:

const gulp = require("gulp"),
    appRoot = require("app-root-path"),
    sass = require("gulp-sass"),
    path = require("path"),
    utilities = require(appRoot + "/Tools/Utilities-Functions/utilities-functions.js"),
    fs = require("fs"),
    through = require("through2"),
    uncss = require("uncss");

gulp.task("sass", () => {
    return (
        gulp
            .src("./Pages/**/*.scss")
            // Compile .scss into .css
            .pipe(sass())
            // Remove unused CSS
            .pipe(
                through.obj(function(file, encoding, callback) {
                    try {
                        const cssFileContent = file.contents.toString(); // Get the css file contents
                        let transformedFile = file.clone(), // Clone new  file for manipulation
                            fileName = path.basename(file.path),
                            htmlFilePath;

                        // This is a simple function that returns the file name without extension (homepage.css >> homepage)
                        fileName = utilities.getFileNameWithoutExtension(fileName);

                        // File path for the .html file
                        htmlFilePath = "/fullPath/Pages/" + fileName + "/compiled/html/" + fileName + ".html";

                        // Check if there is any css to be checked and if .html file exists
                        if (cssFileContent.length && fs.existsSync(htmlFilePath)) {
                            // Call uncss to remove unused css
                            uncss([htmlFilePath], { raw: cssFileContent }, function(error, output) {
                                if (error) {
                                    callback(null, transformedFile);
                                }

                                // Set new contents with the "used" css only (uncss' output)
                                transformedFile.contents = Buffer.from(output);

                                callback(null, transformedFile);
                            });
                        } else {
                            callback(null, transformedFile);
                        }
                    } catch (e) {
                        console.log("Gulp error - uncss: " + e.message);
                        callback(null, transformedFile);
                    }
                })
            )
            // Set the destination folder (main css)
            .pipe(gulp.dest("./dist/css"))
    );
});

基本上我使用through构建了一个自定义 gulp 流。这允许您读取有关当前处理的文件的信息,执行您想要的任何逻辑,然后使用新的转换文件调用回调。

更详细地说,我做了什么:

  1. 读取文件信息(文件名及其位置)
  2. 获取我要检查我的 CSS 的 HTML 的位置
  3. 运行uncss(而不是我最初使用的 purgecss),因为在这个工具上我可以发送原始 CSS,这在我的情况下很方便
  4. 从 uncss 的输出中,我用这个输出影响了 CSS 文件的内容
  5. 使用这个新的转换文件调用回调
于 2020-02-10T12:17:05.203 回答