Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recover reject css with webpack plugin #819

Closed
4 changes: 4 additions & 0 deletions packages/purgecss-webpack-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ new PurgeCSSPlugin({

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

* #### rejectedCss
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how complicated it would be, but I imagine it would be more webpack friendly if it uses their template string configuration options.

So it would allow for something like:

const options = {
  output: '[name].rejected.[ext]'
}


If `true` generate another output file based on output name containing rejected css.<br> For an entry point named `foo`, the purged file will be named `foo.css` and the rejected one `foo-rejected.css`

## 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
13 changes: 12 additions & 1 deletion packages/purgecss-webpack-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export default class PurgeCSSPlugin {
keyframes: options.keyframes,
output: options.output,
rejected: options.rejected,
rejectedCss: options.rejectedCss,
variables: options.variables,
safelist: options.safelist,
blocklist: options.blocklist,
Expand All @@ -127,7 +128,17 @@ export default class PurgeCSSPlugin {

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
compilation.updateAsset(name, new ConcatSource(purged.css));
let source: Source = new ConcatSource(purged.css)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason why you are re-using the source variable? I would suggest something more in the original style like:

compilation.updateAsset(name, new ConcatSource(purged.css));
if (purged.rejectedCss !== undefined) {
  const rejectedName = path.dirname(name) + '/' + path.basename(name, '.css') + '-rejected' + path.extname(name);
  const source = new ConcatSource(purged.rejectedCss);
  if (compilation.getAsset(rejectedName) {
    compilation.emitAsset(rejectedName, source);  
  } else {
   compilation.updateAsset(rejectedName, source);
  }
}

Some minor nitpick would be to maintain consistent with use of semi-colons in the file.

compilation.updateAsset(name, source);
if (purged.rejectedCss !== undefined) {
source = new ConcatSource(purged.rejectedCss)
const rejectedName: string = path.dirname(name) + '/' + path.basename(name, '.css') + '-rejected' + path.extname(name)
if (compilation.getAsset(rejectedName) === undefined) {
compilation.emitAsset(rejectedName, source);
} else {
compilation.updateAsset(rejectedName, source);
}
}
}
}
}
Expand Down