Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(grunt-purgecss): Fix plugin not ouputting all files #723

Merged
merged 4 commits into from Jul 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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