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

Function for path to evaluate on each compilation #10

Merged
merged 2 commits into from
Jan 5, 2018
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
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ new PurgecssPlugin({
})
```

If you want to regenerate the paths list on every compilation (e.g. with `--watch`), then you can also pass a function:
```js
new PurgecssPlugin({
paths: () => glob.sync(`${PATHS.src}/*`)
})
```

* #### only

You can specify entrypoints to the purgecss-webpack-plugin with the option only:
Expand All @@ -88,6 +95,27 @@ new PurgecssPlugin({
})
```

* #### whitelist and whitelistPatterns

Similar as for the `paths` option, you also can define functions for the these options:

```js
function collectWhitelist() {
// do something to collect the whitelist
return ['whitelisted'];
}
function collectWhitelistPatterns() {
// do something to collect the whitelist
return [/^whitelisted-/];
}

// In the webpack configuration
new PurgecssPlugin({
whitelist: collectWhitelist,
whitelistPatterns: collectWhitelistPatterns
})
```

## Contributing

Please read [CONTRIBUTING.md](./.github/CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull requests to us.
Expand Down
15 changes: 15 additions & 0 deletions __tests__/__snapshots__/webpack-integration.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Webpack Integration Tests path-and-whitelist-functions 1`] = `
".hello {
color: red;
}

.whitelisted {
color: green;
}

md\\\\:w-2\\\\/3 {
color: red;
}
"
`;

exports[`Webpack Integration Tests simple 1`] = `
".hello {
color: red;
Expand Down
11 changes: 11 additions & 0 deletions __tests__/cases/path-and-whitelist-functions/expected/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.hello {
color: red;
}

.whitelisted {
color: green;
}

md\:w-2\/3 {
color: red;
}
13 changes: 13 additions & 0 deletions __tests__/cases/path-and-whitelist-functions/src/content.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<html>

<head>
<title>Purgecss webpack test</title>
</head>

<body>
<div class="md:w-2/3"></div>
<div class="hello"></div>
<div></div>
</body>

</html>
1 change: 1 addition & 0 deletions __tests__/cases/path-and-whitelist-functions/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './../style.css'
15 changes: 15 additions & 0 deletions __tests__/cases/path-and-whitelist-functions/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.hello {
color: red;
}

.unused {
color: blue;
}

.whitelisted {
color: green;
}

md\:w-2\/3 {
color: red;
}
41 changes: 41 additions & 0 deletions __tests__/cases/path-and-whitelist-functions/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const path = require('path')
const glob = require('glob')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const PurgecssPlugin = require('../../../src/').default

class CustomExtractor {
static extract(content) {
return content.match(/[A-z0-9-:/]+/g)
}
}

const PATHS = {
src: path.join(__dirname, 'src')
}

module.exports = {
entry: './src/index.js',
module: {
rules: [
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
use: 'css-loader?sourceMap'
})
}
]
},
plugins: [
new ExtractTextPlugin('style.css'),
new PurgecssPlugin({
paths: () => glob.sync(`${PATHS.src}/*`),
whitelist: () => ['whitelisted'],
extractors: [
{
extractor: CustomExtractor,
extensions: ['html', 'js']
}
]
})
]
}
11 changes: 9 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,19 @@ export default class PurgecssPlugin {

// Compile through Purgecss and attach to output.
// This loses sourcemaps should there be any!
const purgecss = new Purgecss({
const options = {
...this.options,
content: filesToSearch,
css: [asset.source()],
stdin: true
})
};
if (typeof options.whitelist === 'function') {
options.whitelist = options.whitelist();
}
if (typeof options.whitelistPatterns === 'function') {
options.whitelistPatterns = options.whitelistPatterns();
}
const purgecss = new Purgecss(options);
compilation.assets[name] = new ConcatSource(purgecss.purge()[0].css)
})
})
Expand Down
8 changes: 6 additions & 2 deletions src/parse.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
export const entryPaths = paths => {
const ret = paths || []
let ret = paths || []

if (typeof ret === 'function') {
ret = ret();
}

// Convert possible string to an array
if (typeof ret === 'string') {
return [ret]
ret = [ret]
}

return ret
Expand Down