Skip to content

Commit 726faaa

Browse files
committed
feat(purgecss-webpack-plugin): load config file automatically #767
1 parent 7009294 commit 726faaa

File tree

3 files changed

+37
-23
lines changed

3 files changed

+37
-23
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const path = require("path");
2+
const glob = require("glob");
3+
4+
const customExtractor = (content) => {
5+
const res = content.match(/[A-z0-9-:/]+/g) || [];
6+
return res;
7+
};
8+
9+
const PATHS = {
10+
src: path.join(__dirname, "src"),
11+
};
12+
13+
module.exports = {
14+
paths: glob.sync(`${PATHS.src}/*`),
15+
safelist: ["safelisted"],
16+
extractors: [
17+
{
18+
extractor: customExtractor,
19+
extensions: ["html", "js"],
20+
},
21+
],
22+
}

packages/purgecss-webpack-plugin/__tests__/cases/simple/webpack.config.js

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
1-
const path = require("path");
2-
const glob = require("glob");
31
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
42
const { PurgeCSSPlugin } = require("../../../src/");
53

6-
const customExtractor = (content) => {
7-
const res = content.match(/[A-z0-9-:/]+/g) || [];
8-
return res;
9-
};
10-
11-
const PATHS = {
12-
src: path.join(__dirname, "src"),
13-
};
14-
154
module.exports = {
165
mode: "development",
176
devtool: "source-map",
@@ -41,15 +30,6 @@ module.exports = {
4130
new MiniCssExtractPlugin({
4231
filename: "[name].css",
4332
}),
44-
new PurgeCSSPlugin({
45-
paths: glob.sync(`${PATHS.src}/*`),
46-
safelist: ["safelisted"],
47-
extractors: [
48-
{
49-
extractor: customExtractor,
50-
extensions: ["html", "js"],
51-
},
52-
],
53-
}),
33+
new PurgeCSSPlugin({}),
5434
],
5535
};

packages/purgecss-webpack-plugin/src/index.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import * as fs from "fs";
22
import * as path from "path";
33
import {
44
PurgeCSS,
5-
defaultOptions,
65
ResultPurge,
76
UserDefinedOptions as PurgeCSSUserDefinedOptions,
7+
defaultOptions,
88
} from "purgecss";
99
import { Compilation, Compiler, sources } from "webpack";
1010
import { PurgedStats, UserDefinedOptions } from "./types";
@@ -128,7 +128,19 @@ export class PurgeCSSPlugin {
128128
}
129129

130130
initializePlugin(compilation: Compilation): void {
131-
compilation.hooks.additionalAssets.tapPromise(pluginName, () => {
131+
compilation.hooks.additionalAssets.tapPromise(pluginName, async () => {
132+
let configFileOptions: UserDefinedOptions | undefined;
133+
try {
134+
const t = path.resolve(process.cwd(), "purgecss.config.js");
135+
configFileOptions = await import(t);
136+
} catch {
137+
// no config file present
138+
}
139+
this.options = {
140+
...(configFileOptions ? configFileOptions : {}),
141+
...this.options,
142+
};
143+
132144
const entryPaths =
133145
typeof this.options.paths === "function"
134146
? this.options.paths()

0 commit comments

Comments
 (0)