-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrequire.js
126 lines (108 loc) · 4.19 KB
/
require.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
/**
* @fileOverview An implentation of JavaScript modules for use in an ASP or
* WScript environment. This implementation is based on the one used in
* Narhwal
*
* @see <a href="https://wiki.mozilla.org/ServerJS/Modules/SecurableModules">ServerJS/Modules/SecurableModules</a>
* @see <a href="http://github.com/tlrobinson/narwhal/tree/master">Narwhal</a>
* @author Nathan L Smith <[email protected]>
* @date February 21, 2009
*/
/*global ActiveXObject, Response, Server, WScript, exports, require */
/*jslint evil:true */
(function () {
// Don't do anything if require is already there
if (typeof require === "function") { return; }
/** Global exports object */
if (typeof exports === "undefined") { exports = {}; }
/** A print function for use in logging */
function print() {
var out = Array.prototype.slice.call(arguments).join(" ");
if (typeof WScript === "object") {
WScript.echo(out);
} else if (Response && typeof Response.write !== "undefined") {
Response.write(out + "<br />");
}
}
////////////////////////////////////////////////////////////////////////////////
// Internal functions for file manipulation
////////////////////////////////////////////////////////////////////////////////
/**
* An implentation of readFile, which opens a file from the filesystem and
* returns its contents as a string
* @private
*/
function readFile(fileName) {
var contents,
fileSystem = new ActiveXObject("Scripting.FileSystemObject"),
mapPath = function mapPath(path) {
var isASP = typeof Server === "object" &&
typeof Server.mapPath !== "undefined";
return isASP ? Server.mapPath(path) : path;
};
fileName = mapPath(fileName);
if (fileSystem.fileExists(fileName)) {
try { // JScript will throw an error if the file is empty
contents = fileSystem.openTextFile(fileName).readAll();
} catch (e) { contents = ""; }
} else { throw new Error("File " + fileName + " does not exist"); }
return contents;
}
function dirname(path) {
var raw = String(path),
match = raw.match(/^(.*)\/[^\/]+\/?$/);
if (match && match[1]) { return match[1]; }
else if (raw.charAt(0) == "/") { return "/"; }
else { return "."; }
}
function canonicalize(path) {
return path.replace(/[^\/]+\/\.\.\//g, "").replace(/([^\.])\.\//g, "$1").
replace(/^\.\//g, "").replace(/\/\/+/g, "/");
}
////////////////////////////////////////////////////////////////////////////////
function _require(name, parentPath, loadOnce) {
var result, pwd, extensions, paths, path, searchDirectory, ext;
if (name.charAt(0) === "/") {
result = _attemptLoad(name, name, loadOnce);
if (result) { return result; }
} else {
pwd = dirname(parentPath);
extensions = (/\.\w+$/).test(name) ? [""] : require.extensions;
paths = ["."].concat(require.paths);
for (var j = 0; j < extensions.length; j++) {
ext = extensions[j];
for (var i = 0; i < paths.length; i++) {
searchDirectory = (paths[i] === ".") ? pwd : paths[i];
path = searchDirectory + "/" + name + ext;
result = _attemptLoad(name, path, loadOnce);
if (result) { return result; }
}
}
}
throw new Error("couldn't find " + name);
}
function _requireFactory(path, loadOnce) {
return function(name) {
return _require(name, path, loadOnce || false);
};
}
function _attemptLoad(name, path, loadOnce) {
path = canonicalize(path);
var module, moduleCode;
// see if the module is already loaded
if (require.loaded[path] && loadOnce) { return require.loaded[path]; }
try { moduleCode = readFile(path); } catch (e) {}
if (typeof moduleCode !== "undefined") {
require.loaded[path] = {};
module = new Function("require", "exports", moduleCode);
module(_requireFactory(path, true), require.loaded[path]);
return require.loaded[path];
}
return false;
}
/** The global require function */
require = function (name) { return _require(name, ".", true); };
require.paths = ["lib"];
require.loaded = {};
require.extensions = [".js"];
})();