UNPKG

sheets-registry-transformer

Version:
33 lines (28 loc) 984 B
const stream = require('stream'); module.exports = sheetsRegisryTransformer = sheetsRegistry => { const cached = {}; function getFreshStyles() { const freshStyles = []; const registry = sheetsRegistry ? sheetsRegistry.sheetsRegistry.registry : []; registry.forEach(member => { const key = member.toString(); if (!cached[key]) { cached[key] = true; freshStyles.push(key); return; } }); return freshStyles; } return new stream.Transform({ transform: function appendStyleChunks(chunk, encoding, callback) { const subsheet = getFreshStyles().join(''); if (!!subsheet) { this.push(Buffer.from(`<style type="text/css">${subsheet}</style>${chunk.toString()}`)); } else { this.push(Buffer.from(chunk.toString())); } callback(); } }); }