-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathmodule.js
More file actions
132 lines (102 loc) · 3.58 KB
/
Copy pathmodule.js
File metadata and controls
132 lines (102 loc) · 3.58 KB
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
function resource(path) {
return window.__resources__[path].data;
}
(function() {
var process = {};
var modulePaths = ['/', '/libs'];
var path; // Will be loaded further down
function resolveModulePath(request, parent) {
// If not a relative path then search the modulePaths for it
var start = request.substring(0, 2);
if (start !== "./" && start !== "..") {
return modulePaths;
}
var parentIsIndex = path.basename(parent.filename).match(/^index\.js$/),
parentPath = parentIsIndex ? parent.id : path.dirname(parent.id);
// Relative path so searching inside parent's directory
return [path.dirname(parent.filename)];
}
function findModulePath(id, dirs) {
if (id.charAt(0) === '/') {
dirs = [''];
}
for (var i = 0; i < dirs.length; i++) {
var dir = dirs[i];
var p = path.join(dir, id);
// Check for index first
if (path.exists(path.join(p, 'index.js'))) {
return path.join(p, 'index.js');
} else if (path.exists(p + '.js')) {
return p + '.js';
}
}
return false;
}
function loadModule(request, parent) {
parent = parent || process.mainModule;
var paths = resolveModulePath(request, parent),
filename = findModulePath(request, paths);
if (filename === false) {
throw "Unable to find module: " + request;
}
if (parent) {
var cachedModule = parent.moduleCache[filename];
if (cachedModule) {
return cachedModule;
}
}
//console.log('Loading module: ', filename);
var module = new Module(filename, parent);
module._initialize(filename);
return module;
}
function Module(id, parent) {
this.id = id;
this.parent = parent;
this.children = [];
this.exports = {};
if (parent) {
this.moduleCache = parent.moduleCache;
parent.children.push(this);
} else {
this.moduleCache = {};
}
this.moduleCache[this.id] = this;
this.filename = null;
this.dirname = null;
}
Module.prototype._initialize = function(filename) {
var module = this;
function require(request) {
return loadModule(request, module).exports;
}
this.filename = filename;
// Work around incase this IS the path module
if (path) {
this.dirname = path.dirname(filename);
} else {
this.dirname = '';
}
require.paths = modulePaths;
require.main = process.mainModule;
window.__resources__[this.filename].data.apply(this.exports, [this.exports, require, this, this.filename, this.dirname])
return this;
}
// Manually load the path module because we need it to load other modules
path = (new Module('path'))._initialize('/path.js').exports;
// Populate globals
var globals = loadModule('global').exports;
for (var x in globals) {
window[x] = globals[x];
}
var util = loadModule('util').exports;
util.ready(function() {
process.mainModule = loadModule(__main_module_name__);
});
// Add a global require. Useful in the debug console.
window.require = function require(request, parent) {
return loadModule(request, parent).exports;
}
window.require.paths = modulePaths;
window.require.main = process.mainModule;
})();