forked from seajs/seajs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsea.js
129 lines (105 loc) · 2.85 KB
/
sea.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
/**
* Add the capability to load CMD modules in node environment
* @author [email protected]
*/
var fs = require("fs")
var path = require("path")
var vm = require("vm")
var normalize = require("./winos").normalize
var moduleStack = []
var uriCache = {}
var nativeLoad
runSeaJS("../dist/sea-debug.js")
hackNative()
attach()
keep()
seajs.config({ cwd: normalize(process.cwd()) + "/" })
function runSeaJS(filepath) {
var code = fs.readFileSync(path.join(__dirname, filepath), "utf8")
code = code.replace("})(this);", "})(exports);")
// Run "sea.js" code in a fake browser environment
var sandbox = require("./sandbox")
vm.runInNewContext(code, sandbox, "sea-debug.vm")
global.seajs = sandbox.exports.seajs
global.define = sandbox.exports.define
}
function hackNative() {
var Module = module.constructor
nativeLoad = Module._load
Module._load = function(request, parent, isMain) {
var exports = nativeLoad(request, parent, isMain)
var _filename = Module._resolveFilename(request, parent)
var filename = normalize(_filename)
var mod = seajs.cache[filename]
if (mod) {
if (mod.status < seajs.Module.STATUS.EXECUTING) {
seajs.use(filename)
}
exports = Module._cache[_filename] = mod.exports
}
return exports
}
var _compile = Module.prototype._compile
Module.prototype._compile = function(content, filename) {
moduleStack.push(this)
try {
return _compile.call(this, content, filename)
}
finally {
moduleStack.pop()
}
}
}
function attach() {
seajs.on("request", requestListener)
seajs.on("define", defineListener)
}
function requestListener(data) {
var requestUri = pure(data.requestUri)
var ext = path.extname(requestUri)
//process.stdout.write("requestUri = " + requestUri + "\n")
if (ext === ".js") {
// Use native `require` instead of script-inserted version
nativeLoad(requestUri)
data.onRequest()
data.requested = true
}
// Throw error if this function is the last request handler
else if (seajs.data.events["request"].length === 1) {
throw new Error("Do NOT support to load this file in node environment: "
+ requestUri)
}
}
function defineListener(data) {
if (!data.uri) {
var derivedUri = normalize(moduleStack[moduleStack.length - 1].id)
data.uri = uriCache[derivedUri] || derivedUri
}
}
function keep() {
var _off = seajs.off
var events = seajs.data.events
seajs.off = function(name, callback) {
// Remove *all* events
if (!(name || callback)) {
// For Node.js to work properly
for (var prop in events) {
delete events[prop]
}
}
else {
_off(name, callback)
}
attach()
return seajs
}
}
function pure(uri) {
// Remove timestamp etc
var ret = uri.replace(/\?.*$/, "")
// Cache it
if (ret !== uri) {
uriCache[ret] = uri
}
return ret
}