Skip to content

Commit

Permalink
feat: webpack 5 compatibility
Browse files Browse the repository at this point in the history
- fix deprecation warning
- use types from webpack
- remove unused code
  • Loading branch information
Ffloriel committed Nov 15, 2020
1 parent 2ce212e commit f2cfcf5
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 213 deletions.
91 changes: 0 additions & 91 deletions packages/purgecss-webpack-plugin/__tests__/search.test.ts

This file was deleted.

3 changes: 1 addition & 2 deletions packages/purgecss-webpack-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,13 @@
},
"dependencies": {
"purgecss": "^3.0.0",
"webpack": "^4.42.1",
"webpack": "^5.4.0",
"webpack-sources": "^2.0.0"
},
"bugs": {
"url": "https://github.com/FullHuman/purgecss/issues"
},
"devDependencies": {
"@types/webpack": "^4.41.12",
"@types/webpack-sources": "^2.0.0",
"css-loader": "^5.0.0",
"mini-css-extract-plugin": "^1.2.0"
Expand Down
105 changes: 48 additions & 57 deletions packages/purgecss-webpack-plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
import * as fs from "fs";
import path from "path";
import PurgeCSS, { defaultOptions } from "purgecss";
import { ConcatSource } from "webpack-sources";
import * as search from "./search";
import { File, UserDefinedOptions, PurgedStats, PurgeAsset } from "./types";
import { UserDefinedOptions, PurgedStats } from "./types";

import { Compiler, Stats, compilation as compilationType } from "webpack";

type Compilation = compilationType.Compilation;
import { Compiler, Compilation } from "webpack";

const styleExtensions = [".css", ".scss", ".styl", ".sass", ".less"];
const pluginName = "PurgeCSS";

/**
* Get the filename without ?hash
* @param fileName file name
*/
function getFormattedFilename(fileName: string): string {
if (fileName.includes("?")) {
return fileName.split("?").slice(0, -1).join("");
}
return fileName;
}

/**
* Returns true if the filename is of types of one of the specified extensions
* @param filename file name
* @param extensions extensions
*/
function isFileOfTypes(filename: string, extensions: string[]): boolean {
const extension = path.extname(getFormattedFilename(filename));
return extensions.includes(extension);
}

export default class PurgeCSSPlugin {
options: UserDefinedOptions;
purgedStats: PurgedStats = {};
Expand All @@ -20,40 +39,10 @@ export default class PurgeCSSPlugin {
}

apply(compiler: Compiler): void {
compiler.hooks.compilation.tap(pluginName, (compilation: Compilation) => {
this.initializePlugin(compilation);
});
compiler.hooks.done.tap(pluginName, this.onHooksDone.bind(this));
}

onHooksDone(stats: Stats): void {
if (stats.hasErrors()) {
if (this.options.verbose) {
console.warn("purge-webpack-plugin: pausing due to webpack errors");
}
return;
}

if (this.options.rejected) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
stats["purged"] = this.purgedStats;
}
}

getAssetsToPurge(
assetsFromCompilation: PurgeAsset[],
files: string[]
): PurgeAsset[] {
return assetsFromCompilation.filter((asset) => {
if (this.options.only) {
return this.options.only.some((only) => {
return asset && asset.name.includes(only);
});
} else {
return asset && files.includes(asset.name);
}
});
compiler.hooks.compilation.tap(
pluginName,
this.initializePlugin.bind(this)
);
}

initializePlugin(compilation: Compilation): void {
Expand All @@ -75,24 +64,24 @@ export default class PurgeCSSPlugin {
compilation: Compilation,
entryPaths: string[]
): Promise<void> {
const assetsFromCompilation = search.getAssets(compilation.assets, [
".css",
]);
const assetsFromCompilation = Object.entries(compilation.assets).filter(
([name]) => {
return isFileOfTypes(name, [".css"]);
}
);

for (const chunk of compilation.chunks) {
const { files } = chunk;
const assetsToPurge = this.getAssetsToPurge(assetsFromCompilation, files);

for (const { name, asset } of assetsToPurge) {
const filesToSearch = entryPaths
.concat(
search.files(
chunk,
this.options.moduleExtensions || [],
(file: File) => file.resource
)
)
.filter((v) => !styleExtensions.some((ext) => v.endsWith(ext)));
const assetsToPurge = assetsFromCompilation.filter(([name]) => {
if (this.options.only) {
return this.options.only.some((only) => name.includes(only));
}
return chunk.files.has(name);
});

for (const [name, asset] of assetsToPurge) {
const filesToSearch = entryPaths.filter(
(v) => !styleExtensions.some((ext) => v.endsWith(ext))
);

// Compile through Purgecss and attach to output.
// This loses sourcemaps should there be any!
Expand All @@ -102,7 +91,7 @@ export default class PurgeCSSPlugin {
content: filesToSearch,
css: [
{
raw: asset.source(),
raw: asset.source().toString(),
},
],
};
Expand All @@ -129,7 +118,9 @@ export default class PurgeCSSPlugin {
this.purgedStats[name] = purged.rejected;
}

compilation.assets[name] = new ConcatSource(purged.css);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
compilation.updateAsset(name, new ConcatSource(purged.css));
}
}
}
Expand Down
63 changes: 0 additions & 63 deletions packages/purgecss-webpack-plugin/src/search.ts

This file was deleted.

0 comments on commit f2cfcf5

Please sign in to comment.