-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake.js
208 lines (115 loc) · 3.24 KB
/
make.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
var isArray = Array.isArray;
var isString = function(v) { return typeof v === 'string'; }
var isObject = function(v) { return typeof v === 'object'; }
var isFunction = function(v) { return typeof v === 'function'; }
var fs = require('fs');
var make_json_js_fileName = 'make.json.js';
var make_json_js_fileContent = fs.readFileSync(make_json_js_fileName, 'binary');
var makeJson;
eval('makeJson = ' + make_json_js_fileContent);
var name = makeJson.name;
var sources = makeJson.sources;
var stylesheets = makeJson.stylesheets;
var pre = makeJson.pre;
var dependencies = makeJson.dependencies;
var workers = makeJson.workers;
var oSources = "",
oStylesheets = "",
oPre = "",
oDependencies = "",
oWorkers = "";
if(sources) {
oSources = concatFiles(sources, ';\n');
}
if(stylesheets) {
oStylesheets = concatFiles(stylesheets, '\n');
}
if(pre) {
oPre = concatFiles(pre, ';\n');
}
if(dependencies) {
oDependencies = concatDependencies(dependencies);
}
if(workers) {
oWorkers = concatWorkers(workers);
}
var oJs, oCss;
oCss = oStylesheets;
oJs = [oDependencies, oWorkers, oPre, oSources].join(';\n');
var buildDir = 'build/';
var oJsFileName = buildDir + name + '.js';
var oCssFileName = buildDir + name + '.css';
fs.writeFileSync(oJsFileName, oJs);
fs.writeFileSync(oCssFileName, oCss);
var workerProxies = {};
function concatWorkers(workers) {
var cat = [];
for(var i=0,ii=workers.length;i<ii;i++) {
var worker = workers[i];
cat.push(processWorker(worker));
}
cat = cat.join('\n');
var ret = '(function(){' +
'var workerProxies = {};' +
cat +
'\n' +
'var _Worker = window.Worker;\n' +
'window.Worker = function(a) {\n' +
'if(workerProxies[a]) { a = workerProxies[a]; }\n' +
'return new _Worker(a);\n' +
'}\n' +
'})();\n';
return ret;
}
function processWorker(worker) {
var proxy = worker.proxy;
var sources = worker.sources;
var main = worker.main;
var oSources = concatFiles(sources);
var oMain = concatFiles([main]);
var src = [oSources, oMain].join('\n');
// src = '(function(){' +
// 'importScripts = function(){};' +
// src +
// '})()';
// src = 'workerProxies['+JSON.stringify(proxy)+'] = URL.createObjectURL( new Blob([' +
// src +
// '], { type: "application/javascript" }));\n';
src = 'function(){' +
'importScripts = function(){};' +
src +
'}.toString()';
src = 'workerProxies['+JSON.stringify(proxy)+'] = URL.createObjectURL( new Blob(["(",' +
src +
', ")()"], { type: "application/javascript" }));\n';
return src;
}
function concatDependencies(deps, joinChar) {
joinChar = (joinChar === undefined) ? '\n': joinChar;
var cat = [];
for(var i=0,ii=deps.length;i<ii;i++) {
var fileName = deps[i].src;
var fileContent = fs.readFileSync(fileName, 'binary');
cat.push(fileContent);
}
cat = cat.join(joinChar);
return cat;
}
function concatFiles(fileNames, joinChar) {
joinChar = (joinChar === undefined) ? '\n': joinChar;
var cat = [];
for(var i=0,ii=fileNames.length;i<ii;i++) {
var fileName = fileNames[i];
var fileContent = fs.readFileSync(fileName, 'binary');
cat.push(fileContent);
}
cat = cat.join(joinChar);
return cat;
}
function repeatStr(str, n) {
var ret = '';
for(var i=0;i<n;i++) {
ret += str;
}
return ret;
}