Purgecss webpack plugin/only filter fix #933
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Proposed changes
Currently, when
only
option is used, each asset is processed again for each chunk. Reason for that is, ifonly
option is set, code that checks if chunk contains the file is skipped. For every chunk, same files that pass through theonly
filter is processed again and again. This causes a massive performance hit on a big code base. You can see it in the code block below.purgecss/packages/purgecss-webpack-plugin/src/index.ts
Lines 167 to 174 in 17dc7ad
Types of changes
This PR changes above filter function so filter for
only
only returns if it is false. If a file passesonly
filter, it will also be checked against file list in the chunk.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.I did not add tests that actually tests if a file is compiled multiple times or not which is the main point of this PR. But I fixed a test that I believe was not testing what it intends to test.
simple-with-exclusion
test case ofpurgecss-webpack-plugin
excluded everything frompurgecss
instead of excluding one part. You can see that there exists a file with namestyle_that_we_want_to_purge.css
, yet in the expected output,unused
css class is not purged at all (snippet below).purgecss/packages/purgecss-webpack-plugin/__tests__/cases/simple-with-exclusion/expected/styles.css
Lines 1 to 19 in 17dc7ad
To fix this, I disabled the chunk group that merged
style.css
andstyle_that_we_want_to_purge.css
intostyles.css
so we have two css chunks.only
option specifies one of them and expected output does not haveunused
anymore.Docs mention
only
option filters by entrypoint name, but it actually filters by chunk name, because compilation assets are ultimately chunks. I think huskys pre-commit hook changed a lot of things in documentation file. I only changed 135th line. If you want, I can commit again with--no-verify
.Further comments