Skip to content

Commit

Permalink
Accept globs for css parameter (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-ketch authored May 21, 2017
1 parent 5f0a56d commit 243523f
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 29 deletions.
9 changes: 0 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,3 @@ Metalsmith()

Note that if you'd like to overwrite the original CSS file, set the `output`
name to match the `css` filename.

---

## Limitations

- The plugin currently only accepts one CSS file as the input, such as a
compiled Sass stylesheet.
- PurifyCSS does not seem to prune HTML tags (`h1`, `p`, etc.), but works
great for CSS selectors such as classes and IDs.
4 changes: 4 additions & 0 deletions __tests__/fixtures/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@ <h1>This is a tag</h1>
<p class="className">Testing with a classname</p>

<p class="unusual(class)">This should remain</p>

<div class="multipleCSSfiles">This should remain if using style2.css</div>

<span>This should remain if using style2.css</span>
</body>
</html>
3 changes: 3 additions & 0 deletions __tests__/fixtures/src/style3.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
span {
margin: 10px;
}
3 changes: 3 additions & 0 deletions __tests__/fixtures/src/styles2.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.multipleCSSfiles {
border: 1px solid hotpink;
}
27 changes: 26 additions & 1 deletion __tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,29 @@ test('whitelist option is respected', () => {
return build(options).then((files) => {
expect(files['styles-cleaned.css'].contents.toString()).toContain('#whitelisted');
})
});
});

test('an array of stylesheets is accepted', () => {
const options = {
content: ['*.html', '*.js'],
css: ['styles.css', 'styles2.css'],
output: 'styles-cleaned.css',
};

return build(options).then((files) => {
expect(files['styles-cleaned.css'].contents.toString()).toContain('.multipleCSSfiles');
})
});


test('a glob is accepted for CSS parameter', () => {
const options = {
content: ['*.html', '*.js'],
css: ['*.css'],
output: 'styles-cleaned.css',
};

return build(options).then((files) => {
expect(files['styles-cleaned.css'].contents.toString()).toContain('span');
})
});
36 changes: 19 additions & 17 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,26 @@ const plugin = (options) => {
delete options.output;

return function(files, metalsmith, done) {
// Stringify HTML & other structural markup
const fileNames = Object.keys(files);
const filteredFiles = [...content].reduce((filteredFiles, filename) => {
return filteredFiles.concat(
minimatch.match(fileNames, filename, { matchBase: true })
);
}, []);

const structure = [...filteredFiles].reduce((structure, filename) => {
structure.push(files[filename].contents.toString());
return structure;
}, []).toString();

// Stringify CSS
const styles = [...css].reduce((styles, filename) => {
styles.push(files[filename].contents.toString());
return styles;
}, []).toString();

const filterFiles = (targetFiles) => {
return [...targetFiles].reduce((matchedFiles, filename) => {
return matchedFiles.concat(
minimatch.match(fileNames, filename, { matchBase: true })
);
}, []);
};

const concatContents = (targetFiles) => {
return [...targetFiles].reduce((contents, filename) => {
contents.push(files[filename].contents.toString());
return contents;
}, []).toString();
}

// Stringify matched HTML & CSS file contents
const structure = concatContents(filterFiles(content));
const styles = concatContents(filterFiles(css));

// Pass code and CSS to Purify
purifyCSS(structure, styles, options, (results) => {
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "metalsmith-purifycss",
"version": "0.1.0",
"version": "0.2.0",
"description": "PurifyCSS plugin for Metalsmith static site generator",
"main": "lib/index.js",
"repository": "https://github.com/alex-ketch/metalsmith-purifycss",
Expand All @@ -23,6 +23,8 @@
]
},
"jest": {
"testMatch": ["**/__tests__/index.test.js"]
"testMatch": [
"**/__tests__/index.test.js"
]
}
}

0 comments on commit 243523f

Please sign in to comment.