forked from TiddoLangerak/tracegl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefine.js
More file actions
200 lines (164 loc) · 4.95 KB
/
define.js
File metadata and controls
200 lines (164 loc) · 4.95 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// | Browser and NodeJS module (re)loader |_____/
// |
// | (C) Mozilla Corp
// | licensed under MPL 2.0 http://www.mozilla.org/MPL/
// \____________________________________________/
function define(id, fac){
//PACKSTART
// | returns path of file
function path(p){ //
if(!p) return ''
p = p.replace(/\.\//g, '')
var b = p.match(/([\s\S]*)\/[^\/]*$/)
return b ? b[1] : ''
}
// | normalizes relative path r against base b
function norm(r, b){
b = b.split(/\//)
r = r.replace(/\.\.\//g,function(){ b.pop(); return ''}).replace(/\.\//g, '')
var v = b.join('/')+ '/' + r
if(v.charAt(0)!='/') v = '/'+v
return v
}
//PACKEND
if(typeof process !== "undefined"){
// | Node.JS Path
// \____________________________________________/
if(global.define) return
var fs = require("fs")
var cp = require("child_process")
var Module = require("module")
var modules = []
var _compile = module.constructor.prototype._compile
// hook compile to keep track of module objects
module.constructor.prototype._compile = function(content, filename){
modules.push(this);
try {
return _compile.call(this, content, filename)
}
finally {
modules.pop()
}
};
var outer = define
module.exports = global.define = function(id, fac) {
if(fac instanceof Array) throw new Error("injects-style not supported")
if (!fac) fac = id
var m = modules[modules.length-1] || require.main
// store module and factory just like in the other envs
global.define.module[m.filename] = m
global.define.factory[m.filename] = fac
var req = function(m, id) {
if(id instanceof Array || arguments.length != 2 || id.indexOf('!') != -1)
throw new Error("unsupported require style")
var f = Module._resolveFilename(id, m)
if (f instanceof Array) f = f[0]
// lets output a filename on stderr for watching
if(global.define.log && f.indexOf('/') != -1) process.stderr.write('<[<['+f+']>]>')
return require(f)
}.bind(this, m)
if (typeof fac !== "function") return m.exports = fac
req.factory = function(){
throw new Error('factory not supported in unpackaged')
}
var ret = fac.call(m.exports, req, m.exports, m)
if (ret) m.exports = ret
}
global.define.require = require
global.define.outer = outer
global.define.path = path
global.define.norm = norm
global.define.module = {}
global.define.factory = {}
return
}
// | Browser Path
// \____________________________________________/
//PACKSTART
function def(id, fac){
if(!fac) fac = id, id = null
def.factory[id || '_'] = fac
}
def.module = {}
def.factory = {}
def.urls = {}
def.tags = {}
function req(id, base){
if(!base) base = ''
if(typeof require !== "undefined" && id.charAt(0) != '.') return require(id)
id = norm(id, base)
var c = def.module[id]
if(c) return c.exports
var f = def.factory[id]
if(!f) throw new Error('module not available '+id + ' in base' + base)
var m = {exports:{}}
var localreq = def.mkreq(id)
var ret = f(localreq, m.exports, m)
if(ret) m.exports = ret
def.module[id] = m
return m.exports
}
def.mkreq = function(base){
function localreq(i){
return def.req(i, path(base))
}
localreq.reload = function(i, cb){
var id = norm(i, base)
script(id, 'reload', function(){
delete def.module[id] // cause reexecution of module
cb( req(i, base) )
})
}
localreq.absolute = function(i){
return norm(i, path(base))
}
return localreq
}
def.req = req
def.outer = define
if(typeof require !== 'undefined') def.require = require
def.path = path
def.norm = norm
define = def
def(id, fac)
//PACKEND
// the separate file script loader
def.dling = 0
def.rldid = 0
var base = path(window.location.href)
function script(file, parent, cb){
var s = document.createElement('script')
var p = path(file)
file = file.replace(/\.\//g, '/')
s.type = 'text/javascript'
if(cb) rld = '?'+def.rldid++
else rld = ''
s.src = base + file + (file.indexOf(".js")!= -1 ? "" : ".js" ) + rld
def.tags[file] = s
def.dling++
function load(){
var f = def.factory._
def.factory[file] = f
def.urls[file] = s.src
f.toString().replace(/require\s*\(\s*["']([^"']+)["']\s*\)/g, function(x, i){
if(i.charAt(0) != '.') return
i = norm(i, p)
if(!def.tags[i] && !def.factory[i]) script(i, file)
})
if(cb) cb()
else if(!--def.dling) req(def.main, '') // no more deps
}
s.onerror = function(){ console.error("Error loading " + s.src + " from "+parent) }
s.onload = load
s.onreadystatechange = function(){
if(s.readyState == 'loaded' || s.readyState == 'complete') load()
}
document.getElementsByTagName('head')[0].appendChild(s)
}
def.main = document.body.getAttribute("define-main") ||
window.location.pathname.replace(/^(.*\/)(.*?)/,"$2").replace(".html","")
if(!def.main || def.main.match(/\/(?:index)?$/)) def.main = "./main"
else if(def.main.indexOf('./') != 0)def.main = "./" + def.main
script(def.main, 'root')
}
define()