-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcompile.js
47 lines (39 loc) · 2.01 KB
/
compile.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
"use strict";
module.exports = compile;
// By using a named "eval" most browsers will execute in the global scope.
// http://www.davidflanagan.com/2010/12/global-eval-in.html
// Unfortunately execScript doesn't always return the value of the evaluated expression (at least in Chrome)
var globalEval = /*this.execScript ||*/eval;
// For Firebug evaled code isn't debuggable otherwise
// http://code.google.com/p/fbug/issues/detail?id=2198
if (global.navigator && global.navigator.userAgent.indexOf("Firefox") >= 0) {
globalEval = new Function("_", "return eval(_)");
}
function compile(module) {
// Here we use a couple tricks to make debugging better in various browsers:
// TODO: determine if these are all necessary / the best options
// 1. name the function with something inteligible since some debuggers display the first part of each eval (Firebug)
// 2. append the "//# sourceURL=filename" hack (Safari, Chrome, Firebug)
// * http://pmuellr.blogspot.com/2009/06/debugger-friendly.html
// * http://blog.getfirebug.com/2009/08/11/give-your-eval-a-name-with-sourceurl/
// TODO: investigate why this isn't working in Firebug.
// 3. set displayName property on the factory function (Safari, Chrome)
var displayName = module.filename.replace(/[^\w\d]|^\d/g, "_");
try {
module.factory = globalEval(
"(function " +
displayName +
"(require, exports, module, __filename, __dirname) {" +
module.text +
"//*/\n})\n//# sourceURL=" +
module.system.location + module.id
);
} catch (exception) {
exception.message = exception.message + " in " + module.filename;
throw exception;
}
// This should work and would be simpler, but Firebug does not show scripts executed via "new Function()" constructor.
// TODO: sniff browser?
// module.factory = new Function("require", "exports", "module", module.text + "\n//*/"+sourceURLComment);
module.factory.displayName = module.filename;
}