Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add whitelistPatternsGreedy option #424

Merged
merged 1 commit into from Jun 13, 2020
Merged

Add whitelistPatternsGreedy option #424

merged 1 commit into from Jun 13, 2020

Conversation

benface
Copy link
Contributor

@benface benface commented May 29, 2020

Proposed changes

I am proposing a new whitelistPatternsGreedy option, similar to the existing whitelistPatterns and whitelistPatternsChildren options, but that whitelists an entire selector if any part of the selector matches the regex.

If you have a selector like this:

.my-module.color--blue {}

And my-module is found by the extractor but not color--blue, no combination of the current whitelistPatterns / whitelistPatternsChildren exists that would prevent this selector from being purged.

With whitelistPatternsGreedy, you could whitelist this selector (and any other that contains the my-module class) like this:

{
  whitelistPatternsGreedy: [/^my-module$/],
}

The reason I would like to add this feature is that I'm looking for a way to whitelist all scoped CSS in Vue, regardless of whether the classes are found in the HTML or not:

<template>
  <div :class="`size--${size}`"></div>
</template>

<style scoped>
  .size--small {
    font-size: 12px;
  }
  .size--large {
    font-size: 18px;
  }
</style>

Due to the dynamic class attribution (size--${size}), the size--small and size--large classes would normally be purged with the following config:

{
  whitelistPatterns: [/data-v-.*/],
  defaultExtractor: (content) => {
    const contentWithoutStyleBlocks = content.replace(/<style[^]+?<\/style>/gi, '');
    return contentWithoutStyleBlocks.match(/[A-Za-z0-9-_:/]+/g) || [];
  },
}

Of course, we could opt to not remove the <style> blocks from the extracted content and it would also fix the issue here, but I like having that to prevent uselessly keeping Tailwind utility classes that are only used with @apply. So instead, we can replace whitelistPatterns with whitelistPatternsGreedy and any selector that contains a data-v-* attribute will be whitelisted.

Thank you!

Types of changes

What types of changes does your code introduce?
Put an x in the boxes that apply

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code.

  • Lint and unit tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

@benface
Copy link
Contributor Author

benface commented May 29, 2020

If you have a selector like this:
.my-module.color--blue {}
And my-module is found by the extractor but not color--blue, no combination of the current whitelistPatterns / whitelistPatternsChildren exists that would prevent this selector from being purged.

I just realized that this is not exactly true: whitelistPatternsChildren would actually work here but I would consider it a bug because if you switch the classes in the selector (so .color--blue.my-module) then it wouldn't work because it would look up color--blue first, see that it is not found in the extracted selectors, and automatically purge the rule. I don't think the class order should matter.

@Ffloriel
Copy link
Member

LGTM, thanks for the PR

@Ffloriel Ffloriel merged commit adacfd7 into FullHuman:master Jun 13, 2020
@Ffloriel Ffloriel mentioned this pull request Jun 17, 2020
kodiakhq bot pushed a commit to X-oss-byte/Nextjs that referenced this pull request Oct 8, 2023
[![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [@fullhuman/postcss-purgecss](https://purgecss.com) ([source](https://togithub.com/FullHuman/purgecss)) | [`1.3.0` -> `5.0.0`](https://renovatebot.com/diffs/npm/@fullhuman%2fpostcss-purgecss/1.3.0/5.0.0) | [![age](https://developer.mend.io/api/mc/badges/age/npm/@fullhuman%2fpostcss-purgecss/5.0.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@fullhuman%2fpostcss-purgecss/5.0.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@fullhuman%2fpostcss-purgecss/1.3.0/5.0.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@fullhuman%2fpostcss-purgecss/1.3.0/5.0.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) |

---

### Release Notes

<details>
<summary>FullHuman/purgecss (@&#8203;fullhuman/postcss-purgecss)</summary>

### [`v5.0.0`](https://togithub.com/FullHuman/purgecss/blob/HEAD/CHANGELOG.md#500-2022-09-13)

[Compare Source](https://togithub.com/FullHuman/purgecss/compare/v4.1.3...v5.0.0)

##### Bug Fixes

-   add safelist variables to postcss-purgecss [#&#8203;840](https://togithub.com/FullHuman/purgecss/issues/840) ([c822058](https://togithub.com/FullHuman/purgecss/commit/c822058a703a0544c0bb683427937d9683688742))
-   **gulp-purgecss:** support skippedContentGlobs option [#&#8203;853](https://togithub.com/FullHuman/purgecss/issues/853) ([b72de77](https://togithub.com/FullHuman/purgecss/commit/b72de77323fd74d5d53695409fef26e369656839))
-   Purgecss webpack plugin/only filter fix ([#&#8203;933](https://togithub.com/FullHuman/purgecss/issues/933)) ([f8e4c2c](https://togithub.com/FullHuman/purgecss/commit/f8e4c2c48440c26ca612b2486a6e9561be6f56d0))
-   **purgecss-webpack-plugin:** add sourcemap support [#&#8203;409](https://togithub.com/FullHuman/purgecss/issues/409) ([b3f73ed](https://togithub.com/FullHuman/purgecss/commit/b3f73ed5293fa1d840aff98b5796f635343d48ec))
-   **purgecss-webpack-plugin:** export as named export as well as default ([#&#8203;821](https://togithub.com/FullHuman/purgecss/issues/821)) ([a6a2c8e](https://togithub.com/FullHuman/purgecss/commit/a6a2c8e0634324cc9ab276e02d5447d5d6c1e6e3))
-   wrong path import ([4f3ddd0](https://togithub.com/FullHuman/purgecss/commit/4f3ddd0a47ec29cf8a723bd68ebc4b724e03dc7e))

##### Features

-   add source map support [#&#8203;526](https://togithub.com/FullHuman/purgecss/issues/526) ([f2a9c5a](https://togithub.com/FullHuman/purgecss/commit/f2a9c5ac575af9a1e3f85be63b758fb9c37077e1))
-   **gulp-purgecss:** add support for gulp-sourcemaps [#&#8203;257](https://togithub.com/FullHuman/purgecss/issues/257) ([55c26d2](https://togithub.com/FullHuman/purgecss/commit/55c26d2790b8502f115180cfe02aba5720c84b7b))
-   **postcss-purgecss:** load options from purgecss config ([4de3bd8](https://togithub.com/FullHuman/purgecss/commit/4de3bd8e198985670502f77c57567eb43fcccbd4))
-   **purgecss-webpack-plugin:** load config file automatically [#&#8203;767](https://togithub.com/FullHuman/purgecss/issues/767) ([726faaa](https://togithub.com/FullHuman/purgecss/commit/726faaa1e6eeb96f6c6b223d1ba925f9d7f33cba))

##### BREAKING CHANGES

-   **purgecss-webpack-plugin:** drop webpack 4 support

### [`v4.1.3`](https://togithub.com/FullHuman/purgecss/compare/v4.1.2...v4.1.3)

[Compare Source](https://togithub.com/FullHuman/purgecss/compare/v4.1.2...v4.1.3)

### [`v4.1.2`](https://togithub.com/FullHuman/purgecss/compare/v4.1.1...v4.1.2)

[Compare Source](https://togithub.com/FullHuman/purgecss/compare/v4.1.1...v4.1.2)

### [`v4.1.1`](https://togithub.com/FullHuman/purgecss/compare/v4.1.0...v4.1.1)

[Compare Source](https://togithub.com/FullHuman/purgecss/compare/v4.1.0...v4.1.1)

### [`v4.1.0`](https://togithub.com/FullHuman/purgecss/blob/HEAD/CHANGELOG.md#410-2021-11-28)

[Compare Source](https://togithub.com/FullHuman/purgecss/compare/v4.0.3...v4.1.0)

##### Bug Fixes

-   Allow Absolute Paths ([#&#8203;679](https://togithub.com/FullHuman/purgecss/issues/679)) ([15335a2](https://togithub.com/FullHuman/purgecss/commit/15335a2f4643a60593ab071064ad51db8f47ce77))
-   css variable removed when declared in wrong order ([89ece42](https://togithub.com/FullHuman/purgecss/commit/89ece42929a3fd62b2a84fb0b57c0ac16c98908f)), closes [#&#8203;518](https://togithub.com/FullHuman/purgecss/issues/518)
-   Fix interaction with other plugins ([#&#8203;647](https://togithub.com/FullHuman/purgecss/issues/647)) ([fb08e3a](https://togithub.com/FullHuman/purgecss/commit/fb08e3a3c27a1b25e4f023cbcbfca35a0cc2b43e))
-   **grunt-purgecss:** Fix plugin not ouputting all files ([#&#8203;723](https://togithub.com/FullHuman/purgecss/issues/723)) ([646e419](https://togithub.com/FullHuman/purgecss/commit/646e419cac01b3e647cc77ffebcfd5c31d087e23))
-   **gulp-purgecss:** fix support for stream input ([fd5d3bf](https://togithub.com/FullHuman/purgecss/commit/fd5d3bf578344f2512d1c0961d8dbbb1f3b8d0d5))
-   Keep keyframe decimals for prefixed [@&#8203;keyframes](https://togithub.com/keyframes) ([#&#8203;749](https://togithub.com/FullHuman/purgecss/issues/749)) ([b804441](https://togithub.com/FullHuman/purgecss/commit/b80444195c08e2ae62f5e5f249a1e346b1d70097))
-   **purgecss-from-pug:** class attribute with multiple values not correctly handled with pug ([#&#8203;678](https://togithub.com/FullHuman/purgecss/issues/678)) ([ba6285d](https://togithub.com/FullHuman/purgecss/commit/ba6285def36c304ca7ae3fa5999cec378065060c)), closes [#&#8203;677](https://togithub.com/FullHuman/purgecss/issues/677)

##### Features

-   add dynamicAttributes option ([2f9cc65](https://togithub.com/FullHuman/purgecss/commit/2f9cc653ba77b0eb58ebed40269bcb6923b3fe83))
-   add option to ouput unused css ([#&#8203;763](https://togithub.com/FullHuman/purgecss/issues/763)) ([3a3d958](https://togithub.com/FullHuman/purgecss/commit/3a3d9582f28434acceaca5622f82c39f598a85e1))
-   add purgecss-from-tsx ([#&#8203;716](https://togithub.com/FullHuman/purgecss/issues/716)) ([e556afc](https://togithub.com/FullHuman/purgecss/commit/e556afc6401a56886f10f8fda7f729853c0735e7))
-   export all types [#&#8203;780](https://togithub.com/FullHuman/purgecss/issues/780) ([1a959c6](https://togithub.com/FullHuman/purgecss/commit/1a959c6700bf82975fd52ac32ec1012b3e2e1b5b))
-   new purgecss-from-jsx plugin ([#&#8203;692](https://togithub.com/FullHuman/purgecss/issues/692)) ([3570c7d](https://togithub.com/FullHuman/purgecss/commit/3570c7db9a4028e494ad684a7689f2cc5a99a585))

### [`v4.0.3`](https://togithub.com/FullHuman/purgecss/blob/HEAD/CHANGELOG.md#403-2021-03-20)

[Compare Source](https://togithub.com/FullHuman/purgecss/compare/v4.0.2...v4.0.3)

##### Features

-   add dynamic attributes option ([9b0fdc3](https://togithub.com/FullHuman/purgecss/commit/9b0fdc3219b1b24856e302e0798e4d22ab006a5a)), closes [#&#8203;588](https://togithub.com/FullHuman/purgecss/issues/588)
-   add skippedContentGlobs option to webpack and gulp plugin ([e3dce1a](https://togithub.com/FullHuman/purgecss/commit/e3dce1a43791d32f5967d4aa320f2ec80690b299))

### [`v4.0.2`](https://togithub.com/FullHuman/purgecss/blob/HEAD/CHANGELOG.md#402-2021-02-21)

[Compare Source](https://togithub.com/FullHuman/purgecss/compare/v4.0.1...v4.0.2)

### [`v4.0.1`](https://togithub.com/FullHuman/purgecss/blob/HEAD/CHANGELOG.md#401-2021-02-21)

[Compare Source](https://togithub.com/FullHuman/purgecss/compare/v4.0.0...v4.0.1)

##### Bug Fixes

-   attribute selector with spaces being removed ([418dc7e](https://togithub.com/FullHuman/purgecss/commit/418dc7e0a7a30b57e1d7d9d10272e8f47eeb46fb)), closes [#&#8203;392](https://togithub.com/FullHuman/purgecss/issues/392)
-   webpack plugin option blocklist types ([5b029c2](https://togithub.com/FullHuman/purgecss/commit/5b029c21eb0269bc464d8e5f5a4ab9c3eed23064))

##### Features

-   add option blocklist to webpack plugin ([#&#8203;610](https://togithub.com/FullHuman/purgecss/issues/610)) ([0148cea](https://togithub.com/FullHuman/purgecss/commit/0148cea584f9a09c26846b505dbb4945b4c1b536))

### [`v4.0.0`](https://togithub.com/FullHuman/purgecss/blob/HEAD/CHANGELOG.md#400-2021-01-17)

[Compare Source](https://togithub.com/FullHuman/purgecss/compare/v3.1.3...v4.0.0)

##### Bug Fixes

-   **test:** webpack test type error ([c41ad27](https://togithub.com/FullHuman/purgecss/commit/c41ad27fac09fca62343ba5bdc0a6b7f53aeb757))

##### Features

-   **postcss-purgecss:** remove compatibility with postcss 7 ([48ce28f](https://togithub.com/FullHuman/purgecss/commit/48ce28fa10cb4bd72385f2ac0b25303f67c581be)), closes [#&#8203;488](https://togithub.com/FullHuman/purgecss/issues/488) [#&#8203;540](https://togithub.com/FullHuman/purgecss/issues/540)

##### BREAKING CHANGES

-   **postcss-purgecss:** dropping support for postcss 7
    Drop PostCSS 7 support, use [@&#8203;fullhuman/postcss-purgecss](https://togithub.com/fullhuman/postcss-purgecss) 3.0 with PostCSS 7.

### [`v3.1.3`](https://togithub.com/FullHuman/purgecss/releases/tag/v3.1.3)

[Compare Source](https://togithub.com/FullHuman/purgecss/compare/v3.1.0...v3.1.3)

##### Changes

-   **postcss-purgecss** remove postcss 8 as peer dependency
-   **purgecss-webpack-plugin** fix backward compatibility with webpack 4
-   **PurgeCSS** Update to PostCSS 8
-   **postcss-purgecss** Add compatibility with PostCSS 8
-   **purgecss-webpack-plugin** Fix deprecation warning with Webpack 5

##### Bug Fixes

-   ci update to postcss 8.2 ([72302ff](https://togithub.com/FullHuman/purgecss/commit/72302ffa475a2f74af4211c5a3caa70f2de4abfd))
-   safelist option in CLI ([43dbe3c](https://togithub.com/FullHuman/purgecss/commit/43dbe3c498292a9c1ef2166d8c714275fcb9d62a)), closes [#&#8203;513](https://togithub.com/FullHuman/purgecss/issues/513)

### [`v3.1.0`](https://togithub.com/FullHuman/purgecss/compare/v3.0.0...v3.1.0)

[Compare Source](https://togithub.com/FullHuman/purgecss/compare/v3.0.0...v3.1.0)

### [`v3.0.0`](https://togithub.com/FullHuman/purgecss/releases/tag/v3.0.0)

[Compare Source](https://togithub.com/FullHuman/purgecss/compare/v2.3.0...v3.0.0)

#### 3.0.0

##### Simplifying whitelist option

Whitelist started as a simple option named `whitelist` but grew up with the different use-cases and needs that appear with time. Another option called `whitelistPatterns` appeared, then `whitelistPatternsChildren`, …
It is now more complex and complicated, and somewhat difficult to remember how to use it with different options.

To summarize the changes, the whitelist options are now grouped in one option called safelist. And the most used options can be defined in one array.

Two forms are available. The simple form is:

```ts
safelist: ['invisibleClass', /^nav-/]
```

In this form, safelist is an array that can take a string or a regex.

The complex form is:

```ts
safelist: {
 standard: ['invisibleClass', /^nav-/],
 deep: [],
 greedy: [],
 keyframes: [],
 variables: []
}
```

In this form, safelist is an object taking optional properties:
standard is the same as the simple form and replaces `whitelist` and whitelistPatterns
deep replaces whitelistPatternsChildren
greedy is a new option coming from [FullHuman/purgecss#424
keyframes can be used to add keyframes to the safelist, when using keyframes: true ([FullHuman/purgecss#418)
variables can be used to add CSS variables to the safelist, when using variables: true

##### New Option: Blocklist

Blocklist will block the CSS selectors from appearing in the final output CSS. The selectors will be removed even when they are seen as used by PurgeCSS.

    blocklist: ['usedClass', /^nav-/]

Even if `nav-links` and `usedClass` are found in your content files (HTML, Javascript), they will be removed.

##### CLI

PurgeCSS is using commander.js for its CLI. The recent version of commander.js introduced the possibility of making an option variadic. This means when specifying multiple option arguments, the parsed option value will be an array.
A few options are now taking advantage of this new feature: content, css, and the newly introduced option safelist and blocklist. It is no longer necessary to separate the list items with a comma.

##### From changelog:

-   **CLI:** add blocklist option ([3961afb](https://togithub.com/FullHuman/purgecss/commit/3961afbc6d90eae83fe4862a4498857fa7ba3ff6))
-   add blocklist option ([04223f7](https://togithub.com/FullHuman/purgecss/commit/04223f7fe27f8d818961a53900a7c5293d2322b6))
-   add safelist option, replace whitelist
-   add safelist keyframes and css variables ([dc59d30](https://togithub.com/FullHuman/purgecss/commit/dc59d309a4a4be9845c40966a19f9705c42a33a1)), closes [#&#8203;418](https://togithub.com/FullHuman/purgecss/issues/418) [#&#8203;439](https://togithub.com/FullHuman/purgecss/issues/439)

### [`v2.3.0`](https://togithub.com/FullHuman/purgecss/compare/v2.2.0...v2.3.0)

[Compare Source](https://togithub.com/FullHuman/purgecss/compare/v2.2.0...v2.3.0)

### [`v2.2.0`](https://togithub.com/FullHuman/purgecss/compare/v2.1.2...v2.2.0)

[Compare Source](https://togithub.com/FullHuman/purgecss/compare/v2.1.2...v2.2.0)

### [`v2.1.2`](https://togithub.com/FullHuman/purgecss/compare/v2.1.0...v2.1.2)

[Compare Source](https://togithub.com/FullHuman/purgecss/compare/v2.1.0...v2.1.2)

### [`v2.1.0`](https://togithub.com/FullHuman/purgecss/compare/v2.0.6...v2.1.0)

[Compare Source](https://togithub.com/FullHuman/purgecss/compare/v2.0.6...v2.1.0)

### [`v2.0.6`](https://togithub.com/FullHuman/purgecss/compare/v2.0.5...v2.0.6)

[Compare Source](https://togithub.com/FullHuman/purgecss/compare/v2.0.5...v2.0.6)

### [`v2.0.5`](https://togithub.com/FullHuman/purgecss/releases/tag/v2.0.5): v2.0

[Compare Source](https://togithub.com/FullHuman/purgecss/compare/v2.0.4...v2.0.5)

Changes:

-   Asynchronous

```js
import PurgeCSS from "purgecss";

const purgecssResults = await new PurgeCSS().purge({
    // options
});
```

-   Extractor is a function

```js
content => content.match(/[\w-/:]+(?<!:)/g) || []
```

-   `variables` option to remove unused CSS variables
-   Drop support of NodeJS < 8

### [`v2.0.4`](https://togithub.com/FullHuman/purgecss/compare/v2.0.3...v2.0.4)

[Compare Source](https://togithub.com/FullHuman/purgecss/compare/v2.0.3...v2.0.4)

### [`v2.0.3`](https://togithub.com/FullHuman/purgecss/compare/cdff1f6f85a3c72d6be7e5fd6e67ff2660d9725a...v2.0.3)

[Compare Source](https://togithub.com/FullHuman/purgecss/compare/v1.3.0...v2.0.3)

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://developer.mend.io/github/X-oss-byte/Nextjs).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants