This repository has been archived by the owner on Dec 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
136 lines (100 loc) · 2.56 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
var Batch = require('batch')
, fs = require('fs')
, path = require('path')
, sass = require('node-sass-wrapper')
, debug = require('debug')('component-sass');
/**
* Options.
*
* Keep em null so that we just their Sass defaults.
*/
var options = {
compass : null,
import : null,
loadPath : null,
noCache : null,
precision : null,
require : null,
style : null
};
/**
* Replace Sass files with CSS files.
*/
module.exports = function (builder) {
builder.hook('before styles', function (builder, callback) {
if (!builder.config.styles) return callback();
var files = builder.config.styles.filter(sassFilter)
, batch = new Batch();
files.forEach(function (file) {
batch.push(function (done) {
debug('compiling: %s', file);
sass.compile(builder.path(file), options, function (err, css) {
if (err) {
debug('error compiling: %s, %s', file, err);
return done(err);
}
var newFile = path.basename(file, path.extname(file)) + '.css';
builder.addFile('styles', newFile, css);
builder.removeFile('styles', file);
done();
});
});
});
batch.end(callback);
});
};
/**
* Use Compass.
*/
module.exports.compass = function (enabled) {
options.compass = enabled;
};
/**
* Add a path to the IMPORT_PATH sass option, which lets files in that directory
* findable by Sass's @import directive.
*/
module.exports.loadPath = function (path) {
options.loadPath = path;
};
/**
* Cache compilation.
*/
module.exports.noCache = function (enabled) {
options.noCache = enabled;
};
/**
* Set decimal precision.
*/
module.exports.precision = function (precision) {
options.precision = precision;
};
/**
* Require Sass plugin.
*/
module.exports.require = function (plugin) {
options.require = plugin;
};
/**
* Set output style.
*/
module.exports.style = function (style) {
options.style = style;
};
/**
* Specify a file to import. This is different thatn `loadPath` in that it will
* actually wrap your sass files in an import for this file each time, so you
* don't need to manually import them.
*
* This is useful for adding utils like mixins, functions to all your Sass files
* in your project (like your own project-specific Compass).
*/
module.exports.import = function (file) {
options.import = file;
};
/**
* Filtering function for .sass and .scss files.
*/
function sassFilter (filename) {
var ext = path.extname(filename);
if (ext === '.sass' || ext === '.scss') return true;
}