UNPKG

ho-compiler

Version:

Less compilation for ho builds

139 lines (111 loc) 4.89 kB
'use strict'; var _interopRequireWildcard = function (obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }; var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }; var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); var _get = function get(object, property, receiver) { var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } }; var _inherits = function (subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; }; Object.defineProperty(exports, '__esModule', { value: true }); /** * Ho-compile * --- * * Less compiler */ var _stream = require('stream'); var _stream2 = _interopRequireWildcard(_stream); var _core = require('core-js/library/es6/object'); var _core2 = _interopRequireWildcard(_core); var _less = require('less'); var _less2 = _interopRequireWildcard(_less); var _log = require('./log'); var _log2 = _interopRequireWildcard(_log); /** * Compiler * @class * @extends stream.Transform */ var Compiler = (function (_stream$Transform) { /** * @constructs * @param opts <Object> * @param paths <Array:String> less import paths * @param compress <Boolean> should compress less output * @param filename <String> initial file path * @param sourceMap <Boolean> should include source map in output * @throws on stream error * @throws on less compile error */ function Compiler(opts) { _classCallCheck(this, Compiler); _get(Object.getPrototypeOf(Compiler.prototype), 'constructor', this).call(this); this.opts = _core2['default'].assign({ paths: '', compress: false, filename: null, sourceMap: false }, opts); this.input = ''; } _inherits(Compiler, _stream$Transform); _createClass(Compiler, [{ key: '_transform', /** * _transform * Collects input * @private */ value: function _transform(chunk, enc, next) { if (chunk !== null) { this.input += chunk; } next(); } }, { key: '_flush', /** * _flush * Called when input has been collected */ value: function _flush(done) { var _this = this; this.compile(this.input, function (err, res) { if (err) { _this.emit('error', err); } _this.push(res.css); done(); }); } }, { key: 'compile', /** * compile * Turns less into css * @param input <String> less to convert * @param cb <Function> * @param err <Error> non-null on any less compilation errors * @param output <String> converted less */ value: function compile(input, cb) { _less2['default'].render(input, { filename: this.opts.filename, paths: this.opts.paths, compress: this.opts.compress, sourceMap: { sourceMapFileInline: this.opts.sourceMap, outputSourceFiles: this.opts.sourceMap } }).then(function (output) { cb(null, output); })['catch'](function (err) { _log2['default'].error('Error compiling less'); cb(err); }); } }]); return Compiler; })(_stream2['default'].Transform); exports['default'] = Compiler; module.exports = exports['default'];