This repository was archived by the owner on Apr 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathamd-loader.js
More file actions
64 lines (51 loc) · 1.78 KB
/
amd-loader.js
File metadata and controls
64 lines (51 loc) · 1.78 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
// This is intended to be the simplest possible AMD shim that works
// It is not intended a general AMD loader just enough to load this package
// This relies on the fact that Node.js require() is synchronous.
// It attempts to let the node.js module loader do as much work as possible
// Also provides a way to replace modules with api compatible counterparts
var path = require('path');
module.exports = function amdload(absoluteFilename, map) {
// Store this so we can put it back later.
var oldDefine = global.define;
map = map || {};
var loaded = {}, dirs = [], exported;
/**
* These two functions operate as a pair
*/
var amdrequire = function amdrequire(filepath) {
// Return real node modules if we have them mapped
if (filepath in map) {
return require(map[filepath]);
}
// Resolve target against 'current working directory'
var fullpath = path.resolve(dirs[0], filepath);
if (!loaded[fullpath]) {
// Put current operation on stack
dirs.unshift(path.dirname(fullpath));
// setup fake define and delegate to real require()
global.define = define;
require(fullpath);
// Capture and store exported module
loaded[fullpath] = exported;
exported = null;
// Restore previous define() state
if (oldDefine) {
global.define = define;
} else {
delete global.define;
}
// return to cwd from before define
dirs.shift();
}
// return value captured by define()
return loaded[fullpath];
};
var define = function define(deps, factory) {
// Load all dependencies
var modules = deps.map(amdrequire);
// Capture the exported value
exported = factory.apply(null, modules);
};
define.amd = true;
return amdrequire(absoluteFilename);
};