question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to Exclude Folders From Content Globs

See original GitHub issue

Describe the bug I’m filing this as a bug because the top results on Google for “purgeCSS skip folder” link to outdated advice from the PurgeCSS team. Specifically: https://github.com/FullHuman/purgecss/issues/158#issuecomment-456084860

You advise folks to use this syntax to skip a certain folder:

content: [
    '!(skippedFolder)/**/*.html',
    ...
]

The trouble is that your glob dependency removed support for ! as a negation operator in version 6.0. You currently use version 7 of glob. The ! operator is supported only in glob 5.x and below.

The way to skip folders in glob 6.x+ is to use their ignore option, which PurgeCSS does not currently expose.

To Reproduce Simply try to use the ! operator to negate a content glob; it will fail and PurgeCSS will remove rules that it SHOULD keep because it’s not scanning files appropriately.

Expected behavior PurgeCSS should allow users to skip certain folders, such as node_modules—especially because these folders can contain thousands of subfolders and having PurgeCSS scan all of them needlessly makes you look slow and bad.

Screenshots N/A

Environment (please complete the following information):

  • OS: macOS 11.2.2
  • Package purgeCSS
  • Version 4.0.0

Additional Context There are other people asking for ways to exclude folders: https://github.com/FullHuman/purgecss/issues/551

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
tordanscommented, Mar 19, 2021

FYI for those looking for a workaround until https://github.com/FullHuman/purgecss/pull/638 (thanks bdkjones) is usable in your codebase, you could do something like this:

This will remove all paths that include the /styleguide/ folder.

const contentGlobWithoutStyleguide = () => {
  // (1) Recreate the file list the same way PostCSS does it
  const glob = require("glob")
  return [
    './app/**/*.html.erb',
    './app/**/*.html.slim',
    './app/helpers/**/*.rb',
    './app/javascript/**/*.js',
    './app/javascript/**/*.jsx',
    './config/locales/**/*.yml',
    './config/locales/**/*.json',
  ].flatMap((pattern) => glob.sync(pattern, { nodir: true }))
    .filter((path) => !path.includes('/styleguides/')) // (2) Ignore Styleguide in PurgeCSS
}

environment.plugins.push(
  require('@fullhuman/postcss-purgecss')({
    content: contentGlobWithoutStyleguide(),
    safelist: …,
    defaultExtractor: …
  })
)

To test this, you can console.log the outpout of the defaultExtractor. For example like this:

postCssDebugOutputForDevelopmentEnv = true // default false
…
      defaultExtractor: content => {
        let match = content.match(/…/gi) || []
        if (postCssDebugOutputForDevelopmentEnv === true) {
          console.log(match)
        }
        return match
      }
1reaction
bdkjonescommented, Mar 13, 2021
Read more comments on GitHub >

github_iconTop Results From Across the Web

grunt (minimatch/glob) folder exclusion - Stack Overflow
grunt uses minimatch (similar to bsdglob) under the hood to match files, but I can't seem to figure out how to do a...
Read more >
jsconfig.json Reference - Visual Studio Code
Using the "exclude" property. The exclude attribute (a glob pattern) tells the language service what files are not part of your source code....
Read more >
Security features of Live Share - Microsoft Learn
The excludeFiles property allows you to specify a list of glob file patterns (very much like those found .gitignore files) that prevents Live ......
Read more >
Ignoring Code - Prettier
Use .prettierignore to ignore (i.e. not reformat) certain files and folders completely. Use “prettier-ignore” comments to ignore parts of files.
Read more >
Rclone Filtering
Rclone matching rules follow a glob style: ... To copy the contents of folder data into folder bkp excluding the contents of subfolder...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found