forked from FullHuman/purgecss
/
index.ts
50 lines (44 loc) · 1.46 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
import PurgeCSS, { defaultOptions } from "purgecss";
import { UserDefinedOptions } from "./../../purgecss/src/types/index";
function getAvailableFiles(
grunt: IGrunt,
files: string[] | undefined = []
): string[] {
return files.filter((filepath) => {
// Warn on and remove invalid source files (if nonull was set).
if (!grunt.file.exists(filepath)) {
grunt.log.warn(`Source file "${filepath}" not found.`);
return false;
}
return true;
});
}
function gruntPurgeCSS(grunt: IGrunt): void {
grunt.registerMultiTask("purgecss", "Grunt plugin for PurgeCSS", function () {
const done = this.async();
const options = this.options<UserDefinedOptions>(defaultOptions);
const promisedPurgedFiles = [];
for (const file of this.files) {
const source = getAvailableFiles(grunt, file.src);
const purgedCss = new PurgeCSS()
.purge({
...options,
css: source,
})
.then((purgeCSSResults) => {
if (typeof file.dest === "undefined") {
throw new Error(`Destination file not found`);
}
grunt.file.write(file.dest, purgeCSSResults[0].css);
// Print a success message
grunt.log.writeln(`File "${file.dest}" created.`);
})
promisedPurgedFiles.push(purgedCss);
}
Promise.all(promisedPurgedFiles)
.then(() => {
done();
}).catch(() => done(false))
});
}
export default gruntPurgeCSS;