-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
55 lines (42 loc) · 1.61 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
var sass = require('node-sass');
var path = require('path');
var fs = require('fs');
var async = require('async');
module.exports = function(builder) {
builder.hook('before styles', function(pkg, next) {
// No styles in this package
if(pkg.root !== true || !pkg.config.styles) return next();
// Get all the coffee files from the scripts list
var sassfiles = pkg.config.styles.filter(function(file){
return path.extname(file) === '.scss';
});
// No sass files
if( sassfiles.length === 0 ) return next();
// Sass load paths
var loadPaths = (pkg.config.paths || []).map(pkg.path, pkg).concat(pkg.path('components'));
// Get the real path for each file relative to the package
// Must pass in package as context to make sure we have the correct context
var realSassFiles = sassfiles.map(pkg.path, pkg);
// Function to compile sass files that will include the package
// load paths as Sass load paths
var compileSassFile = function(str, callback) {
sass.render(str, callback, { includePaths: loadPaths });
};
// Read all the sass files
async.map(sassfiles, fs.readFile, function(err, results){
if(err) return next(err);
results = results.map(function(data, i){
return data.toString();
});
// Compile them all
async.map(results, compileSassFile, function(err, compiledFiles){
if(err) return next(err);
compiledFiles.forEach(function(data, i){
pkg.addFile('styles', sassfiles[i], data);
pkg.removeFile('styles', sassfiles[i]);
});
next();
});
});
});
};