-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
147 lines (124 loc) · 3.02 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
/* jshint node: true */
'use strict';
var stylus = require('stylus'),
through = require('through2'),
glob = require('glob'),
resolve = require('resolve'),
flatten = require('lodash.flatten'),
uniq = require('lodash.uniq'),
merge = require('lodash.merge');
var defaultOptions = {
set: {
paths: []
},
include: [],
import: [],
define: {},
use: []
};
var defaultSourceMapSettings = {
inline: true,
comment: true
};
// TODO: allow all options
function getPackageOptions() {
var pkg;
var options = {};
try {
pkg = require(process.cwd() + '/package.json');
} catch (e) {}
if (!pkg || !pkg.stylify) {
return options;
}
if (pkg.stylify.use) {
options.use = pkg.stylify.use;
}
if (pkg.stylify.paths) {
options.set = {
paths: pkg.stylify.paths
};
}
return options;
}
function parsePaths(paths) {
paths = Array.isArray(paths) ? paths : [];
return uniq(flatten(paths.map(function(path) {
return glob.sync(path);
})));
}
function resolveUses(uses) {
if (!Array.isArray(uses)) {
uses = [uses];
}
return uses.map(function(mod) {
if (typeof mod === 'string') {
mod = require(resolve.sync(mod, {
basedir: process.cwd()
}));
}
return mod;
});
}
function applyOptions(stylus, options) {
['set', 'include', 'import', 'define', 'use'].forEach(function(method) {
var option = options[method];
if (Array.isArray(option)) {
for (var i = 0; i < option.length; i++)
stylus[method](option[i]);
} else {
for (var prop in option)
stylus[method](prop, option[prop]);
}
});
}
function compile(file, data, options) {
var style = stylus(data);
applyOptions(style, options);
var sourceMapSettings = style.get('sourcemap');
// always use inline source maps if enabled
if (typeof sourceMapSettings === 'object') {
sourceMapSettings = merge(sourceMapSettings, defaultSourceMapSettings);
}
if (sourceMapSettings === true) {
sourceMapSettings = defaultSourceMapSettings;
}
style.set('sourcemap', sourceMapSettings);
// enable compression unless explicitly disabled
style.set('compress', style.get('compress') !== false);
style.set('filename', file);
var compiled = style.render().trim();
return {
css: 'module.exports = ' + JSON.stringify(compiled) + ';',
deps: style.deps()
};
}
module.exports = function (file, options) {
if (!/\.styl$/.test(file)) return through();
var data = '';
var packageOptions = merge(
{},
defaultOptions,
getPackageOptions()
);
options = merge(packageOptions, options || {});
options.use = resolveUses(options.use);
options.set.paths = parsePaths(options.set.paths);
function write(buf, enc, cb) {
data += buf.toString();
cb();
}
function end(cb) {
var out;
try {
out = compile(file, data, options);
} catch (err) {
return cb(err);
}
out.deps.forEach(function(dep){
this.emit('file', dep);
}, this);
this.push(out.css);
cb();
}
return through(write, end);
};