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

Commit

Permalink
feat: Add minify option like js-reader (#35)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
stipsan authored Sep 24, 2019
1 parent ba7cc65 commit 3d67933
Show file tree
Hide file tree
Showing 7 changed files with 1,149 additions and 88 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,19 @@ Example
const bundle = await cssReader([feed, ...feeds])
```

Returns: `Promise<string>` - A promise that resolves to a bundle string.
This module has the following API:

### function(feeds, [options])

Supported arguments are:

* `feeds` - Array - An Array of feeds.
* `options` - Object - configuration.
* `options.minify` (default: false) Specify whether to minify code using [cssnano](https://cssnano.co/).

Returns: `Promise<string>` - A promise that resolves to a bundle string that is processed by [autoprefixer](https://github.com/postcss/autoprefixer).

Remember to specify a [browserslist](https://github.com/browserslist/browserslist) in your project to ensure cssnano and autoprefixer support the browsers your users use.

## Contributing

Expand Down
22 changes: 20 additions & 2 deletions lib/reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

const assert = require('assert');
const { EOL } = require('os');
const postcss = require('postcss');
const autoprefixer = require('autoprefixer');
const cssnano = require('cssnano');

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

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

const opts = {
minify: false,
...options,
};

const feedMap = new Map();
feeds.forEach(feed => {
// because feedMap preserves order, when we get a duplicate we actually
Expand All @@ -25,7 +33,17 @@ module.exports = async function reader(feeds = []) {
});

// backwards compatible refactor to `.source` from `.content`
return Array.from(feedMap.values())
const precss = Array.from(feedMap.values())
.map(feed => (feed.source || feed.content).trim())
.join(EOL + EOL);

const processor = postcss(autoprefixer());

if (opts.minify) {
processor.use(cssnano());
}

const { css } = await processor.process(precss, { map: false });

return css;
};
Loading

0 comments on commit 3d67933

Please sign in to comment.