forked from enyojs/enyo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.js
280 lines (277 loc) · 8.02 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
(function() {
enyo = window.enyo || {};
enyo.path = {
paths: {
},
addPath: function(inName, inPath) {
return this.paths[inName] = 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;
},
// match $name
rewritePattern: /\$([^\/\\]*)(\/)?/g,
// 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;
result = result.replace(this.rewritePattern, fn);
} while (working);
return result;
}
};
enyo.loaderFactory = function(inMachine) {
this.machine = inMachine;
// package information
this.packages = [];
// module information
this.modules = [];
// stylesheet paths
this.sheets = [];
// (protected) internal dependency stack
this.stack = [];
};
enyo.loaderFactory.prototype = {
packageName: "",
packageFolder: "",
verbose: false,
//
loadScript: function(inScript) {
this.machine.script(inScript);
},
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) {
// block.packageName is the name of the package that interrupted us
//this.report("finished package", block.packageName);
this.verbose && 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();
}
},
finish: function() {
this.packageFolder = "";
this.verbose && console.log("-------------- fini");
},
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 {
enyo.path.addPaths(d);
}
}
}
},
require: function(inPath, inBlock) {
// process aliases
var path = enyo.path.rewrite(inPath);
// get path root
var prefix = this.getPathPrefix(inPath);
// assemble path
path = prefix + path;
// process path
if (path.slice(-3) == "css") {
this.verbose && console.log("+ stylesheet: [" + prefix + "][" + inPath + "]");
this.requireStylesheet(path);
} else if (path.slice(-2) == "js" && path.slice(-10) != "package.js") {
this.verbose && console.log("+ module: [" + prefix + "][" + inPath + "]");
this.requireScript(inPath, 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 != "$") && (inPath.slice(0, 5) != "http:")) {
return this.packageFolder;
}
return "";
},
requireStylesheet: function(inPath) {
// stylesheet
this.sheets.push(inPath);
this.loadSheet(inPath);
},
requireScript: function(inRawPath, inPath) {
// script file
this.modules.push({
packageName: this.packageName,
rawPath: inRawPath,
path: inPath
});
this.loadScript(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 alias = '', target = '', 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;
//
// build friendly aliasing:
//
for (var i=parts.length-1; i >= 0; i--) {
if (parts[i] == "source") {
parts.splice(i, 1);
break;
}
}
target = parts.join("/");
//
// portable aliasing:
//
// packages that are rooted at a folder named "enyo" or "lib" do not
// include that root path in their alias
//
// remove */lib or */enyo prefix
//
// e.g. foo/bar/baz/lib/zot -> zot package
//
for (var i=parts.length-1, p; p=parts[i]; i--) {
if (p == "lib" || p == "enyo") {
parts = parts.slice(i+1);
break;
}
}
// remove ".." and "."
for (var i=parts.length-1, p; p=parts[i]; i--) {
if (p == ".." || p == ".") {
parts.splice(i, 1);
}
}
//
alias = parts.join("-");
}
return {
alias: alias,
target: target,
folder: folder,
manifest: manifest
};
},
aliasPackage: function(inPath) {
var parts = this.decodePackagePath(inPath);
// cache manifest path
this.manifest = parts.manifest;
// cache package info for named packages
if (parts.alias) {
// debug only
/*
var old = enyo.path.paths[parts.name];
if (old && old != parts.folder) {
this.verbose && console.warn("mapping alias [" + parts.name + "] to [" + parts.folder + "] replacing [" + old + "]");
}
this.verbose && console.log("mapping alias [" + parts.name + "] to [" + parts.folder + "]");
*/
//
// create a path alias for this package
enyo.path.addPath(parts.alias, parts.target);
//
// cache current name
this.packageName = parts.alias;
// cache package information
this.packages.push({
name: parts.alias,
folder: parts.folder
});
}
// cache current folder
this.packageFolder = parts.folder;
},
requirePackage: function(inPath, inBlock) {
// cache the interrupted packageFolder
inBlock.folder = this.packageFolder;
this.aliasPackage(inPath);
// 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);
this.verbose && 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);
}
};
})();