forked from pmuellr/incubator-cordova-weinre
/
modjewel.js
361 lines (295 loc) · 12.4 KB
/
modjewel.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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
//----------------------------------------------------------------------------
// an implementation of the require() function as specified for use with
// CommonJS Modules - see http://commonjs.org/specs/modules/1.0.html
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// inspired from David Flanagan's require() function documented here:
// http://www.davidflanagan.com/2009/11/a-module-loader.html
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// only supports "preloaded" modules ala define() (AMD)
// http://wiki.commonjs.org/wiki/Modules/AsynchronousDefinition
// but the id parameter is required
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// function wrapper
//----------------------------------------------------------------------------
(function(){
//----------------------------------------------------------------------------
// some constants
//----------------------------------------------------------------------------
var PROGRAM = "modjewel"
var VERSION = "2.0.0"
var global = this
//----------------------------------------------------------------------------
// if require() is already defined, leave
//----------------------------------------------------------------------------
if (global.modjewel) {
log("modjewel global variable already defined")
return
}
global.modjewel = null
//----------------------------------------------------------------------------
// "globals" (local to this function scope though)
//----------------------------------------------------------------------------
var ModuleStore
var ModulePreloadStore
var MainModule
var WarnOnRecursiveRequire = false
//----------------------------------------------------------------------------
// the require function
//----------------------------------------------------------------------------
function get_require(currentModule) {
var result = function require(moduleId) {
if (moduleId.match(/^\.{1,2}\//)) {
moduleId = normalize(currentModule, moduleId)
}
if (hop(ModuleStore, moduleId)) {
var module = ModuleStore[moduleId]
if (module.__isLoading) {
if (WarnOnRecursiveRequire) {
var fromModule = currentModule ? currentModule.id : "<root>"
console.log("module '" + moduleId + "' recursively require()d from '" + fromModule + "', problem?")
}
}
currentModule.moduleIdsRequired.push(moduleId)
return module.exports
}
if (!hop(ModulePreloadStore, moduleId)) {
var fromModule = currentModule ? currentModule.id : "<root>"
error("module '" + moduleId + "' not found from '" + fromModule + "', must be define()'d first")
}
var factory = ModulePreloadStore[moduleId][0]
var prereqs = ModulePreloadStore[moduleId][1]
var module = create_module(moduleId)
var newRequire = get_require(module)
ModuleStore[moduleId] = module
module.__isLoading = true
try {
currentModule.moduleIdsRequired.push(moduleId)
var prereqModules = []
for (var i=0; i<prereqs.length; i++) {
var prereqId = prereqs[i]
var prereqModule
if (prereqId == "require") prereqModule = newRequire
else if (prereqId == "exports") prereqModule = module.exports
else if (prereqId == "module") prereqModule = module
else prereqModule = newRequire(prereqId)
prereqModules.push(prereqModule)
}
if (typeof factory == "function") {
var result = factory.apply(null, prereqModules)
if (result) {
module.exports = result
}
}
else {
module.exports = factory
}
}
finally {
module.__isLoading = false
}
return module.exports
}
result.define = require_define
result.implementation = PROGRAM
result.version = VERSION
return result
}
//----------------------------------------------------------------------------
// shorter version of hasOwnProperty
//----------------------------------------------------------------------------
function hop(object, name) {
return Object.prototype.hasOwnProperty.call(object, name)
}
//----------------------------------------------------------------------------
// create a new module
//----------------------------------------------------------------------------
function create_module(id) {
return {
id: id,
uri: id,
exports: {},
prereqIds: [],
moduleIdsRequired: []
}
}
//----------------------------------------------------------------------------
// reset the stores
//----------------------------------------------------------------------------
function require_reset() {
ModuleStore = {}
ModulePreloadStore = {}
MainModule = create_module(null)
var require = get_require(MainModule)
var define = require_define
define("modjewel", modjewel_module)
global.modjewel = require("modjewel")
global.modjewel.require = require
global.modjewel.define = define
global.modjewel.define.amd = {implementation: PROGRAM, version: VERSION}
}
//----------------------------------------------------------------------------
// used by pre-built modules that can be included via <script src=>
// a simplification of
// http://wiki.commonjs.org/wiki/Modules/AsynchronousDefinition
// where id is required
//----------------------------------------------------------------------------
function require_define(moduleId, prereqs, factory) {
var rem = ["require", "exports", "module"]
if (typeof moduleId != "string") {
console.log("modjewel.define(): first parameter must be a string; was: " + moduleId)
return
}
if (arguments.length == 2) {
factory = prereqs
prereqs = null
}
if (!prereqs || prereqs.length == 0) {
prereqs = rem
}
if (typeof factory != "function") {
if (factory) {
ModulePreloadStore[moduleId] = [factory, prereqs]
return
}
console.log("modjewel.define(): factory was falsy: " + factory)
return
}
if (moduleId.match(/^\./)) {
console.log("modjewel.define(): moduleId must not start with '.': '" + moduleName + "'")
return
}
if (hop(ModulePreloadStore, moduleId)) {
console.log("modjewel.define(): module '" + moduleId + "' has already been defined")
return
}
ModulePreloadStore[moduleId] = [factory, prereqs]
}
//----------------------------------------------------------------------------
// get the path of a module
//----------------------------------------------------------------------------
function getModulePath(module) {
if (!module || !module.id) return ""
var parts = module.id.split("/")
return parts.slice(0, parts.length-1).join("/")
}
//----------------------------------------------------------------------------
// normalize a 'file name' with . and .. with a 'directory name'
//----------------------------------------------------------------------------
function normalize(module, file) {
var modulePath = getModulePath(module)
var dirParts = ("" == modulePath) ? [] : modulePath.split("/")
var fileParts = file.split("/")
for (var i=0; i<fileParts.length; i++) {
var filePart = fileParts[i]
if (filePart == ".") {
}
else if (filePart == "..") {
if (dirParts.length > 0) {
dirParts.pop()
}
else {
// error("error normalizing '" + module + "' and '" + file + "'")
// eat non-valid .. paths
}
}
else {
dirParts.push(filePart)
}
}
return dirParts.join("/")
}
//----------------------------------------------------------------------------
// throw an error
//----------------------------------------------------------------------------
function error(message) {
throw new Error(PROGRAM + ": " + message)
}
//----------------------------------------------------------------------------
// get a list of loaded modules
//----------------------------------------------------------------------------
function modjewel_getLoadedModuleIds() {
var result = []
for (moduleId in ModuleStore) {
result.push(moduleId)
}
return result
}
//----------------------------------------------------------------------------
// get a list of the preloaded module ids
//----------------------------------------------------------------------------
function modjewel_getPreloadedModuleIds() {
var result = []
for (moduleId in ModulePreloadStore) {
result.push(moduleId)
}
return result
}
//----------------------------------------------------------------------------
// get a module by module id
//----------------------------------------------------------------------------
function modjewel_getModule(moduleId) {
if (null == moduleId) return MainModule
return ModuleStore[moduleId]
}
//----------------------------------------------------------------------------
// get a list of module ids which have been required by the specified module id
//----------------------------------------------------------------------------
function modjewel_getModuleIdsRequired(moduleId) {
var module = modjewel_getModule(moduleId)
if (null == module) return null
return module.moduleIdsRequired.slice()
}
//----------------------------------------------------------------------------
// set the WarnOnRecursiveRequireFlag
// - if you make use of "module.exports =" in your code, you will want this on
//----------------------------------------------------------------------------
function modjewel_warnOnRecursiveRequire(value) {
if (arguments.length == 0) return WarnOnRecursiveRequire
WarnOnRecursiveRequire = !!value
}
//----------------------------------------------------------------------------
// the modjewel module
//----------------------------------------------------------------------------
function modjewel_module(require, exports, module) {
exports.VERSION = VERSION
exports.require = null // filled in later
exports.define = null // filled in later
exports.getLoadedModuleIds = modjewel_getLoadedModuleIds
exports.getPreloadedModuleIds = modjewel_getPreloadedModuleIds
exports.getModule = modjewel_getModule
exports.getModuleIdsRequired = modjewel_getModuleIdsRequired
exports.warnOnRecursiveRequire = modjewel_warnOnRecursiveRequire
}
//----------------------------------------------------------------------------
// log a message
//----------------------------------------------------------------------------
function log(message) {
console.log("modjewel: " + message)
}
//----------------------------------------------------------------------------
// make the require function a global
//----------------------------------------------------------------------------
require_reset()
//----------------------------------------------------------------------------
})();