UNPKG

1.2 kBJavaScriptView Raw
1'use strict';
2
3/**
4 * Less plugin for Crixalis. Depends on static and processor plugins.
5 * Provides simple wrapper around [Less](http://lesscss.org) css preprocessor.
6 * @module Crixalis
7 * @submodule less
8 * @for Controller
9 */
10
11var Crixalis = require('../controller'),
12 Parser = require('less').Parser,
13 cache = {};
14
15module.exports = function (options) {
16
17 /**
18 * Compile css using less and send it to client
19 * @method less
20 * @param {String} filename Path to less source file
21 * @chainable
22 */
23 Crixalis.plugin('processor', {
24 method : 'less',
25 extension : 'css',
26 compile : function (filename, data, options) {
27 options.filename = filename;
28 options.async = true;
29
30 var parser = new Parser(options),
31 that = this;
32
33 parser.parse(data, function (error, result) {
34 if (!error && result) {
35 try {
36 result = result.toCSS({ compress: true });
37 } catch (exception) {
38 error = exception;
39 }
40 }
41
42 /* Less parser throws exceptions that are not Error instances */
43 if (error && !(error instanceof Error)) {
44 error = new Error([error.message, error.filename].join(' '));
45 }
46
47 that.forward(error, result);
48 });
49 }
50 });
51};