This repository has been archived by the owner on Mar 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
amd.js
90 lines (78 loc) · 2.57 KB
/
amd.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
// Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
/*jshint ignore:start */
var require;
var define;
/*jshint ignore:end */
(function () {
"use strict";
var defined = {};
define = function (id, dependencies, factory) {
if (!Array.isArray(dependencies)) {
factory = dependencies;
dependencies = [];
}
var mod = {
dependencies: normalize(id, dependencies),
factory: factory
};
if (dependencies.indexOf('exports') !== -1) {
mod.exports = {};
}
defined[id] = mod;
};
// WinJS/Core depends on ./Core/_Base
// should return WinJS/Core/_Base
function normalize(id, dependencies) {
var parent = id.split('/');
parent.pop();
return dependencies.map(function (dep) {
if (dep[0] === '.') {
var parts = dep.split('/');
var current = parent.slice(0);
parts.forEach(function (part) {
if (part === '..') {
current.pop();
} else if (part !== '.') {
current.push(part);
}
});
return current.join('/');
} else {
return dep;
}
});
}
function resolve(dependencies, exports) {
return dependencies.map(function (depName) {
if (depName === 'exports') {
return exports;
}
var dep = defined[depName];
if (!dep) {
throw new Error("Undefined dependency: " + depName);
}
if (!dep.resolved) {
dep.resolved = load(dep.dependencies, dep.factory, dep.exports);
// exports shadows whatever the module factory returns
if (dep.exports) {
dep.resolved = dep.exports;
}
}
return dep.resolved;
});
}
function load(dependencies, factory, exports) {
var deps = resolve(dependencies, exports);
if (factory && factory.apply) {
return factory.apply(null, deps);
} else {
return factory;
}
}
require = function (dependencies, factory) { //jshint ignore:line
if (!Array.isArray(dependencies)) {
dependencies = [dependencies];
}
load(dependencies, factory);
};
})();