<commit_before>import * as glob from "glob";
import PluginError from "plugin-error";
import { PurgeCSS } from "purgecss";
import * as internal from "stream";
import through from "through2";
import VinylFile from "vinyl";
import applySourceMap from "vinyl-sourcemaps-apply";
import { UserDefinedOptions } from "./types";

export { UserDefinedOptions };

const PLUGIN_NAME = "gulp-purgecss";

function getFiles(contentArray: string[]): string[] {
  return contentArray.reduce((acc: string[], content) => {
    return [...acc, ...glob.sync(content)];
  }, []);
}

/**
 *
 * @param options - options
 * @returns
 *
 * @public
 */
function gulpPurgeCSS(options: UserDefinedOptions): internal.Transform {
  return through.obj(async function (file: VinylFile, _encoding, callback) {
    // empty
    if (file.isNull()) return callback(null, file);
    // buffer
    if (file.isBuffer()) {
      try {
        const optionsGulp = {
          ...options,
          content: getFiles(options.content),
          css: [
            {
              raw: file.contents.toString(),
            },
          ],
          stdin: true,
          sourceMap: !!file.sourceMap,
        };
        const purgedCSSResults = await new PurgeCSS().purge(optionsGulp);
        const purge = purgedCSSResults[0];
        const result =
          optionsGulp.rejected && purge.rejected
            ? purge.rejected.join(" {}\n") + " {}"
            : purge.css;
        file.contents = Buffer.from(result, "utf-8");

        // apply source map to the chain
        if (file.sourceMap) {
          applySourceMap(file, purge.sourceMap);
        }
        callback(null, file);
      } catch (e: unknown) {
        if (e instanceof Error) {
          this.emit("error", new PluginError(PLUGIN_NAME, e.message));
        }
      }
    }
    // stream<commit_msg>fix(gulp-purgecss): support skippedContentGlobs option #853<commit_after>import * as glob from "glob";
import PluginError from "plugin-error";
import { PurgeCSS } from "purgecss";
import * as internal from "stream";
import through from "through2";
import VinylFile from "vinyl";
import applySourceMap from "vinyl-sourcemaps-apply";
import { UserDefinedOptions } from "./types";

export { UserDefinedOptions };

const PLUGIN_NAME = "gulp-purgecss";

function getFiles(contentArray: string[], ignore?: string[]): string[] {
  return contentArray.reduce((acc: string[], content) => {
    return [...acc, ...glob.sync(content, { ignore })];
  }, []);
}

/**
 *
 * @param options - options
 * @returns
 *
 * @public
 */
function gulpPurgeCSS(options: UserDefinedOptions): internal.Transform {
  return through.obj(async function (file: VinylFile, _encoding, callback) {
    // empty
    if (file.isNull()) return callback(null, file);
    // buffer
    if (file.isBuffer()) {
      try {
        const optionsGulp = {
          ...options,
          content: getFiles(options.content, options.skippedContentGlobs),
          css: [
            {
              raw: file.contents.toString(),
            },
          ],
          stdin: true,
          sourceMap: !!file.sourceMap,
        };
        const purgedCSSResults = await new PurgeCSS().purge(optionsGulp);
        const purge = purgedCSSResults[0];
        const result =
          optionsGulp.rejected && purge.rejected
            ? purge.rejected.join(" {}\n") + " {}"
            : purge.css;
        file.contents = Buffer.from(result, "utf-8");

        // apply source map to the chain
        if (file.sourceMap) {
          applySourceMap(file, purge.sourceMap);
        }
        callback(null, file);
      } catch (e: unknown) {
        if (e instanceof Error) {
          this.emit("error", new PluginError(PLUGIN_NAME, e.message));
        }
      }
    }
    // stream
{'fraction_non_alphanumeric': 0.13713527851458887, 'fraction_numerical': 0.0023872679045092837, 'mean_word_length': 2.4126696832579184, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5813230', 'n_tokens_mistral': 1119, 'n_tokens_neox': 1097, 'n_words': 267}