-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubmarine-bundler.js
145 lines (124 loc) · 3.4 KB
/
submarine-bundler.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
(function(global){
if(global.Submarine){
return;
}
var _isAnyRequired = false;
global.Submarine = {};
Submarine.all = {};
Submarine.map = {};
Submarine.useGlobalRequire = false;
Submarine.useGlobalVars = true;
var resolvedPathCache = {};
var pathsMapReverse = null;
var resolvePath = function(currentPath, relativePath){
var cacheKey = currentPath + ':' + relativePath;
if(resolvedPathCache[cacheKey]){
return resolvedPathCache[cacheKey];
}
var currentPathParts = (currentPath || '').split('/');
currentPathParts.pop();
var relativePathParts = (relativePath || '').split('/');
var m1;
var newPathParts = [];
while(true){
m1 = relativePathParts.shift();
if(m1 == '..'){
if (!currentPathParts.length) {
newPathParts.push('..');
}
currentPathParts.pop();
continue;
}
if(m1 == '.'){
continue;
}
if (m1 != '.') {
newPathParts.push(m1);
}
break;
}
var resolvedPath = currentPathParts.concat(newPathParts).concat(relativePathParts).join('/');
return resolvedPathCache[cacheKey] = resolvedPath;
}
var createRelativeRequire = function(selfModuleId){
var selfModuleIdReal = selfModuleId;
if (Submarine.map[selfModuleId] && Submarine.map[selfModuleId].realPath) {
selfModuleIdReal = Submarine.map[selfModuleId].realPath;
}
return function requireRelative(moduleIdRelative){
if(moduleIdRelative.indexOf('./') == -1 && moduleIdRelative.indexOf('../') == -1){
var moduleId = moduleIdRelative;
}
else {
var moduleId = resolvePath(selfModuleIdReal, moduleIdRelative);
}
if (!_isAnyRequired) {
_isAnyRequired = true;
}
if (!pathsMapReverse) {
pathsMapReverse = {};
for(var mm in Submarine.map){
if (!Submarine.map[mm].realPath) {
continue;
}
pathsMapReverse[Submarine.map[mm].realPath] = mm;
}
}
var mod = Submarine.all[moduleId];
if (!mod) {
if (pathsMapReverse[moduleId]) {
mod = Submarine.all[pathsMapReverse[moduleId]];
}
else if (pathsMapReverse[moduleId + '.js']) {
mod = Submarine.all[pathsMapReverse[moduleId + '.js']];
}
}
Submarine.beforeRequire(moduleId);
if (!mod) {
var exports = {};
if(Submarine.useGlobalRequire && global.require){
exports = global.require(moduleId);
}
else if(Submarine.useGlobalVars && window[moduleId]) {
exports = window[moduleId];
}
else {
console.error('Module "' + moduleId + '" not found!. (required in "' + selfModuleId + '"');
}
Submarine.afterRequire(moduleId, exports);
return exports;
}
if(!mod.invoked){
try {
mod.invoked = true;
mod.def(mod.require, mod.module.exports, mod.module);
}
catch(e){
throw e;
}
}
Submarine.afterRequire(moduleId, mod.module.exports);
return mod.module.exports;
}
}
var requireAbsolute = createRelativeRequire(null);
Submarine.beforeRequire = function(moduleId){
}
Submarine.afterRequire = function(moduleId, exports){
}
Submarine.require = function(moduleId){
return requireAbsolute(moduleId);
};
Submarine.register = function(moduleId, moduleDef){
var mod = Submarine.all[moduleId] || {};
mod.id = moduleId;
mod.def = moduleDef;
mod.module = mod.module || {exports: {}};
mod.require = mod.require || createRelativeRequire(moduleId);
mod.invoked = false;
Submarine.all[moduleId] = mod;
};
if(!global.require){
global.require = Submarine.require;
}
})(window);