This repository has been archived by the owner on Oct 18, 2022. It is now read-only.
forked from gabrielgrant/broccoli-less-single
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
97 lines (77 loc) · 2.89 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
var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');
var includePathSearcher = require('include-path-searcher');
var quickTemp = require('quick-temp');
var mapSeries = require('promise-map-series');
var less = require('less');
var _ = require('lodash');
var RSVP = require('rsvp');
var Writer = require('broccoli-writer');
var helpers = require('broccoli-kitchen-sink-helpers');
module.exports = LessCompiler;
LessCompiler.prototype = Object.create(Writer.prototype);
LessCompiler.prototype.constructor = LessCompiler;
function LessCompiler (sourceTrees, inputFile, outputFile, options) {
if (!(this instanceof LessCompiler)) return new LessCompiler(sourceTrees, inputFile, outputFile, options);
this.sourceTrees = sourceTrees;
this.inputFile = inputFile;
this.outputFile = outputFile;
this.lessOptions = options || {};
this.cache = {};
this.cached = undefined;
}
LessCompiler.prototype.write = function (readTree, destDir) {
var self = this;
var destFile = destDir + '/' + this.outputFile;
mkdirp.sync(path.dirname(destFile));
return mapSeries(this.sourceTrees, readTree)
.then(function (includePaths) {
var newCache = {};
// glob off of input tree
var inputFiles = includePaths.map(function(path) {
var files = helpers.multiGlob(['**/*.less'], {cwd: path});
return files.map(function(filepath) { return path + '/' + filepath; });
});
var needRecompile = false;
_.flatten(inputFiles).forEach(function(path) {
var statsHash = helpers.hashStats(fs.statSync(path));
if (self.cache[statsHash] === undefined) {
// cache miss, recompile needed!
needRecompile = true;
}
newCache[statsHash] = true; // eventually this could store some extra state
});
self.cache = newCache;
if (needRecompile === false) {
fs.writeFileSync(destFile, self.cached, { encoding: 'utf8' });
return RSVP.Promise.resolve(self.tmpDestDir);
}
var lessOptions = {
filename: includePathSearcher.findFileSync(self.inputFile, includePaths),
paths: includePaths,
};
_.merge(lessOptions, self.lessOptions);
lessOptions.paths = [path.dirname(lessOptions.filename)].concat(lessOptions.paths);
data = fs.readFileSync(lessOptions.filename, 'utf8');
var parser = new(less.Parser)(lessOptions);
var promise = new RSVP.Promise(function(resolve, reject) {
parser.parse(data, function (e, tree) {
try {
if (e) {
less.writeError(e, lessOptions);
reject(e);
}
var css = tree.toCSS(lessOptions);
fs.writeFileSync(destFile, css, {
encoding: 'utf8'
});
resolve(self._tmpDestDir);
} catch(error) {
reject(error);
}
});
});
return promise;
});
};