evaluator.js
4.38 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
// Except for a block in #visitImport, everything here is an exact copy of the
// source in stylus this currently depends on. If stylus is updated, update
// this appropriately.
var Evaluator = require('stylus/lib/visitor/evaluator')
, nodes = require('stylus/lib/nodes')
, Stack = require('stylus/lib/stack')
, Frame = require('stylus/lib/stack/frame')
, Scope = require('stylus/lib/stack/scope')
, utils = require('stylus/lib/utils')
, bifs = require('stylus/lib/functions')
, basename = require('path').basename
, dirname = require('path').dirname
, relative = require('path').relative
, join = require('path').join
, colors = require('stylus/lib/colors')
// , debug = require('debug')('stylus:evaluator')
, fs = require('fs');
module.exports = CachedPathEvaluator;
/**
* Import `file` and return Block node.
*
* @api private
*/
function importFile(node, file, literal, index) {
var importStack = this.importStack
, Parser = require('stylus/lib/parser')
, stat;
// Handling the `require`
if (node.once) {
if (this.requireHistory[file]) return nodes.null;
this.requireHistory[file] = true;
if (literal && !this.includeCSS) {
return node;
}
}
// Expose imports
node.path = file;
node.dirname = dirname(file);
// Store the modified time
stat = fs.statSync(file);
node.mtime = stat.mtime;
this.paths.push(node.dirname);
// Avoid overflows from importing the same file over again
if (file === importStack[importStack.length - 1]) return nodes.null;
if (this.options._imports) this.options._imports.push(node.clone());
// Parse the file
importStack.push(file);
nodes.filename = file;
var str;
if (this.cache.sources && this.cache.sources[file]) {
str = this.cache.sources[file];
} else {
str = fs.readFileSync(file, 'utf8');
}
if (literal && !this.resolveURL) return new nodes.Literal(str.replace(/\r\n?/g, '\n'));
// parse
var block = new nodes.Block
, parser = new Parser(str, utils.merge({ root: block }, this.options));
try {
block = parser.parse();
} catch (err) {
err.filename = file;
err.lineno = parser.lexer.lineno;
err.input = str;
throw err;
}
// Evaluate imported "root"
block.parent = this.root;
block.scope = false;
var ret = this.visit(block);
importStack.pop();
if (importStack.length || index) this.paths.pop();
return ret;
}
function CachedPathEvaluator(root, options) {
Evaluator.apply(this, arguments);
this.cache = options.cache;
}
CachedPathEvaluator.prototype = Object.create(Evaluator.prototype);
CachedPathEvaluator.prototype.constructor = CachedPathEvaluator;
CachedPathEvaluator.prototype.visitImport = function(imported) {
this.return++;
var path = this.visit(imported.path).first
, nodeName = imported.once ? 'require' : 'import'
, found
, literal
, index;
this.return--;
// debug('import %s', path);
// url() passed
if ('url' == path.name) {
if (imported.once) throw new Error('You cannot @require a url');
return imported;
}
// Ensure string
if (!path.string) throw new Error('@' + nodeName + ' string expected');
var name = path = path.string;
// Absolute URL
if (/url\s*\(\s*['"]?(?:https?:)?\/\//i.test(path)) {
if (imported.once) throw new Error('You cannot @require a url');
return imported;
}
// Literal
if (/\.css(?:"|$)/.test(path)) {
literal = true;
if (!imported.once && !this.includeCSS) {
return imported;
}
}
// support optional .styl
if (!literal && !/\.styl$/i.test(path)) path += '.styl';
/*****************************************************************************
* THIS IS THE ONLY BLOCK THAT DIFFERS FROM THE ACTUAL STYLUS IMPLEMENTATION. *
*****************************************************************************/
// Lookup
var dirname = this.paths[this.paths.length - 1];
found = this.cache.find(path, dirname);
index = this.cache.isIndex(path, dirname);
if (!found) {
found = utils.find(path, this.paths, this.filename);
if (!found) {
found = utils.lookupIndex(name, this.paths, this.filename);
index = true;
}
}
// Throw if import failed
if (!found) throw new Error('failed to locate @' + nodeName + ' file ' + path);
var block = new nodes.Block;
for (var i = 0, len = found.length; i < len; ++i) {
block.push(importFile.call(this, imported, found[i], literal, index));
}
return block;
}