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