-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
88 lines (68 loc) · 2.24 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
var LESS = require('less');
var fs = require('fs');
var path = require('path');
var nodeEnv = process.env.NODE_ENV || 'development';
if (! Object.assign) {
console.error("\n----\n");
console.error("! Object.assign is not defined");
console.error(" Please install node v4.0.0+ or npm install es6-object-assign");
console.error("\n----\n");
}
exports.serve = function (filePath, options) {
options = options || {};
if ( ! fs.existsSync(filePath) ) {
throw Error('[node-less-endpoint] File does not exist: ' + filePath);
}
options.watchDir = options.watchDir || path.dirname(filePath);
options.includePaths = options.includePaths || [path.dirname(filePath)];
options.includePaths.push( path.join(process.cwd(), 'node_modules') );
var cache = null;
var lessOptions = {
filename: filePath,
paths: options.includePaths,
outputStyle: options.outputStyle || 'nested'
};
if (nodeEnv === 'development') {
fs.watch(options.watchDir, { recursive: true }, function(e, file) {
if (! isLessFile(file) ) return;
if (options.debug) {
console.log("[node-less-endpoint] Clearing cache for:", path.basename(filePath));
}
cache = null;
});
}
else if (nodeEnv === 'production') {
Object.assign(lessOptions, {
compress: true
})
}
return function (req, res) {
res.set('Content-Type', 'text/css');
if (cache) {
return res.send(cache);
}
// LESS.render only renders a string; we must read target file first
fs.readFile(filePath, 'utf8', function (err, fileContents) {
if (err) {
res.status(500).send(err);
throw err;
}
LESS.render( fileContents, Object.assign({}, lessOptions), function(error, result) {
if (error) {
console.log("\n----------------------------\n");
console.log("LESS ERROR:", error.message);
console.log("\tin file", error.file);
console.log("\ton line", error.line, "col", error.column);
console.log("\n----------------------------\n");
return res.status(500).send(error);
}
cache = result.css;
res.send(result.css);
});
})
}
}
var ext = /\.less$/;
function isLessFile (file) {
return file.match(ext);
}