Skip to content
This repository has been archived by the owner on Feb 3, 2020. It is now read-only.

Commit

Permalink
Merge pull request #29 from timbotnik/timbotnik/support-rejected
Browse files Browse the repository at this point in the history
Add support for rejected option.
  • Loading branch information
Ffloriel authored May 29, 2019
2 parents f9c8bad + b2a7c47 commit 803f228
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 3 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ Type: `Array<RegExp>`

You can whitelist selectors based on a regular expression with whitelistPatterns.

### `rejected`
Type: `boolean`
Default value: `false`

If true, purged selectors will be captured and rendered as PostCSS messages.
Use with a PostCSS reporter plugin like [`postcss-reporter`](https://github.com/postcss/postcss-reporter)
to print the purged selectors to the console as they are processed.

### `keyframes`
Type: `boolean`
Default value: `false`
Expand Down
26 changes: 26 additions & 0 deletions __tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,32 @@ describe('Purgecss postcss plugin', () => {
})
}

for (const file of files) {
it(`queues messages when using reject flag: ${file}`, done => {
const input = fs
.readFileSync(`${__dirname}/fixtures/src/${file}/${file}.css`)
.toString()
const expected = fs
.readFileSync(`${__dirname}/fixtures/expected/${file}.css`)
.toString()
postcss([
purgecss({
content: [`${__dirname}/fixtures/src/${file}/${file}.html`],
rejected: true
})
])
.process(input)
.then(result => {
expect(result.css).toBe(expected)
expect(result.warnings().length).toBe(0)
expect(result.messages.length).toBeGreaterThan(0)
expect(result.messages[0].text).toMatch(/unused-class/)
expect(result.messages[0].text).toMatch(/another-one-not-found/)
done()
})
})
}

it('remove unused css with config file', done => {
const input = fs
.readFileSync(`${__dirname}/fixtures/src/simple/simple.css`)
Expand Down
13 changes: 12 additions & 1 deletion lib/postcss-purgecss.es.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var loadConfigFile = function loadConfigFile(configFile) {
};

var index = postcss.plugin('postcss-plugin-purgecss', function (opts) {
return function (root) {
return function (root, result) {
if (typeof opts === 'string' || typeof opts === 'undefined') opts = loadConfigFile(opts);

if (!opts.css || !opts.css.length) {
Expand Down Expand Up @@ -83,6 +83,17 @@ var index = postcss.plugin('postcss-plugin-purgecss', function (opts) {
if (purgecss.options.keyframes) purgecss.removeUnusedKeyframes(); // purge font face

if (purgecss.options.fontFace) purgecss.removeUnusedFontFaces();

if (purgecss.options.rejected && purgecss.selectorsRemoved.size > 0) {
result.messages.push({
type: 'purgecss',
plugin: 'postcss-purgecss',
text: "purging ".concat(purgecss.selectorsRemoved.size, " selectors:\n ").concat(Array.from(purgecss.selectorsRemoved).map(function (selector) {
return selector.trim();
}).join('\n '))
});
purgecss.selectorsRemoved.clear();
}
};
});

Expand Down
13 changes: 12 additions & 1 deletion lib/postcss-purgecss.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ var loadConfigFile = function loadConfigFile(configFile) {
};

var index = postcss.plugin('postcss-plugin-purgecss', function (opts) {
return function (root) {
return function (root, result) {
if (typeof opts === 'string' || typeof opts === 'undefined') opts = loadConfigFile(opts);

if (!opts.css || !opts.css.length) {
Expand Down Expand Up @@ -87,6 +87,17 @@ var index = postcss.plugin('postcss-plugin-purgecss', function (opts) {
if (purgecss.options.keyframes) purgecss.removeUnusedKeyframes(); // purge font face

if (purgecss.options.fontFace) purgecss.removeUnusedFontFaces();

if (purgecss.options.rejected && purgecss.selectorsRemoved.size > 0) {
result.messages.push({
type: 'purgecss',
plugin: 'postcss-purgecss',
text: "purging ".concat(purgecss.selectorsRemoved.size, " selectors:\n ").concat(Array.from(purgecss.selectorsRemoved).map(function (selector) {
return selector.trim();
}).join('\n '))
});
purgecss.selectorsRemoved.clear();
}
};
});

Expand Down
12 changes: 11 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const loadConfigFile = configFile => {
}

export default postcss.plugin('postcss-plugin-purgecss', function(opts) {
return function(root) {
return function(root, result) {
if (typeof opts === 'string' || typeof opts === 'undefined')
opts = loadConfigFile(opts)

Expand Down Expand Up @@ -62,5 +62,15 @@ export default postcss.plugin('postcss-plugin-purgecss', function(opts) {

// purge font face
if (purgecss.options.fontFace) purgecss.removeUnusedFontFaces()

if (purgecss.options.rejected && purgecss.selectorsRemoved.size > 0) {
result.messages.push({
type: 'purgecss',
plugin: 'postcss-purgecss',
text: `purging ${purgecss.selectorsRemoved.size} selectors:
${Array.from(purgecss.selectorsRemoved).map(selector => selector.trim()).join('\n ')}`,
})
purgecss.selectorsRemoved.clear()
}
}
})

0 comments on commit 803f228

Please sign in to comment.