Skip to content

Commit

Permalink
feat(postcss-purgecss): remove compatibility with postcss 7
Browse files Browse the repository at this point in the history
 #488
fix #540

BREAKING CHANGE: dropping support for postcss 7
  • Loading branch information
Ffloriel committed Jan 10, 2021
1 parent c41ad27 commit 48ce28f
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 129 deletions.
9 changes: 4 additions & 5 deletions packages/postcss-purgecss/__tests__/index.test.ts
@@ -1,6 +1,5 @@
import fs from "fs";
import { UserDefinedOptions } from "../src/types";
import postcss, { PluginCreator } from "postcss";
import postcss from "postcss";

import purgeCSSPlugin from "../src/";

Expand All @@ -16,7 +15,7 @@ describe("Purgecss postcss plugin", () => {
.readFileSync(`${__dirname}/fixtures/expected/${file}.css`)
.toString();
const result = await postcss([
(purgeCSSPlugin as PluginCreator<UserDefinedOptions>)({
purgeCSSPlugin({
content: [`${__dirname}/fixtures/src/${file}/${file}.html`],
fontFace: true,
keyframes: true,
Expand All @@ -43,7 +42,7 @@ describe("Purgecss postcss plugin", () => {
.mockReturnValue([`${__dirname}/fixtures/src/${file}/${file}.html`]);

postcss([
(purgeCSSPlugin as PluginCreator<UserDefinedOptions>)({
purgeCSSPlugin({
contentFunction,
fontFace: true,
keyframes: true,
Expand All @@ -68,7 +67,7 @@ describe("Purgecss postcss plugin", () => {
.readFileSync(`${__dirname}/fixtures/expected/simple.css`)
.toString();
postcss([
(purgeCSSPlugin as PluginCreator<UserDefinedOptions>)({
purgeCSSPlugin({
content: [`${__dirname}/fixtures/src/simple/simple.html`],
rejected: true,
}),
Expand Down
87 changes: 84 additions & 3 deletions packages/postcss-purgecss/src/index.ts
@@ -1,4 +1,85 @@
import postcss from "postcss";
import { createPurgeCSSPlugin } from "./postCSSPurgeCSS";
import { Helpers, PluginCreator, Root } from "postcss";

export default createPurgeCSSPlugin(postcss);
import PurgeCSS, {
defaultOptions,
mergeExtractorSelectors,
standardizeSafelist,
} from "purgecss";

import { RawContent, UserDefinedOptions } from "./types";

const PLUGIN_NAME = "postcss-purgecss";

async function purgeCSS(
opts: UserDefinedOptions,
root: Root,
{ result }: Helpers
): Promise<void> {
const purgeCSS = new PurgeCSS();
const options = {
...defaultOptions,
...opts,
safelist: standardizeSafelist(opts?.safelist),
};

if (opts && typeof opts.contentFunction === "function") {
options.content = opts.contentFunction(
(root.source && root.source.input.file) || ""
);
}

purgeCSS.options = options;

const { content, extractors } = options;

const fileFormatContents = content.filter(
(o) => typeof o === "string"
) as string[];
const rawFormatContents = content.filter(
(o) => typeof o === "object"
) as RawContent[];

const cssFileSelectors = await purgeCSS.extractSelectorsFromFiles(
fileFormatContents,
extractors
);
const cssRawSelectors = await purgeCSS.extractSelectorsFromString(
rawFormatContents,
extractors
);

const selectors = mergeExtractorSelectors(cssFileSelectors, cssRawSelectors);

//purge unused selectors
purgeCSS.walkThroughCSS(root, selectors);

if (purgeCSS.options.fontFace) purgeCSS.removeUnusedFontFaces();
if (purgeCSS.options.keyframes) purgeCSS.removeUnusedKeyframes();
if (purgeCSS.options.variables) purgeCSS.removeUnusedCSSVariables();

if (purgeCSS.options.rejected && purgeCSS.selectorsRemoved.size > 0) {
result.messages.push({
type: "purgecss",
plugin: "postcss-purgecss",
text: `purging ${purgeCSS.selectorsRemoved.size} selectors:
${Array.from(purgeCSS.selectorsRemoved)
.map((selector) => selector.trim())
.join("\n ")}`,
});
purgeCSS.selectorsRemoved.clear();
}
}

const purgeCSSPlugin: PluginCreator<UserDefinedOptions> = function (opts) {
if (typeof opts === "undefined")
throw new Error("PurgeCSS plugin does not have the correct options");
return {
postcssPlugin: PLUGIN_NAME,
Once(root, helpers) {
return purgeCSS(opts, root, helpers);
},
};
};
purgeCSSPlugin.postcss = true;

export default purgeCSSPlugin;
113 changes: 0 additions & 113 deletions packages/postcss-purgecss/src/postCSSPurgeCSS.ts

This file was deleted.

8 changes: 0 additions & 8 deletions packages/postcss-purgecss/src/types/index.ts
Expand Up @@ -3,9 +3,6 @@ import {
StringRegExpArray,
} from "../../../purgecss/src/types/index";

import postCSS7 from "postcss7";
import * as postCSS8 from "postcss";

export interface RawContent<T = string> {
extension: string;
raw: T;
Expand Down Expand Up @@ -34,8 +31,3 @@ export interface UserDefinedOptions {
safelist?: UserDefinedSafelist;
blocklist?: StringRegExpArray;
}

export type PostCSSv7 = typeof postCSS7;
export type PostCSSv8 = typeof postCSS8.default;

export type PostCSSVersions = PostCSSv8 | PostCSSv7;

0 comments on commit 48ce28f

Please sign in to comment.