Skip to content
This repository was archived by the owner on Jun 24, 2024. It is now read-only.

Commit 3d67933

Browse files
authored
feat: Add minify option like js-reader (#35)
* Update package-lock.json * add needed deps * implement cssnano and autoprefixer on the css feed * add tests and docs * elaborate on the api docs * add snapshot test to reduce ambiguity on minify test
1 parent ba7cc65 commit 3d67933

File tree

7 files changed

+1149
-88
lines changed

7 files changed

+1149
-88
lines changed

README.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,19 @@ Example
8585
const bundle = await cssReader([feed, ...feeds])
8686
```
8787

88-
Returns: `Promise<string>` - A promise that resolves to a bundle string.
88+
This module has the following API:
89+
90+
### function(feeds, [options])
91+
92+
Supported arguments are:
93+
94+
* `feeds` - Array - An Array of feeds.
95+
* `options` - Object - configuration.
96+
* `options.minify` (default: false) Specify whether to minify code using [cssnano](https://cssnano.co/).
97+
98+
Returns: `Promise<string>` - A promise that resolves to a bundle string that is processed by [autoprefixer](https://github.com/postcss/autoprefixer).
99+
100+
Remember to specify a [browserslist](https://github.com/browserslist/browserslist) in your project to ensure cssnano and autoprefixer support the browsers your users use.
89101

90102
## Contributing
91103

lib/reader.js

+20-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
const assert = require('assert');
44
const { EOL } = require('os');
5+
const postcss = require('postcss');
6+
const autoprefixer = require('autoprefixer');
7+
const cssnano = require('cssnano');
58

6-
module.exports = async function reader(feeds = []) {
9+
module.exports = async function reader(feeds = [], options = {}) {
710
assert(
811
feeds.length,
912
`Expected at least 1 feed to be given. Instead got "${feeds.length}"`
@@ -15,6 +18,11 @@ module.exports = async function reader(feeds = []) {
1518

1619
feeds = [].concat(...feeds);
1720

21+
const opts = {
22+
minify: false,
23+
...options,
24+
};
25+
1826
const feedMap = new Map();
1927
feeds.forEach(feed => {
2028
// because feedMap preserves order, when we get a duplicate we actually
@@ -25,7 +33,17 @@ module.exports = async function reader(feeds = []) {
2533
});
2634

2735
// backwards compatible refactor to `.source` from `.content`
28-
return Array.from(feedMap.values())
36+
const precss = Array.from(feedMap.values())
2937
.map(feed => (feed.source || feed.content).trim())
3038
.join(EOL + EOL);
39+
40+
const processor = postcss(autoprefixer());
41+
42+
if (opts.minify) {
43+
processor.use(cssnano());
44+
}
45+
46+
const { css } = await processor.process(precss, { map: false });
47+
48+
return css;
3149
};

0 commit comments

Comments
 (0)