Skip to content
This repository has been archived by the owner on May 29, 2024. It is now read-only.

Feat/upgrade purgecss to version 4 #63

Merged
merged 5 commits into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
node_modules
test-results
.jest-cache
*.tgz
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
registry=https://registry.npmjs.org
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ module.exports = {
loader: '@americanexpress/purgecss-loader',
options: {
paths: [path.join(somePath, 'src/**/*.{js,jsx}')],
whitelistPatternsChildren: [/:global$/],
safelist: [/:global$/],
},
},
],
Expand All @@ -82,9 +82,8 @@ the example to express this loader's compatibility.
| `fontFace` | `boolean` (default: false) see [options] | `false` |
| `keyframes` | `boolean` (default: false) see [options] | `false` |
| `variables` | `boolean` (default: false) see [options] | `false` |
| `whitelist` | `string[]` see [options]| `false` |
| `whitelistPatterns` | `Array<RegExp>` see [options] | `false` |
| `whitelistPatternsChildren` | `Array<RegExp>` see [options] | `false` |
| `safelist` | `UserDefinedSafelist` see [options]| `false` |
| `blocklist` | `StringRegExpArray` see [options] | `false` |

[glob]: https://github.com/isaacs/node-glob
[purgecss extractors]: https://www.purgecss.com/extractors.html
Expand Down
2 changes: 1 addition & 1 deletion __fixtures__/root.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
.isUsed {
color: #00175a;
}
.notUsed {
.isNotUsed {
color: red;
}
}
13 changes: 7 additions & 6 deletions __tests__/loader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
const path = require('path');
const webpack = require('webpack');
const MemoryFs = require('memory-fs');
const findIndex = require('lodash/findIndex');

Check warning on line 18 in __tests__/loader.spec.js

View workflow job for this annotation

GitHub Actions / Node 16.x

require('lodash/findIndex') detected. Consider using the native Array.prototype.findIndex()

Check warning on line 18 in __tests__/loader.spec.js

View workflow job for this annotation

GitHub Actions / Node 16.x

require('lodash/findIndex') detected. Consider using the native Array.prototype.findIndex()

jest.setTimeout(10000);

const runLoader = ({
entry = '../__fixtures__/Component.jsx',
} = {}) => {
const runLoader = (safelist,
{
entry = '../__fixtures__/Component.jsx',
} = {}) => {
const compiler = webpack({
context: __dirname,
entry,
Expand Down Expand Up @@ -57,7 +58,7 @@
loader: path.resolve(__dirname, '../loader.js'),
options: {
paths: [path.resolve(__dirname, '../__fixtures__/**/*.{js,jsx}')],
whitelistPatternsChildren: [/:global$/],
safelist,
},
},
],
Expand Down Expand Up @@ -95,9 +96,9 @@
expect(output).toContain('isUsed');
});

it('should not strip global classes', async () => {
it('should not strip safelisted global classes', async () => {
const componentEntry = { entry: '../__fixtures__/ComponentGlobal.jsx' };
const stats = await runLoader(componentEntry);
const stats = await runLoader([/:global$/], componentEntry);
const { modules } = stats.toJson();
const cssModuleIndex = findIndex(modules, {
name: '../node_modules/css-loader??ref--5-1!../loader.js??ref--5-2!../__fixtures__/root.scss',
Expand Down
9 changes: 4 additions & 5 deletions loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const { getOptions } = require('loader-utils');

module.exports = async function purifyCssLoader(content) {
const {
paths, extractors = [], fontFace = false, keyframes = false, variables = false, whitelist,
whitelistPatterns, whitelistPatternsChildren,
paths, extractors = [], fontFace = false, keyframes = false, variables = false, safelist = [],
blocklist = [],
} = getOptions(this);
const purgeCSSResult = await new PurgeCSS().purge({
content: paths,
Expand All @@ -27,9 +27,8 @@ module.exports = async function purifyCssLoader(content) {
fontFace,
keyframes,
variables,
whitelist,
whitelistPatterns,
whitelistPatternsChildren,
safelist,
blocklist,
});
return purgeCSSResult[0].css;
};
Loading
Loading