Skip to content

Instantly share code, notes, and snippets.

@tonistiigi
Created October 22, 2012 14:51
Show Gist options
  • Save tonistiigi/3931858 to your computer and use it in GitHub Desktop.
Save tonistiigi/3931858 to your computer and use it in GitHub Desktop.
Stylus compilation information exporter
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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment