Skip to content

Commit 646e419

Browse files
authored
fix(grunt-purgecss): Fix plugin not ouputting all files (#723)
* Add additional .css files * Add failling test: grunt not processing all files * Wait for all files being purged before returning the result * Manual code styling
1 parent 59995f7 commit 646e419

File tree

12 files changed

+40
-11
lines changed

12 files changed

+40
-11
lines changed

packages/grunt-purgecss/Gruntfile.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ module.exports = grunt => {
99
content: ['./__tests__/fixtures/src/simple/**/*.html']
1010
},
1111
files: {
12+
'__tests__/tmp/menu.css': ['__tests__/fixtures/src/menu.css'],
13+
'__tests__/tmp/profile.css': ['__tests__/fixtures/src/profile.css'],
14+
'__tests__/tmp/footer.css': ['__tests__/fixtures/src/footer.css'],
1215
'__tests__/tmp/simple.css': ['__tests__/fixtures/src/simple/simple.css']
1316
}
1417
}
@@ -21,4 +24,4 @@ module.exports = grunt => {
2124
// By default, lint and run all tests.
2225
grunt.registerTask('default', ['purgecss']);
2326

24-
};
27+
};

packages/grunt-purgecss/__tests__/fixtures/expected/footer.css

Whitespace-only changes.

packages/grunt-purgecss/__tests__/fixtures/expected/menu.css

Whitespace-only changes.

packages/grunt-purgecss/__tests__/fixtures/expected/profile.css

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.footer-unused-class {
2+
background: black;
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.menu-unused-class {
2+
background: red;
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.profile-unused-class {
2+
background: hotpink;
3+
}
Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { execSync } from "child_process";
22
import fs from "fs";
3+
import path from "path";
34

45
describe("Purgecss grunt plugin", () => {
56
const cwd = process.cwd();
@@ -9,18 +10,31 @@ describe("Purgecss grunt plugin", () => {
910
execSync("npx grunt");
1011
});
1112

13+
function emptyFolder(directory: string) {
14+
fs.readdir(directory, (err, files) => {
15+
if (err) throw err;
16+
17+
for (const file of files) {
18+
fs.unlink(path.join(directory, file), err => {
19+
if (err) throw err;
20+
});
21+
}
22+
});
23+
}
24+
1225
afterAll(() => {
26+
emptyFolder(`${__dirname}/tmp`);
1327
process.chdir(cwd);
1428
});
1529

16-
const files = ["simple.css"];
30+
const files = ["simple.css", "footer.css", "menu.css", "profile.css"];
1731
for (const file of files) {
1832
it(`remove unused css successfully: ${file}`, () => {
1933
const actual = fs.readFileSync(`${__dirname}/tmp/${file}`).toString();
2034
const expected = fs
2135
.readFileSync(`${__dirname}/fixtures/expected/${file}`)
2236
.toString();
23-
expect(actual).toBe(expected);
37+
expect(actual.replace(/\s/g, '')).toBe(expected.replace(/\s/g, ''));
2438
});
2539
}
2640
});

packages/grunt-purgecss/src/index.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ function gruntPurgeCSS(grunt: IGrunt): void {
1919
grunt.registerMultiTask("purgecss", "Grunt plugin for PurgeCSS", function () {
2020
const done = this.async();
2121
const options = this.options<UserDefinedOptions>(defaultOptions);
22+
const promisedPurgedFiles = [];
2223
for (const file of this.files) {
2324
const source = getAvailableFiles(grunt, file.src);
24-
new PurgeCSS()
25+
const purgedCss = new PurgeCSS()
2526
.purge({
2627
...options,
2728
css: source,
@@ -34,12 +35,15 @@ function gruntPurgeCSS(grunt: IGrunt): void {
3435
grunt.file.write(file.dest, purgeCSSResults[0].css);
3536
// Print a success message
3637
grunt.log.writeln(`File "${file.dest}" created.`);
37-
done();
3838
})
39-
.catch(() => {
40-
done(false);
41-
});
39+
40+
promisedPurgedFiles.push(purgedCss);
4241
}
42+
Promise.all(promisedPurgedFiles)
43+
.then(() => {
44+
done();
45+
}).catch(() => done(false))
46+
4347
});
4448
}
4549

packages/grunt-purgecss/tasks/purgecss.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)