Skip to content

Commit fb08e3a

Browse files
authored
fix: Fix interaction with other plugins (#647)
* test: Add (failing) test for working with other plugins * fix: PurgeCSS must run on exit to work with other plugins Otherwise it tries to understand selectors before they have been processed by other plugins. For example, when using both postcss-nested and purgecss * chore: cleanup
1 parent 89ece42 commit fb08e3a

File tree

5 files changed

+50
-1
lines changed

5 files changed

+50
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.prefixed-used-class {
2+
color: black;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.used-class {
2+
color: black;
3+
}
4+
5+
.unused-class {
6+
color: black;
7+
}
8+
9+
.another-one-not-found {
10+
color: black;
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<html>
2+
3+
<body>
4+
5+
<div class="prefixed-used-class"></div>
6+
</body>
7+
8+
</html>

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

+27
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,31 @@ describe("Purgecss postcss plugin", () => {
8282
done();
8383
});
8484
});
85+
86+
it(`lets other plugins transform selectors before purging`, async () => {
87+
const input = fs
88+
.readFileSync(`${__dirname}/fixtures/src/other-plugins/other-plugins.css`)
89+
.toString();
90+
const expected = fs
91+
.readFileSync(`${__dirname}/fixtures/expected/other-plugins.css`)
92+
.toString();
93+
const result = await postcss([
94+
{
95+
postcssPlugin: "postcss-test-prefixer",
96+
Rule(rule) {
97+
if (rule.selector.startsWith(".")) {
98+
rule.selector = ".prefixed-" + rule.selector.slice(1);
99+
}
100+
},
101+
},
102+
purgeCSSPlugin({
103+
content: [`${__dirname}/fixtures/src/other-plugins/other-plugins.html`],
104+
fontFace: true,
105+
keyframes: true,
106+
}),
107+
]).process(input, { from: undefined });
108+
109+
expect(result.css).toBe(expected);
110+
expect(result.warnings().length).toBe(0);
111+
});
85112
});

packages/postcss-purgecss/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ const purgeCSSPlugin: postcss.PluginCreator<UserDefinedOptions> = function (
7777
throw new Error("PurgeCSS plugin does not have the correct options");
7878
return {
7979
postcssPlugin: PLUGIN_NAME,
80-
Once(root, helpers) {
80+
OnceExit(root, helpers) {
8181
return purgeCSS(opts, root, helpers);
8282
},
8383
};

0 commit comments

Comments
 (0)