Skip to content

Commit

Permalink
fix: Fix interaction with other plugins (#647)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rluba committed Apr 12, 2021
1 parent 89ece42 commit fb08e3a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 1 deletion.
@@ -0,0 +1,3 @@
.prefixed-used-class {
color: black;
}
@@ -0,0 +1,11 @@
.used-class {
color: black;
}

.unused-class {
color: black;
}

.another-one-not-found {
color: black;
}
@@ -0,0 +1,8 @@
<html>

<body>

<div class="prefixed-used-class"></div>
</body>

</html>
27 changes: 27 additions & 0 deletions packages/postcss-purgecss/__tests__/index.test.ts
Expand Up @@ -82,4 +82,31 @@ describe("Purgecss postcss plugin", () => {
done();
});
});

it(`lets other plugins transform selectors before purging`, async () => {
const input = fs
.readFileSync(`${__dirname}/fixtures/src/other-plugins/other-plugins.css`)
.toString();
const expected = fs
.readFileSync(`${__dirname}/fixtures/expected/other-plugins.css`)
.toString();
const result = await postcss([
{
postcssPlugin: "postcss-test-prefixer",
Rule(rule) {
if (rule.selector.startsWith(".")) {
rule.selector = ".prefixed-" + rule.selector.slice(1);
}
},
},
purgeCSSPlugin({
content: [`${__dirname}/fixtures/src/other-plugins/other-plugins.html`],
fontFace: true,
keyframes: true,
}),
]).process(input, { from: undefined });

expect(result.css).toBe(expected);
expect(result.warnings().length).toBe(0);
});
});
2 changes: 1 addition & 1 deletion packages/postcss-purgecss/src/index.ts
Expand Up @@ -77,7 +77,7 @@ const purgeCSSPlugin: postcss.PluginCreator<UserDefinedOptions> = function (
throw new Error("PurgeCSS plugin does not have the correct options");
return {
postcssPlugin: PLUGIN_NAME,
Once(root, helpers) {
OnceExit(root, helpers) {
return purgeCSS(opts, root, helpers);
},
};
Expand Down

0 comments on commit fb08e3a

Please sign in to comment.