|
1 | 1 | # asset-pipe-css-reader
|
2 | 2 |
|
| 3 | +A module that takes any number of css feed streams (provided by asset-pipe sinks) and bundles them into a readable stream of css content. |
| 4 | + |
| 5 | +This is an internal module intended for use by other modules in the [asset-pipe project](https://github.com/asset-pipe). |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +Given any number of css feed streams, the reader will: |
| 10 | +1. Merge streams into a single stream |
| 11 | +2. Dedupe any items with identical id hashes keeping the last occurrence |
| 12 | +3. Ensure order of streams given is preserved for the final output CSS |
| 13 | + |
| 14 | +### Input data format |
| 15 | + |
| 16 | +The following is an example of a feed file: |
| 17 | +(Something like /path/to/feeds/feed-a.json) |
| 18 | + |
| 19 | +```js |
| 20 | +[ |
| 21 | + { |
| 22 | + // Unique id for entry. Created by hashing together name, version and file |
| 23 | + id: '4f32a8e1c6cf6e5885241f3ea5fee583560b2dfde38b21ec3f9781c91d58f42e', |
| 24 | + name: 'my-module-1', |
| 25 | + version: '1.0.1', |
| 26 | + file: 'my-module-1/main.css', |
| 27 | + // bundled css content with any @import statements inlined |
| 28 | + content: '/* ... */' |
| 29 | + } |
| 30 | +] |
| 31 | +``` |
| 32 | + |
| 33 | +## Installation |
| 34 | + |
| 35 | +```bash |
| 36 | +npm install asset-pipe-css-reader |
| 37 | +``` |
| 38 | + |
| 39 | +## Usage |
| 40 | + |
| 41 | +### Require the reader |
| 42 | +```js |
| 43 | +const CssReader = require('asset-pipe-css-reader') |
| 44 | +``` |
| 45 | + |
| 46 | +### Instantiating the reader |
| 47 | + |
| 48 | +Either pass a single stream created by an asset-pipe sink: |
| 49 | +```js |
| 50 | +const sink = new SinkFs({ |
| 51 | + path: '/path/to/css/feeds' |
| 52 | +}); |
| 53 | +const feedStream = sink.reader('feed-a.json'); |
| 54 | +const reader = new CssReader(feedStream) |
| 55 | +``` |
| 56 | + |
| 57 | +Or pass an array of streams: |
| 58 | +```js |
| 59 | +const sink = new SinkFs({ |
| 60 | + path: '/path/to/css/feeds' |
| 61 | +}); |
| 62 | +const feedStream1 = sink.reader('feed-a.json'); |
| 63 | +const feedStream2 = sink.reader('feed-b.json'); |
| 64 | +const reader = new CssReader([feedStream1, feedStream2]) |
| 65 | +``` |
| 66 | + |
| 67 | +### Consuming content from the reader |
| 68 | + |
| 69 | +You should wait for the reader to become ready by listening for the `pipeline ready` event. |
| 70 | + |
| 71 | +The reader is a readable stream so in order to access the data you may register a data handler |
| 72 | +and listen for chunks to be passed to the handler: |
| 73 | +```js |
| 74 | +reader.on('pipeline ready', () => { |
| 75 | + reader.on('data', data => { |
| 76 | + // .. |
| 77 | + }) |
| 78 | +}) |
| 79 | +``` |
| 80 | + |
| 81 | +You might also pipe the reader into a writeable or transform stream: |
| 82 | +```js |
| 83 | +const { writeFile } = require('fs') |
| 84 | +const consumer = writeFile('/path/to/save/file') |
| 85 | + |
| 86 | +reader.on('pipeline ready', () => { |
| 87 | + reader.pipe(consumer) |
| 88 | +}) |
| 89 | +``` |
| 90 | + |
| 91 | +## API |
| 92 | + |
| 93 | +### Methods |
| 94 | + |
| 95 | +#### constructor |
| 96 | + |
| 97 | +Constructor takes a single stream or array of streams. Streams should be produced with an asset-pipe sink such as: |
| 98 | +- [asset-pipe-sink-s3](https://github.com/asset-pipe/asset-pipe-sink-s3`) |
| 99 | +- [asset-pipe-sink-fs](https://github.com/asset-pipe/asset-pipe-sink-fs`) |
| 100 | +- [asset-pipe-sink-gcs](https://github.com/asset-pipe/asset-pipe-sink-gcs`) |
| 101 | +- [asset-pipe-sink-mem](https://github.com/asset-pipe/asset-pipe-sink-mem`) |
| 102 | + |
| 103 | +Examples |
| 104 | +```js |
| 105 | +new CssReader(stream) |
| 106 | +``` |
| 107 | +```js |
| 108 | +new CssReader([stream, ...stream]) |
| 109 | +``` |
| 110 | + |
| 111 | +Returns: `Readable Stream` |
| 112 | + |
| 113 | +### Events |
| 114 | + |
| 115 | +#### file found |
| 116 | + |
| 117 | +Event produced whenever an underlying feed stream successfully reads its feed |
| 118 | +file from disk |
| 119 | + |
| 120 | +```js |
| 121 | +cssReader.on('file found', file => {}) |
| 122 | +``` |
| 123 | + |
| 124 | +Param: `file`, name of the file given to the feed stream to read from |
| 125 | + |
| 126 | +#### file not found |
| 127 | + |
| 128 | +Event produced whenever an underlying feed stream is unable to read its feed |
| 129 | +file from disk |
| 130 | + |
| 131 | +```js |
| 132 | +cssReader.on('file not found', file => {}) |
| 133 | +``` |
| 134 | + |
| 135 | +Param: `file`, name of the file given to the feed stream to read from |
| 136 | + |
| 137 | +#### pipeline ready |
| 138 | + |
| 139 | +Event produced once all feed file streams have been successfully merged into |
| 140 | +a pipeline |
| 141 | + |
| 142 | +```js |
| 143 | +cssReader.on('pipeline ready', () => {}) |
| 144 | +``` |
| 145 | + |
| 146 | +#### data |
| 147 | + |
| 148 | +Event that emits chunks of CSS content to a consumer |
| 149 | + |
| 150 | +```js |
| 151 | +cssReader.on('data', chunk => {}) |
| 152 | +``` |
| 153 | + |
| 154 | +Param: `chunk`, a piece of CSS text |
| 155 | + |
| 156 | +#### error |
| 157 | + |
| 158 | +Event produced whenever any of the various stages of the pipeline emit errors |
| 159 | + |
| 160 | +```js |
| 161 | +cssReader.on('error', err => {}) |
| 162 | +``` |
| 163 | + |
| 164 | +Param: `err`, Error forwarded from merged streams or otherwise emitted from the pipeline |
0 commit comments