-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
161 lines (139 loc) · 3.05 KB
/
index.js
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
'use strict';
/**
* Module dependencies.
*/
var fs = require('fs');
var utils = require('./utils');
/**
* Less support.
*/
var engine = utils.engineUtils.fromStringRenderer('less');
/**
* Expose `less`, to give users access to the same instance
*/
engine.less = require.less || (require.less = require('less'));
/**
* engine defaults
*/
engine.options = {
src: {ext: '.less'},
dest: {ext: '.css'},
layout: null,
lessRenderMode: 'string',
silent: false,
compress: false,
sourceMap: false,
paths: [],
};
var optsKeys = [
'chunkInput',
'compress',
'dumpLineNumbers',
'globalVars',
'modifyVars',
'ieCompat',
'importantScope',
'importMultiple',
'insecure',
'javascriptEnabled',
'mime',
'paths',
'pluginManager',
'processImports',
'reference',
'relativeUrls',
'rootpath',
'sourceMap',
'strictImports',
'strictMath',
'strictUnits',
'syncImport',
'urlArgs',
'useFileCache'
];
/**
* Less string support. Process the given `str` of less and invoke
* the callback `callback(err, css)`.
*
* ```js
* var str = '@red: #900;\n.foo {color: @red;}';
* engine.render(str, function (err, css) {
* console.log(css);
* //=> '.foo {\n color: #990000;\n}\n'
* });
* ```
*
* @param {String} `str`
* @param {Object|Function} `options` or callback.
* @param {Function} `callback`
* @api public
*/
engine.render = function render(str, options, cb) {
if (typeof options === 'function') {
cb = options;
options = {};
}
options = utils.merge({}, options, options.settings);
// only pass valid options to less
var keys = optsKeys.concat(Object.keys(engine.options));
var opts = utils.pick(options, keys);
utils.merge(opts, engine.options, options);
try {
engine.less.render(str, opts, function (err, res) {
if (err) return cb(formatError(err));
if (opts.lessRenderMode === 'object') {
cb(null, res);
} else {
engine.res = res;
cb(null, res.css);
}
});
} catch (err) {
cb(err);
}
};
/**
* Less file support. Process a `.less` file at the given `fp` and
* callback `callback(err, css)`.
*
* ```js
* var less = require('engine-less');
* less.renderFile('my-styles.less', {}, function (err, css) {
* console.log(css);
* //=> '.aaa {\n color: blue;\n}\n'
* });
* ```
*
* @param {String} `filepath`
* @param {Object|Function} `options` or callback function.
* @param {Function} `callback`
* @api public
*/
engine.renderFile = function renderFile(fp, options, cb) {
if (typeof options === 'function') {
cb = options;
options = {};
}
try {
// arguments[0] = fs.readFileSync(arguments[0], 'utf8');
// engine.render.apply(engine, arguments);
var str = fs.readFileSync(fp, 'utf8');
utils.merge(engine.options. options);
engine.render(str, options, cb);
} catch (err) {
cb(err);
}
};
/**
* Expose `engine`
*/
module.exports = engine;
/**
* Helper function
*/
function formatError(err) {
err.message = err.message
+ ' in file: ' + err.filename
+ ' line no: ' + err.line;
return err;
}