1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | var stylus = require('./stylus')
|
12 | , fs = require('fs')
|
13 | , url = require('url')
|
14 | , dirname = require('path').dirname
|
15 | , mkdirp = require('mkdirp')
|
16 | , join = require('path').join
|
17 | , sep = require('path').sep
|
18 | , debug = require('debug')('stylus:middleware');
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | var imports = {};
|
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 | module.exports = function(options){
|
82 | options = options || {};
|
83 |
|
84 |
|
85 | if ('string' == typeof options) {
|
86 | options = { src: options };
|
87 | }
|
88 |
|
89 |
|
90 | var force = options.force;
|
91 |
|
92 |
|
93 | var src = options.src;
|
94 | if (!src) throw new Error('stylus.middleware() requires "src" directory');
|
95 |
|
96 |
|
97 | var dest = options.dest || src;
|
98 |
|
99 |
|
100 | options.compile = options.compile || function(str, path){
|
101 |
|
102 | if (options.sourcemap) {
|
103 | if ('boolean' == typeof options.sourcemap)
|
104 | options.sourcemap = {};
|
105 | options.sourcemap.inline = true;
|
106 | }
|
107 |
|
108 | return stylus(str)
|
109 | .set('filename', path)
|
110 | .set('compress', options.compress)
|
111 | .set('firebug', options.firebug)
|
112 | .set('linenos', options.linenos)
|
113 | .set('sourcemap', options.sourcemap);
|
114 | };
|
115 |
|
116 |
|
117 | return function stylus(req, res, next){
|
118 | if ('GET' != req.method && 'HEAD' != req.method) return next();
|
119 | var path = url.parse(req.url).pathname;
|
120 | if (/\.css$/.test(path)) {
|
121 |
|
122 | if (typeof dest == 'string' || typeof dest == 'function') {
|
123 |
|
124 | var overlap = compare((typeof dest == 'function' ? dest(path) : dest), path).length;
|
125 | if (sep == path.charAt(0)) overlap++;
|
126 | path = path.slice(overlap);
|
127 | }
|
128 |
|
129 | var cssPath, stylusPath;
|
130 | cssPath = (typeof dest == 'function')
|
131 | ? dest(path)
|
132 | : join(dest, path);
|
133 | stylusPath = (typeof src == 'function')
|
134 | ? src(path)
|
135 | : join(src, path.replace('.css', '.styl'));
|
136 |
|
137 |
|
138 | function error(err) {
|
139 | next('ENOENT' == err.code
|
140 | ? null
|
141 | : err);
|
142 | }
|
143 |
|
144 |
|
145 | if (force) return compile();
|
146 |
|
147 |
|
148 | function compile() {
|
149 | debug('read %s', cssPath);
|
150 | fs.readFile(stylusPath, 'utf8', function(err, str){
|
151 | if (err) return error(err);
|
152 | var style = options.compile(str, stylusPath);
|
153 | var paths = style.options._imports = [];
|
154 | imports[stylusPath] = null;
|
155 | style.render(function(err, css){
|
156 | if (err) return next(err);
|
157 | debug('render %s', stylusPath);
|
158 | imports[stylusPath] = paths;
|
159 | mkdirp(dirname(cssPath), 0700, function(err){
|
160 | if (err) return error(err);
|
161 | fs.writeFile(cssPath, css, 'utf8', next);
|
162 | });
|
163 | });
|
164 | });
|
165 | }
|
166 |
|
167 |
|
168 |
|
169 | if (!imports[stylusPath]) return compile();
|
170 |
|
171 |
|
172 | fs.stat(stylusPath, function(err, stylusStats){
|
173 | if (err) return error(err);
|
174 | fs.stat(cssPath, function(err, cssStats){
|
175 |
|
176 | if (err) {
|
177 | if ('ENOENT' == err.code) {
|
178 | debug('not found %s', cssPath);
|
179 | compile();
|
180 | } else {
|
181 | next(err);
|
182 | }
|
183 | } else {
|
184 |
|
185 | if (stylusStats.mtime > cssStats.mtime) {
|
186 | debug('modified %s', cssPath);
|
187 | compile();
|
188 |
|
189 | } else {
|
190 | checkImports(stylusPath, function(changed){
|
191 | if (debug && changed.length) {
|
192 | changed.forEach(function(path) {
|
193 | debug('modified import %s', path);
|
194 | });
|
195 | }
|
196 | changed.length ? compile() : next();
|
197 | });
|
198 | }
|
199 | }
|
200 | });
|
201 | });
|
202 | } else {
|
203 | next();
|
204 | }
|
205 | }
|
206 | };
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 | function checkImports(path, fn) {
|
217 | var nodes = imports[path];
|
218 | if (!nodes) return fn();
|
219 | if (!nodes.length) return fn();
|
220 |
|
221 | var pending = nodes.length
|
222 | , changed = [];
|
223 |
|
224 | nodes.forEach(function(imported){
|
225 | fs.stat(imported.path, function(err, stat){
|
226 |
|
227 | if (err || !imported.mtime || stat.mtime > imported.mtime) {
|
228 | changed.push(imported.path);
|
229 | }
|
230 | --pending || fn(changed);
|
231 | });
|
232 | });
|
233 | }
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 | function compare(pathA, pathB) {
|
245 | pathA = pathA.split(sep);
|
246 | pathB = pathB.split(sep);
|
247 | if (!pathA[pathA.length - 1]) pathA.pop();
|
248 | if (!pathB[0]) pathB.shift();
|
249 | var overlap = [];
|
250 |
|
251 | while (pathA[pathA.length - 1] == pathB[0]) {
|
252 | overlap.push(pathA.pop());
|
253 | pathB.shift();
|
254 | }
|
255 | return overlap.join(sep);
|
256 | }
|