forked from enyojs/enyo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.js
292 lines (281 loc) · 8.41 KB
/
loader.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
/* global enyo:true */
(function() {
enyo = window.enyo || {options: {}};
enyo.pathResolverFactory = function() {
this.paths = {};
this.pathNames = [];
};
enyo.pathResolverFactory.prototype = {
addPath: function(inName, inPath) {
this.paths[inName] = inPath;
this.pathNames.push(inName);
this.pathNames.sort(function(a, b) {
return b.length - a.length;
});
return inPath;
},
addPaths: function(inPaths) {
if (inPaths) {
for (var n in inPaths) {
this.addPath(n, inPaths[n]);
}
}
},
includeTrailingSlash: function(inPath) {
return (inPath && inPath.slice(-1) !== "/") ? inPath + "/" : inPath;
},
// replace macros of the form $pathname with the mapped value of paths.pathname
rewrite: function (inPath) {
var working, its = this.includeTrailingSlash, paths = this.paths;
var fn = function(macro, name) {
working = true;
return its(paths[name]) || '';
};
var result = inPath;
do {
working = false;
for (var i=0; i<this.pathNames.length; i++) {
var regex = new RegExp("\\$(" + this.pathNames[i] + ")(\\/)?", "g");
result = result.replace(regex, fn);
}
} while (working);
return result;
}
};
enyo.path = new enyo.pathResolverFactory();
enyo.loaderFactory = function(inMachine, inPathResolver) {
this.machine = inMachine;
// package information
this.packages = [];
// module information
this.modules = [];
// stylesheet paths
this.sheets = [];
// designer metadata paths
this.designs = [];
// (protected) internal dependency stack
this.stack = [];
this.pathResolver = inPathResolver || enyo.path;
this.packageName = "";
this.packageFolder = "";
this.finishCallbacks = {};
};
enyo.loaderFactory.prototype = {
verbose: false,
loadScript: function(inScript, success, failure) {
this.machine.script(inScript, success, failure);
},
loadSheet: function(inSheet) {
this.machine.sheet(inSheet);
},
loadPackage: function(inPackage) {
this.machine.script(inPackage);
},
report: function() {
},
//
load: function(/*<inDependency0, inDependency1 ...>*/) {
// begin processing dependencies
this.more({
index: 0,
depends: arguments || []
});
},
more: function(inBlock) {
// a 'block' is a dependency list with a bookmark
// the bookmark (index) allows us to interrupt
// processing and then continue asynchronously.
if (inBlock) {
// returns true if this block has asynchronous requirements
// in that case, we unwind the stack. The asynchronous loader
// must provide the continuation (by calling 'more' again).
if (this.continueBlock(inBlock)) {
return;
}
}
// A package is now complete. Pop the block that was interrupted for that package (if any).
var block = this.stack.pop();
if (block) {
// propagate failed scripts to queued block
if(enyo.runtimeLoading && inBlock.failed) {
block.failed = block.failed || [];
block.failed.push.apply(block.failed, inBlock.failed);
}
// block.packageName is the name of the package that interrupted us
//this.report("finished package", block.packageName);
if (this.verbose) {
window.console.groupEnd("* finish package (" + (block.packageName || "anon") + ")");
}
// cache the folder for the currently processing package
this.packageFolder = block.folder;
// no current package
this.packageName = "";
// process this new block
this.more(block);
} else {
this.finish(inBlock);
}
},
finish: function(inBlock) {
this.packageFolder = "";
if (this.verbose) {
window.console.log("-------------- fini");
}
for (var i in this.finishCallbacks) {
if (this.finishCallbacks[i]) {
var callback = this.finishCallbacks[i];
this.finishCallbacks[i] = null;
callback(inBlock);
}
}
},
continueBlock: function(inBlock) {
while (inBlock.index < inBlock.depends.length) {
var d = inBlock.depends[inBlock.index++];
if (d) {
if (typeof d == "string") {
if (this.require(d, inBlock)) {
// return true to indicate we need to interrupt
// processing until asynchronous file load completes
// the load process itself must provide the
// continuation
return true;
}
} else {
this.pathResolver.addPaths(d);
}
}
}
},
require: function(inPath, inBlock) {
// process aliases
var path = this.pathResolver.rewrite(inPath);
// get path root
var prefix = this.getPathPrefix(inPath);
// assemble path
path = prefix + path;
// process path
if ((path.slice(-4).toLowerCase() == ".css") || (path.slice(-5).toLowerCase() == ".less")) {
if (this.verbose) {
window.console.log("+ stylesheet: [" + prefix + "][" + inPath + "]");
}
this.requireStylesheet(path);
} else if (path.slice(-3).toLowerCase() == ".js" && path.slice(-10).toLowerCase() != "package.js") {
if (this.verbose) {
window.console.log("+ module: [" + prefix + "][" + inPath + "]");
}
return this.requireScript(inPath, path, inBlock);
} else if (path.slice(-7).toLowerCase() == ".design") {
if (this.verbose) {
window.console.log("+ design metadata: [" + prefix + "][" + inPath + "]");
}
this.requireDesign(path);
} else {
// package
this.requirePackage(path, inBlock);
// return true to indicate a package was located and
// we need to interrupt further processing until it's completed
return true;
}
},
getPathPrefix: function(inPath) {
var delim = inPath.slice(0, 1);
if ((delim != "/") && (delim != "\\") && (delim != "$") && !/^https?:/i.test(inPath)) {
return this.packageFolder;
}
return "";
},
requireStylesheet: function(inPath) {
// stylesheet
this.sheets.push(inPath);
this.loadSheet(inPath);
},
requireScript: function(inRawPath, inPath, inBlock) {
// script file
this.modules.push({
packageName: this.packageName,
rawPath: inRawPath,
path: inPath
});
if(enyo.runtimeLoading) {
var _this = this;
var success = function() {
_this.more(inBlock);
};
var failure = function() {
inBlock.failed = inBlock.failed || [];
inBlock.failed.push(inPath);
_this.more(inBlock);
};
this.loadScript(inPath, success, failure);
} else {
this.loadScript(inPath);
}
return enyo.runtimeLoading;
},
requireDesign: function(inPath) {
// designer metadata (no loading here)
this.designs.push({
packageName: this.packageName,
path: inPath
});
},
decodePackagePath: function(inPath) {
// A package path can be encoded in two ways:
//
// 1. [folder]
// 2. [folder]/[*package.js]
//
// Note: manifest file name must end in "package.js"
//
var folder = '', manifest = 'package.js';
// convert back slashes to forward slashes, remove double slashes, split on slash
var parts = inPath.replace(/\\/g, "/").replace(/\/\//g, "/").replace(/:\//, "://").split("/");
if (parts.length) {
// if inPath has a trailing slash, parts has an empty string which we pop off and ignore
var name = parts.pop() || parts.pop() || "";
// test if name includes the manifest tag
if (name.slice(-manifest.length) !== manifest) {
// if not a manifest name, it's part of the folder path
parts.push(name);
} else {
// otherwise this is the manifest name
manifest = name;
}
//
folder = parts.join("/");
folder = (folder ? folder + "/" : "");
manifest = folder + manifest;
}
return {
folder: folder,
manifest: manifest
};
},
aliasPackage: function(inPath) {
var parts = this.decodePackagePath(inPath);
// cache manifest path
this.manifest = parts.manifest;
},
requirePackage: function(inPath, inBlock) {
// cache the interrupted packageFolder
inBlock.folder = this.packageFolder;
// set new manifest/packageFolder
var parts = this.decodePackagePath(inPath);
this.manifest = parts.manifest;
this.packageFolder = parts.folder;
// cache the name of the package 'inBlock' is loading now
inBlock.packageName = this.packageName;
// push inBlock on the continuation stack
this.stack.push(inBlock);
// console/user reporting
this.report("loading package", this.packageName);
if (this.verbose) {
window.console.group("* start package [" + this.packageName + "]");
}
// load the actual package. the package MUST call a continuation function
// or the process will halt.
this.loadPackage(this.manifest);
}
};
})();