Skip to content

Commit

Permalink
Merge pull request #58 from SassNinja/feature/show-rejected
Browse files Browse the repository at this point in the history
Consider rejected option
  • Loading branch information
jsnanigans authored and Ffloriel committed Oct 30, 2019
1 parent 8b69afe commit 261c677
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
4 changes: 4 additions & 0 deletions packages/purgecss-webpack-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ new PurgecssPlugin({
})
```

* #### rejected

If `true` all removed selectors are added to the [Stats Data](https://webpack.js.org/api/stats/) as `purged`.

## Contributing

Please read [CONTRIBUTING.md](./CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull requests to us.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ var files = function files(chunk) {
var webpackVersion = 4;
var styleExtensions = ['.css', '.scss', '.styl', '.sass', '.less'];
var pluginName = 'PurgeCSS';
var purgedStats = {};

var PurgecssPlugin =
/*#__PURE__*/
Expand All @@ -178,10 +179,25 @@ function () {
compiler.hooks.compilation.tap(pluginName, function (compilation) {
_this.initializePlugin(compilation);
});
compiler.hooks.done.tapAsync(pluginName, function (stats, cb) {
_this.addStats(stats);

cb();
});
} else {
compiler.plugin('this-compilation', function (compilation) {
_this.initializePlugin(compilation);
});
compiler.plugin('done', function (stats) {
_this.addStats(stats);
});
}
}
}, {
key: "addStats",
value: function addStats(stats) {
if (this.options.rejected) {
stats.purged = purgedStats;
}
}
}, {
Expand Down Expand Up @@ -259,7 +275,13 @@ function () {
}

var purgecss = new Purgecss(options);
compilation.assets[name] = new ConcatSource(purgecss.purge()[0].css);
var purged = purgecss.purge()[0];

if (purged.rejected) {
purgedStats[name] = purged.rejected;
}

compilation.assets[name] = new ConcatSource(purged.css);
});
});
callback();
Expand Down
24 changes: 23 additions & 1 deletion packages/purgecss-webpack-plugin/lib/purgecss-webpack-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ var files = function files(chunk) {
var webpackVersion = 4;
var styleExtensions = ['.css', '.scss', '.styl', '.sass', '.less'];
var pluginName = 'PurgeCSS';
var purgedStats = {};

var PurgecssPlugin =
/*#__PURE__*/
Expand All @@ -182,10 +183,25 @@ function () {
compiler.hooks.compilation.tap(pluginName, function (compilation) {
_this.initializePlugin(compilation);
});
compiler.hooks.done.tapAsync(pluginName, function (stats, cb) {
_this.addStats(stats);

cb();
});
} else {
compiler.plugin('this-compilation', function (compilation) {
_this.initializePlugin(compilation);
});
compiler.plugin('done', function (stats) {
_this.addStats(stats);
});
}
}
}, {
key: "addStats",
value: function addStats(stats) {
if (this.options.rejected) {
stats.purged = purgedStats;
}
}
}, {
Expand Down Expand Up @@ -263,7 +279,13 @@ function () {
}

var purgecss = new Purgecss(options);
compilation.assets[name] = new webpackSources.ConcatSource(purgecss.purge()[0].css);
var purged = purgecss.purge()[0];

if (purged.rejected) {
purgedStats[name] = purged.rejected;
}

compilation.assets[name] = new webpackSources.ConcatSource(purged.css);
});
});
callback();
Expand Down
23 changes: 22 additions & 1 deletion packages/purgecss-webpack-plugin/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ let webpackVersion = 4

const styleExtensions = ['.css', '.scss', '.styl', '.sass', '.less']
const pluginName = 'PurgeCSS'
const purgedStats = {}

export default class PurgecssPlugin {
constructor(options) {
Expand All @@ -23,10 +24,23 @@ export default class PurgecssPlugin {
compiler.hooks.compilation.tap(pluginName, compilation => {
this.initializePlugin(compilation)
})
compiler.hooks.done.tapAsync(pluginName, (stats, cb) => {
this.addStats(stats);
cb();
})
} else {
compiler.plugin('this-compilation', compilation => {
this.initializePlugin(compilation)
})
compiler.plugin('done', stats => {
this.addStats(stats)
})
}
}

addStats(stats) {
if (this.options.rejected) {
stats.purged = purgedStats
}
}

Expand Down Expand Up @@ -100,8 +114,15 @@ export default class PurgecssPlugin {
if (typeof options.whitelistPatternsChildren === 'function') {
options.whitelistPatternsChildren = options.whitelistPatternsChildren()
}

const purgecss = new Purgecss(options)
compilation.assets[name] = new ConcatSource(purgecss.purge()[0].css)
const purged = purgecss.purge()[0];

if (purged.rejected) {
purgedStats[name] = purged.rejected;
}

compilation.assets[name] = new ConcatSource(purged.css)
})
})

Expand Down

0 comments on commit 261c677

Please sign in to comment.