/
require.js
137 lines (122 loc) · 4.41 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
127
128
129
130
131
132
133
134
135
136
137
void(function() {
var initRequire = function(){
var XHR = window.XMLHttpRequest || function() { return new ActiveXObject("Msxml2.XMLHTTP"); },
log = window.console && console.log
// Regex to split a filename into [*, dir, basename, ext], posix version, from https://github.com/ry/node/blob/master/lib/path.js
var splitPathRegex = /^(.+\/(?!$)|\/)?((?:.+?)?(\.[^.]*)?)$/,
getDir = function(path) { return splitPathRegex.exec(path)[1] || '' }
var slashDotSlashRegex = /\/\.\//g,
doubleSlashRegex = /\/\//g,
endInSlashRegex = /\/$/g
var resolvePath = function(path) {
var pathParts = path
.replace(doubleSlashRegex, '/')
.replace(slashDotSlashRegex, '/')
.replace(endInSlashRegex, '')
.split('/')
var i=0
while (i < pathParts.length) {
if (pathParts[i] == '..') {
pathParts.splice(i - 1, 2)
i--
} else {
i++
}
}
var possiblesBase = pathParts.join('/')
return [possiblesBase + '.js', possiblesBase + '/index.js']
}
var fetchFile = function(possiblePaths) {
while (possiblePaths.length) {
var possiblePath = possiblePaths[0],
url = location.protocol + '//' + location.host + possiblePath,
xhr = new XHR()
if (!require._attempted[url]) {
require._attempted[url] = true
try {
xhr.open('GET', url, false)
xhr.send(null)
} catch(e) {
log(url, 'fetchFile open/send error:', e)
}
if (xhr.status == -1100) { // safari file://
// XXX: We have no way to tell in opera if a file exists and is empty, or is 404
log(url, 'fetchFile xhr.status error:', xhr.status)
} else if (xhr.status < 400) {
return xhr.responseText;
}
}
possiblePaths.shift()
}
return null
}
window.require = function(modulePath) {
var baseStack = require._base,
currentBase = baseStack[baseStack.length - 1],
possiblePaths = resolvePath(currentBase + modulePath)
for (var i=0, path; path = possiblePaths[i]; i++) {
if (require._modules[path]) { return require._modules[path].exports }
}
var moduleCode = fetchFile(possiblePaths),
foundPath = possiblePaths[0] // fetchFile will remove bad paths from possiblePaths
if (!foundPath) {
log(path, 'failed to load module', modulePath)
return
}
require._base.push(getDir(foundPath))
var moduleFriendlyName = foundPath.replace(/[\/.]/g, '_')
try {
var inContextCode = '(function(module){ var exports = module.exports;\n'+ moduleCode +'\n})',
importerFunction = require._evaluate(inContextCode, foundPath)
} catch(e) {
if(e instanceof SyntaxError) { log(foundPath, 'a syntax error prevented execution') }
throw e
}
// the "module" object in a node module
require._modules[foundPath] = { exports: {} }
try {
importerFunction(require._modules[foundPath])
} catch(e) {
if(e.type == 'syntax_error') {
log(foundPath, 'Syntax error while importing module', e)
throw e
} else if (!e.requireLogged) {
e.requireLogged = true
if (e.type == 'stack_overflow') {
log(foundPath, 'Stack overflow while importing module', e)
} else {
log(foundPath, 'enable "break on error" in your debugger to debug', e)
}
throw e
}
}
require._base.pop()
return require._modules[foundPath].exports
}
}
if (typeof module != 'undefined' && module.exports) {
var requireFn = arguments.callee
module.exports = { toString: function(){ return 'void(' + requireFn + ')()'} }
} else if (typeof window != 'undefined' && !window.require) {
initRequire()
require._modules = {}
require._attempted = {}
require._base = [location.pathname.replace(/\/[^\/]*$/, '/')]
// evaluate must not have access to the local scope of require, or else imported modules will
// see require's local variables as well.
// IE6 won't return an anonymous function from eval, so use the function constructor instead
require._evaluate = typeof eval('(function(){})') == 'undefined'
? function(src, path) { return (new Function('return ' + src))() }
: function(src, path) { var src = src + '\n//@ sourceURL=' + path; return eval(src) }
void(function(){
var scripts = document.getElementsByTagName('script'),
requireSrcRegex = /\/require\.js$/
for (var i=0, script; script = scripts[i]; i++) {
if (!script.src.match(requireSrcRegex)) { continue }
var appURL = script.getAttribute('main')
if (appURL) { require(appURL) }
return
}
})()
}
})()