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
Closed
17 changes: 17 additions & 0 deletions packages/purgecss-webpack-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,23 @@ 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]'
}


```ts
// Output the rejected css as `[base]-rejected.[ext]`
new PurgeCSSPlugin({
rejectedCss: true,
});

// or use a custom output path
new PurgeCSSPlugin({
rejectedCss: '[base].rejected.css',
});
```
Generate an output file containing the rejected css.
If the value is a string it will be used as a Webpack [template string](https://webpack.js.org/configuration/output/#template-strings) to generate the filename.
If `true` the template path will default to `[base]-rejected.[ext]`. So 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@


.rejected {
color: blue;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>

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

<body>
<div class="preserved"></div>
</body>

</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "./style.css";
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

.preserved {
color: red;
}

.rejected {
color: blue;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const path = require("path");
const glob = require("glob");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const PurgecssPlugin = require("../../../src").default;

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

module.exports = {
mode: "development",
entry: "./src/index.js",
context: path.resolve(__dirname),
optimization: {
splitChunks: {
cacheGroups: {
styles: {
name: "styles",
type: "css/mini-extract",
test: /\.css$/,
chunks: "all",
enforce: true,
},
},
},
},
module: {
rules: [
{
test: /\.css$/,
use: [MiniCssExtractPlugin.loader, "css-loader"],
},
],
},
plugins: [
new MiniCssExtractPlugin({
filename: "[name].css",
}),
new PurgecssPlugin({
paths: () => glob.sync(`${PATHS.src}/*`),
rejectedCss: "[base].rejected.css",
}),
],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@


.rejected {
color: blue;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>

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

<body>
<div class="preserved"></div>
</body>

</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "./style.css";
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

.preserved {
color: red;
}

.rejected {
color: blue;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const path = require("path");
const glob = require("glob");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const PurgecssPlugin = require("../../../src").default;

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

module.exports = {
mode: "development",
entry: "./src/index.js",
context: path.resolve(__dirname),
optimization: {
splitChunks: {
cacheGroups: {
styles: {
name: "styles",
type: "css/mini-extract",
test: /\.css$/,
chunks: "all",
enforce: true,
},
},
},
},
module: {
rules: [
{
test: /\.css$/,
use: [MiniCssExtractPlugin.loader, "css-loader"],
},
],
},
plugins: [
new MiniCssExtractPlugin({
filename: "[name].css",
}),
new PurgecssPlugin({
paths: () => glob.sync(`${PATHS.src}/*`),
rejectedCss: true,
}),
],
};
2 changes: 2 additions & 0 deletions packages/purgecss-webpack-plugin/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ describe("Webpack integration", () => {
"path-and-safelist-functions",
"simple",
"simple-with-exclusion",
"rejected-css",
"rejected-css-template-name",
];

for (const testCase of cases) {
Expand Down
18 changes: 18 additions & 0 deletions 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 @@ -128,6 +129,23 @@ export default class PurgeCSSPlugin {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
compilation.updateAsset(name, new ConcatSource(purged.css));
if (purged.rejectedCss !== undefined) {
const rejectedCssSource = new ConcatSource(purged.rejectedCss);
const rejectedNameTemplate = typeof options.rejectedCss === "string" ? options.rejectedCss : "[base]-rejected.[ext]";
const rejectedName = compilation.getPath(rejectedNameTemplate, {
basename: path.basename(name, path.extname(name)),
filename: name,
});
if (compilation.getAsset(rejectedName) === undefined) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
compilation.emitAsset(rejectedName, rejectedCssSource);
} else {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
compilation.updateAsset(rejectedName, rejectedCssSource);
}
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/purgecss-webpack-plugin/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface UserDefinedOptions {
moduleExtensions?: string[];
output?: string;
rejected?: boolean;
rejectedCss?: boolean | string;
stdin?: boolean;
stdout?: boolean;
variables?: boolean;
Expand Down