-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfactory.js
131 lines (112 loc) · 4.02 KB
/
factory.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
/**
* Plugin for require js to access the factories
* of the modules. Needed for unit-testing...
*/
define(function() {
var factories = [];
function interceptModuleExecuteCallback() {
var oldExecCb = require.execCb;
require.execCb = function(fullname, callback) {
factories[fullname] = callback;
return oldExecCb.apply(this, arguments);
}
}
function createBrowserPlugin() {
interceptModuleExecuteCallback();
function load(name, req, localLoad, config) {
req([name], function (value) {
localLoad(factories[name]);
});
}
return {
load: load
}
}
function extractFactoryFunctionFromModule(moduleText) {
var factoryRegex = /function[\s\S]*\}/;
var match = factoryRegex.exec(moduleText);
if (match) {
return match[0];
}
return null;
}
function createBuildPlugin(env) {
var readFile;
if (env === 'rhino') {
readFile = readFileWithRhino;
} else {
readFile = readFileWithNodeJs;
}
function load(name, req, localLoad, config) {
var url = req.toUrl(name);
var text = readFile(url);
factories[name] = extractFactoryFunctionFromModule(text);
localLoad();
}
function write(pluginName, moduleName, localWrite) {
var factory = factories[moduleName];
localWrite("define('" + pluginName + "!" + moduleName + "',function() { return " + factory + "});");
}
return {
load: load,
write: write
}
}
function readFileWithNodeJs(url) {
//Using special require.nodeRequire, something added by r.js.
var fs = require.nodeRequire('fs');
return fs.readFileSync(url, 'utf8');
}
function readFileWithRhino(url) {
var encoding = "utf-8",
file = new java.io.File(url),
lineSeparator = java.lang.System.getProperty("line.separator"),
input = new java.io.BufferedReader(new java.io.InputStreamReader(new java.io.FileInputStream(file), encoding)),
stringBuffer, line,
content = '';
try {
stringBuffer = new java.lang.StringBuffer();
line = input.readLine();
// Byte Order Mark (BOM) - The Unicode Standard, version 3.0, page 324
// http://www.unicode.org/faq/utf_bom.html
// Note that when we use utf-8, the BOM should appear as "EF BB BF", but it doesn't due to this bug in the JDK:
// http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4508058
if (line && line.length() && line.charAt(0) === 0xfeff) {
// Eat the BOM, since we've already found the encoding on this file,
// and we plan to concatenating this buffer with others; the BOM should
// only appear at the top of a file.
line = line.substring(1);
}
stringBuffer.append(line);
while ((line = input.readLine()) !== null) {
stringBuffer.append(lineSeparator);
stringBuffer.append(line);
}
//Make sure we return a JavaScript string and not a Java string.
content = String(stringBuffer.toString()); //String
} finally {
input.close();
}
return content;
}
function getEnvironment() {
if (typeof process !== "undefined" &&
process.versions &&
!!process.versions.node) {
return 'node';
}
if (typeof window !== "undefined" && window.navigator && window.document) {
return 'browser';
}
if (typeof Packages !== 'undefined') {
return 'rhino';
}
return 'undefined';
}
var env = getEnvironment();
if (env==='browser') {
return createBrowserPlugin();
} else {
return createBuildPlugin(env);
}
});