middleware.js
6.93 KB
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
255
256
/*!
* Stylus - middleware
* Copyright (c) Automattic <developer.wordpress.com>
* MIT Licensed
*/
/**
* Module dependencies.
*/
var stylus = require('./stylus')
, fs = require('fs')
, url = require('url')
, dirname = require('path').dirname
, mkdirp = require('mkdirp')
, join = require('path').join
, sep = require('path').sep
, debug = require('debug')('stylus:middleware');
/**
* Import map.
*/
var imports = {};
/**
* Return Connect middleware with the given `options`.
*
* Options:
*
* `force` Always re-compile
* `src` Source directory used to find .styl files,
* a string or function accepting `(path)` of request.
* `dest` Destination directory used to output .css files,
* a string or function accepting `(path)` of request,
* when undefined defaults to `src`.
* `compile` Custom compile function, accepting the arguments
* `(str, path)`.
* `compress` Whether the output .css files should be compressed
* `firebug` Emits debug infos in the generated CSS that can
* be used by the FireStylus Firebug plugin
* `linenos` Emits comments in the generated CSS indicating
* the corresponding Stylus line
* 'sourcemap' Generates a sourcemap in sourcemaps v3 format
*
* Examples:
*
* Here we set up the custom compile function so that we may
* set the `compress` option, or define additional functions.
*
* By default the compile function simply sets the `filename`
* and renders the CSS.
*
* function compile(str, path) {
* return stylus(str)
* .set('filename', path)
* .set('compress', true);
* }
*
* Pass the middleware to Connect, grabbing .styl files from this directory
* and saving .css files to _./public_. Also supplying our custom `compile` function.
*
* Following that we have a `static()` layer setup to serve the .css
* files generated by Stylus.
*
* var app = connect();
*
* app.middleware({
* src: __dirname
* , dest: __dirname + '/public'
* , compile: compile
* })
*
* app.use(connect.static(__dirname + '/public'));
*
* @param {Object} options
* @return {Function}
* @api public
*/
module.exports = function(options){
options = options || {};
// Accept src/dest dir
if ('string' == typeof options) {
options = { src: options };
}
// Force compilation
var force = options.force;
// Source dir required
var src = options.src;
if (!src) throw new Error('stylus.middleware() requires "src" directory');
// Default dest dir to source
var dest = options.dest || src;
// Default compile callback
options.compile = options.compile || function(str, path){
// inline sourcemap
if (options.sourcemap) {
if ('boolean' == typeof options.sourcemap)
options.sourcemap = {};
options.sourcemap.inline = true;
}
return stylus(str)
.set('filename', path)
.set('compress', options.compress)
.set('firebug', options.firebug)
.set('linenos', options.linenos)
.set('sourcemap', options.sourcemap);
};
// Middleware
return function stylus(req, res, next){
if ('GET' != req.method && 'HEAD' != req.method) return next();
var path = url.parse(req.url).pathname;
if (/\.css$/.test(path)) {
if (typeof dest == 'string') {
// check for dest-path overlap
var overlap = compare(dest, path).length;
if ('/' == path.charAt(0)) overlap++;
path = path.slice(overlap);
}
var cssPath, stylusPath;
cssPath = (typeof dest == 'function')
? dest(path)
: join(dest, path);
stylusPath = (typeof src == 'function')
? src(path)
: join(src, path.replace('.css', '.styl'));
// Ignore ENOENT to fall through as 404
function error(err) {
next('ENOENT' == err.code
? null
: err);
}
// Force
if (force) return compile();
// Compile to cssPath
function compile() {
debug('read %s', cssPath);
fs.readFile(stylusPath, 'utf8', function(err, str){
if (err) return error(err);
var style = options.compile(str, stylusPath);
var paths = style.options._imports = [];
imports[stylusPath] = null;
style.render(function(err, css){
if (err) return next(err);
debug('render %s', stylusPath);
imports[stylusPath] = paths;
mkdirp(dirname(cssPath), parseInt('0700', 8), function(err){
if (err) return error(err);
fs.writeFile(cssPath, css, 'utf8', next);
});
});
});
}
// Re-compile on server restart, disregarding
// mtimes since we need to map imports
if (!imports[stylusPath]) return compile();
// Compare mtimes
fs.stat(stylusPath, function(err, stylusStats){
if (err) return error(err);
fs.stat(cssPath, function(err, cssStats){
// CSS has not been compiled, compile it!
if (err) {
if ('ENOENT' == err.code) {
debug('not found %s', cssPath);
compile();
} else {
next(err);
}
} else {
// Source has changed, compile it
if (stylusStats.mtime > cssStats.mtime) {
debug('modified %s', cssPath);
compile();
// Already compiled, check imports
} else {
checkImports(stylusPath, function(changed){
if (debug && changed.length) {
changed.forEach(function(path) {
debug('modified import %s', path);
});
}
changed.length ? compile() : next();
});
}
}
});
});
} else {
next();
}
}
};
/**
* Check `path`'s imports to see if they have been altered.
*
* @param {String} path
* @param {Function} fn
* @api private
*/
function checkImports(path, fn) {
var nodes = imports[path];
if (!nodes) return fn();
if (!nodes.length) return fn();
var pending = nodes.length
, changed = [];
nodes.forEach(function(imported){
fs.stat(imported.path, function(err, stat){
// error or newer mtime
if (err || !imported.mtime || stat.mtime > imported.mtime) {
changed.push(imported.path);
}
--pending || fn(changed);
});
});
}
/**
* get the overlaping path from the end of path A, and the begining of path B.
*
* @param {String} pathA
* @param {String} pathB
* @return {String}
* @api private
*/
function compare(pathA, pathB) {
pathA = pathA.split(sep);
pathB = pathB.split('/');
if (!pathA[pathA.length - 1]) pathA.pop();
if (!pathB[0]) pathB.shift();
var overlap = [];
while (pathA[pathA.length - 1] == pathB[0]) {
overlap.push(pathA.pop());
pathB.shift();
}
return overlap.join('/');
}