Skip to content

Instantly share code, notes, and snippets.

@fson
Forked from maccman/slug.js
Created January 4, 2012 17:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fson/1561156 to your computer and use it in GitHub Desktop.
Save fson/1561156 to your computer and use it in GitHub Desktop.
Hem & Less
var hem = new (require('hem'));
var less = require('less');
var fs = require('fs');
var argv = process.argv.slice(2);
var path = require('path');
var util = require('util');
hem.compilers.less = function (pathname) {
var content, result;
content = fs.readFileSync(pathname, 'utf8');
result = '';
less.render(content, { paths: [path.dirname(pathname)] }, function(err, css) {
if (err) { throw err; }
result = css;
});
return result;
};
require.extensions['.less'] = function(module, filename) {
var source;
source = JSON.stringify(hem.compilers.less(filename));
return module._compile("module.exports = " + source, filename);
};
// Monkey patch importer of LESS to load files synchronously
less.Parser.importer = function (file, paths, callback) {
var pathname, data;
paths.unshift('.');
for (var i = 0; i < paths.length; i++) {
try {
pathname = path.join(paths[i], file);
fs.statSync(pathname);
break;
} catch (e) {
pathname = null;
}
}
if (!pathname) {
util.error("file '" + file + "' wasn't found.\n");
process.exit(1);
}
try {
data = fs.readFileSync(pathname, 'utf-8');
} catch (e) {
util.error(e);
}
new(less.Parser)({
paths: [path.dirname(pathname)].concat(paths),
filename: pathname
}).parse(data, function (e, root) {
if (e) less.writeError(e);
callback(root);
});
}
hem.exec(argv[0]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment