Skip to content

Commit

Permalink
feat(purgecss-webpack-plugin): load config file automatically #767
Browse files Browse the repository at this point in the history
  • Loading branch information
Ffloriel committed Feb 24, 2022
1 parent 7009294 commit 726faaa
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 23 deletions.
@@ -0,0 +1,22 @@
const path = require("path");
const glob = require("glob");

const customExtractor = (content) => {
const res = content.match(/[A-z0-9-:/]+/g) || [];
return res;
};

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

module.exports = {
paths: glob.sync(`${PATHS.src}/*`),
safelist: ["safelisted"],
extractors: [
{
extractor: customExtractor,
extensions: ["html", "js"],
},
],
}
@@ -1,17 +1,6 @@
const path = require("path");
const glob = require("glob");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const { PurgeCSSPlugin } = require("../../../src/");

const customExtractor = (content) => {
const res = content.match(/[A-z0-9-:/]+/g) || [];
return res;
};

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

module.exports = {
mode: "development",
devtool: "source-map",
Expand Down Expand Up @@ -41,15 +30,6 @@ module.exports = {
new MiniCssExtractPlugin({
filename: "[name].css",
}),
new PurgeCSSPlugin({
paths: glob.sync(`${PATHS.src}/*`),
safelist: ["safelisted"],
extractors: [
{
extractor: customExtractor,
extensions: ["html", "js"],
},
],
}),
new PurgeCSSPlugin({}),
],
};
16 changes: 14 additions & 2 deletions packages/purgecss-webpack-plugin/src/index.ts
Expand Up @@ -2,9 +2,9 @@ import * as fs from "fs";
import * as path from "path";
import {
PurgeCSS,
defaultOptions,
ResultPurge,
UserDefinedOptions as PurgeCSSUserDefinedOptions,
defaultOptions,
} from "purgecss";
import { Compilation, Compiler, sources } from "webpack";
import { PurgedStats, UserDefinedOptions } from "./types";
Expand Down Expand Up @@ -128,7 +128,19 @@ export class PurgeCSSPlugin {
}

initializePlugin(compilation: Compilation): void {
compilation.hooks.additionalAssets.tapPromise(pluginName, () => {
compilation.hooks.additionalAssets.tapPromise(pluginName, async () => {
let configFileOptions: UserDefinedOptions | undefined;
try {
const t = path.resolve(process.cwd(), "purgecss.config.js");
configFileOptions = await import(t);
} catch {
// no config file present
}
this.options = {
...(configFileOptions ? configFileOptions : {}),
...this.options,
};

const entryPaths =
typeof this.options.paths === "function"
? this.options.paths()
Expand Down

0 comments on commit 726faaa

Please sign in to comment.