-
-
Notifications
You must be signed in to change notification settings - Fork 247
/
index.ts
144 lines (124 loc) · 4.27 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import * as fs from "fs";
import PurgeCSS, { defaultOptions } from "purgecss";
import { ConcatSource } from "webpack-sources";
import * as search from "./search";
import { File, UserDefinedOptions, PurgedStats, PurgeAsset } from "./types";
import { Compiler, Stats, compilation as compilationType } from "webpack";
type Compilation = compilationType.Compilation;
const styleExtensions = [".css", ".scss", ".styl", ".sass", ".less"];
const pluginName = "PurgeCSS";
export default class PurgeCSSPlugin {
options: UserDefinedOptions;
purgedStats: PurgedStats = {};
constructor(options: UserDefinedOptions) {
this.options = options;
}
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-ignore
// @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);
}
});
}
initializePlugin(compilation: Compilation): void {
compilation.hooks.additionalAssets.tapPromise(pluginName, () => {
const entryPaths =
typeof this.options.paths === "function"
? this.options.paths()
: this.options.paths;
entryPaths.forEach((p) => {
if (!fs.existsSync(p)) throw new Error(`Path ${p} does not exist.`);
});
return this.runPluginHook(compilation, entryPaths);
});
}
async runPluginHook(
compilation: Compilation,
entryPaths: string[]
): Promise<void> {
const assetsFromCompilation = search.getAssets(compilation.assets, [
".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)));
// Compile through Purgecss and attach to output.
// This loses sourcemaps should there be any!
const options = {
...defaultOptions,
...this.options,
content: filesToSearch,
css: [
{
raw: asset.source(),
},
],
};
if (typeof options.whitelist === "function") {
options.whitelist = options.whitelist();
}
if (typeof options.whitelistPatterns === "function") {
options.whitelistPatterns = options.whitelistPatterns();
}
if (typeof options.whitelistPatternsChildren === "function") {
options.whitelistPatternsChildren = options.whitelistPatternsChildren();
}
const purgecss = await new PurgeCSS().purge({
content: options.content,
css: options.css,
defaultExtractor: options.defaultExtractor,
extractors: options.extractors,
fontFace: options.fontFace,
keyframes: options.keyframes,
output: options.output,
rejected: options.rejected,
variables: options.variables,
whitelist: options.whitelist,
whitelistPatterns: options.whitelistPatterns,
whitelistPatternsChildren: options.whitelistPatternsChildren,
});
const purged = purgecss[0];
if (purged.rejected) {
this.purgedStats[name] = purged.rejected;
}
compilation.assets[name] = new ConcatSource(purged.css);
}
}
}
}