-
Notifications
You must be signed in to change notification settings - Fork 16
/
concat.js
306 lines (254 loc) · 8.31 KB
/
concat.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
#!/usr/bin/env node
/**
* Script that takes files using angularjs-loader and concatenate them in order
* of dependencies.
*/
// We load angularjs-loader for being able to re-use most of its code.
window = {
__angularjs_loader_noinit: true
};
require('./angularjs-loader');
var angularJsLoader = {};
for (var name in window) {
angularJsLoader[name.replace(/^angularjs_loader_?/, '')] = window[name];
}
angular = window.angular;
var allDependencies = {};
var absoluteDependencies = [];
var root = '';
var stack = [];
var config = extend(angularJsLoader.config, {
shouldRecurse: {},
modules: []
});
var priority = 0;
// /**
// * Utility functions.
// */
// function noop() {};
function chainNoop() { return this; }
function extend(orig, extension, override) {
if (override === void 0)
override = true;
for (var name in extension) {
if (override || !(name in orig)) {
orig[name] = extension[name];
}
}
return orig;
}
function loadDependency(list, shouldRequire) {
var stackLength = stack.length;
var top;
if (stackLength > 0) {
top = stack[stack.length - 1];
}
for (var i = 0; i < list.length; i++) {
var name = list[i];
var path = angularJsLoader.pathFromModuleName(name, top && allDependencies[top].path);
if (path === null) {
continue;
}
var isAbsolute = path.search(/^(https?:)?\/\/.+/) == 0;
if (isAbsolute) {
if (absoluteDependencies.indexOf(path) == -1) {
absoluteDependencies.push(path);
}
continue;
}
if (top && allDependencies[top].deps.indexOf(name) == -1) {
allDependencies[top].deps.push(name);
}
if (!(name in allDependencies)) {
allDependencies[name] = {
deps: [],
name: name,
isRoot: false,
path: isAbsolute ? path : root + '/' + path,
priority: priority++
}
}
else {
// Already loaded.
continue;
}
stack.push(name);
shouldRequire = shouldRequire || config.shouldRecurse[name];
if (path !== null && shouldRequire && !isAbsolute) {
try {
require(root + '/' + path);
}
catch (e) {
console.error(e);
}
}
var x = stack.pop();
if (x != name) {
throw 'Value popped should have been the same as value pushed.';
}
}
if (stackLength != stack.length) {
throw 'Stack was changed during a dependency loading.';
}
};
function solveDependencies() {
var ordered = [];
// Make a copy.
var deps = extend({}, allDependencies);
var circular = false;
while (true) {
while (circular == false) {
circular = true;
for (var name in deps) {
var d = deps[name];
if (d.deps.length == 0) {
// We have a winner.
ordered.push(name);
delete deps[name];
circular = false;
// Delete it from dependencies. It's been revolved now.
for (var dname in deps) {
if (deps[dname].deps.indexOf(name) != -1) {
deps[dname].deps.splice(deps[dname].deps.indexOf(name), 1);
}
}
}
}
}
// Check for circular dependencies.
var lowestPriority = Infinity;
var lowestName = null;
// We have a circular dependency.
for (var name in deps) {
if (deps[name].priority < lowestPriority) {
lowestPriority = deps[name].priority;
lowestName = name;
}
}
if (lowestName === null) {
break; // Done!
}
// We have a winner.
ordered.push(lowestName);
delete deps[lowestName];
circular = false;
// Delete it from dependencies. It's been revolved now.
for (var dname in deps) {
if (deps[dname].deps.indexOf(lowestName) != -1) {
deps[dname].deps.splice(deps[dname].deps.indexOf(lowestName), 1);
}
}
}
return ordered;
}
angular.module = function(name, opt_deps /*, ...*/) {
if (opt_deps) {
loadDependency(opt_deps, true);
}
// A dummy module.
return {
config: chainNoop,
run: chainNoop,
controller: chainNoop,
directive: chainNoop,
factory: chainNoop,
filter: chainNoop,
value: chainNoop
}
};
angular.loader = function(path, options) {
if (typeof path === 'string') {
path = [path];
}
loadDependency(path, false);
// Fake promises.
return {
then: function(fn) {
try {
fn();
}
catch (ex) {
// Do Nothing with it.
}
}
}
}
angular.loader.config = function(cfg, additionalConfig) {
extend(config.path, cfg.path, false);
extend(config.checker, cfg.checker, false);
config.pathTransform = config.pathTransform.concat(cfg.pathTransform);
if (additionalConfig) {
extend(config.path, additionalConfig.path, true);
extend(config.checker, additionalConfig.checker, true);
config.shouldRecurse = additionalConfig.shouldRecurse;
config.pathTransform = config.pathTransform.concat(additionalConfig.pathTransform || []);
config.modules = config.modules.concat(additionalConfig.modules || []);
}
return {
then: function(fn) {
console.error(fn);
}
}
}
function main() {
var args = process.argv.splice(2);
for (var i = 0; i < args.length; i++) {
var path = args[i];
var pathArray = path.split('/');
var file = pathArray.pop();
root = pathArray.join('/');
loadDependency([root + '/' + file], true);
}
// Now go through all of them, and build an order of inclusion.
// This will reveal circular dependencies. We resolve them by having
// a priority associated with the dependency. The lower the priority,
// the better the order of inclusion.
var orderedDependencies = solveDependencies();
var fs = require('fs');
// Output a useless loader that should be optimized out.
// If you need scripts to manipulate this part, use the START and END
// tokens.
console.log(
"/** AngularJS Loader Script */\n"
+ "/*@START@@@@@@@@@@@@@@@@@@@@*/\n"
+ "/** @extern */"
+ "window['angular'] = window['angular'] || {};\n"
+ "/** @extern */"
+ "window['angular'].loader = function() {"
+ "return {then: function(fn) { window.setTimeout(fn, 0)}}"
+ "};\n"
+ "/** @extern */"
+ "window['angular'].loader.config = function() {};\n"
+ "function __angularjs_insertScript(path) {\n"
+ " var newScriptTag = document.createElement('script');\n"
+ " newScriptTag.type = 'text/javascript';\n"
+ " newScriptTag.src = path;\n"
+ "\n"
+ " newScriptTag.addEventListener('load', function(ev) {\n"
// + " d.resolve(caller);\n"
+ " });\n"
+ " newScriptTag.addEventListener('error', function(ev) {\n"
+ " error(12, [path], 'Error while loading the script: {0}.');\n"
+ " });\n"
+ " document.head.appendChild(newScriptTag);\n"
+ "};\n"
+ "/*@END@@@@@@@@@@@@@@@@@@@@@@*/\n"
+ "\n"
);
for (var i = 0; i < absoluteDependencies.length; i++) {
console.log('__angularjs_insertScript("' + absoluteDependencies[i] + '");\n');
}
for (var i = 0; i < orderedDependencies.length; i++) {
var src = allDependencies[orderedDependencies[i]].path;
var data = fs.readFileSync(src, 'utf-8');
console.log('/* __angularjs_loader("' + src + '") */');
console.log(data);
}
// Output the bootstrapping code, since we don't rely on the loader.
console.log(
'window.setTimeout(function() { angular.bootstrap(document, ["'
+ config.modules.join('","') + '"]); }, 0);'
);
}
main();
// console.log(allDependencies);