forked from kriszyp/xstyle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
166 lines (163 loc) · 4.7 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
/*
* 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.
*/
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){
function pseudoRequire(deps){
[].push.apply(requiredModules, deps);
}
pseudoRequire.isBuild = true;
xstyle = factory(pseudoRequire);
};
var requiredModules = [];
if(typeof define == 'undefined'){
var fs = require('fs'),
pathModule = require('path'),
xstyle;
define = pseudoDefine;
require('./xstyle');
}else{
define(['build/fs'], function(fsModule){
fs = fsModule;
pathModule = {
resolve: function(base, target){
return (base.replace(/[^\/]+$/, '') + target)
.replace(/\/[^\/]*\/\.\./g, '')
.replace(/\/\./g,'');
},
dirname: function(path){
return path.replace(/[\/\\][^\/\\]*$/, '');
},
relative: function(basePath, path){
return path.slice(this.dirname(basePath).length + 1);
},
join: function(base, target){
return ((base[base.length - 1] == '/' ? base : (base + '/'))+ target)
.replace(/\/[^\/]*\/\.\./g, '')
.replace(/\/\./g,'');
}
}
return function(xstyleText){
var define = pseudoDefine;
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);
var output = processed.standardCss;
if(processed.xstyleCss){
output = 'x-xstyle{content:"' +
processed.xstyleCss.replace(/["\\\n\r]/g, '\\$&') +
'";}' + output;
}
if(target){
fs.writeFileSync(target, output);
}else{
console.log(output);
}
}
function minify(cssText){
return cssText.
replace(/\/\*([^\*]|\*[^\/])*\*\//g, ' ').
replace(/\s*("(\\\\|[^\"])*"|'(\\\\|[^\'])*'|[;}{:])\s*/g,"$1");
}
var mimeTypes = {
eot: "application/vnd.ms-fontobject",
woff: "application/font-woff",
gif: "image/gif",
jpg: "image/jpeg",
jpeg: "image/jpeg",
png: "image/png"
}
function processCss(cssText, basePath, inlineAllResources){
console.log("processing",basePath);
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){
//console.log("relativePath", relativePath, pathModule.resolve(path, url), pathModule.join(relativePath, url));
if(inlineAllResources || /#inline$/.test(url)){
// we can inline the resource
suffix = url.match(/\.(\w+)(#|\?|$)/);
suffix = suffix && suffix[1];
url = url.replace(/[\?#].*/,'');
return 'url(data:' + (mimeTypes[suffix] || 'application/octet-stream') +
';base64,' + fs.readFileSync(pathModule.resolve(path, url)).toString("base64") + ')';
}
// or we adjust the URL
return 'url("' + pathModule.join(relativePath, url).replace(/\\/g, '/') + '")';
});
}
xstyle.parse.getStyleSheet = function(importRule, sequence, styleSheet){
var path = pathModule.resolve(styleSheet.href, sequence[1].value);
var localSource = '';
try{
localSource = fs.readFileSync(path).toString("utf-8");
}catch(e){
console.error(e);
}
browserCss.push(correctUrls(localSource, path));
return {
localSource: localSource,
href: path || '.',
insertRule: insertRule,
cssRules: []
}
};
var browserCss = [cssText];
var xstyleCss = [];
var rootRule = xstyle.parse(cssText, {href:basePath || '.', cssRules:[], insertRule: insertRule});
var intrinsicVariables = {
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);
//console.log('browserCss', browserCss);
return {
standardCss: minify(browserCss.join('')),
xstyleCss: xstyleCss.join(';'),
requiredModules: requiredModules
};
}
if(typeof module != 'undefined' && require.main == module){
main.apply(this, process.argv.slice(2));
}