This repository has been archived by the owner on Mar 20, 2021. It is now read-only.
/
stylus-worker.js
254 lines (215 loc) · 6.24 KB
/
stylus-worker.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
var _ = require('underscore'),
fs = require('fs'),
fu = require('../fileUtil'),
nib = require('nib'),
stylusImages = require('stylus-images'),
path = require('path'),
join = path.join,
normalize = path.normalize,
resources = require('../util/resources');
var stylus = stylusImages.stylus || require('stylus');
var importCache = {};
// Reset the import cache if the file has changed
fu.on('cache:reset', function(path) {
if (path) {
delete importCache[path];
} else {
importCache = {};
}
});
process.on('message', function(msg) {
if (msg.type === 'cache:reset') {
fu.emit('cache:reset', msg.path);
} else {
exec(msg, function(err, data) {
return process.send({err: err, stack: err && err.stack, data: data});
});
}
});
function exec(msg, callback) {
var lookupPath = msg.lookupPath,
minimize = msg.minimize,
useNib = msg.useNib,
styleRoot = msg.styleRoot,
source = msg.source,
imageOptions = msg.imageOptions,
plugins = msg.plugins;
fu.lookupPath(lookupPath);
imageOptions.nameResolver = function(url) {
if (this.mixinPaths && this.mixinPaths.length) {
return msg.mixins[this.mixinPaths[0]].root + url.href;
} else {
return url.href;
}
};
var options = {
images: imageOptions,
externals: [], // Used to track "imports" that aren't actually stylus imports
_imports: [],
_importCache: importCache
};
var compiler = (stylusImages.plugin ? stylusImages : stylus)(source, options)
.set('filename', msg.filename)
.set('compress', minimize);
if (!stylusImages.plugin) {
compiler.use(stylusImages(options.images));
}
compiler
.use(pathScopes(msg))
.include(lookupPath);
if (styleRoot) {
compiler.include(styleRoot);
}
if (useNib) {
compiler
.include(nib.path)
.use(nib);
}
// Allow any plugins to do their thing
_.each(plugins, function(plugin) {
var data = plugin.data;
plugin = require(plugin.plugin);
plugin(compiler, data);
});
try {
compiler.render(function(err, data) {
var inputs = compiler.options._imports
.concat(compiler.options.externals)
.map(function(file) { return file.path || file; })
// Filter out nib files from any watch as these are known and not likely to change
.filter(function(file) { return _.isString(file) && file.indexOf('/nib/') === -1 && file.indexOf('\\nib\\') === -1; });
if (err && err.code === 'EMFILE') {
// If we are in EMFILE mode just kill everything now as this is a systemic problem
// and we need the user to fix this.
var err = new Error();
err.code = err.errno = 'EMFILE';
callback(err);
return;
}
callback(err, {
data: data,
inputs: inputs,
noSeparator: true
});
});
} catch (err) {
callback(err);
}
}
function pathScopes(msg) {
function getPaths(mixin) {
var root,
stylusRoot;
mixin = _.isString(mixin) ? msg.mixins[mixin] : mixin;
if (mixin) {
root = mixin.root;
root = normalize(fu.resolvePath(root));
stylusRoot = mixin.stylusRoot;
if (stylusRoot) {
stylusRoot = root + stylusRoot;
}
} else {
root = msg.lookupPath;
if (msg.styleRoot) {
stylusRoot = fu.resolvePath(msg.styleRoot);
}
}
var pathList = [root || '.'];
if (stylusRoot) {
pathList.push(stylusRoot);
}
return pathList;
}
return function(extend) {
stylus.utils.lookup = function(path, paths, ignore) {
var mixinName = mixinPaths[0],
mixin = msg.mixins[mixinName],
override;
path = resources.relativePath(path, mixin);
if (mixin) {
// Checkout for overrides
var overrides = mixin.overrides;
if (overrides) {
_.find(paths, function(root) {
// Strip the mixin path
root = resources.relativePath(root, mixin);
if (root === '.' || root === './') {
root = '';
}
override = overrides[root + path];
return override;
});
override = override || overrides[path];
}
if (override) {
if (override.override !== true) {
path = override.override;
}
} else {
// If we are not overriding remove any paths outside of the mixin that may have been
// inserted by via include operations
var mixinRoot = normalize(fu.resolvePath(mixin.root));
if (mixinRoot !== './') {
paths = _.filter(paths, function(path) {
return path.indexOf(mixinRoot) === 0;
});
}
}
}
// If we are in override mode reset the default paths
if (!override) {
// Remove any duplicate paths that may have been created by the import handling
paths = _.unique(paths, false, function(value) {
return value && value.replace(/[\\\/]$/, '');
});
} else {
paths = getPaths(override.root ? override : undefined);
}
return $superForkedLookup.call(this, path, paths, ignore);
};
var mixinPaths = [];
extend.define('push-mixin', function(name) {
var mixin = name.val;
mixinPaths.unshift(mixin);
this.mixinPaths = mixinPaths;
this.paths = getPaths(mixin);
});
extend.define('pop-mixin', function() {
mixinPaths.shift();
this.mixinPaths = mixinPaths;
this.paths = getPaths(mixinPaths[0]);
});
};
}
// Forked from stylus pre-0.34.0
// Effectively reverts the changes from
// https://github.com/LearnBoost/stylus/pull/1070/files
function $superForkedLookup(path, paths, ignore){
var lookup,
i = paths.length;
// Absolute
if (stylus.utils.absolute(path)) {
try {
fs.statSync(path);
return path;
} catch (err) {
// Ignore, continue on
// to trying relative lookup.
// Needed for url(/images/foo.png)
// for example
}
}
// Relative
while (i--) {
try {
lookup = join(paths[i], path);
if (ignore == lookup) {
continue;
}
fs.statSync(lookup);
return lookup;
} catch (err) {
// Ignore
}
}
}