-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
194 lines (155 loc) · 3.48 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
'use strict';
var fs = require('fs');
var path = require('path');
var css = require('css');
var globby = require('globby');
var parseImport = require('parse-import');
var urlRegex = require('url-regex');
/**
* Get options
*
* @param {Array} rules
* @param {Object} opts
* @api private
*/
function getOptions(rules, opts) {
var dir;
var obj = {
source: opts.source,
transform: opts.transform || function (val) {
return val;
}
};
obj.path = opts.path || [];
obj.path = Array.isArray(obj.path) ? obj.path : [obj.path];
if (rules.length && rules[0].position && rules[0].position.source) {
dir = path.dirname(rules[0].position.source);
}
if (dir && obj.path.indexOf(dir) === -1) {
obj.path.unshift(dir);
}
if (!obj.path.length) {
obj.path.push(process.cwd());
}
return obj;
}
/**
* Create error
*
* @param {String} file
* @param {String} src
* @param {Array} paths
* @api private
*/
function createError(file, src, paths) {
var err = ['Failed to find ' + file];
if (src) {
err.push('from ' + src);
}
err.push([
'in [',
' ' + paths.join(',\n '),
']'
].join('\n'));
return err.join(' ');
}
/**
* Create bad import rule error
*
* @param {String} rule
* @api private
*/
function createImportError(rule) {
var url = rule.import ? rule.import.replace(/\r?\n/g, '\\n') : '<no url>';
var err = ['Bad import url: @import ' + url];
if (rule.position) {
err.push(' starting at line ' + rule.position.start.line + ' column ' + rule.position.start.column);
err.push(' ending at line ' + rule.position.end.line + ' column ' + rule.position.end.column);
if (rule.position.source) {
err.push(' in ' + rule.position.source);
}
}
return err.join('\n');
}
/**
* Check if a file exists
*
* @param {String} file
* @param {String} src
* @param {Object} opts
* @api private
*/
function exists(file, src, opts) {
var files = opts.path.map(function (dir) {
return path.join(dir, file);
});
files = globby.sync(files);
if (!files.length) {
throw new Error(createError(file, src, opts.path));
}
return files[0];
}
/**
* Read the contents of a file
*
* @param {String} file
* @param {Object} opts
* @api private
*/
function read(file, opts) {
var encoding = opts.encoding || 'utf8';
var data = opts.transform(fs.readFileSync(file, encoding));
return css.parse(data, {source: file}).stylesheet;
}
/**
* Run
*
* @param {Object} style
* @param {Object} opts
* @api private
*/
function run(style, opts) {
opts = getOptions(style.rules, opts || {});
var rules = style.rules || [];
var ret = [];
rules.forEach(function (rule) {
if (rule.type !== 'import') {
ret.push(rule);
return;
}
var importRule = '@import ' + rule.import + ';';
var data = parseImport(importRule)[0];
var pos = rule.position ? rule.position.source : null;
if (!data) {
throw Error(createImportError(rule));
}
if (urlRegex({exact: true}).test(data.path)) {
ret.push(rule);
return;
}
opts.source = exists(data.path, pos, opts);
if (opts.path.indexOf(path.dirname(opts.source)) === -1) {
opts.path.unshift(path.dirname(opts.source));
}
var content = read(opts.source, opts);
run(content, opts);
if (!data.condition || !data.condition.length) {
ret = ret.concat(content.rules);
return;
}
ret.push({
media: data.condition,
rules: content.rules,
type: 'media'
});
});
style.rules = ret;
}
/**
* Module exports
*/
module.exports = function (opts) {
return function (style) {
run(style, opts);
};
};