/
File.js
75 lines (61 loc) · 1.64 KB
/
File.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
define([ './env' ], function (env) {
// from dojo loader
function resolveRelativeId(path) {
var result = [], segment, lastSegment;
path = path.split('/');
while (path.length) {
segment = path.shift();
if (segment === '..' && result.length && lastSegment != '..') {
result.pop();
}
else if(segment != '.') {
result.push((lastSegment = segment));
} // else ignore '.'
}
return result.join('/');
}
function getModuleIdFromPath(path) {
var result = resolveRelativeId(path),
match = false;
for (var module in env.config.prefixMap) {
var pathPrefix = env.config.baseUrl + env.config.prefixMap[module];
// avoid accidental matching of partial paths
if (pathPrefix.charAt(-1) !== '/') {
pathPrefix += '/';
}
if (result.indexOf(pathPrefix) === 0) {
result = result.substr(pathPrefix.length);
match = true;
break;
}
}
result = result.replace(/^\/|\.js$/g, '');
// TODO: Update to use more traditional AMD module map pattern
return !match ? result : (result === 'main' ? module : module + '/' + result);
}
function File(filename) {
if (!(this instanceof File)) {
return new File(filename);
}
this.filename = filename;
this.moduleId = getModuleIdFromPath(filename);
return this;
}
File.prototype = {
constructor: File,
/**
* The absolute filename of the current file.
* @type string
*/
filename: undefined,
/**
* The module ID based on the provided filename.
* @type string
*/
moduleId: undefined,
toString: function () {
return '[object File(filename: ' + this.filename + ', moduleId: ' + this.moduleId + ')]';
}
};
return File;
});