public
Last active

Stylus compilation information exporter

  • Download Gist
gistfile1.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
var EventEmitter = require('events').EventEmitter;
 
/**
* Only works with https://github.com/tonistiigi/stylus/tree/sourcemaps
* Usage:
* var StylusInfo = require('./stylusinfo').StylusInfo;
* var logger = new StylusInfo();
* var options = ...
* options._info = logger;
* stylus(str, options).render(function(err, css) {
* console.log(logger.export());
* });
*/
 
function Selector(selectors, file, line) {
this.selectors = selectors;
this.file = file;
this.line = line;
}
 
function Call(pos, call, file, line) {
this.pos = pos;
this.call = call;
this.file = file;
this.line = line;
}
 
function Extend(pos, selector, file, line) {
this.pos = pos;
this.selector = selector;
this.file = file;
this.line = line;
}
 
function Ident(pos, ident, file, line) {
this.pos = pos;
this.ident = ident;
this.file = file;
this.line = line;
}
 
 
function StylusInfo() {
this.selectors = [];
this.calls = [];
this.extends = [];
this.idents = [];
this.files = [];
 
var self = this;
 
this.on('selector', function(selectors, file, line) {
self.selectors.push(new Selector(selectors, self.getFile(file), line));
});
 
var calls = {};
this.on('call', function(pos, call, file, line) {
file = self.getFile(file);
var key = pos + '::' + call;
if (!calls[key]) {
self.calls.push(new Call(pos, call, file, line));
calls[key] = true;
}
});
 
var _extends = {};
this.on('extend', function(pos, selector, file, line) {
file = self.getFile(file);
var key = pos + '::' + selector;
if (!_extends[key]) {
self.extends.push(new Extend(pos, selector, file, line));
_extends[key] = true;
}
});
 
var _idents = {};
this.on('ident', function(pos, ident, file, line) {
file = self.getFile(file);
var key = pos + '::' + ident;
if (!_idents[key]) {
self.idents.push(new Ident(pos, ident, file, line));
_idents[key] = true;
}
});
 
}
StylusInfo.prototype = new EventEmitter;
 
 
StylusInfo.prototype.getFile = function (path) {
var index = this.files.indexOf(path);
if (index === -1) {
index = this.files.length;
this.files.push(path);
}
return index;
}
 
StylusInfo.prototype.export = function () {
return {
selectors: this.selectors,
calls: this.calls,
extends: this.extends,
idents: this.idents,
files: this.files,
}
}
 
module.exports = function() {
return new StylusInfo();
}
module.exports.StylusInfo = StylusInfo;

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.