-
Notifications
You must be signed in to change notification settings - Fork 55
/
build.js
355 lines (350 loc) · 10.1 KB
/
build.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
* This module can be executed standalone in node, to build CSS files, inlining imports, and
* pre-processing extensions for faster run-time execution. This module is also
* used by the AMD build process.
*/
var operatorMatch = {
'{': '}',
'[': ']',
'(': ')'
};
var nextId = 1;
document = {
createElement: function(){
return {style:
{// TODO: may want to import static has features to determine if some of these exist
}
};
},
getElementsByTagName: function(){
return [];
},
addEventListener: function(){}
};
navigator = {
userAgent: "build"
};
var pseudoDefine = function(id, deps, factory){
parse = factory({
isBuild: true,
isTagSupported: function(){
return true;
}
});
};
var requiredModules = [], base64Module;
if(typeof define == 'undefined'){
var fs = require('fs'),
pathModule = require('path'),
parse;
define = pseudoDefine;
require('./core/parser');
}else{
define(['build/fs', 'build/fileUtils', './build/base64'], function(fsModule, fileUtils, base64){
fs = fsModule;
base64Module = base64;
// must create our own path module for Rhino :/
pathModule = {
resolve: function(base, target){
return fileUtils.compactPath(base.replace(/[^\/]+$/, '') + target);
},
dirname: function(path){
return path.replace(/[\/\\][^\/\\]*$/, '');
},
relative: function(basePath, path){
basePath = this.dirname(basePath);
var baseParts = basePath.split(/[\/\\]/);
var parts = path.split(/[\/\\]/);
var l = parts.length;
for (var i = 0; i < l; i++){
if (parts[i] !== baseParts[i]){
break;
}
}
parts.splice(0, i);
for (; i < baseParts.length; i++){
parts.unshift('..');
}
return parts.join('/');
},
join: function(base, target){
return fileUtils.compactPath((base[base.length - 1] == '/' ? base : (base + '/')) + target);
}
};
return function(xstyleText){
var define = pseudoDefine;
var vm;
try {
require(['dojo/node!vm']);
vm = require('dojo/node!vm');
} catch (e) {
}
vm ?
vm.runInNewContext(xstyleText, {
define: define,
console: console
}, 'xstyle/core/parser.js') :
eval(xstyleText);
return processCss;
};
});
}
function main(source, target){
var imported = {};
var basePath = source.replace(/[^\/]*$/, '');
var cssText = fs.readFileSync(source).toString("utf-8");
var processed = processCss(cssText, basePath, basePath);
var output = processed.standardCss;
if(processed.xstyleCss){
output += 'x-xstyle{content:"' +
processed.xstyleCss.replace(/["\\\n\r]/g, '\\$&') +
'";}';
}
if(target){
fs.writeFileSync(target, output);
}else{
console.log(output);
}
}
function minify(cssText){
return cssText.
replace(/\/\*([^\*]|\*[^\/])*\*\//g, ' ').
replace(/\s*("(\\\\|[^\"])*"|'(\\\\|[^\'])*'|[;}{:])\s*/g,"$1").
replace(/[^\x00-\x7F]([0-9a-fA-F])?/g, function(character, hexComesNext){
// escape non-ascii characters to be safe
return '\\' + character.charCodeAt(0).toString(16) + (hexComesNext ? ' ' + hexComesNext : '');
});
}
var mimeTypes = {
eot: "application/vnd.ms-fontobject",
woff: "application/font-woff",
gif: "image/gif",
jpg: "image/jpeg",
jpeg: "image/jpeg",
png: "image/png",
svg:"image/svg+xml"
}
function processCss(cssText, basePath, cssPath, inlineAllResources){
function XRule(){
}
var ruleCount = 0;
XRule.prototype = {
newCall: function(name){
var call = new Call(name);
call.parent = this;
return call;
},
childRules: 0,
newRule: function(name){
var rule = (this.rules || (this.rules = {}))[name] = new XRule();
rule.ruleIndex = this.childRules++;
rule.parent = this;
return rule;
},
getDefinition: function(name, includeRules){
var parentRule = this;
do{
var target = parentRule.definitions && parentRule.definitions[name]
|| (includeRules && parentRule.rules && parentRule.rules[name]);
parentRule = parentRule.parent;
}while(!target && parentRule);
if(target){
target.name = name;
}
return target;
},
declareDefinition: function(name, value, conditional){
// TODO: access staticHasFeatures to check conditional
var valueString = {
toString: function(){
return value.toString(2);
}
};
(this.xstyleCss = this.xstyleCss || []).push(name + '=', valueString, ';');
var definitions = (this.definitions || (this.definitions = {}));
definitions[name] = value || new XRule();
},
setValue: function(name, value){
var target = this.getDefinition(name);
var browserCss = this.browserCss = this.browserCss || [];
if(!this.ruleStarted && !this.root){
this.ruleStarted = true;
browserCss.push(this.selector, '{');
}
if(!target){
browserCss.push(name, ':', value, ';');
}
if(target || typeof value == 'object'){
if(!this.xstyleStarted){
this.xstyleStarted = true;
this.xstyleCss = this.xstyleCss || [];
}
this.xstyleCss.push(name + ':' + value + ';');
}
},
onArguments: function(){},
setMediaSelector: function(selector){
this.selector = '';
browserCss.push(selector, '{');
this.isMediaBlock = true;
},
toString: function(mode){
var str = '';
str += this.xstyleCss ? this.xstyleCss.join('') : '';
for(var i in this.rules){
var rule = this.rules[i];
if(rule.ref && !rule.creating){
str += rule.toString(1);
}
}
if(!this.root && !this.isMediaBlock && this.xstyleStarted && (str || mode != 1)){
str = ((mode == 2 && this.bases) || this.tagName || '') + '{' + this.ref + str.replace(/\s+/g, ' ') + '}';
}
return str;
},
onRule: function(){
if(this.browserCss){
this.browserCss.push('}');
this.ref = '/' + ruleCount;
ruleCount++;
browserCss.push(this.browserCss.join(''));
}
if(this.isMediaBlock){
browserCss.push('}');
}
},
extend: function(derivative, fullExtension){
var definitions = this.definitions;
if(definitions && fullExtension){
// TODO: need to mixin this in, if it already exists
derivative.definitions = Object.create(definitions);
}
(derivative.bases = derivative.bases || []).push(this.name);
}
};
// a class representing function calls
function Call(value){
// we store the caller and the arguments
this.caller = value;
this.args = [];
}
var CallPrototype = Call.prototype = new XRule;
CallPrototype.declareProperty = CallPrototype.setValue = function(name, value){
// handle these both as addition of arguments
this.args.push(value);
};
CallPrototype.toString = function(){
var operator = this.operator;
return operator + this.args + operatorMatch[operator];
};
function insertRule(cssText){
//browserCss.push(cssText);
}
function correctUrls(cssText, path){
// correct all the URLs in the stylesheets
// determine the directory path
path = pathModule.dirname(path) + '/';
//console.log("starting path", basePath , path);
// compute the relative path from where we are to the base path where the stylesheet will go
var relativePath = pathModule.relative(basePath, path);
return cssText.replace(/url\s*\(\s*['"]?([^'"\)]*)['"]?\s*\)/g, function(t, url){
if(url.match(/^data:/)){
return url;
}
if(/^#/.test(url)){ //do not replace urls which have only fragment (e.g. VML behavior property: "behavior: url(#default#VML);"")
return url;
}
if(inlineAllResources || /#inline$/.test(url)){
// we can inline the resource
suffix = url.match(/\.(\w+)(#|\?|$)/);
suffix = suffix && suffix[1];
url = url.replace(/[\?#].*/,'');
if(typeof java !== 'undefined'){
// reading binary is hard in rhino
var file = new java.io.File(pathModule.join(path, url));
var length = file.length();
var fis = new java.io.FileInputStream(file);
var bytes = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, length);
fis.read(bytes, 0, length);
var jsBytes = new Array(length);
for(var i = 0; i < bytes.length;i++){
var singleByte = bytes[i];
if(singleByte < 0){
singleByte = 256 + singleByte;
}
jsBytes[i] = singleByte;
}
var moduleText =base64Module.encode(jsBytes);
}else{
// in node base64 encoding is easy
var moduleText = fs.readFileSync(pathModule.join(path, url)).toString("base64");
}
return 'url("data:' + (mimeTypes[suffix] || 'application/octet-stream') +
';base64,' + moduleText + '")';
}
// or we adjust the URL
var adjustedPath = pathModule.resolve(relativePath, url);
return 'url("' + adjustedPath + '")';
});
};
parse.getStyleSheet = function(importRule, sequence, styleSheet){
var importValue = sequence[1];
var path;
try{
if(importValue.operator === '('){
// a url() call
var firstArg = importValue.args[0];
// extract from a literal string, or just grab the raw text
// note that the correctUrls should have already resolved this path
path = firstArg.length? firstArg[0].value : firstArg.toString();
}else{
// assume it is a string
path = pathModule.resolve(styleSheet.href, importValue.value);
}
}catch(error){
console.error('Can not parse import value of ', error, importValue, firstArg);
}
var localSource = '';
try{
localSource = fs.readFileSync(path).toString('utf-8');
}catch(e){
console.error(e.stack || e);
}
localSource = correctUrls(localSource, path);
return {
localSource: localSource,
href: path || '.',
insertRule: insertRule,
cssRules: []
};
};
var browserCss = [];//[correctUrls(cssText, basePath + "placeholder.css")];
var rootRule = new XRule;
rootRule.root = true;
parse(rootRule, correctUrls(cssText, cssPath),
{href: cssPath || '.', cssRules:[], insertRule: insertRule});
rootRule.definitions = {
Math:1,
require:1,
item: 1,
'native': 1,
prefixed: 1
}
function visit(parent){
//browserCss.push(parent.selector + '{' + parent.cssText + '}');
/*for(var i in parent.variables){
if(!intrinsicVariables.hasOwnProperty(i)){
xstyleCss.push(i,'=',parent.variables[i]);
}
}*/
}
visit(rootRule);
return {
standardCss: minify(browserCss.join('')),
xstyleCss: rootRule.toString(),
requiredModules: requiredModules
};
}
if(typeof module != 'undefined' && require.main == module){
main.apply(this, process.argv.slice(2));
}