Skip to content

Commit

Permalink
fix(grunt-purgecss): Fix plugin not ouputting all files (#723)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
vnctaing committed Jul 31, 2021
1 parent 59995f7 commit 646e419
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 11 deletions.
5 changes: 4 additions & 1 deletion packages/grunt-purgecss/Gruntfile.js
Expand Up @@ -9,6 +9,9 @@ module.exports = grunt => {
content: ['./__tests__/fixtures/src/simple/**/*.html']
},
files: {
'__tests__/tmp/menu.css': ['__tests__/fixtures/src/menu.css'],
'__tests__/tmp/profile.css': ['__tests__/fixtures/src/profile.css'],
'__tests__/tmp/footer.css': ['__tests__/fixtures/src/footer.css'],
'__tests__/tmp/simple.css': ['__tests__/fixtures/src/simple/simple.css']
}
}
Expand All @@ -21,4 +24,4 @@ module.exports = grunt => {
// By default, lint and run all tests.
grunt.registerTask('default', ['purgecss']);

};
};
Empty file.
Empty file.
Empty file.
3 changes: 3 additions & 0 deletions packages/grunt-purgecss/__tests__/fixtures/src/footer.css
@@ -0,0 +1,3 @@
.footer-unused-class {
background: black;
}
3 changes: 3 additions & 0 deletions packages/grunt-purgecss/__tests__/fixtures/src/menu.css
@@ -0,0 +1,3 @@
.menu-unused-class {
background: red;
}
3 changes: 3 additions & 0 deletions packages/grunt-purgecss/__tests__/fixtures/src/profile.css
@@ -0,0 +1,3 @@
.profile-unused-class {
background: hotpink;
}
18 changes: 16 additions & 2 deletions packages/grunt-purgecss/__tests__/index.test.ts
@@ -1,5 +1,6 @@
import { execSync } from "child_process";
import fs from "fs";
import path from "path";

describe("Purgecss grunt plugin", () => {
const cwd = process.cwd();
Expand All @@ -9,18 +10,31 @@ describe("Purgecss grunt plugin", () => {
execSync("npx grunt");
});

function emptyFolder(directory: string) {
fs.readdir(directory, (err, files) => {
if (err) throw err;

for (const file of files) {
fs.unlink(path.join(directory, file), err => {
if (err) throw err;
});
}
});
}

afterAll(() => {
emptyFolder(`${__dirname}/tmp`);
process.chdir(cwd);
});

const files = ["simple.css"];
const files = ["simple.css", "footer.css", "menu.css", "profile.css"];
for (const file of files) {
it(`remove unused css successfully: ${file}`, () => {
const actual = fs.readFileSync(`${__dirname}/tmp/${file}`).toString();
const expected = fs
.readFileSync(`${__dirname}/fixtures/expected/${file}`)
.toString();
expect(actual).toBe(expected);
expect(actual.replace(/\s/g, '')).toBe(expected.replace(/\s/g, ''));
});
}
});
14 changes: 9 additions & 5 deletions packages/grunt-purgecss/src/index.ts
Expand Up @@ -19,9 +19,10 @@ 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);
new PurgeCSS()
const purgedCss = new PurgeCSS()
.purge({
...options,
css: source,
Expand All @@ -34,12 +35,15 @@ function gruntPurgeCSS(grunt: IGrunt): void {
grunt.file.write(file.dest, purgeCSSResults[0].css);
// Print a success message
grunt.log.writeln(`File "${file.dest}" created.`);
done();
})
.catch(() => {
done(false);
});

promisedPurgedFiles.push(purgedCss);
}
Promise.all(promisedPurgedFiles)
.then(() => {
done();
}).catch(() => done(false))

});
}

Expand Down
2 changes: 1 addition & 1 deletion packages/grunt-purgecss/tasks/purgecss.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/purgecss/bin/purgecss.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion packages/purgecss/src/index.ts
Expand Up @@ -348,7 +348,6 @@ class PurgeCSS {
extractors: Extractors[]
): Promise<ExtractorResultSets> {
const selectors = new ExtractorResultSets([]);

for (const globfile of files) {
let filesNames: string[] = [];

Expand Down

0 comments on commit 646e419

Please sign in to comment.