Skip to content

Commit 48ce28f

Browse files
committed
feat(postcss-purgecss): remove compatibility with postcss 7
#488 fix #540 BREAKING CHANGE: dropping support for postcss 7
1 parent c41ad27 commit 48ce28f

File tree

4 files changed

+88
-129
lines changed

4 files changed

+88
-129
lines changed

packages/postcss-purgecss/__tests__/index.test.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import fs from "fs";
2-
import { UserDefinedOptions } from "../src/types";
3-
import postcss, { PluginCreator } from "postcss";
2+
import postcss from "postcss";
43

54
import purgeCSSPlugin from "../src/";
65

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

4544
postcss([
46-
(purgeCSSPlugin as PluginCreator<UserDefinedOptions>)({
45+
purgeCSSPlugin({
4746
contentFunction,
4847
fontFace: true,
4948
keyframes: true,
@@ -68,7 +67,7 @@ describe("Purgecss postcss plugin", () => {
6867
.readFileSync(`${__dirname}/fixtures/expected/simple.css`)
6968
.toString();
7069
postcss([
71-
(purgeCSSPlugin as PluginCreator<UserDefinedOptions>)({
70+
purgeCSSPlugin({
7271
content: [`${__dirname}/fixtures/src/simple/simple.html`],
7372
rejected: true,
7473
}),
Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,85 @@
1-
import postcss from "postcss";
2-
import { createPurgeCSSPlugin } from "./postCSSPurgeCSS";
1+
import { Helpers, PluginCreator, Root } from "postcss";
32

4-
export default createPurgeCSSPlugin(postcss);
3+
import PurgeCSS, {
4+
defaultOptions,
5+
mergeExtractorSelectors,
6+
standardizeSafelist,
7+
} from "purgecss";
8+
9+
import { RawContent, UserDefinedOptions } from "./types";
10+
11+
const PLUGIN_NAME = "postcss-purgecss";
12+
13+
async function purgeCSS(
14+
opts: UserDefinedOptions,
15+
root: Root,
16+
{ result }: Helpers
17+
): Promise<void> {
18+
const purgeCSS = new PurgeCSS();
19+
const options = {
20+
...defaultOptions,
21+
...opts,
22+
safelist: standardizeSafelist(opts?.safelist),
23+
};
24+
25+
if (opts && typeof opts.contentFunction === "function") {
26+
options.content = opts.contentFunction(
27+
(root.source && root.source.input.file) || ""
28+
);
29+
}
30+
31+
purgeCSS.options = options;
32+
33+
const { content, extractors } = options;
34+
35+
const fileFormatContents = content.filter(
36+
(o) => typeof o === "string"
37+
) as string[];
38+
const rawFormatContents = content.filter(
39+
(o) => typeof o === "object"
40+
) as RawContent[];
41+
42+
const cssFileSelectors = await purgeCSS.extractSelectorsFromFiles(
43+
fileFormatContents,
44+
extractors
45+
);
46+
const cssRawSelectors = await purgeCSS.extractSelectorsFromString(
47+
rawFormatContents,
48+
extractors
49+
);
50+
51+
const selectors = mergeExtractorSelectors(cssFileSelectors, cssRawSelectors);
52+
53+
//purge unused selectors
54+
purgeCSS.walkThroughCSS(root, selectors);
55+
56+
if (purgeCSS.options.fontFace) purgeCSS.removeUnusedFontFaces();
57+
if (purgeCSS.options.keyframes) purgeCSS.removeUnusedKeyframes();
58+
if (purgeCSS.options.variables) purgeCSS.removeUnusedCSSVariables();
59+
60+
if (purgeCSS.options.rejected && purgeCSS.selectorsRemoved.size > 0) {
61+
result.messages.push({
62+
type: "purgecss",
63+
plugin: "postcss-purgecss",
64+
text: `purging ${purgeCSS.selectorsRemoved.size} selectors:
65+
${Array.from(purgeCSS.selectorsRemoved)
66+
.map((selector) => selector.trim())
67+
.join("\n ")}`,
68+
});
69+
purgeCSS.selectorsRemoved.clear();
70+
}
71+
}
72+
73+
const purgeCSSPlugin: PluginCreator<UserDefinedOptions> = function (opts) {
74+
if (typeof opts === "undefined")
75+
throw new Error("PurgeCSS plugin does not have the correct options");
76+
return {
77+
postcssPlugin: PLUGIN_NAME,
78+
Once(root, helpers) {
79+
return purgeCSS(opts, root, helpers);
80+
},
81+
};
82+
};
83+
purgeCSSPlugin.postcss = true;
84+
85+
export default purgeCSSPlugin;

packages/postcss-purgecss/src/postCSSPurgeCSS.ts

Lines changed: 0 additions & 113 deletions
This file was deleted.

packages/postcss-purgecss/src/types/index.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ import {
33
StringRegExpArray,
44
} from "../../../purgecss/src/types/index";
55

6-
import postCSS7 from "postcss7";
7-
import * as postCSS8 from "postcss";
8-
96
export interface RawContent<T = string> {
107
extension: string;
118
raw: T;
@@ -34,8 +31,3 @@ export interface UserDefinedOptions {
3431
safelist?: UserDefinedSafelist;
3532
blocklist?: StringRegExpArray;
3633
}
37-
38-
export type PostCSSv7 = typeof postCSS7;
39-
export type PostCSSv8 = typeof postCSS8.default;
40-
41-
export type PostCSSVersions = PostCSSv8 | PostCSSv7;

0 commit comments

Comments
 (0)