/
x.js
196 lines (158 loc) · 5.96 KB
/
x.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
* @license r.js 0.25.0 Copyright (c) 2010-2011, The Dojo Foundation All Rights Reserved.
* Available via the MIT or new BSD license.
* see: http://github.com/jrburke/requirejs for details
*/
/*
* This is a bootstrap script to allow running RequireJS in the command line
* in either a Java/Rhino or Node environment. It is modified by the top-level
* dist.js file to inject other files to completely enable this file. It is
* the shell of the r.js file.
*/
/*jslint strict: false, evil: true */
/*global readFile: true, process: false, Packages: false, print: false,
console: false, java: false */
var requirejs, require, define;
(function (console, args, readFileFunc) {
var fileName, env, fs, vm, path, exec, rhinoContext, dir, nodeRequire,
nodeDefine, exists,
version = '0.25.0',
jsSuffixRegExp = /\.js$/,
//Indicates so build/build.js that the modules for the optimizer
//are built-in.
isRjs = true,
commandOption = '',
//Used by jslib/rhino/args.js
rhinoArgs = args,
readFile = typeof readFileFunc !== 'undefined' ? readFileFunc : null;
function showHelp() {
console.log('See https://github.com/jrburke/r.js for usage.');
}
if (typeof Packages !== 'undefined') {
env = 'rhino';
fileName = args[0];
if (fileName && fileName.indexOf('-') === 0) {
commandOption = fileName.substring(1);
fileName = args[1];
}
//Set up execution context.
rhinoContext = Packages.org.mozilla.javascript.ContextFactory.getGlobal().enterContext();
exec = function (string, name) {
return rhinoContext.evaluateString(this, string, name, 0, null);
};
exists = function (fileName) {
return (new java.io.File(fileName)).exists();
};
//Define a console.log for easier logging. Don't
//get fancy though.
if (typeof console === 'undefined') {
console = {
log: function () {
print.apply(undefined, arguments);
}
};
}
} else if (typeof process !== 'undefined') {
env = 'node';
//Get the fs module via Node's require before it
//gets replaced. Used in require/node.js
fs = require('fs');
vm = require('vm');
path = require('path');
nodeRequire = require;
nodeDefine = define;
//Temporarily hide require and define to allow require.js to define
//them.
require = undefined;
define = undefined;
readFile = function (path) {
return fs.readFileSync(path, 'utf8');
};
exec = function (string, name) {
return vm.runInThisContext(this.requirejsVars.require.makeNodeWrapper(string),
name ? fs.realpathSync(name) : '');
};
exists = function (fileName) {
return path.existsSync(fileName);
};
fileName = process.argv[2];
if (fileName && fileName.indexOf('-') === 0) {
commandOption = fileName.substring(1);
fileName = process.argv[3];
}
}
//INSERT require.js
if (env === 'rhino') {
//INSERT build/jslib/rhino.js
} else if (env === 'node') {
this.requirejsVars = {
require: require,
define: define,
nodeRequire: nodeRequire
};
require.nodeRequire = nodeRequire;
//INSERT build/jslib/node.js
}
//Support a default file name to execute. Useful for hosted envs
//like Joyent where it defaults to a server.js as the only executed
//script. But only do it if this is not an optimization run.
if (commandOption !== 'o' && (!fileName || !jsSuffixRegExp.test(fileName))) {
fileName = 'main.js';
}
/**
* Loads the library files that can be used for the optimizer, or for other
* tasks.
*/
function loadLib() {
//INSERT LIB
}
if (commandOption === 'o') {
//Do the optimizer work.
loadLib();
//INSERT build/build.js
} else if (commandOption === 'v') {
console.log('r.js: ' + version + ', RequireJS: ' + this.requirejsVars.require.version);
} else if (commandOption === 'convert') {
loadLib();
this.requirejsVars.require(['env!env/args', 'commonJs', 'env!env/print'],
function (args, commonJs, print) {
var srcDir, outDir;
srcDir = args[0];
outDir = args[1];
if (!srcDir || !outDir) {
print('Usage: path/to/commonjs/modules output/dir');
return;
}
commonJs.convertDir(args[0], args[1]);
});
} else {
//Just run an app
//Load the bundled libraries for use in the app.
if (commandOption === 'lib') {
loadLib();
}
//Use the file name's directory as the baseUrl if available.
dir = fileName.replace(/\\/g, '/');
if (dir.indexOf('/') !== -1) {
dir = dir.split('/');
dir.pop();
dir = dir.join('/');
exec("require({baseUrl: '" + dir + "'});");
}
if (exists(fileName)) {
if (env === 'node') {
//Put the file name's directory on the paths
//so node_modules in that directory will be
//consulted instead of relying on a node_modules
//in the r.js location.
//Put the current working direct
nodeRequire.paths.unshift(path.join(path.dirname(fs.realpathSync(fileName)), 'node_modules'));
}
exec(readFile(fileName), fileName);
} else {
showHelp();
}
}
}((typeof console !== 'undefined' ? console : undefined),
(typeof Packages !== 'undefined' ? Array.prototype.slice.call(arguments, 0) : []),
(typeof readFile !== 'undefined' ? readFile : undefined)));