|
237
|
1 |
/**
|
|
|
2 |
* @license r.js 1.0.1 Copyright (c) 2010-2011, The Dojo Foundation All Rights Reserved.
|
|
|
3 |
* Available via the MIT or new BSD license.
|
|
|
4 |
* see: http://github.com/jrburke/requirejs for details
|
|
|
5 |
*/
|
|
|
6 |
|
|
|
7 |
/*
|
|
|
8 |
* This is a bootstrap script to allow running RequireJS in the command line
|
|
|
9 |
* in either a Java/Rhino or Node environment. It is modified by the top-level
|
|
|
10 |
* dist.js file to inject other files to completely enable this file. It is
|
|
|
11 |
* the shell of the r.js file.
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
/*jslint strict: false, evil: true, nomen: false */
|
|
|
15 |
/*global readFile: true, process: false, Packages: false, print: false,
|
|
|
16 |
console: false, java: false, module: false */
|
|
|
17 |
|
|
|
18 |
var requirejs, require, define;
|
|
|
19 |
(function (console, args, readFileFunc) {
|
|
|
20 |
|
|
|
21 |
var fileName, env, fs, vm, path, exec, rhinoContext, dir, nodeRequire,
|
|
|
22 |
nodeDefine, exists, reqMain, loadedOptimizedLib,
|
|
|
23 |
version = '1.0.1',
|
|
|
24 |
jsSuffixRegExp = /\.js$/,
|
|
|
25 |
commandOption = '',
|
|
|
26 |
//Used by jslib/rhino/args.js
|
|
|
27 |
rhinoArgs = args,
|
|
|
28 |
readFile = typeof readFileFunc !== 'undefined' ? readFileFunc : null;
|
|
|
29 |
|
|
|
30 |
function showHelp() {
|
|
|
31 |
console.log('See https://github.com/jrburke/r.js for usage.');
|
|
|
32 |
}
|
|
|
33 |
|
|
|
34 |
if (typeof Packages !== 'undefined') {
|
|
|
35 |
env = 'rhino';
|
|
|
36 |
|
|
|
37 |
fileName = args[0];
|
|
|
38 |
|
|
|
39 |
if (fileName && fileName.indexOf('-') === 0) {
|
|
|
40 |
commandOption = fileName.substring(1);
|
|
|
41 |
fileName = args[1];
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
//Set up execution context.
|
|
|
45 |
rhinoContext = Packages.org.mozilla.javascript.ContextFactory.getGlobal().enterContext();
|
|
|
46 |
|
|
|
47 |
exec = function (string, name) {
|
|
|
48 |
return rhinoContext.evaluateString(this, string, name, 0, null);
|
|
|
49 |
};
|
|
|
50 |
|
|
|
51 |
exists = function (fileName) {
|
|
|
52 |
return (new java.io.File(fileName)).exists();
|
|
|
53 |
};
|
|
|
54 |
|
|
|
55 |
//Define a console.log for easier logging. Don't
|
|
|
56 |
//get fancy though.
|
|
|
57 |
if (typeof console === 'undefined') {
|
|
|
58 |
console = {
|
|
|
59 |
log: function () {
|
|
|
60 |
print.apply(undefined, arguments);
|
|
|
61 |
}
|
|
|
62 |
};
|
|
|
63 |
}
|
|
|
64 |
} else if (typeof process !== 'undefined') {
|
|
|
65 |
env = 'node';
|
|
|
66 |
|
|
|
67 |
//Get the fs module via Node's require before it
|
|
|
68 |
//gets replaced. Used in require/node.js
|
|
|
69 |
fs = require('fs');
|
|
|
70 |
vm = require('vm');
|
|
|
71 |
path = require('path');
|
|
|
72 |
nodeRequire = require;
|
|
|
73 |
nodeDefine = define;
|
|
|
74 |
reqMain = require.main;
|
|
|
75 |
|
|
|
76 |
//Temporarily hide require and define to allow require.js to define
|
|
|
77 |
//them.
|
|
|
78 |
require = undefined;
|
|
|
79 |
define = undefined;
|
|
|
80 |
|
|
|
81 |
readFile = function (path) {
|
|
|
82 |
return fs.readFileSync(path, 'utf8');
|
|
|
83 |
};
|
|
|
84 |
|
|
|
85 |
exec = function (string, name) {
|
|
|
86 |
return vm.runInThisContext(this.requirejsVars.require.makeNodeWrapper(string),
|
|
|
87 |
name ? fs.realpathSync(name) : '');
|
|
|
88 |
};
|
|
|
89 |
|
|
|
90 |
exists = function (fileName) {
|
|
|
91 |
return path.existsSync(fileName);
|
|
|
92 |
};
|
|
|
93 |
|
|
|
94 |
|
|
|
95 |
fileName = process.argv[2];
|
|
|
96 |
|
|
|
97 |
if (fileName && fileName.indexOf('-') === 0) {
|
|
|
98 |
commandOption = fileName.substring(1);
|
|
|
99 |
fileName = process.argv[3];
|
|
|
100 |
}
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
/** vim: et:ts=4:sw=4:sts=4
|
|
|
104 |
* @license RequireJS 1.0.1 Copyright (c) 2010-2011, The Dojo Foundation All Rights Reserved.
|
|
|
105 |
* Available via the MIT or new BSD license.
|
|
|
106 |
* see: http://github.com/jrburke/requirejs for details
|
|
|
107 |
*/
|
|
|
108 |
/*jslint strict: false, plusplus: false, sub: true */
|
|
|
109 |
/*global window: false, navigator: false, document: false, importScripts: false,
|
|
|
110 |
jQuery: false, clearInterval: false, setInterval: false, self: false,
|
|
|
111 |
setTimeout: false, opera: false */
|
|
|
112 |
|
|
|
113 |
|
|
|
114 |
(function () {
|
|
|
115 |
//Change this version number for each release.
|
|
|
116 |
var version = "1.0.1",
|
|
|
117 |
commentRegExp = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg,
|
|
|
118 |
cjsRequireRegExp = /require\(\s*["']([^'"\s]+)["']\s*\)/g,
|
|
|
119 |
currDirRegExp = /^\.\//,
|
|
|
120 |
jsSuffixRegExp = /\.js$/,
|
|
|
121 |
ostring = Object.prototype.toString,
|
|
|
122 |
ap = Array.prototype,
|
|
|
123 |
aps = ap.slice,
|
|
|
124 |
apsp = ap.splice,
|
|
|
125 |
isBrowser = !!(typeof window !== "undefined" && navigator && document),
|
|
|
126 |
isWebWorker = !isBrowser && typeof importScripts !== "undefined",
|
|
|
127 |
//PS3 indicates loaded and complete, but need to wait for complete
|
|
|
128 |
//specifically. Sequence is "loading", "loaded", execution,
|
|
|
129 |
// then "complete". The UA check is unfortunate, but not sure how
|
|
|
130 |
//to feature test w/o causing perf issues.
|
|
|
131 |
readyRegExp = isBrowser && navigator.platform === 'PLAYSTATION 3' ?
|
|
|
132 |
/^complete$/ : /^(complete|loaded)$/,
|
|
|
133 |
defContextName = "_",
|
|
|
134 |
//Oh the tragedy, detecting opera. See the usage of isOpera for reason.
|
|
|
135 |
isOpera = typeof opera !== "undefined" && opera.toString() === "[object Opera]",
|
|
|
136 |
empty = {},
|
|
|
137 |
contexts = {},
|
|
|
138 |
globalDefQueue = [],
|
|
|
139 |
interactiveScript = null,
|
|
|
140 |
checkLoadedDepth = 0,
|
|
|
141 |
useInteractive = false,
|
|
|
142 |
req, cfg = {}, currentlyAddingScript, s, head, baseElement, scripts, script,
|
|
|
143 |
src, subPath, mainScript, dataMain, i, ctx, jQueryCheck, checkLoadedTimeoutId;
|
|
|
144 |
|
|
|
145 |
function isFunction(it) {
|
|
|
146 |
return ostring.call(it) === "[object Function]";
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
function isArray(it) {
|
|
|
150 |
return ostring.call(it) === "[object Array]";
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
/**
|
|
|
154 |
* Simple function to mix in properties from source into target,
|
|
|
155 |
* but only if target does not already have a property of the same name.
|
|
|
156 |
* This is not robust in IE for transferring methods that match
|
|
|
157 |
* Object.prototype names, but the uses of mixin here seem unlikely to
|
|
|
158 |
* trigger a problem related to that.
|
|
|
159 |
*/
|
|
|
160 |
function mixin(target, source, force) {
|
|
|
161 |
for (var prop in source) {
|
|
|
162 |
if (!(prop in empty) && (!(prop in target) || force)) {
|
|
|
163 |
target[prop] = source[prop];
|
|
|
164 |
}
|
|
|
165 |
}
|
|
|
166 |
return req;
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
/**
|
|
|
170 |
* Constructs an error with a pointer to an URL with more information.
|
|
|
171 |
* @param {String} id the error ID that maps to an ID on a web page.
|
|
|
172 |
* @param {String} message human readable error.
|
|
|
173 |
* @param {Error} [err] the original error, if there is one.
|
|
|
174 |
*
|
|
|
175 |
* @returns {Error}
|
|
|
176 |
*/
|
|
|
177 |
function makeError(id, msg, err) {
|
|
|
178 |
var e = new Error(msg + '\nhttp://requirejs.org/docs/errors.html#' + id);
|
|
|
179 |
if (err) {
|
|
|
180 |
e.originalError = err;
|
|
|
181 |
}
|
|
|
182 |
return e;
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
/**
|
|
|
186 |
* Used to set up package paths from a packagePaths or packages config object.
|
|
|
187 |
* @param {Object} pkgs the object to store the new package config
|
|
|
188 |
* @param {Array} currentPackages an array of packages to configure
|
|
|
189 |
* @param {String} [dir] a prefix dir to use.
|
|
|
190 |
*/
|
|
|
191 |
function configurePackageDir(pkgs, currentPackages, dir) {
|
|
|
192 |
var i, location, pkgObj;
|
|
|
193 |
|
|
|
194 |
for (i = 0; (pkgObj = currentPackages[i]); i++) {
|
|
|
195 |
pkgObj = typeof pkgObj === "string" ? { name: pkgObj } : pkgObj;
|
|
|
196 |
location = pkgObj.location;
|
|
|
197 |
|
|
|
198 |
//Add dir to the path, but avoid paths that start with a slash
|
|
|
199 |
//or have a colon (indicates a protocol)
|
|
|
200 |
if (dir && (!location || (location.indexOf("/") !== 0 && location.indexOf(":") === -1))) {
|
|
|
201 |
location = dir + "/" + (location || pkgObj.name);
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
//Create a brand new object on pkgs, since currentPackages can
|
|
|
205 |
//be passed in again, and config.pkgs is the internal transformed
|
|
|
206 |
//state for all package configs.
|
|
|
207 |
pkgs[pkgObj.name] = {
|
|
|
208 |
name: pkgObj.name,
|
|
|
209 |
location: location || pkgObj.name,
|
|
|
210 |
//Remove leading dot in main, so main paths are normalized,
|
|
|
211 |
//and remove any trailing .js, since different package
|
|
|
212 |
//envs have different conventions: some use a module name,
|
|
|
213 |
//some use a file name.
|
|
|
214 |
main: (pkgObj.main || "main")
|
|
|
215 |
.replace(currDirRegExp, '')
|
|
|
216 |
.replace(jsSuffixRegExp, '')
|
|
|
217 |
};
|
|
|
218 |
}
|
|
|
219 |
}
|
|
|
220 |
|
|
|
221 |
/**
|
|
|
222 |
* jQuery 1.4.3-1.5.x use a readyWait/ready() pairing to hold DOM
|
|
|
223 |
* ready callbacks, but jQuery 1.6 supports a holdReady() API instead.
|
|
|
224 |
* At some point remove the readyWait/ready() support and just stick
|
|
|
225 |
* with using holdReady.
|
|
|
226 |
*/
|
|
|
227 |
function jQueryHoldReady($, shouldHold) {
|
|
|
228 |
if ($.holdReady) {
|
|
|
229 |
$.holdReady(shouldHold);
|
|
|
230 |
} else if (shouldHold) {
|
|
|
231 |
$.readyWait += 1;
|
|
|
232 |
} else {
|
|
|
233 |
$.ready(true);
|
|
|
234 |
}
|
|
|
235 |
}
|
|
|
236 |
|
|
|
237 |
if (typeof define !== "undefined") {
|
|
|
238 |
//If a define is already in play via another AMD loader,
|
|
|
239 |
//do not overwrite.
|
|
|
240 |
return;
|
|
|
241 |
}
|
|
|
242 |
|
|
|
243 |
if (typeof requirejs !== "undefined") {
|
|
|
244 |
if (isFunction(requirejs)) {
|
|
|
245 |
//Do not overwrite and existing requirejs instance.
|
|
|
246 |
return;
|
|
|
247 |
} else {
|
|
|
248 |
cfg = requirejs;
|
|
|
249 |
requirejs = undefined;
|
|
|
250 |
}
|
|
|
251 |
}
|
|
|
252 |
|
|
|
253 |
//Allow for a require config object
|
|
|
254 |
if (typeof require !== "undefined" && !isFunction(require)) {
|
|
|
255 |
//assume it is a config object.
|
|
|
256 |
cfg = require;
|
|
|
257 |
require = undefined;
|
|
|
258 |
}
|
|
|
259 |
|
|
|
260 |
/**
|
|
|
261 |
* Creates a new context for use in require and define calls.
|
|
|
262 |
* Handle most of the heavy lifting. Do not want to use an object
|
|
|
263 |
* with prototype here to avoid using "this" in require, in case it
|
|
|
264 |
* needs to be used in more super secure envs that do not want this.
|
|
|
265 |
* Also there should not be that many contexts in the page. Usually just
|
|
|
266 |
* one for the default context, but could be extra for multiversion cases
|
|
|
267 |
* or if a package needs a special context for a dependency that conflicts
|
|
|
268 |
* with the standard context.
|
|
|
269 |
*/
|
|
|
270 |
function newContext(contextName) {
|
|
|
271 |
var context, resume,
|
|
|
272 |
config = {
|
|
|
273 |
waitSeconds: 7,
|
|
|
274 |
baseUrl: "./",
|
|
|
275 |
paths: {},
|
|
|
276 |
pkgs: {},
|
|
|
277 |
catchError: {}
|
|
|
278 |
},
|
|
|
279 |
defQueue = [],
|
|
|
280 |
specified = {
|
|
|
281 |
"require": true,
|
|
|
282 |
"exports": true,
|
|
|
283 |
"module": true
|
|
|
284 |
},
|
|
|
285 |
urlMap = {},
|
|
|
286 |
defined = {},
|
|
|
287 |
loaded = {},
|
|
|
288 |
waiting = {},
|
|
|
289 |
waitAry = [],
|
|
|
290 |
urlFetched = {},
|
|
|
291 |
managerCounter = 0,
|
|
|
292 |
managerCallbacks = {},
|
|
|
293 |
plugins = {},
|
|
|
294 |
//Used to indicate which modules in a build scenario
|
|
|
295 |
//need to be full executed.
|
|
|
296 |
needFullExec = {},
|
|
|
297 |
fullExec = {},
|
|
|
298 |
resumeDepth = 0;
|
|
|
299 |
|
|
|
300 |
/**
|
|
|
301 |
* Trims the . and .. from an array of path segments.
|
|
|
302 |
* It will keep a leading path segment if a .. will become
|
|
|
303 |
* the first path segment, to help with module name lookups,
|
|
|
304 |
* which act like paths, but can be remapped. But the end result,
|
|
|
305 |
* all paths that use this function should look normalized.
|
|
|
306 |
* NOTE: this method MODIFIES the input array.
|
|
|
307 |
* @param {Array} ary the array of path segments.
|
|
|
308 |
*/
|
|
|
309 |
function trimDots(ary) {
|
|
|
310 |
var i, part;
|
|
|
311 |
for (i = 0; (part = ary[i]); i++) {
|
|
|
312 |
if (part === ".") {
|
|
|
313 |
ary.splice(i, 1);
|
|
|
314 |
i -= 1;
|
|
|
315 |
} else if (part === "..") {
|
|
|
316 |
if (i === 1 && (ary[2] === '..' || ary[0] === '..')) {
|
|
|
317 |
//End of the line. Keep at least one non-dot
|
|
|
318 |
//path segment at the front so it can be mapped
|
|
|
319 |
//correctly to disk. Otherwise, there is likely
|
|
|
320 |
//no path mapping for a path starting with '..'.
|
|
|
321 |
//This can still fail, but catches the most reasonable
|
|
|
322 |
//uses of ..
|
|
|
323 |
break;
|
|
|
324 |
} else if (i > 0) {
|
|
|
325 |
ary.splice(i - 1, 2);
|
|
|
326 |
i -= 2;
|
|
|
327 |
}
|
|
|
328 |
}
|
|
|
329 |
}
|
|
|
330 |
}
|
|
|
331 |
|
|
|
332 |
/**
|
|
|
333 |
* Given a relative module name, like ./something, normalize it to
|
|
|
334 |
* a real name that can be mapped to a path.
|
|
|
335 |
* @param {String} name the relative name
|
|
|
336 |
* @param {String} baseName a real name that the name arg is relative
|
|
|
337 |
* to.
|
|
|
338 |
* @returns {String} normalized name
|
|
|
339 |
*/
|
|
|
340 |
function normalize(name, baseName) {
|
|
|
341 |
var pkgName, pkgConfig;
|
|
|
342 |
|
|
|
343 |
//Adjust any relative paths.
|
|
|
344 |
if (name && name.charAt(0) === ".") {
|
|
|
345 |
//If have a base name, try to normalize against it,
|
|
|
346 |
//otherwise, assume it is a top-level require that will
|
|
|
347 |
//be relative to baseUrl in the end.
|
|
|
348 |
if (baseName) {
|
|
|
349 |
if (config.pkgs[baseName]) {
|
|
|
350 |
//If the baseName is a package name, then just treat it as one
|
|
|
351 |
//name to concat the name with.
|
|
|
352 |
baseName = [baseName];
|
|
|
353 |
} else {
|
|
|
354 |
//Convert baseName to array, and lop off the last part,
|
|
|
355 |
//so that . matches that "directory" and not name of the baseName's
|
|
|
356 |
//module. For instance, baseName of "one/two/three", maps to
|
|
|
357 |
//"one/two/three.js", but we want the directory, "one/two" for
|
|
|
358 |
//this normalization.
|
|
|
359 |
baseName = baseName.split("/");
|
|
|
360 |
baseName = baseName.slice(0, baseName.length - 1);
|
|
|
361 |
}
|
|
|
362 |
|
|
|
363 |
name = baseName.concat(name.split("/"));
|
|
|
364 |
trimDots(name);
|
|
|
365 |
|
|
|
366 |
//Some use of packages may use a . path to reference the
|
|
|
367 |
//"main" module name, so normalize for that.
|
|
|
368 |
pkgConfig = config.pkgs[(pkgName = name[0])];
|
|
|
369 |
name = name.join("/");
|
|
|
370 |
if (pkgConfig && name === pkgName + '/' + pkgConfig.main) {
|
|
|
371 |
name = pkgName;
|
|
|
372 |
}
|
|
|
373 |
}
|
|
|
374 |
}
|
|
|
375 |
return name;
|
|
|
376 |
}
|
|
|
377 |
|
|
|
378 |
/**
|
|
|
379 |
* Creates a module mapping that includes plugin prefix, module
|
|
|
380 |
* name, and path. If parentModuleMap is provided it will
|
|
|
381 |
* also normalize the name via require.normalize()
|
|
|
382 |
*
|
|
|
383 |
* @param {String} name the module name
|
|
|
384 |
* @param {String} [parentModuleMap] parent module map
|
|
|
385 |
* for the module name, used to resolve relative names.
|
|
|
386 |
*
|
|
|
387 |
* @returns {Object}
|
|
|
388 |
*/
|
|
|
389 |
function makeModuleMap(name, parentModuleMap) {
|
|
|
390 |
var index = name ? name.indexOf("!") : -1,
|
|
|
391 |
prefix = null,
|
|
|
392 |
parentName = parentModuleMap ? parentModuleMap.name : null,
|
|
|
393 |
originalName = name,
|
|
|
394 |
normalizedName, url, pluginModule;
|
|
|
395 |
|
|
|
396 |
if (index !== -1) {
|
|
|
397 |
prefix = name.substring(0, index);
|
|
|
398 |
name = name.substring(index + 1, name.length);
|
|
|
399 |
}
|
|
|
400 |
|
|
|
401 |
if (prefix) {
|
|
|
402 |
prefix = normalize(prefix, parentName);
|
|
|
403 |
}
|
|
|
404 |
|
|
|
405 |
//Account for relative paths if there is a base name.
|
|
|
406 |
if (name) {
|
|
|
407 |
if (prefix) {
|
|
|
408 |
pluginModule = defined[prefix];
|
|
|
409 |
if (pluginModule && pluginModule.normalize) {
|
|
|
410 |
//Plugin is loaded, use its normalize method.
|
|
|
411 |
normalizedName = pluginModule.normalize(name, function (name) {
|
|
|
412 |
return normalize(name, parentName);
|
|
|
413 |
});
|
|
|
414 |
} else {
|
|
|
415 |
normalizedName = normalize(name, parentName);
|
|
|
416 |
}
|
|
|
417 |
} else {
|
|
|
418 |
//A regular module.
|
|
|
419 |
normalizedName = normalize(name, parentName);
|
|
|
420 |
|
|
|
421 |
url = urlMap[normalizedName];
|
|
|
422 |
if (!url) {
|
|
|
423 |
//Calculate url for the module, if it has a name.
|
|
|
424 |
url = context.nameToUrl(normalizedName, null, parentModuleMap);
|
|
|
425 |
|
|
|
426 |
//Store the URL mapping for later.
|
|
|
427 |
urlMap[normalizedName] = url;
|
|
|
428 |
}
|
|
|
429 |
}
|
|
|
430 |
}
|
|
|
431 |
|
|
|
432 |
return {
|
|
|
433 |
prefix: prefix,
|
|
|
434 |
name: normalizedName,
|
|
|
435 |
parentMap: parentModuleMap,
|
|
|
436 |
url: url,
|
|
|
437 |
originalName: originalName,
|
|
|
438 |
fullName: prefix ? prefix + "!" + (normalizedName || '') : normalizedName
|
|
|
439 |
};
|
|
|
440 |
}
|
|
|
441 |
|
|
|
442 |
/**
|
|
|
443 |
* Determine if priority loading is done. If so clear the priorityWait
|
|
|
444 |
*/
|
|
|
445 |
function isPriorityDone() {
|
|
|
446 |
var priorityDone = true,
|
|
|
447 |
priorityWait = config.priorityWait,
|
|
|
448 |
priorityName, i;
|
|
|
449 |
if (priorityWait) {
|
|
|
450 |
for (i = 0; (priorityName = priorityWait[i]); i++) {
|
|
|
451 |
if (!loaded[priorityName]) {
|
|
|
452 |
priorityDone = false;
|
|
|
453 |
break;
|
|
|
454 |
}
|
|
|
455 |
}
|
|
|
456 |
if (priorityDone) {
|
|
|
457 |
delete config.priorityWait;
|
|
|
458 |
}
|
|
|
459 |
}
|
|
|
460 |
return priorityDone;
|
|
|
461 |
}
|
|
|
462 |
|
|
|
463 |
function makeContextModuleFunc(func, relModuleMap, enableBuildCallback) {
|
|
|
464 |
return function () {
|
|
|
465 |
//A version of a require function that passes a moduleName
|
|
|
466 |
//value for items that may need to
|
|
|
467 |
//look up paths relative to the moduleName
|
|
|
468 |
var args = aps.call(arguments, 0), lastArg;
|
|
|
469 |
if (enableBuildCallback &&
|
|
|
470 |
isFunction((lastArg = args[args.length - 1]))) {
|
|
|
471 |
lastArg.__requireJsBuild = true;
|
|
|
472 |
}
|
|
|
473 |
args.push(relModuleMap);
|
|
|
474 |
return func.apply(null, args);
|
|
|
475 |
};
|
|
|
476 |
}
|
|
|
477 |
|
|
|
478 |
/**
|
|
|
479 |
* Helper function that creates a require function object to give to
|
|
|
480 |
* modules that ask for it as a dependency. It needs to be specific
|
|
|
481 |
* per module because of the implication of path mappings that may
|
|
|
482 |
* need to be relative to the module name.
|
|
|
483 |
*/
|
|
|
484 |
function makeRequire(relModuleMap, enableBuildCallback) {
|
|
|
485 |
var modRequire = makeContextModuleFunc(context.require, relModuleMap, enableBuildCallback);
|
|
|
486 |
|
|
|
487 |
mixin(modRequire, {
|
|
|
488 |
nameToUrl: makeContextModuleFunc(context.nameToUrl, relModuleMap),
|
|
|
489 |
toUrl: makeContextModuleFunc(context.toUrl, relModuleMap),
|
|
|
490 |
defined: makeContextModuleFunc(context.requireDefined, relModuleMap),
|
|
|
491 |
specified: makeContextModuleFunc(context.requireSpecified, relModuleMap),
|
|
|
492 |
isBrowser: req.isBrowser
|
|
|
493 |
});
|
|
|
494 |
return modRequire;
|
|
|
495 |
}
|
|
|
496 |
|
|
|
497 |
/*
|
|
|
498 |
* Queues a dependency for checking after the loader is out of a
|
|
|
499 |
* "paused" state, for example while a script file is being loaded
|
|
|
500 |
* in the browser, where it may have many modules defined in it.
|
|
|
501 |
*/
|
|
|
502 |
function queueDependency(manager) {
|
|
|
503 |
context.paused.push(manager);
|
|
|
504 |
}
|
|
|
505 |
|
|
|
506 |
function execManager(manager) {
|
|
|
507 |
var i, ret, err, errFile, errModuleTree,
|
|
|
508 |
cb = manager.callback,
|
|
|
509 |
map = manager.map,
|
|
|
510 |
fullName = map.fullName,
|
|
|
511 |
args = manager.deps,
|
|
|
512 |
listeners = manager.listeners;
|
|
|
513 |
|
|
|
514 |
//Call the callback to define the module, if necessary.
|
|
|
515 |
if (cb && isFunction(cb)) {
|
|
|
516 |
if (config.catchError.define) {
|
|
|
517 |
try {
|
|
|
518 |
ret = req.execCb(fullName, manager.callback, args, defined[fullName]);
|
|
|
519 |
} catch (e) {
|
|
|
520 |
err = e;
|
|
|
521 |
}
|
|
|
522 |
} else {
|
|
|
523 |
ret = req.execCb(fullName, manager.callback, args, defined[fullName]);
|
|
|
524 |
}
|
|
|
525 |
|
|
|
526 |
if (fullName) {
|
|
|
527 |
//If setting exports via "module" is in play,
|
|
|
528 |
//favor that over return value and exports. After that,
|
|
|
529 |
//favor a non-undefined return value over exports use.
|
|
|
530 |
if (manager.cjsModule && manager.cjsModule.exports !== undefined) {
|
|
|
531 |
ret = defined[fullName] = manager.cjsModule.exports;
|
|
|
532 |
} else if (ret === undefined && manager.usingExports) {
|
|
|
533 |
//exports already set the defined value.
|
|
|
534 |
ret = defined[fullName];
|
|
|
535 |
} else {
|
|
|
536 |
//Use the return value from the function.
|
|
|
537 |
defined[fullName] = ret;
|
|
|
538 |
//If this module needed full execution in a build
|
|
|
539 |
//environment, mark that now.
|
|
|
540 |
if (needFullExec[fullName]) {
|
|
|
541 |
fullExec[fullName] = true;
|
|
|
542 |
}
|
|
|
543 |
}
|
|
|
544 |
}
|
|
|
545 |
} else if (fullName) {
|
|
|
546 |
//May just be an object definition for the module. Only
|
|
|
547 |
//worry about defining if have a module name.
|
|
|
548 |
ret = defined[fullName] = cb;
|
|
|
549 |
|
|
|
550 |
//If this module needed full execution in a build
|
|
|
551 |
//environment, mark that now.
|
|
|
552 |
if (needFullExec[fullName]) {
|
|
|
553 |
fullExec[fullName] = true;
|
|
|
554 |
}
|
|
|
555 |
}
|
|
|
556 |
|
|
|
557 |
//Clean up waiting. Do this before error calls, and before
|
|
|
558 |
//calling back listeners, so that bookkeeping is correct
|
|
|
559 |
//in the event of an error and error is reported in correct order,
|
|
|
560 |
//since the listeners will likely have errors if the
|
|
|
561 |
//onError function does not throw.
|
|
|
562 |
if (waiting[manager.id]) {
|
|
|
563 |
delete waiting[manager.id];
|
|
|
564 |
manager.isDone = true;
|
|
|
565 |
context.waitCount -= 1;
|
|
|
566 |
if (context.waitCount === 0) {
|
|
|
567 |
//Clear the wait array used for cycles.
|
|
|
568 |
waitAry = [];
|
|
|
569 |
}
|
|
|
570 |
}
|
|
|
571 |
|
|
|
572 |
//Do not need to track manager callback now that it is defined.
|
|
|
573 |
delete managerCallbacks[fullName];
|
|
|
574 |
|
|
|
575 |
//Allow instrumentation like the optimizer to know the order
|
|
|
576 |
//of modules executed and their dependencies.
|
|
|
577 |
if (req.onResourceLoad && !manager.placeholder) {
|
|
|
578 |
req.onResourceLoad(context, map, manager.depArray);
|
|
|
579 |
}
|
|
|
580 |
|
|
|
581 |
if (err) {
|
|
|
582 |
errFile = (fullName ? makeModuleMap(fullName).url : '') ||
|
|
|
583 |
err.fileName || err.sourceURL;
|
|
|
584 |
errModuleTree = err.moduleTree;
|
|
|
585 |
err = makeError('defineerror', 'Error evaluating ' +
|
|
|
586 |
'module "' + fullName + '" at location "' +
|
|
|
587 |
errFile + '":\n' +
|
|
|
588 |
err + '\nfileName:' + errFile +
|
|
|
589 |
'\nlineNumber: ' + (err.lineNumber || err.line), err);
|
|
|
590 |
err.moduleName = fullName;
|
|
|
591 |
err.moduleTree = errModuleTree;
|
|
|
592 |
return req.onError(err);
|
|
|
593 |
}
|
|
|
594 |
|
|
|
595 |
//Let listeners know of this manager's value.
|
|
|
596 |
for (i = 0; (cb = listeners[i]); i++) {
|
|
|
597 |
cb(ret);
|
|
|
598 |
}
|
|
|
599 |
|
|
|
600 |
return undefined;
|
|
|
601 |
}
|
|
|
602 |
|
|
|
603 |
/**
|
|
|
604 |
* Helper that creates a callack function that is called when a dependency
|
|
|
605 |
* is ready, and sets the i-th dependency for the manager as the
|
|
|
606 |
* value passed to the callback generated by this function.
|
|
|
607 |
*/
|
|
|
608 |
function makeArgCallback(manager, i) {
|
|
|
609 |
return function (value) {
|
|
|
610 |
//Only do the work if it has not been done
|
|
|
611 |
//already for a dependency. Cycle breaking
|
|
|
612 |
//logic in forceExec could mean this function
|
|
|
613 |
//is called more than once for a given dependency.
|
|
|
614 |
if (!manager.depDone[i]) {
|
|
|
615 |
manager.depDone[i] = true;
|
|
|
616 |
manager.deps[i] = value;
|
|
|
617 |
manager.depCount -= 1;
|
|
|
618 |
if (!manager.depCount) {
|
|
|
619 |
//All done, execute!
|
|
|
620 |
execManager(manager);
|
|
|
621 |
}
|
|
|
622 |
}
|
|
|
623 |
};
|
|
|
624 |
}
|
|
|
625 |
|
|
|
626 |
function callPlugin(pluginName, depManager) {
|
|
|
627 |
var map = depManager.map,
|
|
|
628 |
fullName = map.fullName,
|
|
|
629 |
name = map.name,
|
|
|
630 |
plugin = plugins[pluginName] ||
|
|
|
631 |
(plugins[pluginName] = defined[pluginName]),
|
|
|
632 |
load;
|
|
|
633 |
|
|
|
634 |
//No need to continue if the manager is already
|
|
|
635 |
//in the process of loading.
|
|
|
636 |
if (depManager.loading) {
|
|
|
637 |
return;
|
|
|
638 |
}
|
|
|
639 |
depManager.loading = true;
|
|
|
640 |
|
|
|
641 |
load = function (ret) {
|
|
|
642 |
depManager.callback = function () {
|
|
|
643 |
return ret;
|
|
|
644 |
};
|
|
|
645 |
execManager(depManager);
|
|
|
646 |
|
|
|
647 |
loaded[depManager.id] = true;
|
|
|
648 |
|
|
|
649 |
//The loading of this plugin
|
|
|
650 |
//might have placed other things
|
|
|
651 |
//in the paused queue. In particular,
|
|
|
652 |
//a loader plugin that depends on
|
|
|
653 |
//a different plugin loaded resource.
|
|
|
654 |
resume();
|
|
|
655 |
};
|
|
|
656 |
|
|
|
657 |
//Allow plugins to load other code without having to know the
|
|
|
658 |
//context or how to "complete" the load.
|
|
|
659 |
load.fromText = function (moduleName, text) {
|
|
|
660 |
/*jslint evil: true */
|
|
|
661 |
var hasInteractive = useInteractive;
|
|
|
662 |
|
|
|
663 |
//Indicate a the module is in process of loading.
|
|
|
664 |
loaded[moduleName] = false;
|
|
|
665 |
context.scriptCount += 1;
|
|
|
666 |
|
|
|
667 |
//Indicate this is not a "real" module, so do not track it
|
|
|
668 |
//for builds, it does not map to a real file.
|
|
|
669 |
context.fake[moduleName] = true;
|
|
|
670 |
|
|
|
671 |
//Turn off interactive script matching for IE for any define
|
|
|
672 |
//calls in the text, then turn it back on at the end.
|
|
|
673 |
if (hasInteractive) {
|
|
|
674 |
useInteractive = false;
|
|
|
675 |
}
|
|
|
676 |
|
|
|
677 |
req.exec(text);
|
|
|
678 |
|
|
|
679 |
if (hasInteractive) {
|
|
|
680 |
useInteractive = true;
|
|
|
681 |
}
|
|
|
682 |
|
|
|
683 |
//Support anonymous modules.
|
|
|
684 |
context.completeLoad(moduleName);
|
|
|
685 |
};
|
|
|
686 |
|
|
|
687 |
//No need to continue if the plugin value has already been
|
|
|
688 |
//defined by a build.
|
|
|
689 |
if (fullName in defined) {
|
|
|
690 |
load(defined[fullName]);
|
|
|
691 |
} else {
|
|
|
692 |
//Use parentName here since the plugin's name is not reliable,
|
|
|
693 |
//could be some weird string with no path that actually wants to
|
|
|
694 |
//reference the parentName's path.
|
|
|
695 |
plugin.load(name, makeRequire(map.parentMap, true), load, config);
|
|
|
696 |
}
|
|
|
697 |
}
|
|
|
698 |
|
|
|
699 |
/**
|
|
|
700 |
* Adds the manager to the waiting queue. Only fully
|
|
|
701 |
* resolved items should be in the waiting queue.
|
|
|
702 |
*/
|
|
|
703 |
function addWait(manager) {
|
|
|
704 |
if (!waiting[manager.id]) {
|
|
|
705 |
waiting[manager.id] = manager;
|
|
|
706 |
waitAry.push(manager);
|
|
|
707 |
context.waitCount += 1;
|
|
|
708 |
}
|
|
|
709 |
}
|
|
|
710 |
|
|
|
711 |
/**
|
|
|
712 |
* Function added to every manager object. Created out here
|
|
|
713 |
* to avoid new function creation for each manager instance.
|
|
|
714 |
*/
|
|
|
715 |
function managerAdd(cb) {
|
|
|
716 |
this.listeners.push(cb);
|
|
|
717 |
}
|
|
|
718 |
|
|
|
719 |
function getManager(map, shouldQueue) {
|
|
|
720 |
var fullName = map.fullName,
|
|
|
721 |
prefix = map.prefix,
|
|
|
722 |
plugin = prefix ? plugins[prefix] ||
|
|
|
723 |
(plugins[prefix] = defined[prefix]) : null,
|
|
|
724 |
manager, created, pluginManager;
|
|
|
725 |
|
|
|
726 |
if (fullName) {
|
|
|
727 |
manager = managerCallbacks[fullName];
|
|
|
728 |
}
|
|
|
729 |
|
|
|
730 |
if (!manager) {
|
|
|
731 |
created = true;
|
|
|
732 |
manager = {
|
|
|
733 |
//ID is just the full name, but if it is a plugin resource
|
|
|
734 |
//for a plugin that has not been loaded,
|
|
|
735 |
//then add an ID counter to it.
|
|
|
736 |
id: (prefix && !plugin ?
|
|
|
737 |
(managerCounter++) + '__p@:' : '') +
|
|
|
738 |
(fullName || '__r@' + (managerCounter++)),
|
|
|
739 |
map: map,
|
|
|
740 |
depCount: 0,
|
|
|
741 |
depDone: [],
|
|
|
742 |
depCallbacks: [],
|
|
|
743 |
deps: [],
|
|
|
744 |
listeners: [],
|
|
|
745 |
add: managerAdd
|
|
|
746 |
};
|
|
|
747 |
|
|
|
748 |
specified[manager.id] = true;
|
|
|
749 |
|
|
|
750 |
//Only track the manager/reuse it if this is a non-plugin
|
|
|
751 |
//resource. Also only track plugin resources once
|
|
|
752 |
//the plugin has been loaded, and so the fullName is the
|
|
|
753 |
//true normalized value.
|
|
|
754 |
if (fullName && (!prefix || plugins[prefix])) {
|
|
|
755 |
managerCallbacks[fullName] = manager;
|
|
|
756 |
}
|
|
|
757 |
}
|
|
|
758 |
|
|
|
759 |
//If there is a plugin needed, but it is not loaded,
|
|
|
760 |
//first load the plugin, then continue on.
|
|
|
761 |
if (prefix && !plugin) {
|
|
|
762 |
pluginManager = getManager(makeModuleMap(prefix), true);
|
|
|
763 |
pluginManager.add(function (plugin) {
|
|
|
764 |
//Create a new manager for the normalized
|
|
|
765 |
//resource ID and have it call this manager when
|
|
|
766 |
//done.
|
|
|
767 |
var newMap = makeModuleMap(map.originalName, map.parentMap),
|
|
|
768 |
normalizedManager = getManager(newMap, true);
|
|
|
769 |
|
|
|
770 |
//Indicate this manager is a placeholder for the real,
|
|
|
771 |
//normalized thing. Important for when trying to map
|
|
|
772 |
//modules and dependencies, for instance, in a build.
|
|
|
773 |
manager.placeholder = true;
|
|
|
774 |
|
|
|
775 |
normalizedManager.add(function (resource) {
|
|
|
776 |
manager.callback = function () {
|
|
|
777 |
return resource;
|
|
|
778 |
};
|
|
|
779 |
execManager(manager);
|
|
|
780 |
});
|
|
|
781 |
});
|
|
|
782 |
} else if (created && shouldQueue) {
|
|
|
783 |
//Indicate the resource is not loaded yet if it is to be
|
|
|
784 |
//queued.
|
|
|
785 |
loaded[manager.id] = false;
|
|
|
786 |
queueDependency(manager);
|
|
|
787 |
addWait(manager);
|
|
|
788 |
}
|
|
|
789 |
|
|
|
790 |
return manager;
|
|
|
791 |
}
|
|
|
792 |
|
|
|
793 |
function main(inName, depArray, callback, relModuleMap) {
|
|
|
794 |
var moduleMap = makeModuleMap(inName, relModuleMap),
|
|
|
795 |
name = moduleMap.name,
|
|
|
796 |
fullName = moduleMap.fullName,
|
|
|
797 |
manager = getManager(moduleMap),
|
|
|
798 |
id = manager.id,
|
|
|
799 |
deps = manager.deps,
|
|
|
800 |
i, depArg, depName, depPrefix, cjsMod;
|
|
|
801 |
|
|
|
802 |
if (fullName) {
|
|
|
803 |
//If module already defined for context, or already loaded,
|
|
|
804 |
//then leave. Also leave if jQuery is registering but it does
|
|
|
805 |
//not match the desired version number in the config.
|
|
|
806 |
if (fullName in defined || loaded[id] === true ||
|
|
|
807 |
(fullName === "jquery" && config.jQuery &&
|
|
|
808 |
config.jQuery !== callback().fn.jquery)) {
|
|
|
809 |
return;
|
|
|
810 |
}
|
|
|
811 |
|
|
|
812 |
//Set specified/loaded here for modules that are also loaded
|
|
|
813 |
//as part of a layer, where onScriptLoad is not fired
|
|
|
814 |
//for those cases. Do this after the inline define and
|
|
|
815 |
//dependency tracing is done.
|
|
|
816 |
specified[id] = true;
|
|
|
817 |
loaded[id] = true;
|
|
|
818 |
|
|
|
819 |
//If module is jQuery set up delaying its dom ready listeners.
|
|
|
820 |
if (fullName === "jquery" && callback) {
|
|
|
821 |
jQueryCheck(callback());
|
|
|
822 |
}
|
|
|
823 |
}
|
|
|
824 |
|
|
|
825 |
//Attach real depArray and callback to the manager. Do this
|
|
|
826 |
//only if the module has not been defined already, so do this after
|
|
|
827 |
//the fullName checks above. IE can call main() more than once
|
|
|
828 |
//for a module.
|
|
|
829 |
manager.depArray = depArray;
|
|
|
830 |
manager.callback = callback;
|
|
|
831 |
|
|
|
832 |
//Add the dependencies to the deps field, and register for callbacks
|
|
|
833 |
//on the dependencies.
|
|
|
834 |
for (i = 0; i < depArray.length; i++) {
|
|
|
835 |
depArg = depArray[i];
|
|
|
836 |
//There could be cases like in IE, where a trailing comma will
|
|
|
837 |
//introduce a null dependency, so only treat a real dependency
|
|
|
838 |
//value as a dependency.
|
|
|
839 |
if (depArg) {
|
|
|
840 |
//Split the dependency name into plugin and name parts
|
|
|
841 |
depArg = makeModuleMap(depArg, (name ? moduleMap : relModuleMap));
|
|
|
842 |
depName = depArg.fullName;
|
|
|
843 |
depPrefix = depArg.prefix;
|
|
|
844 |
|
|
|
845 |
//Fix the name in depArray to be just the name, since
|
|
|
846 |
//that is how it will be called back later.
|
|
|
847 |
depArray[i] = depName;
|
|
|
848 |
|
|
|
849 |
//Fast path CommonJS standard dependencies.
|
|
|
850 |
if (depName === "require") {
|
|
|
851 |
deps[i] = makeRequire(moduleMap);
|
|
|
852 |
} else if (depName === "exports") {
|
|
|
853 |
//CommonJS module spec 1.1
|
|
|
854 |
deps[i] = defined[fullName] = {};
|
|
|
855 |
manager.usingExports = true;
|
|
|
856 |
} else if (depName === "module") {
|
|
|
857 |
//CommonJS module spec 1.1
|
|
|
858 |
manager.cjsModule = cjsMod = deps[i] = {
|
|
|
859 |
id: name,
|
|
|
860 |
uri: name ? context.nameToUrl(name, null, relModuleMap) : undefined,
|
|
|
861 |
exports: defined[fullName]
|
|
|
862 |
};
|
|
|
863 |
} else if (depName in defined && !(depName in waiting) &&
|
|
|
864 |
(!(fullName in needFullExec) ||
|
|
|
865 |
(fullName in needFullExec && fullExec[depName]))) {
|
|
|
866 |
//Module already defined, and not in a build situation
|
|
|
867 |
//where the module is a something that needs full
|
|
|
868 |
//execution and this dependency has not been fully
|
|
|
869 |
//executed. See r.js's requirePatch.js for more info
|
|
|
870 |
//on fullExec.
|
|
|
871 |
deps[i] = defined[depName];
|
|
|
872 |
} else {
|
|
|
873 |
//Mark this dependency as needing full exec if
|
|
|
874 |
//the current module needs full exec.
|
|
|
875 |
if (fullName in needFullExec) {
|
|
|
876 |
needFullExec[depName] = true;
|
|
|
877 |
//Reset state so fully executed code will get
|
|
|
878 |
//picked up correctly.
|
|
|
879 |
delete defined[depName];
|
|
|
880 |
urlFetched[depArg.url] = false;
|
|
|
881 |
}
|
|
|
882 |
|
|
|
883 |
//Either a resource that is not loaded yet, or a plugin
|
|
|
884 |
//resource for either a plugin that has not
|
|
|
885 |
//loaded yet.
|
|
|
886 |
manager.depCount += 1;
|
|
|
887 |
manager.depCallbacks[i] = makeArgCallback(manager, i);
|
|
|
888 |
getManager(depArg, true).add(manager.depCallbacks[i]);
|
|
|
889 |
}
|
|
|
890 |
}
|
|
|
891 |
}
|
|
|
892 |
|
|
|
893 |
//Do not bother tracking the manager if it is all done.
|
|
|
894 |
if (!manager.depCount) {
|
|
|
895 |
//All done, execute!
|
|
|
896 |
execManager(manager);
|
|
|
897 |
} else {
|
|
|
898 |
addWait(manager);
|
|
|
899 |
}
|
|
|
900 |
}
|
|
|
901 |
|
|
|
902 |
/**
|
|
|
903 |
* Convenience method to call main for a define call that was put on
|
|
|
904 |
* hold in the defQueue.
|
|
|
905 |
*/
|
|
|
906 |
function callDefMain(args) {
|
|
|
907 |
main.apply(null, args);
|
|
|
908 |
}
|
|
|
909 |
|
|
|
910 |
/**
|
|
|
911 |
* jQuery 1.4.3+ supports ways to hold off calling
|
|
|
912 |
* calling jQuery ready callbacks until all scripts are loaded. Be sure
|
|
|
913 |
* to track it if the capability exists.. Also, since jQuery 1.4.3 does
|
|
|
914 |
* not register as a module, need to do some global inference checking.
|
|
|
915 |
* Even if it does register as a module, not guaranteed to be the precise
|
|
|
916 |
* name of the global. If a jQuery is tracked for this context, then go
|
|
|
917 |
* ahead and register it as a module too, if not already in process.
|
|
|
918 |
*/
|
|
|
919 |
jQueryCheck = function (jqCandidate) {
|
|
|
920 |
if (!context.jQuery) {
|
|
|
921 |
var $ = jqCandidate || (typeof jQuery !== "undefined" ? jQuery : null);
|
|
|
922 |
|
|
|
923 |
if ($) {
|
|
|
924 |
//If a specific version of jQuery is wanted, make sure to only
|
|
|
925 |
//use this jQuery if it matches.
|
|
|
926 |
if (config.jQuery && $.fn.jquery !== config.jQuery) {
|
|
|
927 |
return;
|
|
|
928 |
}
|
|
|
929 |
|
|
|
930 |
if ("holdReady" in $ || "readyWait" in $) {
|
|
|
931 |
context.jQuery = $;
|
|
|
932 |
|
|
|
933 |
//Manually create a "jquery" module entry if not one already
|
|
|
934 |
//or in process. Note this could trigger an attempt at
|
|
|
935 |
//a second jQuery registration, but does no harm since
|
|
|
936 |
//the first one wins, and it is the same value anyway.
|
|
|
937 |
callDefMain(["jquery", [], function () {
|
|
|
938 |
return jQuery;
|
|
|
939 |
}]);
|
|
|
940 |
|
|
|
941 |
//Ask jQuery to hold DOM ready callbacks.
|
|
|
942 |
if (context.scriptCount) {
|
|
|
943 |
jQueryHoldReady($, true);
|
|
|
944 |
context.jQueryIncremented = true;
|
|
|
945 |
}
|
|
|
946 |
}
|
|
|
947 |
}
|
|
|
948 |
}
|
|
|
949 |
};
|
|
|
950 |
|
|
|
951 |
function forceExec(manager, traced) {
|
|
|
952 |
if (manager.isDone) {
|
|
|
953 |
return undefined;
|
|
|
954 |
}
|
|
|
955 |
|
|
|
956 |
var fullName = manager.map.fullName,
|
|
|
957 |
depArray = manager.depArray,
|
|
|
958 |
i, depName, depManager, prefix, prefixManager, value;
|
|
|
959 |
|
|
|
960 |
if (fullName) {
|
|
|
961 |
if (traced[fullName]) {
|
|
|
962 |
return defined[fullName];
|
|
|
963 |
}
|
|
|
964 |
|
|
|
965 |
traced[fullName] = true;
|
|
|
966 |
}
|
|
|
967 |
|
|
|
968 |
//Trace through the dependencies.
|
|
|
969 |
if (depArray) {
|
|
|
970 |
for (i = 0; i < depArray.length; i++) {
|
|
|
971 |
//Some array members may be null, like if a trailing comma
|
|
|
972 |
//IE, so do the explicit [i] access and check if it has a value.
|
|
|
973 |
depName = depArray[i];
|
|
|
974 |
if (depName) {
|
|
|
975 |
//First, make sure if it is a plugin resource that the
|
|
|
976 |
//plugin is not blocked.
|
|
|
977 |
prefix = makeModuleMap(depName).prefix;
|
|
|
978 |
if (prefix && (prefixManager = waiting[prefix])) {
|
|
|
979 |
forceExec(prefixManager, traced);
|
|
|
980 |
}
|
|
|
981 |
depManager = waiting[depName];
|
|
|
982 |
if (depManager && !depManager.isDone && loaded[depName]) {
|
|
|
983 |
value = forceExec(depManager, traced);
|
|
|
984 |
manager.depCallbacks[i](value);
|
|
|
985 |
}
|
|
|
986 |
}
|
|
|
987 |
}
|
|
|
988 |
}
|
|
|
989 |
|
|
|
990 |
return fullName ? defined[fullName] : undefined;
|
|
|
991 |
}
|
|
|
992 |
|
|
|
993 |
/**
|
|
|
994 |
* Checks if all modules for a context are loaded, and if so, evaluates the
|
|
|
995 |
* new ones in right dependency order.
|
|
|
996 |
*
|
|
|
997 |
* @private
|
|
|
998 |
*/
|
|
|
999 |
function checkLoaded() {
|
|
|
1000 |
var waitInterval = config.waitSeconds * 1000,
|
|
|
1001 |
//It is possible to disable the wait interval by using waitSeconds of 0.
|
|
|
1002 |
expired = waitInterval && (context.startTime + waitInterval) < new Date().getTime(),
|
|
|
1003 |
noLoads = "", hasLoadedProp = false, stillLoading = false, prop,
|
|
|
1004 |
err, manager;
|
|
|
1005 |
|
|
|
1006 |
//If there are items still in the paused queue processing wait.
|
|
|
1007 |
//This is particularly important in the sync case where each paused
|
|
|
1008 |
//item is processed right away but there may be more waiting.
|
|
|
1009 |
if (context.pausedCount > 0) {
|
|
|
1010 |
return undefined;
|
|
|
1011 |
}
|
|
|
1012 |
|
|
|
1013 |
//Determine if priority loading is done. If so clear the priority. If
|
|
|
1014 |
//not, then do not check
|
|
|
1015 |
if (config.priorityWait) {
|
|
|
1016 |
if (isPriorityDone()) {
|
|
|
1017 |
//Call resume, since it could have
|
|
|
1018 |
//some waiting dependencies to trace.
|
|
|
1019 |
resume();
|
|
|
1020 |
} else {
|
|
|
1021 |
return undefined;
|
|
|
1022 |
}
|
|
|
1023 |
}
|
|
|
1024 |
|
|
|
1025 |
//See if anything is still in flight.
|
|
|
1026 |
for (prop in loaded) {
|
|
|
1027 |
if (!(prop in empty)) {
|
|
|
1028 |
hasLoadedProp = true;
|
|
|
1029 |
if (!loaded[prop]) {
|
|
|
1030 |
if (expired) {
|
|
|
1031 |
noLoads += prop + " ";
|
|
|
1032 |
} else {
|
|
|
1033 |
stillLoading = true;
|
|
|
1034 |
break;
|
|
|
1035 |
}
|
|
|
1036 |
}
|
|
|
1037 |
}
|
|
|
1038 |
}
|
|
|
1039 |
|
|
|
1040 |
//Check for exit conditions.
|
|
|
1041 |
if (!hasLoadedProp && !context.waitCount) {
|
|
|
1042 |
//If the loaded object had no items, then the rest of
|
|
|
1043 |
//the work below does not need to be done.
|
|
|
1044 |
return undefined;
|
|
|
1045 |
}
|
|
|
1046 |
if (expired && noLoads) {
|
|
|
1047 |
//If wait time expired, throw error of unloaded modules.
|
|
|
1048 |
err = makeError("timeout", "Load timeout for modules: " + noLoads);
|
|
|
1049 |
err.requireType = "timeout";
|
|
|
1050 |
err.requireModules = noLoads;
|
|
|
1051 |
return req.onError(err);
|
|
|
1052 |
}
|
|
|
1053 |
if (stillLoading || context.scriptCount) {
|
|
|
1054 |
//Something is still waiting to load. Wait for it, but only
|
|
|
1055 |
//if a timeout is not already in effect.
|
|
|
1056 |
if ((isBrowser || isWebWorker) && !checkLoadedTimeoutId) {
|
|
|
1057 |
checkLoadedTimeoutId = setTimeout(function () {
|
|
|
1058 |
checkLoadedTimeoutId = 0;
|
|
|
1059 |
checkLoaded();
|
|
|
1060 |
}, 50);
|
|
|
1061 |
}
|
|
|
1062 |
return undefined;
|
|
|
1063 |
}
|
|
|
1064 |
|
|
|
1065 |
//If still have items in the waiting cue, but all modules have
|
|
|
1066 |
//been loaded, then it means there are some circular dependencies
|
|
|
1067 |
//that need to be broken.
|
|
|
1068 |
//However, as a waiting thing is fired, then it can add items to
|
|
|
1069 |
//the waiting cue, and those items should not be fired yet, so
|
|
|
1070 |
//make sure to redo the checkLoaded call after breaking a single
|
|
|
1071 |
//cycle, if nothing else loaded then this logic will pick it up
|
|
|
1072 |
//again.
|
|
|
1073 |
if (context.waitCount) {
|
|
|
1074 |
//Cycle through the waitAry, and call items in sequence.
|
|
|
1075 |
for (i = 0; (manager = waitAry[i]); i++) {
|
|
|
1076 |
forceExec(manager, {});
|
|
|
1077 |
}
|
|
|
1078 |
|
|
|
1079 |
//If anything got placed in the paused queue, run it down.
|
|
|
1080 |
if (context.paused.length) {
|
|
|
1081 |
resume();
|
|
|
1082 |
}
|
|
|
1083 |
|
|
|
1084 |
//Only allow this recursion to a certain depth. Only
|
|
|
1085 |
//triggered by errors in calling a module in which its
|
|
|
1086 |
//modules waiting on it cannot finish loading, or some circular
|
|
|
1087 |
//dependencies that then may add more dependencies.
|
|
|
1088 |
//The value of 5 is a bit arbitrary. Hopefully just one extra
|
|
|
1089 |
//pass, or two for the case of circular dependencies generating
|
|
|
1090 |
//more work that gets resolved in the sync node case.
|
|
|
1091 |
if (checkLoadedDepth < 5) {
|
|
|
1092 |
checkLoadedDepth += 1;
|
|
|
1093 |
checkLoaded();
|
|
|
1094 |
}
|
|
|
1095 |
}
|
|
|
1096 |
|
|
|
1097 |
checkLoadedDepth = 0;
|
|
|
1098 |
|
|
|
1099 |
//Check for DOM ready, and nothing is waiting across contexts.
|
|
|
1100 |
req.checkReadyState();
|
|
|
1101 |
|
|
|
1102 |
return undefined;
|
|
|
1103 |
}
|
|
|
1104 |
|
|
|
1105 |
/**
|
|
|
1106 |
* Resumes tracing of dependencies and then checks if everything is loaded.
|
|
|
1107 |
*/
|
|
|
1108 |
resume = function () {
|
|
|
1109 |
var manager, map, url, i, p, args, fullName;
|
|
|
1110 |
|
|
|
1111 |
resumeDepth += 1;
|
|
|
1112 |
|
|
|
1113 |
if (context.scriptCount <= 0) {
|
|
|
1114 |
//Synchronous envs will push the number below zero with the
|
|
|
1115 |
//decrement above, be sure to set it back to zero for good measure.
|
|
|
1116 |
//require() calls that also do not end up loading scripts could
|
|
|
1117 |
//push the number negative too.
|
|
|
1118 |
context.scriptCount = 0;
|
|
|
1119 |
}
|
|
|
1120 |
|
|
|
1121 |
//Make sure any remaining defQueue items get properly processed.
|
|
|
1122 |
while (defQueue.length) {
|
|
|
1123 |
args = defQueue.shift();
|
|
|
1124 |
if (args[0] === null) {
|
|
|
1125 |
return req.onError(makeError('mismatch', 'Mismatched anonymous define() module: ' + args[args.length - 1]));
|
|
|
1126 |
} else {
|
|
|
1127 |
callDefMain(args);
|
|
|
1128 |
}
|
|
|
1129 |
}
|
|
|
1130 |
|
|
|
1131 |
//Skip the resume of paused dependencies
|
|
|
1132 |
//if current context is in priority wait.
|
|
|
1133 |
if (!config.priorityWait || isPriorityDone()) {
|
|
|
1134 |
while (context.paused.length) {
|
|
|
1135 |
p = context.paused;
|
|
|
1136 |
context.pausedCount += p.length;
|
|
|
1137 |
//Reset paused list
|
|
|
1138 |
context.paused = [];
|
|
|
1139 |
|
|
|
1140 |
for (i = 0; (manager = p[i]); i++) {
|
|
|
1141 |
map = manager.map;
|
|
|
1142 |
url = map.url;
|
|
|
1143 |
fullName = map.fullName;
|
|
|
1144 |
|
|
|
1145 |
//If the manager is for a plugin managed resource,
|
|
|
1146 |
//ask the plugin to load it now.
|
|
|
1147 |
if (map.prefix) {
|
|
|
1148 |
callPlugin(map.prefix, manager);
|
|
|
1149 |
} else {
|
|
|
1150 |
//Regular dependency.
|
|
|
1151 |
if (!urlFetched[url] && !loaded[fullName]) {
|
|
|
1152 |
req.load(context, fullName, url);
|
|
|
1153 |
urlFetched[url] = true;
|
|
|
1154 |
}
|
|
|
1155 |
}
|
|
|
1156 |
}
|
|
|
1157 |
|
|
|
1158 |
//Move the start time for timeout forward.
|
|
|
1159 |
context.startTime = (new Date()).getTime();
|
|
|
1160 |
context.pausedCount -= p.length;
|
|
|
1161 |
}
|
|
|
1162 |
}
|
|
|
1163 |
|
|
|
1164 |
//Only check if loaded when resume depth is 1. It is likely that
|
|
|
1165 |
//it is only greater than 1 in sync environments where a factory
|
|
|
1166 |
//function also then calls the callback-style require. In those
|
|
|
1167 |
//cases, the checkLoaded should not occur until the resume
|
|
|
1168 |
//depth is back at the top level.
|
|
|
1169 |
if (resumeDepth === 1) {
|
|
|
1170 |
checkLoaded();
|
|
|
1171 |
}
|
|
|
1172 |
|
|
|
1173 |
resumeDepth -= 1;
|
|
|
1174 |
|
|
|
1175 |
return undefined;
|
|
|
1176 |
};
|
|
|
1177 |
|
|
|
1178 |
//Define the context object. Many of these fields are on here
|
|
|
1179 |
//just to make debugging easier.
|
|
|
1180 |
context = {
|
|
|
1181 |
contextName: contextName,
|
|
|
1182 |
config: config,
|
|
|
1183 |
defQueue: defQueue,
|
|
|
1184 |
waiting: waiting,
|
|
|
1185 |
waitCount: 0,
|
|
|
1186 |
specified: specified,
|
|
|
1187 |
loaded: loaded,
|
|
|
1188 |
urlMap: urlMap,
|
|
|
1189 |
urlFetched: urlFetched,
|
|
|
1190 |
scriptCount: 0,
|
|
|
1191 |
defined: defined,
|
|
|
1192 |
paused: [],
|
|
|
1193 |
pausedCount: 0,
|
|
|
1194 |
plugins: plugins,
|
|
|
1195 |
needFullExec: needFullExec,
|
|
|
1196 |
fake: {},
|
|
|
1197 |
fullExec: fullExec,
|
|
|
1198 |
managerCallbacks: managerCallbacks,
|
|
|
1199 |
makeModuleMap: makeModuleMap,
|
|
|
1200 |
normalize: normalize,
|
|
|
1201 |
/**
|
|
|
1202 |
* Set a configuration for the context.
|
|
|
1203 |
* @param {Object} cfg config object to integrate.
|
|
|
1204 |
*/
|
|
|
1205 |
configure: function (cfg) {
|
|
|
1206 |
var paths, prop, packages, pkgs, packagePaths, requireWait;
|
|
|
1207 |
|
|
|
1208 |
//Make sure the baseUrl ends in a slash.
|
|
|
1209 |
if (cfg.baseUrl) {
|
|
|
1210 |
if (cfg.baseUrl.charAt(cfg.baseUrl.length - 1) !== "/") {
|
|
|
1211 |
cfg.baseUrl += "/";
|
|
|
1212 |
}
|
|
|
1213 |
}
|
|
|
1214 |
|
|
|
1215 |
//Save off the paths and packages since they require special processing,
|
|
|
1216 |
//they are additive.
|
|
|
1217 |
paths = config.paths;
|
|
|
1218 |
packages = config.packages;
|
|
|
1219 |
pkgs = config.pkgs;
|
|
|
1220 |
|
|
|
1221 |
//Mix in the config values, favoring the new values over
|
|
|
1222 |
//existing ones in context.config.
|
|
|
1223 |
mixin(config, cfg, true);
|
|
|
1224 |
|
|
|
1225 |
//Adjust paths if necessary.
|
|
|
1226 |
if (cfg.paths) {
|
|
|
1227 |
for (prop in cfg.paths) {
|
|
|
1228 |
if (!(prop in empty)) {
|
|
|
1229 |
paths[prop] = cfg.paths[prop];
|
|
|
1230 |
}
|
|
|
1231 |
}
|
|
|
1232 |
config.paths = paths;
|
|
|
1233 |
}
|
|
|
1234 |
|
|
|
1235 |
packagePaths = cfg.packagePaths;
|
|
|
1236 |
if (packagePaths || cfg.packages) {
|
|
|
1237 |
//Convert packagePaths into a packages config.
|
|
|
1238 |
if (packagePaths) {
|
|
|
1239 |
for (prop in packagePaths) {
|
|
|
1240 |
if (!(prop in empty)) {
|
|
|
1241 |
configurePackageDir(pkgs, packagePaths[prop], prop);
|
|
|
1242 |
}
|
|
|
1243 |
}
|
|
|
1244 |
}
|
|
|
1245 |
|
|
|
1246 |
//Adjust packages if necessary.
|
|
|
1247 |
if (cfg.packages) {
|
|
|
1248 |
configurePackageDir(pkgs, cfg.packages);
|
|
|
1249 |
}
|
|
|
1250 |
|
|
|
1251 |
//Done with modifications, assing packages back to context config
|
|
|
1252 |
config.pkgs = pkgs;
|
|
|
1253 |
}
|
|
|
1254 |
|
|
|
1255 |
//If priority loading is in effect, trigger the loads now
|
|
|
1256 |
if (cfg.priority) {
|
|
|
1257 |
//Hold on to requireWait value, and reset it after done
|
|
|
1258 |
requireWait = context.requireWait;
|
|
|
1259 |
|
|
|
1260 |
//Allow tracing some require calls to allow the fetching
|
|
|
1261 |
//of the priority config.
|
|
|
1262 |
context.requireWait = false;
|
|
|
1263 |
//But first, call resume to register any defined modules that may
|
|
|
1264 |
//be in a data-main built file before the priority config
|
|
|
1265 |
//call. Also grab any waiting define calls for this context.
|
|
|
1266 |
context.takeGlobalQueue();
|
|
|
1267 |
resume();
|
|
|
1268 |
|
|
|
1269 |
context.require(cfg.priority);
|
|
|
1270 |
|
|
|
1271 |
//Trigger a resume right away, for the case when
|
|
|
1272 |
//the script with the priority load is done as part
|
|
|
1273 |
//of a data-main call. In that case the normal resume
|
|
|
1274 |
//call will not happen because the scriptCount will be
|
|
|
1275 |
//at 1, since the script for data-main is being processed.
|
|
|
1276 |
resume();
|
|
|
1277 |
|
|
|
1278 |
//Restore previous state.
|
|
|
1279 |
context.requireWait = requireWait;
|
|
|
1280 |
config.priorityWait = cfg.priority;
|
|
|
1281 |
}
|
|
|
1282 |
|
|
|
1283 |
//If a deps array or a config callback is specified, then call
|
|
|
1284 |
//require with those args. This is useful when require is defined as a
|
|
|
1285 |
//config object before require.js is loaded.
|
|
|
1286 |
if (cfg.deps || cfg.callback) {
|
|
|
1287 |
context.require(cfg.deps || [], cfg.callback);
|
|
|
1288 |
}
|
|
|
1289 |
},
|
|
|
1290 |
|
|
|
1291 |
requireDefined: function (moduleName, relModuleMap) {
|
|
|
1292 |
return makeModuleMap(moduleName, relModuleMap).fullName in defined;
|
|
|
1293 |
},
|
|
|
1294 |
|
|
|
1295 |
requireSpecified: function (moduleName, relModuleMap) {
|
|
|
1296 |
return makeModuleMap(moduleName, relModuleMap).fullName in specified;
|
|
|
1297 |
},
|
|
|
1298 |
|
|
|
1299 |
require: function (deps, callback, relModuleMap) {
|
|
|
1300 |
var moduleName, fullName, moduleMap;
|
|
|
1301 |
if (typeof deps === "string") {
|
|
|
1302 |
if (isFunction(callback)) {
|
|
|
1303 |
//Invalid call
|
|
|
1304 |
return req.onError(makeError("requireargs", "Invalid require call"));
|
|
|
1305 |
}
|
|
|
1306 |
|
|
|
1307 |
//Synchronous access to one module. If require.get is
|
|
|
1308 |
//available (as in the Node adapter), prefer that.
|
|
|
1309 |
//In this case deps is the moduleName and callback is
|
|
|
1310 |
//the relModuleMap
|
|
|
1311 |
if (req.get) {
|
|
|
1312 |
return req.get(context, deps, callback);
|
|
|
1313 |
}
|
|
|
1314 |
|
|
|
1315 |
//Just return the module wanted. In this scenario, the
|
|
|
1316 |
//second arg (if passed) is just the relModuleMap.
|
|
|
1317 |
moduleName = deps;
|
|
|
1318 |
relModuleMap = callback;
|
|
|
1319 |
|
|
|
1320 |
//Normalize module name, if it contains . or ..
|
|
|
1321 |
moduleMap = makeModuleMap(moduleName, relModuleMap);
|
|
|
1322 |
fullName = moduleMap.fullName;
|
|
|
1323 |
|
|
|
1324 |
if (!(fullName in defined)) {
|
|
|
1325 |
return req.onError(makeError("notloaded", "Module name '" +
|
|
|
1326 |
moduleMap.fullName +
|
|
|
1327 |
"' has not been loaded yet for context: " +
|
|
|
1328 |
contextName));
|
|
|
1329 |
}
|
|
|
1330 |
return defined[fullName];
|
|
|
1331 |
}
|
|
|
1332 |
|
|
|
1333 |
//Call main but only if there are dependencies or
|
|
|
1334 |
//a callback to call.
|
|
|
1335 |
if (deps && deps.length || callback) {
|
|
|
1336 |
main(null, deps, callback, relModuleMap);
|
|
|
1337 |
}
|
|
|
1338 |
|
|
|
1339 |
//If the require call does not trigger anything new to load,
|
|
|
1340 |
//then resume the dependency processing.
|
|
|
1341 |
if (!context.requireWait) {
|
|
|
1342 |
while (!context.scriptCount && context.paused.length) {
|
|
|
1343 |
//For built layers, there can be some defined
|
|
|
1344 |
//modules waiting for intake into the context,
|
|
|
1345 |
//in particular module plugins. Take them.
|
|
|
1346 |
context.takeGlobalQueue();
|
|
|
1347 |
resume();
|
|
|
1348 |
}
|
|
|
1349 |
}
|
|
|
1350 |
return context.require;
|
|
|
1351 |
},
|
|
|
1352 |
|
|
|
1353 |
/**
|
|
|
1354 |
* Internal method to transfer globalQueue items to this context's
|
|
|
1355 |
* defQueue.
|
|
|
1356 |
*/
|
|
|
1357 |
takeGlobalQueue: function () {
|
|
|
1358 |
//Push all the globalDefQueue items into the context's defQueue
|
|
|
1359 |
if (globalDefQueue.length) {
|
|
|
1360 |
//Array splice in the values since the context code has a
|
|
|
1361 |
//local var ref to defQueue, so cannot just reassign the one
|
|
|
1362 |
//on context.
|
|
|
1363 |
apsp.apply(context.defQueue,
|
|
|
1364 |
[context.defQueue.length - 1, 0].concat(globalDefQueue));
|
|
|
1365 |
globalDefQueue = [];
|
|
|
1366 |
}
|
|
|
1367 |
},
|
|
|
1368 |
|
|
|
1369 |
/**
|
|
|
1370 |
* Internal method used by environment adapters to complete a load event.
|
|
|
1371 |
* A load event could be a script load or just a load pass from a synchronous
|
|
|
1372 |
* load call.
|
|
|
1373 |
* @param {String} moduleName the name of the module to potentially complete.
|
|
|
1374 |
*/
|
|
|
1375 |
completeLoad: function (moduleName) {
|
|
|
1376 |
var args;
|
|
|
1377 |
|
|
|
1378 |
context.takeGlobalQueue();
|
|
|
1379 |
|
|
|
1380 |
while (defQueue.length) {
|
|
|
1381 |
args = defQueue.shift();
|
|
|
1382 |
|
|
|
1383 |
if (args[0] === null) {
|
|
|
1384 |
args[0] = moduleName;
|
|
|
1385 |
break;
|
|
|
1386 |
} else if (args[0] === moduleName) {
|
|
|
1387 |
//Found matching define call for this script!
|
|
|
1388 |
break;
|
|
|
1389 |
} else {
|
|
|
1390 |
//Some other named define call, most likely the result
|
|
|
1391 |
//of a build layer that included many define calls.
|
|
|
1392 |
callDefMain(args);
|
|
|
1393 |
args = null;
|
|
|
1394 |
}
|
|
|
1395 |
}
|
|
|
1396 |
if (args) {
|
|
|
1397 |
callDefMain(args);
|
|
|
1398 |
} else {
|
|
|
1399 |
//A script that does not call define(), so just simulate
|
|
|
1400 |
//the call for it. Special exception for jQuery dynamic load.
|
|
|
1401 |
callDefMain([moduleName, [],
|
|
|
1402 |
moduleName === "jquery" && typeof jQuery !== "undefined" ?
|
|
|
1403 |
function () {
|
|
|
1404 |
return jQuery;
|
|
|
1405 |
} : null]);
|
|
|
1406 |
}
|
|
|
1407 |
|
|
|
1408 |
//If a global jQuery is defined, check for it. Need to do it here
|
|
|
1409 |
//instead of main() since stock jQuery does not register as
|
|
|
1410 |
//a module via define.
|
|
|
1411 |
jQueryCheck();
|
|
|
1412 |
|
|
|
1413 |
//Doing this scriptCount decrement branching because sync envs
|
|
|
1414 |
//need to decrement after resume, otherwise it looks like
|
|
|
1415 |
//loading is complete after the first dependency is fetched.
|
|
|
1416 |
//For browsers, it works fine to decrement after, but it means
|
|
|
1417 |
//the checkLoaded setTimeout 50 ms cost is taken. To avoid
|
|
|
1418 |
//that cost, decrement beforehand.
|
|
|
1419 |
if (req.isAsync) {
|
|
|
1420 |
context.scriptCount -= 1;
|
|
|
1421 |
}
|
|
|
1422 |
resume();
|
|
|
1423 |
if (!req.isAsync) {
|
|
|
1424 |
context.scriptCount -= 1;
|
|
|
1425 |
}
|
|
|
1426 |
},
|
|
|
1427 |
|
|
|
1428 |
/**
|
|
|
1429 |
* Converts a module name + .extension into an URL path.
|
|
|
1430 |
* *Requires* the use of a module name. It does not support using
|
|
|
1431 |
* plain URLs like nameToUrl.
|
|
|
1432 |
*/
|
|
|
1433 |
toUrl: function (moduleNamePlusExt, relModuleMap) {
|
|
|
1434 |
var index = moduleNamePlusExt.lastIndexOf("."),
|
|
|
1435 |
ext = null;
|
|
|
1436 |
|
|
|
1437 |
if (index !== -1) {
|
|
|
1438 |
ext = moduleNamePlusExt.substring(index, moduleNamePlusExt.length);
|
|
|
1439 |
moduleNamePlusExt = moduleNamePlusExt.substring(0, index);
|
|
|
1440 |
}
|
|
|
1441 |
|
|
|
1442 |
return context.nameToUrl(moduleNamePlusExt, ext, relModuleMap);
|
|
|
1443 |
},
|
|
|
1444 |
|
|
|
1445 |
/**
|
|
|
1446 |
* Converts a module name to a file path. Supports cases where
|
|
|
1447 |
* moduleName may actually be just an URL.
|
|
|
1448 |
*/
|
|
|
1449 |
nameToUrl: function (moduleName, ext, relModuleMap) {
|
|
|
1450 |
var paths, pkgs, pkg, pkgPath, syms, i, parentModule, url,
|
|
|
1451 |
config = context.config;
|
|
|
1452 |
|
|
|
1453 |
//Normalize module name if have a base relative module name to work from.
|
|
|
1454 |
moduleName = normalize(moduleName, relModuleMap && relModuleMap.fullName);
|
|
|
1455 |
|
|
|
1456 |
//If a colon is in the URL, it indicates a protocol is used and it is just
|
|
|
1457 |
//an URL to a file, or if it starts with a slash or ends with .js, it is just a plain file.
|
|
|
1458 |
//The slash is important for protocol-less URLs as well as full paths.
|
|
|
1459 |
if (req.jsExtRegExp.test(moduleName)) {
|
|
|
1460 |
//Just a plain path, not module name lookup, so just return it.
|
|
|
1461 |
//Add extension if it is included. This is a bit wonky, only non-.js things pass
|
|
|
1462 |
//an extension, this method probably needs to be reworked.
|
|
|
1463 |
url = moduleName + (ext ? ext : "");
|
|
|
1464 |
} else {
|
|
|
1465 |
//A module that needs to be converted to a path.
|
|
|
1466 |
paths = config.paths;
|
|
|
1467 |
pkgs = config.pkgs;
|
|
|
1468 |
|
|
|
1469 |
syms = moduleName.split("/");
|
|
|
1470 |
//For each module name segment, see if there is a path
|
|
|
1471 |
//registered for it. Start with most specific name
|
|
|
1472 |
//and work up from it.
|
|
|
1473 |
for (i = syms.length; i > 0; i--) {
|
|
|
1474 |
parentModule = syms.slice(0, i).join("/");
|
|
|
1475 |
if (paths[parentModule]) {
|
|
|
1476 |
syms.splice(0, i, paths[parentModule]);
|
|
|
1477 |
break;
|
|
|
1478 |
} else if ((pkg = pkgs[parentModule])) {
|
|
|
1479 |
//If module name is just the package name, then looking
|
|
|
1480 |
//for the main module.
|
|
|
1481 |
if (moduleName === pkg.name) {
|
|
|
1482 |
pkgPath = pkg.location + '/' + pkg.main;
|
|
|
1483 |
} else {
|
|
|
1484 |
pkgPath = pkg.location;
|
|
|
1485 |
}
|
|
|
1486 |
syms.splice(0, i, pkgPath);
|
|
|
1487 |
break;
|
|
|
1488 |
}
|
|
|
1489 |
}
|
|
|
1490 |
|
|
|
1491 |
//Join the path parts together, then figure out if baseUrl is needed.
|
|
|
1492 |
url = syms.join("/") + (ext || ".js");
|
|
|
1493 |
url = (url.charAt(0) === '/' || url.match(/^\w+:/) ? "" : config.baseUrl) + url;
|
|
|
1494 |
}
|
|
|
1495 |
|
|
|
1496 |
return config.urlArgs ? url +
|
|
|
1497 |
((url.indexOf('?') === -1 ? '?' : '&') +
|
|
|
1498 |
config.urlArgs) : url;
|
|
|
1499 |
}
|
|
|
1500 |
};
|
|
|
1501 |
|
|
|
1502 |
//Make these visible on the context so can be called at the very
|
|
|
1503 |
//end of the file to bootstrap
|
|
|
1504 |
context.jQueryCheck = jQueryCheck;
|
|
|
1505 |
context.resume = resume;
|
|
|
1506 |
|
|
|
1507 |
return context;
|
|
|
1508 |
}
|
|
|
1509 |
|
|
|
1510 |
/**
|
|
|
1511 |
* Main entry point.
|
|
|
1512 |
*
|
|
|
1513 |
* If the only argument to require is a string, then the module that
|
|
|
1514 |
* is represented by that string is fetched for the appropriate context.
|
|
|
1515 |
*
|
|
|
1516 |
* If the first argument is an array, then it will be treated as an array
|
|
|
1517 |
* of dependency string names to fetch. An optional function callback can
|
|
|
1518 |
* be specified to execute when all of those dependencies are available.
|
|
|
1519 |
*
|
|
|
1520 |
* Make a local req variable to help Caja compliance (it assumes things
|
|
|
1521 |
* on a require that are not standardized), and to give a short
|
|
|
1522 |
* name for minification/local scope use.
|
|
|
1523 |
*/
|
|
|
1524 |
req = requirejs = function (deps, callback) {
|
|
|
1525 |
|
|
|
1526 |
//Find the right context, use default
|
|
|
1527 |
var contextName = defContextName,
|
|
|
1528 |
context, config;
|
|
|
1529 |
|
|
|
1530 |
// Determine if have config object in the call.
|
|
|
1531 |
if (!isArray(deps) && typeof deps !== "string") {
|
|
|
1532 |
// deps is a config object
|
|
|
1533 |
config = deps;
|
|
|
1534 |
if (isArray(callback)) {
|
|
|
1535 |
// Adjust args if there are dependencies
|
|
|
1536 |
deps = callback;
|
|
|
1537 |
callback = arguments[2];
|
|
|
1538 |
} else {
|
|
|
1539 |
deps = [];
|
|
|
1540 |
}
|
|
|
1541 |
}
|
|
|
1542 |
|
|
|
1543 |
if (config && config.context) {
|
|
|
1544 |
contextName = config.context;
|
|
|
1545 |
}
|
|
|
1546 |
|
|
|
1547 |
context = contexts[contextName] ||
|
|
|
1548 |
(contexts[contextName] = newContext(contextName));
|
|
|
1549 |
|
|
|
1550 |
if (config) {
|
|
|
1551 |
context.configure(config);
|
|
|
1552 |
}
|
|
|
1553 |
|
|
|
1554 |
return context.require(deps, callback);
|
|
|
1555 |
};
|
|
|
1556 |
|
|
|
1557 |
/**
|
|
|
1558 |
* Support require.config() to make it easier to cooperate with other
|
|
|
1559 |
* AMD loaders on globally agreed names.
|
|
|
1560 |
*/
|
|
|
1561 |
req.config = function (config) {
|
|
|
1562 |
return req(config);
|
|
|
1563 |
};
|
|
|
1564 |
|
|
|
1565 |
/**
|
|
|
1566 |
* Export require as a global, but only if it does not already exist.
|
|
|
1567 |
*/
|
|
|
1568 |
if (!require) {
|
|
|
1569 |
require = req;
|
|
|
1570 |
}
|
|
|
1571 |
|
|
|
1572 |
/**
|
|
|
1573 |
* Global require.toUrl(), to match global require, mostly useful
|
|
|
1574 |
* for debugging/work in the global space.
|
|
|
1575 |
*/
|
|
|
1576 |
req.toUrl = function (moduleNamePlusExt) {
|
|
|
1577 |
return contexts[defContextName].toUrl(moduleNamePlusExt);
|
|
|
1578 |
};
|
|
|
1579 |
|
|
|
1580 |
req.version = version;
|
|
|
1581 |
|
|
|
1582 |
//Used to filter out dependencies that are already paths.
|
|
|
1583 |
req.jsExtRegExp = /^\/|:|\?|\.js$/;
|
|
|
1584 |
s = req.s = {
|
|
|
1585 |
contexts: contexts,
|
|
|
1586 |
//Stores a list of URLs that should not get async script tag treatment.
|
|
|
1587 |
skipAsync: {}
|
|
|
1588 |
};
|
|
|
1589 |
|
|
|
1590 |
req.isAsync = req.isBrowser = isBrowser;
|
|
|
1591 |
if (isBrowser) {
|
|
|
1592 |
head = s.head = document.getElementsByTagName("head")[0];
|
|
|
1593 |
//If BASE tag is in play, using appendChild is a problem for IE6.
|
|
|
1594 |
//When that browser dies, this can be removed. Details in this jQuery bug:
|
|
|
1595 |
//http://dev.jquery.com/ticket/2709
|
|
|
1596 |
baseElement = document.getElementsByTagName("base")[0];
|
|
|
1597 |
if (baseElement) {
|
|
|
1598 |
head = s.head = baseElement.parentNode;
|
|
|
1599 |
}
|
|
|
1600 |
}
|
|
|
1601 |
|
|
|
1602 |
/**
|
|
|
1603 |
* Any errors that require explicitly generates will be passed to this
|
|
|
1604 |
* function. Intercept/override it if you want custom error handling.
|
|
|
1605 |
* @param {Error} err the error object.
|
|
|
1606 |
*/
|
|
|
1607 |
req.onError = function (err) {
|
|
|
1608 |
throw err;
|
|
|
1609 |
};
|
|
|
1610 |
|
|
|
1611 |
/**
|
|
|
1612 |
* Does the request to load a module for the browser case.
|
|
|
1613 |
* Make this a separate function to allow other environments
|
|
|
1614 |
* to override it.
|
|
|
1615 |
*
|
|
|
1616 |
* @param {Object} context the require context to find state.
|
|
|
1617 |
* @param {String} moduleName the name of the module.
|
|
|
1618 |
* @param {Object} url the URL to the module.
|
|
|
1619 |
*/
|
|
|
1620 |
req.load = function (context, moduleName, url) {
|
|
|
1621 |
req.resourcesReady(false);
|
|
|
1622 |
|
|
|
1623 |
context.scriptCount += 1;
|
|
|
1624 |
req.attach(url, context, moduleName);
|
|
|
1625 |
|
|
|
1626 |
//If tracking a jQuery, then make sure its ready callbacks
|
|
|
1627 |
//are put on hold to prevent its ready callbacks from
|
|
|
1628 |
//triggering too soon.
|
|
|
1629 |
if (context.jQuery && !context.jQueryIncremented) {
|
|
|
1630 |
jQueryHoldReady(context.jQuery, true);
|
|
|
1631 |
context.jQueryIncremented = true;
|
|
|
1632 |
}
|
|
|
1633 |
};
|
|
|
1634 |
|
|
|
1635 |
function getInteractiveScript() {
|
|
|
1636 |
var scripts, i, script;
|
|
|
1637 |
if (interactiveScript && interactiveScript.readyState === 'interactive') {
|
|
|
1638 |
return interactiveScript;
|
|
|
1639 |
}
|
|
|
1640 |
|
|
|
1641 |
scripts = document.getElementsByTagName('script');
|
|
|
1642 |
for (i = scripts.length - 1; i > -1 && (script = scripts[i]); i--) {
|
|
|
1643 |
if (script.readyState === 'interactive') {
|
|
|
1644 |
return (interactiveScript = script);
|
|
|
1645 |
}
|
|
|
1646 |
}
|
|
|
1647 |
|
|
|
1648 |
return null;
|
|
|
1649 |
}
|
|
|
1650 |
|
|
|
1651 |
/**
|
|
|
1652 |
* The function that handles definitions of modules. Differs from
|
|
|
1653 |
* require() in that a string for the module should be the first argument,
|
|
|
1654 |
* and the function to execute after dependencies are loaded should
|
|
|
1655 |
* return a value to define the module corresponding to the first argument's
|
|
|
1656 |
* name.
|
|
|
1657 |
*/
|
|
|
1658 |
define = function (name, deps, callback) {
|
|
|
1659 |
var node, context;
|
|
|
1660 |
|
|
|
1661 |
//Allow for anonymous functions
|
|
|
1662 |
if (typeof name !== 'string') {
|
|
|
1663 |
//Adjust args appropriately
|
|
|
1664 |
callback = deps;
|
|
|
1665 |
deps = name;
|
|
|
1666 |
name = null;
|
|
|
1667 |
}
|
|
|
1668 |
|
|
|
1669 |
//This module may not have dependencies
|
|
|
1670 |
if (!isArray(deps)) {
|
|
|
1671 |
callback = deps;
|
|
|
1672 |
deps = [];
|
|
|
1673 |
}
|
|
|
1674 |
|
|
|
1675 |
//If no name, and callback is a function, then figure out if it a
|
|
|
1676 |
//CommonJS thing with dependencies.
|
|
|
1677 |
if (!deps.length && isFunction(callback)) {
|
|
|
1678 |
//Remove comments from the callback string,
|
|
|
1679 |
//look for require calls, and pull them into the dependencies,
|
|
|
1680 |
//but only if there are function args.
|
|
|
1681 |
if (callback.length) {
|
|
|
1682 |
callback
|
|
|
1683 |
.toString()
|
|
|
1684 |
.replace(commentRegExp, "")
|
|
|
1685 |
.replace(cjsRequireRegExp, function (match, dep) {
|
|
|
1686 |
deps.push(dep);
|
|
|
1687 |
});
|
|
|
1688 |
|
|
|
1689 |
//May be a CommonJS thing even without require calls, but still
|
|
|
1690 |
//could use exports, and module. Avoid doing exports and module
|
|
|
1691 |
//work though if it just needs require.
|
|
|
1692 |
//REQUIRES the function to expect the CommonJS variables in the
|
|
|
1693 |
//order listed below.
|
|
|
1694 |
deps = (callback.length === 1 ? ["require"] : ["require", "exports", "module"]).concat(deps);
|
|
|
1695 |
}
|
|
|
1696 |
}
|
|
|
1697 |
|
|
|
1698 |
//If in IE 6-8 and hit an anonymous define() call, do the interactive
|
|
|
1699 |
//work.
|
|
|
1700 |
if (useInteractive) {
|
|
|
1701 |
node = currentlyAddingScript || getInteractiveScript();
|
|
|
1702 |
if (node) {
|
|
|
1703 |
if (!name) {
|
|
|
1704 |
name = node.getAttribute("data-requiremodule");
|
|
|
1705 |
}
|
|
|
1706 |
context = contexts[node.getAttribute("data-requirecontext")];
|
|
|
1707 |
}
|
|
|
1708 |
}
|
|
|
1709 |
|
|
|
1710 |
//Always save off evaluating the def call until the script onload handler.
|
|
|
1711 |
//This allows multiple modules to be in a file without prematurely
|
|
|
1712 |
//tracing dependencies, and allows for anonymous module support,
|
|
|
1713 |
//where the module name is not known until the script onload event
|
|
|
1714 |
//occurs. If no context, use the global queue, and get it processed
|
|
|
1715 |
//in the onscript load callback.
|
|
|
1716 |
(context ? context.defQueue : globalDefQueue).push([name, deps, callback]);
|
|
|
1717 |
|
|
|
1718 |
return undefined;
|
|
|
1719 |
};
|
|
|
1720 |
|
|
|
1721 |
define.amd = {
|
|
|
1722 |
multiversion: true,
|
|
|
1723 |
plugins: true,
|
|
|
1724 |
jQuery: true
|
|
|
1725 |
};
|
|
|
1726 |
|
|
|
1727 |
/**
|
|
|
1728 |
* Executes the text. Normally just uses eval, but can be modified
|
|
|
1729 |
* to use a more environment specific call.
|
|
|
1730 |
* @param {String} text the text to execute/evaluate.
|
|
|
1731 |
*/
|
|
|
1732 |
req.exec = function (text) {
|
|
|
1733 |
return eval(text);
|
|
|
1734 |
};
|
|
|
1735 |
|
|
|
1736 |
/**
|
|
|
1737 |
* Executes a module callack function. Broken out as a separate function
|
|
|
1738 |
* solely to allow the build system to sequence the files in the built
|
|
|
1739 |
* layer in the right sequence.
|
|
|
1740 |
*
|
|
|
1741 |
* @private
|
|
|
1742 |
*/
|
|
|
1743 |
req.execCb = function (name, callback, args, exports) {
|
|
|
1744 |
return callback.apply(exports, args);
|
|
|
1745 |
};
|
|
|
1746 |
|
|
|
1747 |
|
|
|
1748 |
/**
|
|
|
1749 |
* Adds a node to the DOM. Public function since used by the order plugin.
|
|
|
1750 |
* This method should not normally be called by outside code.
|
|
|
1751 |
*/
|
|
|
1752 |
req.addScriptToDom = function (node) {
|
|
|
1753 |
//For some cache cases in IE 6-8, the script executes before the end
|
|
|
1754 |
//of the appendChild execution, so to tie an anonymous define
|
|
|
1755 |
//call to the module name (which is stored on the node), hold on
|
|
|
1756 |
//to a reference to this node, but clear after the DOM insertion.
|
|
|
1757 |
currentlyAddingScript = node;
|
|
|
1758 |
if (baseElement) {
|
|
|
1759 |
head.insertBefore(node, baseElement);
|
|
|
1760 |
} else {
|
|
|
1761 |
head.appendChild(node);
|
|
|
1762 |
}
|
|
|
1763 |
currentlyAddingScript = null;
|
|
|
1764 |
};
|
|
|
1765 |
|
|
|
1766 |
/**
|
|
|
1767 |
* callback for script loads, used to check status of loading.
|
|
|
1768 |
*
|
|
|
1769 |
* @param {Event} evt the event from the browser for the script
|
|
|
1770 |
* that was loaded.
|
|
|
1771 |
*
|
|
|
1772 |
* @private
|
|
|
1773 |
*/
|
|
|
1774 |
req.onScriptLoad = function (evt) {
|
|
|
1775 |
//Using currentTarget instead of target for Firefox 2.0's sake. Not
|
|
|
1776 |
//all old browsers will be supported, but this one was easy enough
|
|
|
1777 |
//to support and still makes sense.
|
|
|
1778 |
var node = evt.currentTarget || evt.srcElement, contextName, moduleName,
|
|
|
1779 |
context;
|
|
|
1780 |
|
|
|
1781 |
if (evt.type === "load" || (node && readyRegExp.test(node.readyState))) {
|
|
|
1782 |
//Reset interactive script so a script node is not held onto for
|
|
|
1783 |
//to long.
|
|
|
1784 |
interactiveScript = null;
|
|
|
1785 |
|
|
|
1786 |
//Pull out the name of the module and the context.
|
|
|
1787 |
contextName = node.getAttribute("data-requirecontext");
|
|
|
1788 |
moduleName = node.getAttribute("data-requiremodule");
|
|
|
1789 |
context = contexts[contextName];
|
|
|
1790 |
|
|
|
1791 |
contexts[contextName].completeLoad(moduleName);
|
|
|
1792 |
|
|
|
1793 |
//Clean up script binding. Favor detachEvent because of IE9
|
|
|
1794 |
//issue, see attachEvent/addEventListener comment elsewhere
|
|
|
1795 |
//in this file.
|
|
|
1796 |
if (node.detachEvent && !isOpera) {
|
|
|
1797 |
//Probably IE. If not it will throw an error, which will be
|
|
|
1798 |
//useful to know.
|
|
|
1799 |
node.detachEvent("onreadystatechange", req.onScriptLoad);
|
|
|
1800 |
} else {
|
|
|
1801 |
node.removeEventListener("load", req.onScriptLoad, false);
|
|
|
1802 |
}
|
|
|
1803 |
}
|
|
|
1804 |
};
|
|
|
1805 |
|
|
|
1806 |
/**
|
|
|
1807 |
* Attaches the script represented by the URL to the current
|
|
|
1808 |
* environment. Right now only supports browser loading,
|
|
|
1809 |
* but can be redefined in other environments to do the right thing.
|
|
|
1810 |
* @param {String} url the url of the script to attach.
|
|
|
1811 |
* @param {Object} context the context that wants the script.
|
|
|
1812 |
* @param {moduleName} the name of the module that is associated with the script.
|
|
|
1813 |
* @param {Function} [callback] optional callback, defaults to require.onScriptLoad
|
|
|
1814 |
* @param {String} [type] optional type, defaults to text/javascript
|
|
|
1815 |
* @param {Function} [fetchOnlyFunction] optional function to indicate the script node
|
|
|
1816 |
* should be set up to fetch the script but do not attach it to the DOM
|
|
|
1817 |
* so that it can later be attached to execute it. This is a way for the
|
|
|
1818 |
* order plugin to support ordered loading in IE. Once the script is fetched,
|
|
|
1819 |
* but not executed, the fetchOnlyFunction will be called.
|
|
|
1820 |
*/
|
|
|
1821 |
req.attach = function (url, context, moduleName, callback, type, fetchOnlyFunction) {
|
|
|
1822 |
var node;
|
|
|
1823 |
if (isBrowser) {
|
|
|
1824 |
//In the browser so use a script tag
|
|
|
1825 |
callback = callback || req.onScriptLoad;
|
|
|
1826 |
node = context && context.config && context.config.xhtml ?
|
|
|
1827 |
document.createElementNS("http://www.w3.org/1999/xhtml", "html:script") :
|
|
|
1828 |
document.createElement("script");
|
|
|
1829 |
node.type = type || "text/javascript";
|
|
|
1830 |
node.charset = "utf-8";
|
|
|
1831 |
//Use async so Gecko does not block on executing the script if something
|
|
|
1832 |
//like a long-polling comet tag is being run first. Gecko likes
|
|
|
1833 |
//to evaluate scripts in DOM order, even for dynamic scripts.
|
|
|
1834 |
//It will fetch them async, but only evaluate the contents in DOM
|
|
|
1835 |
//order, so a long-polling script tag can delay execution of scripts
|
|
|
1836 |
//after it. But telling Gecko we expect async gets us the behavior
|
|
|
1837 |
//we want -- execute it whenever it is finished downloading. Only
|
|
|
1838 |
//Helps Firefox 3.6+
|
|
|
1839 |
//Allow some URLs to not be fetched async. Mostly helps the order!
|
|
|
1840 |
//plugin
|
|
|
1841 |
node.async = !s.skipAsync[url];
|
|
|
1842 |
|
|
|
1843 |
if (context) {
|
|
|
1844 |
node.setAttribute("data-requirecontext", context.contextName);
|
|
|
1845 |
}
|
|
|
1846 |
node.setAttribute("data-requiremodule", moduleName);
|
|
|
1847 |
|
|
|
1848 |
//Set up load listener. Test attachEvent first because IE9 has
|
|
|
1849 |
//a subtle issue in its addEventListener and script onload firings
|
|
|
1850 |
//that do not match the behavior of all other browsers with
|
|
|
1851 |
//addEventListener support, which fire the onload event for a
|
|
|
1852 |
//script right after the script execution. See:
|
|
|
1853 |
//https://connect.microsoft.com/IE/feedback/details/648057/script-onload-event-is-not-fired-immediately-after-script-execution
|
|
|
1854 |
//UNFORTUNATELY Opera implements attachEvent but does not follow the script
|
|
|
1855 |
//script execution mode.
|
|
|
1856 |
if (node.attachEvent && !isOpera) {
|
|
|
1857 |
//Probably IE. IE (at least 6-8) do not fire
|
|
|
1858 |
//script onload right after executing the script, so
|
|
|
1859 |
//we cannot tie the anonymous define call to a name.
|
|
|
1860 |
//However, IE reports the script as being in "interactive"
|
|
|
1861 |
//readyState at the time of the define call.
|
|
|
1862 |
useInteractive = true;
|
|
|
1863 |
|
|
|
1864 |
|
|
|
1865 |
if (fetchOnlyFunction) {
|
|
|
1866 |
//Need to use old school onreadystate here since
|
|
|
1867 |
//when the event fires and the node is not attached
|
|
|
1868 |
//to the DOM, the evt.srcElement is null, so use
|
|
|
1869 |
//a closure to remember the node.
|
|
|
1870 |
node.onreadystatechange = function (evt) {
|
|
|
1871 |
//Script loaded but not executed.
|
|
|
1872 |
//Clear loaded handler, set the real one that
|
|
|
1873 |
//waits for script execution.
|
|
|
1874 |
if (node.readyState === 'loaded') {
|
|
|
1875 |
node.onreadystatechange = null;
|
|
|
1876 |
node.attachEvent("onreadystatechange", callback);
|
|
|
1877 |
fetchOnlyFunction(node);
|
|
|
1878 |
}
|
|
|
1879 |
};
|
|
|
1880 |
} else {
|
|
|
1881 |
node.attachEvent("onreadystatechange", callback);
|
|
|
1882 |
}
|
|
|
1883 |
} else {
|
|
|
1884 |
node.addEventListener("load", callback, false);
|
|
|
1885 |
}
|
|
|
1886 |
node.src = url;
|
|
|
1887 |
|
|
|
1888 |
//Fetch only means waiting to attach to DOM after loaded.
|
|
|
1889 |
if (!fetchOnlyFunction) {
|
|
|
1890 |
req.addScriptToDom(node);
|
|
|
1891 |
}
|
|
|
1892 |
|
|
|
1893 |
return node;
|
|
|
1894 |
} else if (isWebWorker) {
|
|
|
1895 |
//In a web worker, use importScripts. This is not a very
|
|
|
1896 |
//efficient use of importScripts, importScripts will block until
|
|
|
1897 |
//its script is downloaded and evaluated. However, if web workers
|
|
|
1898 |
//are in play, the expectation that a build has been done so that
|
|
|
1899 |
//only one script needs to be loaded anyway. This may need to be
|
|
|
1900 |
//reevaluated if other use cases become common.
|
|
|
1901 |
importScripts(url);
|
|
|
1902 |
|
|
|
1903 |
//Account for anonymous modules
|
|
|
1904 |
context.completeLoad(moduleName);
|
|
|
1905 |
}
|
|
|
1906 |
return null;
|
|
|
1907 |
};
|
|
|
1908 |
|
|
|
1909 |
//Look for a data-main script attribute, which could also adjust the baseUrl.
|
|
|
1910 |
if (isBrowser) {
|
|
|
1911 |
//Figure out baseUrl. Get it from the script tag with require.js in it.
|
|
|
1912 |
scripts = document.getElementsByTagName("script");
|
|
|
1913 |
|
|
|
1914 |
for (i = scripts.length - 1; i > -1 && (script = scripts[i]); i--) {
|
|
|
1915 |
//Set the "head" where we can append children by
|
|
|
1916 |
//using the script's parent.
|
|
|
1917 |
if (!head) {
|
|
|
1918 |
head = script.parentNode;
|
|
|
1919 |
}
|
|
|
1920 |
|
|
|
1921 |
//Look for a data-main attribute to set main script for the page
|
|
|
1922 |
//to load. If it is there, the path to data main becomes the
|
|
|
1923 |
//baseUrl, if it is not already set.
|
|
|
1924 |
if ((dataMain = script.getAttribute('data-main'))) {
|
|
|
1925 |
if (!cfg.baseUrl) {
|
|
|
1926 |
//Pull off the directory of data-main for use as the
|
|
|
1927 |
//baseUrl.
|
|
|
1928 |
src = dataMain.split('/');
|
|
|
1929 |
mainScript = src.pop();
|
|
|
1930 |
subPath = src.length ? src.join('/') + '/' : './';
|
|
|
1931 |
|
|
|
1932 |
//Set final config.
|
|
|
1933 |
cfg.baseUrl = subPath;
|
|
|
1934 |
//Strip off any trailing .js since dataMain is now
|
|
|
1935 |
//like a module name.
|
|
|
1936 |
dataMain = mainScript.replace(jsSuffixRegExp, '');
|
|
|
1937 |
}
|
|
|
1938 |
|
|
|
1939 |
//Put the data-main script in the files to load.
|
|
|
1940 |
cfg.deps = cfg.deps ? cfg.deps.concat(dataMain) : [dataMain];
|
|
|
1941 |
|
|
|
1942 |
break;
|
|
|
1943 |
}
|
|
|
1944 |
}
|
|
|
1945 |
}
|
|
|
1946 |
|
|
|
1947 |
//See if there is nothing waiting across contexts, and if not, trigger
|
|
|
1948 |
//resourcesReady.
|
|
|
1949 |
req.checkReadyState = function () {
|
|
|
1950 |
var contexts = s.contexts, prop;
|
|
|
1951 |
for (prop in contexts) {
|
|
|
1952 |
if (!(prop in empty)) {
|
|
|
1953 |
if (contexts[prop].waitCount) {
|
|
|
1954 |
return;
|
|
|
1955 |
}
|
|
|
1956 |
}
|
|
|
1957 |
}
|
|
|
1958 |
req.resourcesReady(true);
|
|
|
1959 |
};
|
|
|
1960 |
|
|
|
1961 |
/**
|
|
|
1962 |
* Internal function that is triggered whenever all scripts/resources
|
|
|
1963 |
* have been loaded by the loader. Can be overridden by other, for
|
|
|
1964 |
* instance the domReady plugin, which wants to know when all resources
|
|
|
1965 |
* are loaded.
|
|
|
1966 |
*/
|
|
|
1967 |
req.resourcesReady = function (isReady) {
|
|
|
1968 |
var contexts, context, prop;
|
|
|
1969 |
|
|
|
1970 |
//First, set the public variable indicating that resources are loading.
|
|
|
1971 |
req.resourcesDone = isReady;
|
|
|
1972 |
|
|
|
1973 |
if (req.resourcesDone) {
|
|
|
1974 |
//If jQuery with DOM ready delayed, release it now.
|
|
|
1975 |
contexts = s.contexts;
|
|
|
1976 |
for (prop in contexts) {
|
|
|
1977 |
if (!(prop in empty)) {
|
|
|
1978 |
context = contexts[prop];
|
|
|
1979 |
if (context.jQueryIncremented) {
|
|
|
1980 |
jQueryHoldReady(context.jQuery, false);
|
|
|
1981 |
context.jQueryIncremented = false;
|
|
|
1982 |
}
|
|
|
1983 |
}
|
|
|
1984 |
}
|
|
|
1985 |
}
|
|
|
1986 |
};
|
|
|
1987 |
|
|
|
1988 |
//FF < 3.6 readyState fix. Needed so that domReady plugin
|
|
|
1989 |
//works well in that environment, since require.js is normally
|
|
|
1990 |
//loaded via an HTML script tag so it will be there before window load,
|
|
|
1991 |
//where the domReady plugin is more likely to be loaded after window load.
|
|
|
1992 |
req.pageLoaded = function () {
|
|
|
1993 |
if (document.readyState !== "complete") {
|
|
|
1994 |
document.readyState = "complete";
|
|
|
1995 |
}
|
|
|
1996 |
};
|
|
|
1997 |
if (isBrowser) {
|
|
|
1998 |
if (document.addEventListener) {
|
|
|
1999 |
if (!document.readyState) {
|
|
|
2000 |
document.readyState = "loading";
|
|
|
2001 |
window.addEventListener("load", req.pageLoaded, false);
|
|
|
2002 |
}
|
|
|
2003 |
}
|
|
|
2004 |
}
|
|
|
2005 |
|
|
|
2006 |
//Set up default context. If require was a configuration object, use that as base config.
|
|
|
2007 |
req(cfg);
|
|
|
2008 |
|
|
|
2009 |
//If modules are built into require.js, then need to make sure dependencies are
|
|
|
2010 |
//traced. Use a setTimeout in the browser world, to allow all the modules to register
|
|
|
2011 |
//themselves. In a non-browser env, assume that modules are not built into require.js,
|
|
|
2012 |
//which seems odd to do on the server.
|
|
|
2013 |
if (req.isAsync && typeof setTimeout !== "undefined") {
|
|
|
2014 |
ctx = s.contexts[(cfg.context || defContextName)];
|
|
|
2015 |
//Indicate that the script that includes require() is still loading,
|
|
|
2016 |
//so that require()'d dependencies are not traced until the end of the
|
|
|
2017 |
//file is parsed (approximated via the setTimeout call).
|
|
|
2018 |
ctx.requireWait = true;
|
|
|
2019 |
setTimeout(function () {
|
|
|
2020 |
ctx.requireWait = false;
|
|
|
2021 |
|
|
|
2022 |
//Any modules included with the require.js file will be in the
|
|
|
2023 |
//global queue, assign them to this context.
|
|
|
2024 |
ctx.takeGlobalQueue();
|
|
|
2025 |
|
|
|
2026 |
//Allow for jQuery to be loaded/already in the page, and if jQuery 1.4.3,
|
|
|
2027 |
//make sure to hold onto it for readyWait triggering.
|
|
|
2028 |
ctx.jQueryCheck();
|
|
|
2029 |
|
|
|
2030 |
if (!ctx.scriptCount) {
|
|
|
2031 |
ctx.resume();
|
|
|
2032 |
}
|
|
|
2033 |
req.checkReadyState();
|
|
|
2034 |
}, 0);
|
|
|
2035 |
}
|
|
|
2036 |
}());
|
|
|
2037 |
|
|
|
2038 |
|
|
|
2039 |
if (env === 'rhino') {
|
|
|
2040 |
/**
|
|
|
2041 |
* @license RequireJS rhino Copyright (c) 2010-2011, The Dojo Foundation All Rights Reserved.
|
|
|
2042 |
* Available via the MIT or new BSD license.
|
|
|
2043 |
* see: http://github.com/jrburke/requirejs for details
|
|
|
2044 |
*/
|
|
|
2045 |
|
|
|
2046 |
/*jslint strict: false */
|
|
|
2047 |
/*global require: false, java: false, load: false */
|
|
|
2048 |
|
|
|
2049 |
(function () {
|
|
|
2050 |
|
|
|
2051 |
require.load = function (context, moduleName, url) {
|
|
|
2052 |
//Indicate a the module is in process of loading.
|
|
|
2053 |
context.scriptCount += 1;
|
|
|
2054 |
|
|
|
2055 |
load(url);
|
|
|
2056 |
|
|
|
2057 |
//Support anonymous modules.
|
|
|
2058 |
context.completeLoad(moduleName);
|
|
|
2059 |
};
|
|
|
2060 |
|
|
|
2061 |
}());
|
|
|
2062 |
} else if (env === 'node') {
|
|
|
2063 |
this.requirejsVars = {
|
|
|
2064 |
require: require,
|
|
|
2065 |
requirejs: require,
|
|
|
2066 |
define: define,
|
|
|
2067 |
nodeRequire: nodeRequire
|
|
|
2068 |
};
|
|
|
2069 |
require.nodeRequire = nodeRequire;
|
|
|
2070 |
|
|
|
2071 |
/**
|
|
|
2072 |
* @license RequireJS node Copyright (c) 2010-2011, The Dojo Foundation All Rights Reserved.
|
|
|
2073 |
* Available via the MIT or new BSD license.
|
|
|
2074 |
* see: http://github.com/jrburke/requirejs for details
|
|
|
2075 |
*/
|
|
|
2076 |
|
|
|
2077 |
/*jslint regexp: false, strict: false */
|
|
|
2078 |
/*global require: false, define: false, requirejsVars: false, process: false */
|
|
|
2079 |
|
|
|
2080 |
/**
|
|
|
2081 |
* This adapter assumes that x.js has loaded it and set up
|
|
|
2082 |
* some variables. This adapter just allows limited RequireJS
|
|
|
2083 |
* usage from within the requirejs directory. The general
|
|
|
2084 |
* node adapater is r.js.
|
|
|
2085 |
*/
|
|
|
2086 |
|
|
|
2087 |
(function () {
|
|
|
2088 |
var nodeReq = requirejsVars.nodeRequire,
|
|
|
2089 |
req = requirejsVars.require,
|
|
|
2090 |
def = requirejsVars.define,
|
|
|
2091 |
fs = nodeReq('fs'),
|
|
|
2092 |
path = nodeReq('path'),
|
|
|
2093 |
vm = nodeReq('vm');
|
|
|
2094 |
|
|
|
2095 |
//Supply an implementation that allows synchronous get of a module.
|
|
|
2096 |
req.get = function (context, moduleName, relModuleMap) {
|
|
|
2097 |
if (moduleName === "require" || moduleName === "exports" || moduleName === "module") {
|
|
|
2098 |
req.onError(new Error("Explicit require of " + moduleName + " is not allowed."));
|
|
|
2099 |
}
|
|
|
2100 |
|
|
|
2101 |
var ret,
|
|
|
2102 |
moduleMap = context.makeModuleMap(moduleName, relModuleMap);
|
|
|
2103 |
|
|
|
2104 |
//Normalize module name, if it contains . or ..
|
|
|
2105 |
moduleName = moduleMap.fullName;
|
|
|
2106 |
|
|
|
2107 |
if (moduleName in context.defined) {
|
|
|
2108 |
ret = context.defined[moduleName];
|
|
|
2109 |
} else {
|
|
|
2110 |
if (ret === undefined) {
|
|
|
2111 |
//Try to dynamically fetch it.
|
|
|
2112 |
req.load(context, moduleName, moduleMap.url);
|
|
|
2113 |
//The above call is sync, so can do the next thing safely.
|
|
|
2114 |
ret = context.defined[moduleName];
|
|
|
2115 |
}
|
|
|
2116 |
}
|
|
|
2117 |
|
|
|
2118 |
return ret;
|
|
|
2119 |
};
|
|
|
2120 |
|
|
|
2121 |
//Add wrapper around the code so that it gets the requirejs
|
|
|
2122 |
//API instead of the Node API, and it is done lexically so
|
|
|
2123 |
//that it survives later execution.
|
|
|
2124 |
req.makeNodeWrapper = function (contents) {
|
|
|
2125 |
return '(function (require, requirejs, define) { ' +
|
|
|
2126 |
contents +
|
|
|
2127 |
'\n}(requirejsVars.require, requirejsVars.requirejs, requirejsVars.define));';
|
|
|
2128 |
};
|
|
|
2129 |
|
|
|
2130 |
req.load = function (context, moduleName, url) {
|
|
|
2131 |
var contents, err;
|
|
|
2132 |
|
|
|
2133 |
//Indicate a the module is in process of loading.
|
|
|
2134 |
context.scriptCount += 1;
|
|
|
2135 |
|
|
|
2136 |
if (path.existsSync(url)) {
|
|
|
2137 |
contents = fs.readFileSync(url, 'utf8');
|
|
|
2138 |
|
|
|
2139 |
contents = req.makeNodeWrapper(contents);
|
|
|
2140 |
try {
|
|
|
2141 |
vm.runInThisContext(contents, fs.realpathSync(url));
|
|
|
2142 |
} catch (e) {
|
|
|
2143 |
err = new Error('Evaluating ' + url + ' as module "' +
|
|
|
2144 |
moduleName + '" failed with error: ' + e);
|
|
|
2145 |
err.originalError = e;
|
|
|
2146 |
err.moduleName = moduleName;
|
|
|
2147 |
err.fileName = url;
|
|
|
2148 |
return req.onError(err);
|
|
|
2149 |
}
|
|
|
2150 |
} else {
|
|
|
2151 |
def(moduleName, function () {
|
|
|
2152 |
try {
|
|
|
2153 |
return (context.config.nodeRequire || req.nodeRequire)(moduleName);
|
|
|
2154 |
} catch (e) {
|
|
|
2155 |
err = new Error('Calling node\'s require("' +
|
|
|
2156 |
moduleName + '") failed with error: ' + e);
|
|
|
2157 |
err.originalError = e;
|
|
|
2158 |
err.moduleName = moduleName;
|
|
|
2159 |
return req.onError(err);
|
|
|
2160 |
}
|
|
|
2161 |
});
|
|
|
2162 |
}
|
|
|
2163 |
|
|
|
2164 |
//Support anonymous modules.
|
|
|
2165 |
context.completeLoad(moduleName);
|
|
|
2166 |
|
|
|
2167 |
return undefined;
|
|
|
2168 |
};
|
|
|
2169 |
|
|
|
2170 |
//Override to provide the function wrapper for define/require.
|
|
|
2171 |
req.exec = function (text) {
|
|
|
2172 |
/*jslint evil: true */
|
|
|
2173 |
text = req.makeNodeWrapper(text);
|
|
|
2174 |
return eval(text);
|
|
|
2175 |
};
|
|
|
2176 |
}());
|
|
|
2177 |
|
|
|
2178 |
}
|
|
|
2179 |
|
|
|
2180 |
//Support a default file name to execute. Useful for hosted envs
|
|
|
2181 |
//like Joyent where it defaults to a server.js as the only executed
|
|
|
2182 |
//script. But only do it if this is not an optimization run.
|
|
|
2183 |
if (commandOption !== 'o' && (!fileName || !jsSuffixRegExp.test(fileName))) {
|
|
|
2184 |
fileName = 'main.js';
|
|
|
2185 |
}
|
|
|
2186 |
|
|
|
2187 |
/**
|
|
|
2188 |
* Loads the library files that can be used for the optimizer, or for other
|
|
|
2189 |
* tasks.
|
|
|
2190 |
*/
|
|
|
2191 |
function loadLib() {
|
|
|
2192 |
/**
|
|
|
2193 |
* @license Copyright (c) 2010-2011, The Dojo Foundation All Rights Reserved.
|
|
|
2194 |
* Available via the MIT or new BSD license.
|
|
|
2195 |
* see: http://github.com/jrburke/requirejs for details
|
|
|
2196 |
*/
|
|
|
2197 |
|
|
|
2198 |
/*jslint strict: false */
|
|
|
2199 |
/*global Packages: false, process: false, window: false, navigator: false,
|
|
|
2200 |
document: false, define: false */
|
|
|
2201 |
|
|
|
2202 |
/**
|
|
|
2203 |
* A plugin that modifies any /env/ path to be the right path based on
|
|
|
2204 |
* the host environment. Right now only works for Node, Rhino and browser.
|
|
|
2205 |
*/
|
|
|
2206 |
(function () {
|
|
|
2207 |
var pathRegExp = /(\/|^)env\/|\{env\}/,
|
|
|
2208 |
env = 'unknown';
|
|
|
2209 |
|
|
|
2210 |
if (typeof Packages !== 'undefined') {
|
|
|
2211 |
env = 'rhino';
|
|
|
2212 |
} else if (typeof process !== 'undefined') {
|
|
|
2213 |
env = 'node';
|
|
|
2214 |
} else if (typeof window !== "undefined" && navigator && document) {
|
|
|
2215 |
env = 'browser';
|
|
|
2216 |
}
|
|
|
2217 |
|
|
|
2218 |
define('env', {
|
|
|
2219 |
load: function (name, req, load, config) {
|
|
|
2220 |
//Allow override in the config.
|
|
|
2221 |
if (config.env) {
|
|
|
2222 |
env = config.env;
|
|
|
2223 |
}
|
|
|
2224 |
|
|
|
2225 |
name = name.replace(pathRegExp, function (match, prefix) {
|
|
|
2226 |
if (match.indexOf('{') === -1) {
|
|
|
2227 |
return prefix + env + '/';
|
|
|
2228 |
} else {
|
|
|
2229 |
return env;
|
|
|
2230 |
}
|
|
|
2231 |
});
|
|
|
2232 |
|
|
|
2233 |
req([name], function (mod) {
|
|
|
2234 |
load(mod);
|
|
|
2235 |
});
|
|
|
2236 |
}
|
|
|
2237 |
});
|
|
|
2238 |
}());
|
|
|
2239 |
if(env === 'node') {
|
|
|
2240 |
/**
|
|
|
2241 |
* @license Copyright (c) 2010-2011, The Dojo Foundation All Rights Reserved.
|
|
|
2242 |
* Available via the MIT or new BSD license.
|
|
|
2243 |
* see: http://github.com/jrburke/requirejs for details
|
|
|
2244 |
*/
|
|
|
2245 |
|
|
|
2246 |
/*jslint strict: false */
|
|
|
2247 |
/*global define: false, process: false */
|
|
|
2248 |
|
|
|
2249 |
define('node/args', function () {
|
|
|
2250 |
//Do not return the "node" or "r.js" arguments
|
|
|
2251 |
var args = process.argv.slice(2);
|
|
|
2252 |
|
|
|
2253 |
//Ignore any command option used for rq.js
|
|
|
2254 |
if (args[0] && args[0].indexOf('-' === 0)) {
|
|
|
2255 |
args = args.slice(1);
|
|
|
2256 |
}
|
|
|
2257 |
|
|
|
2258 |
return args;
|
|
|
2259 |
});
|
|
|
2260 |
|
|
|
2261 |
}
|
|
|
2262 |
|
|
|
2263 |
if(env === 'rhino') {
|
|
|
2264 |
/**
|
|
|
2265 |
* @license Copyright (c) 2010-2011, The Dojo Foundation All Rights Reserved.
|
|
|
2266 |
* Available via the MIT or new BSD license.
|
|
|
2267 |
* see: http://github.com/jrburke/requirejs for details
|
|
|
2268 |
*/
|
|
|
2269 |
|
|
|
2270 |
/*jslint strict: false */
|
|
|
2271 |
/*global define: false, process: false */
|
|
|
2272 |
|
|
|
2273 |
var jsLibRhinoArgs = (typeof rhinoArgs !== 'undefined' && rhinoArgs) || [].concat(Array.prototype.slice.call(arguments, 0));
|
|
|
2274 |
|
|
|
2275 |
define('rhino/args', function () {
|
|
|
2276 |
var args = jsLibRhinoArgs;
|
|
|
2277 |
|
|
|
2278 |
//Ignore any command option used for rq.js
|
|
|
2279 |
if (args[0] && args[0].indexOf('-' === 0)) {
|
|
|
2280 |
args = args.slice(1);
|
|
|
2281 |
}
|
|
|
2282 |
|
|
|
2283 |
return args;
|
|
|
2284 |
});
|
|
|
2285 |
|
|
|
2286 |
}
|
|
|
2287 |
|
|
|
2288 |
if(env === 'node') {
|
|
|
2289 |
/**
|
|
|
2290 |
* @license RequireJS Copyright (c) 2010-2011, The Dojo Foundation All Rights Reserved.
|
|
|
2291 |
* Available via the MIT or new BSD license.
|
|
|
2292 |
* see: http://github.com/jrburke/requirejs for details
|
|
|
2293 |
*/
|
|
|
2294 |
|
|
|
2295 |
/*jslint strict: false */
|
|
|
2296 |
/*global define: false, console: false */
|
|
|
2297 |
|
|
|
2298 |
define('node/load', ['fs'], function (fs) {
|
|
|
2299 |
function load(fileName) {
|
|
|
2300 |
var contents = fs.readFileSync(fileName, 'utf8');
|
|
|
2301 |
process.compile(contents, fileName);
|
|
|
2302 |
}
|
|
|
2303 |
|
|
|
2304 |
return load;
|
|
|
2305 |
});
|
|
|
2306 |
|
|
|
2307 |
}
|
|
|
2308 |
|
|
|
2309 |
if(env === 'rhino') {
|
|
|
2310 |
/**
|
|
|
2311 |
* @license RequireJS Copyright (c) 2010-2011, The Dojo Foundation All Rights Reserved.
|
|
|
2312 |
* Available via the MIT or new BSD license.
|
|
|
2313 |
* see: http://github.com/jrburke/requirejs for details
|
|
|
2314 |
*/
|
|
|
2315 |
|
|
|
2316 |
/*jslint strict: false */
|
|
|
2317 |
/*global define: false, load: false */
|
|
|
2318 |
|
|
|
2319 |
define('rhino/load', function () {
|
|
|
2320 |
return load;
|
|
|
2321 |
});
|
|
|
2322 |
|
|
|
2323 |
}
|
|
|
2324 |
|
|
|
2325 |
if(env === 'node') {
|
|
|
2326 |
/**
|
|
|
2327 |
* @license Copyright (c) 2010-2011, The Dojo Foundation All Rights Reserved.
|
|
|
2328 |
* Available via the MIT or new BSD license.
|
|
|
2329 |
* see: http://github.com/jrburke/requirejs for details
|
|
|
2330 |
*/
|
|
|
2331 |
|
|
|
2332 |
/*jslint plusplus: false, octal:false, strict: false */
|
|
|
2333 |
/*global define: false, process: false */
|
|
|
2334 |
|
|
|
2335 |
define('node/file', ['fs', 'path'], function (fs, path) {
|
|
|
2336 |
|
|
|
2337 |
var isWindows = process.platform === 'win32',
|
|
|
2338 |
file;
|
|
|
2339 |
|
|
|
2340 |
function frontSlash(path) {
|
|
|
2341 |
return path.replace(/\\/g, '/');
|
|
|
2342 |
}
|
|
|
2343 |
|
|
|
2344 |
function exists(path) {
|
|
|
2345 |
if (isWindows && path.charAt(path.length - 1) === '/' &&
|
|
|
2346 |
path.charAt(path.length - 2) !== ':') {
|
|
|
2347 |
path = path.substring(0, path.length - 1);
|
|
|
2348 |
}
|
|
|
2349 |
|
|
|
2350 |
try {
|
|
|
2351 |
fs.statSync(path);
|
|
|
2352 |
return true;
|
|
|
2353 |
} catch (e) {
|
|
|
2354 |
return false;
|
|
|
2355 |
}
|
|
|
2356 |
}
|
|
|
2357 |
|
|
|
2358 |
function mkDir(dir) {
|
|
|
2359 |
if (!exists(dir)) {
|
|
|
2360 |
fs.mkdirSync(dir, 0777);
|
|
|
2361 |
}
|
|
|
2362 |
}
|
|
|
2363 |
|
|
|
2364 |
function mkFullDir(dir) {
|
|
|
2365 |
var parts = dir.split('/'),
|
|
|
2366 |
currDir = '',
|
|
|
2367 |
first = true;
|
|
|
2368 |
|
|
|
2369 |
parts.forEach(function (part) {
|
|
|
2370 |
//First part may be empty string if path starts with a slash.
|
|
|
2371 |
currDir += part + '/';
|
|
|
2372 |
first = false;
|
|
|
2373 |
|
|
|
2374 |
if (part) {
|
|
|
2375 |
mkDir(currDir);
|
|
|
2376 |
}
|
|
|
2377 |
});
|
|
|
2378 |
}
|
|
|
2379 |
|
|
|
2380 |
file = {
|
|
|
2381 |
backSlashRegExp: /\\/g,
|
|
|
2382 |
dirExclusionRegExp: /^\./,
|
|
|
2383 |
getLineSeparator: function () {
|
|
|
2384 |
return '/';
|
|
|
2385 |
},
|
|
|
2386 |
|
|
|
2387 |
exists: function (fileName) {
|
|
|
2388 |
return exists(fileName);
|
|
|
2389 |
},
|
|
|
2390 |
|
|
|
2391 |
parent: function (fileName) {
|
|
|
2392 |
var parts = fileName.split('/');
|
|
|
2393 |
parts.pop();
|
|
|
2394 |
return parts.join('/');
|
|
|
2395 |
},
|
|
|
2396 |
|
|
|
2397 |
/**
|
|
|
2398 |
* Gets the absolute file path as a string, normalized
|
|
|
2399 |
* to using front slashes for path separators.
|
|
|
2400 |
* @param {String} fileName
|
|
|
2401 |
*/
|
|
|
2402 |
absPath: function (fileName) {
|
|
|
2403 |
return frontSlash(path.normalize(frontSlash(fs.realpathSync(fileName))));
|
|
|
2404 |
},
|
|
|
2405 |
|
|
|
2406 |
normalize: function (fileName) {
|
|
|
2407 |
return frontSlash(path.normalize(fileName));
|
|
|
2408 |
},
|
|
|
2409 |
|
|
|
2410 |
isFile: function (path) {
|
|
|
2411 |
return fs.statSync(path).isFile();
|
|
|
2412 |
},
|
|
|
2413 |
|
|
|
2414 |
isDirectory: function (path) {
|
|
|
2415 |
return fs.statSync(path).isDirectory();
|
|
|
2416 |
},
|
|
|
2417 |
|
|
|
2418 |
getFilteredFileList: function (/*String*/startDir, /*RegExp*/regExpFilters, /*boolean?*/makeUnixPaths) {
|
|
|
2419 |
//summary: Recurses startDir and finds matches to the files that match regExpFilters.include
|
|
|
2420 |
//and do not match regExpFilters.exclude. Or just one regexp can be passed in for regExpFilters,
|
|
|
2421 |
//and it will be treated as the "include" case.
|
|
|
2422 |
//Ignores files/directories that start with a period (.).
|
|
|
2423 |
var files = [], topDir, regExpInclude, regExpExclude, dirFileArray,
|
|
|
2424 |
i, stat, filePath, ok, dirFiles, fileName;
|
|
|
2425 |
|
|
|
2426 |
topDir = startDir;
|
|
|
2427 |
|
|
|
2428 |
regExpInclude = regExpFilters.include || regExpFilters;
|
|
|
2429 |
regExpExclude = regExpFilters.exclude || null;
|
|
|
2430 |
|
|
|
2431 |
if (file.exists(topDir)) {
|
|
|
2432 |
dirFileArray = fs.readdirSync(topDir);
|
|
|
2433 |
for (i = 0; i < dirFileArray.length; i++) {
|
|
|
2434 |
fileName = dirFileArray[i];
|
|
|
2435 |
filePath = path.join(topDir, fileName);
|
|
|
2436 |
stat = fs.statSync(filePath);
|
|
|
2437 |
if (stat.isFile()) {
|
|
|
2438 |
if (makeUnixPaths) {
|
|
|
2439 |
//Make sure we have a JS string.
|
|
|
2440 |
if (filePath.indexOf("/") === -1) {
|
|
|
2441 |
filePath = frontSlash(filePath);
|
|
|
2442 |
}
|
|
|
2443 |
}
|
|
|
2444 |
|
|
|
2445 |
ok = true;
|
|
|
2446 |
if (regExpInclude) {
|
|
|
2447 |
ok = filePath.match(regExpInclude);
|
|
|
2448 |
}
|
|
|
2449 |
if (ok && regExpExclude) {
|
|
|
2450 |
ok = !filePath.match(regExpExclude);
|
|
|
2451 |
}
|
|
|
2452 |
|
|
|
2453 |
if (ok && !fileName.match(/^\./)) {
|
|
|
2454 |
files.push(filePath);
|
|
|
2455 |
}
|
|
|
2456 |
} else if (stat.isDirectory() &&
|
|
|
2457 |
(!file.dirExclusionRegExp || !fileName.match(file.dirExclusionRegExp))) {
|
|
|
2458 |
dirFiles = this.getFilteredFileList(filePath, regExpFilters, makeUnixPaths);
|
|
|
2459 |
files.push.apply(files, dirFiles);
|
|
|
2460 |
}
|
|
|
2461 |
}
|
|
|
2462 |
}
|
|
|
2463 |
|
|
|
2464 |
return files; //Array
|
|
|
2465 |
},
|
|
|
2466 |
|
|
|
2467 |
copyDir: function (/*String*/srcDir, /*String*/destDir, /*RegExp?*/regExpFilter, /*boolean?*/onlyCopyNew) {
|
|
|
2468 |
//summary: copies files from srcDir to destDir using the regExpFilter to determine if the
|
|
|
2469 |
//file should be copied. Returns a list file name strings of the destinations that were copied.
|
|
|
2470 |
regExpFilter = regExpFilter || /\w/;
|
|
|
2471 |
|
|
|
2472 |
//Normalize th directory names, but keep front slashes.
|
|
|
2473 |
//path module on windows now returns backslashed paths.
|
|
|
2474 |
srcDir = frontSlash(path.normalize(srcDir));
|
|
|
2475 |
destDir = frontSlash(path.normalize(destDir));
|
|
|
2476 |
|
|
|
2477 |
var fileNames = file.getFilteredFileList(srcDir, regExpFilter, true),
|
|
|
2478 |
copiedFiles = [], i, srcFileName, destFileName;
|
|
|
2479 |
|
|
|
2480 |
for (i = 0; i < fileNames.length; i++) {
|
|
|
2481 |
srcFileName = fileNames[i];
|
|
|
2482 |
destFileName = srcFileName.replace(srcDir, destDir);
|
|
|
2483 |
|
|
|
2484 |
if (file.copyFile(srcFileName, destFileName, onlyCopyNew)) {
|
|
|
2485 |
copiedFiles.push(destFileName);
|
|
|
2486 |
}
|
|
|
2487 |
}
|
|
|
2488 |
|
|
|
2489 |
return copiedFiles.length ? copiedFiles : null; //Array or null
|
|
|
2490 |
},
|
|
|
2491 |
|
|
|
2492 |
copyFile: function (/*String*/srcFileName, /*String*/destFileName, /*boolean?*/onlyCopyNew) {
|
|
|
2493 |
//summary: copies srcFileName to destFileName. If onlyCopyNew is set, it only copies the file if
|
|
|
2494 |
//srcFileName is newer than destFileName. Returns a boolean indicating if the copy occurred.
|
|
|
2495 |
var parentDir;
|
|
|
2496 |
|
|
|
2497 |
//logger.trace("Src filename: " + srcFileName);
|
|
|
2498 |
//logger.trace("Dest filename: " + destFileName);
|
|
|
2499 |
|
|
|
2500 |
//If onlyCopyNew is true, then compare dates and only copy if the src is newer
|
|
|
2501 |
//than dest.
|
|
|
2502 |
if (onlyCopyNew) {
|
|
|
2503 |
if (file.exists(destFileName) && fs.statSync(destFileName).mtime.getTime() >= fs.statSync(srcFileName).mtime.getTime()) {
|
|
|
2504 |
return false; //Boolean
|
|
|
2505 |
}
|
|
|
2506 |
}
|
|
|
2507 |
|
|
|
2508 |
//Make sure destination dir exists.
|
|
|
2509 |
parentDir = path.dirname(destFileName);
|
|
|
2510 |
if (!file.exists(parentDir)) {
|
|
|
2511 |
mkFullDir(parentDir);
|
|
|
2512 |
}
|
|
|
2513 |
|
|
|
2514 |
fs.writeFileSync(destFileName, fs.readFileSync(srcFileName, 'binary'), 'binary');
|
|
|
2515 |
|
|
|
2516 |
return true; //Boolean
|
|
|
2517 |
},
|
|
|
2518 |
|
|
|
2519 |
/**
|
|
|
2520 |
* Reads a *text* file.
|
|
|
2521 |
*/
|
|
|
2522 |
readFile: function (/*String*/path, /*String?*/encoding) {
|
|
|
2523 |
if (encoding === 'utf-8') {
|
|
|
2524 |
encoding = 'utf8';
|
|
|
2525 |
}
|
|
|
2526 |
if (!encoding) {
|
|
|
2527 |
encoding = 'utf8';
|
|
|
2528 |
}
|
|
|
2529 |
|
|
|
2530 |
var text = fs.readFileSync(path, encoding);
|
|
|
2531 |
|
|
|
2532 |
//Hmm, would not expect to get A BOM, but it seems to happen,
|
|
|
2533 |
//remove it just in case.
|
|
|
2534 |
if (text.indexOf('\uFEFF') === 0) {
|
|
|
2535 |
text = text.substring(1, text.length);
|
|
|
2536 |
}
|
|
|
2537 |
|
|
|
2538 |
return text;
|
|
|
2539 |
},
|
|
|
2540 |
|
|
|
2541 |
saveUtf8File: function (/*String*/fileName, /*String*/fileContents) {
|
|
|
2542 |
//summary: saves a *text* file using UTF-8 encoding.
|
|
|
2543 |
file.saveFile(fileName, fileContents, "utf8");
|
|
|
2544 |
},
|
|
|
2545 |
|
|
|
2546 |
saveFile: function (/*String*/fileName, /*String*/fileContents, /*String?*/encoding) {
|
|
|
2547 |
//summary: saves a *text* file.
|
|
|
2548 |
var parentDir;
|
|
|
2549 |
|
|
|
2550 |
if (encoding === 'utf-8') {
|
|
|
2551 |
encoding = 'utf8';
|
|
|
2552 |
}
|
|
|
2553 |
if (!encoding) {
|
|
|
2554 |
encoding = 'utf8';
|
|
|
2555 |
}
|
|
|
2556 |
|
|
|
2557 |
//Make sure destination directories exist.
|
|
|
2558 |
parentDir = path.dirname(fileName);
|
|
|
2559 |
if (!file.exists(parentDir)) {
|
|
|
2560 |
mkFullDir(parentDir);
|
|
|
2561 |
}
|
|
|
2562 |
|
|
|
2563 |
fs.writeFileSync(fileName, fileContents, encoding);
|
|
|
2564 |
},
|
|
|
2565 |
|
|
|
2566 |
deleteFile: function (/*String*/fileName) {
|
|
|
2567 |
//summary: deletes a file or directory if it exists.
|
|
|
2568 |
var files, i, stat;
|
|
|
2569 |
if (file.exists(fileName)) {
|
|
|
2570 |
stat = fs.statSync(fileName);
|
|
|
2571 |
if (stat.isDirectory()) {
|
|
|
2572 |
files = fs.readdirSync(fileName);
|
|
|
2573 |
for (i = 0; i < files.length; i++) {
|
|
|
2574 |
this.deleteFile(path.join(fileName, files[i]));
|
|
|
2575 |
}
|
|
|
2576 |
fs.rmdirSync(fileName);
|
|
|
2577 |
} else {
|
|
|
2578 |
fs.unlinkSync(fileName);
|
|
|
2579 |
}
|
|
|
2580 |
}
|
|
|
2581 |
}
|
|
|
2582 |
};
|
|
|
2583 |
|
|
|
2584 |
return file;
|
|
|
2585 |
|
|
|
2586 |
});
|
|
|
2587 |
|
|
|
2588 |
}
|
|
|
2589 |
|
|
|
2590 |
if(env === 'rhino') {
|
|
|
2591 |
/**
|
|
|
2592 |
* @license RequireJS Copyright (c) 2010-2011, The Dojo Foundation All Rights Reserved.
|
|
|
2593 |
* Available via the MIT or new BSD license.
|
|
|
2594 |
* see: http://github.com/jrburke/requirejs for details
|
|
|
2595 |
*/
|
|
|
2596 |
//Helper functions to deal with file I/O.
|
|
|
2597 |
|
|
|
2598 |
/*jslint plusplus: false, strict: false */
|
|
|
2599 |
/*global java: false, define: false */
|
|
|
2600 |
|
|
|
2601 |
define('rhino/file', function () {
|
|
|
2602 |
var file = {
|
|
|
2603 |
backSlashRegExp: /\\/g,
|
|
|
2604 |
|
|
|
2605 |
dirExclusionRegExp: /^\./,
|
|
|
2606 |
|
|
|
2607 |
getLineSeparator: function () {
|
|
|
2608 |
return file.lineSeparator;
|
|
|
2609 |
},
|
|
|
2610 |
|
|
|
2611 |
lineSeparator: java.lang.System.getProperty("line.separator"), //Java String
|
|
|
2612 |
|
|
|
2613 |
exists: function (fileName) {
|
|
|
2614 |
return (new java.io.File(fileName)).exists();
|
|
|
2615 |
},
|
|
|
2616 |
|
|
|
2617 |
parent: function (fileName) {
|
|
|
2618 |
return file.absPath((new java.io.File(fileName)).getParentFile());
|
|
|
2619 |
},
|
|
|
2620 |
|
|
|
2621 |
normalize: function (fileName) {
|
|
|
2622 |
return file.absPath(fileName);
|
|
|
2623 |
},
|
|
|
2624 |
|
|
|
2625 |
isFile: function (path) {
|
|
|
2626 |
return (new java.io.File(path)).isFile();
|
|
|
2627 |
},
|
|
|
2628 |
|
|
|
2629 |
isDirectory: function (path) {
|
|
|
2630 |
return (new java.io.File(path)).isDirectory();
|
|
|
2631 |
},
|
|
|
2632 |
|
|
|
2633 |
/**
|
|
|
2634 |
* Gets the absolute file path as a string, normalized
|
|
|
2635 |
* to using front slashes for path separators.
|
|
|
2636 |
* @param {java.io.File||String} file
|
|
|
2637 |
*/
|
|
|
2638 |
absPath: function (fileObj) {
|
|
|
2639 |
if (typeof fileObj === "string") {
|
|
|
2640 |
fileObj = new java.io.File(fileObj);
|
|
|
2641 |
}
|
|
|
2642 |
return (fileObj.getAbsolutePath() + "").replace(file.backSlashRegExp, "/");
|
|
|
2643 |
},
|
|
|
2644 |
|
|
|
2645 |
getFilteredFileList: function (/*String*/startDir, /*RegExp*/regExpFilters, /*boolean?*/makeUnixPaths, /*boolean?*/startDirIsJavaObject) {
|
|
|
2646 |
//summary: Recurses startDir and finds matches to the files that match regExpFilters.include
|
|
|
2647 |
//and do not match regExpFilters.exclude. Or just one regexp can be passed in for regExpFilters,
|
|
|
2648 |
//and it will be treated as the "include" case.
|
|
|
2649 |
//Ignores files/directories that start with a period (.).
|
|
|
2650 |
var files = [], topDir, regExpInclude, regExpExclude, dirFileArray,
|
|
|
2651 |
i, fileObj, filePath, ok, dirFiles;
|
|
|
2652 |
|
|
|
2653 |
topDir = startDir;
|
|
|
2654 |
if (!startDirIsJavaObject) {
|
|
|
2655 |
topDir = new java.io.File(startDir);
|
|
|
2656 |
}
|
|
|
2657 |
|
|
|
2658 |
regExpInclude = regExpFilters.include || regExpFilters;
|
|
|
2659 |
regExpExclude = regExpFilters.exclude || null;
|
|
|
2660 |
|
|
|
2661 |
if (topDir.exists()) {
|
|
|
2662 |
dirFileArray = topDir.listFiles();
|
|
|
2663 |
for (i = 0; i < dirFileArray.length; i++) {
|
|
|
2664 |
fileObj = dirFileArray[i];
|
|
|
2665 |
if (fileObj.isFile()) {
|
|
|
2666 |
filePath = fileObj.getPath();
|
|
|
2667 |
if (makeUnixPaths) {
|
|
|
2668 |
//Make sure we have a JS string.
|
|
|
2669 |
filePath = String(filePath);
|
|
|
2670 |
if (filePath.indexOf("/") === -1) {
|
|
|
2671 |
filePath = filePath.replace(/\\/g, "/");
|
|
|
2672 |
}
|
|
|
2673 |
}
|
|
|
2674 |
|
|
|
2675 |
ok = true;
|
|
|
2676 |
if (regExpInclude) {
|
|
|
2677 |
ok = filePath.match(regExpInclude);
|
|
|
2678 |
}
|
|
|
2679 |
if (ok && regExpExclude) {
|
|
|
2680 |
ok = !filePath.match(regExpExclude);
|
|
|
2681 |
}
|
|
|
2682 |
|
|
|
2683 |
if (ok && !fileObj.getName().match(/^\./)) {
|
|
|
2684 |
files.push(filePath);
|
|
|
2685 |
}
|
|
|
2686 |
} else if (fileObj.isDirectory() &&
|
|
|
2687 |
(!file.dirExclusionRegExp || !fileObj.getName().match(file.dirExclusionRegExp))) {
|
|
|
2688 |
dirFiles = this.getFilteredFileList(fileObj, regExpFilters, makeUnixPaths, true);
|
|
|
2689 |
files.push.apply(files, dirFiles);
|
|
|
2690 |
}
|
|
|
2691 |
}
|
|
|
2692 |
}
|
|
|
2693 |
|
|
|
2694 |
return files; //Array
|
|
|
2695 |
},
|
|
|
2696 |
|
|
|
2697 |
copyDir: function (/*String*/srcDir, /*String*/destDir, /*RegExp?*/regExpFilter, /*boolean?*/onlyCopyNew) {
|
|
|
2698 |
//summary: copies files from srcDir to destDir using the regExpFilter to determine if the
|
|
|
2699 |
//file should be copied. Returns a list file name strings of the destinations that were copied.
|
|
|
2700 |
regExpFilter = regExpFilter || /\w/;
|
|
|
2701 |
|
|
|
2702 |
var fileNames = file.getFilteredFileList(srcDir, regExpFilter, true),
|
|
|
2703 |
copiedFiles = [], i, srcFileName, destFileName;
|
|
|
2704 |
|
|
|
2705 |
for (i = 0; i < fileNames.length; i++) {
|
|
|
2706 |
srcFileName = fileNames[i];
|
|
|
2707 |
destFileName = srcFileName.replace(srcDir, destDir);
|
|
|
2708 |
|
|
|
2709 |
if (file.copyFile(srcFileName, destFileName, onlyCopyNew)) {
|
|
|
2710 |
copiedFiles.push(destFileName);
|
|
|
2711 |
}
|
|
|
2712 |
}
|
|
|
2713 |
|
|
|
2714 |
return copiedFiles.length ? copiedFiles : null; //Array or null
|
|
|
2715 |
},
|
|
|
2716 |
|
|
|
2717 |
copyFile: function (/*String*/srcFileName, /*String*/destFileName, /*boolean?*/onlyCopyNew) {
|
|
|
2718 |
//summary: copies srcFileName to destFileName. If onlyCopyNew is set, it only copies the file if
|
|
|
2719 |
//srcFileName is newer than destFileName. Returns a boolean indicating if the copy occurred.
|
|
|
2720 |
var destFile = new java.io.File(destFileName), srcFile, parentDir,
|
|
|
2721 |
srcChannel, destChannel;
|
|
|
2722 |
|
|
|
2723 |
//logger.trace("Src filename: " + srcFileName);
|
|
|
2724 |
//logger.trace("Dest filename: " + destFileName);
|
|
|
2725 |
|
|
|
2726 |
//If onlyCopyNew is true, then compare dates and only copy if the src is newer
|
|
|
2727 |
//than dest.
|
|
|
2728 |
if (onlyCopyNew) {
|
|
|
2729 |
srcFile = new java.io.File(srcFileName);
|
|
|
2730 |
if (destFile.exists() && destFile.lastModified() >= srcFile.lastModified()) {
|
|
|
2731 |
return false; //Boolean
|
|
|
2732 |
}
|
|
|
2733 |
}
|
|
|
2734 |
|
|
|
2735 |
//Make sure destination dir exists.
|
|
|
2736 |
parentDir = destFile.getParentFile();
|
|
|
2737 |
if (!parentDir.exists()) {
|
|
|
2738 |
if (!parentDir.mkdirs()) {
|
|
|
2739 |
throw "Could not create directory: " + parentDir.getAbsolutePath();
|
|
|
2740 |
}
|
|
|
2741 |
}
|
|
|
2742 |
|
|
|
2743 |
//Java's version of copy file.
|
|
|
2744 |
srcChannel = new java.io.FileInputStream(srcFileName).getChannel();
|
|
|
2745 |
destChannel = new java.io.FileOutputStream(destFileName).getChannel();
|
|
|
2746 |
destChannel.transferFrom(srcChannel, 0, srcChannel.size());
|
|
|
2747 |
srcChannel.close();
|
|
|
2748 |
destChannel.close();
|
|
|
2749 |
|
|
|
2750 |
return true; //Boolean
|
|
|
2751 |
},
|
|
|
2752 |
|
|
|
2753 |
readFile: function (/*String*/path, /*String?*/encoding) {
|
|
|
2754 |
//A file read function that can deal with BOMs
|
|
|
2755 |
encoding = encoding || "utf-8";
|
|
|
2756 |
var fileObj = new java.io.File(path),
|
|
|
2757 |
input = new java.io.BufferedReader(new java.io.InputStreamReader(new java.io.FileInputStream(fileObj), encoding)),
|
|
|
2758 |
stringBuffer, line;
|
|
|
2759 |
try {
|
|
|
2760 |
stringBuffer = new java.lang.StringBuffer();
|
|
|
2761 |
line = input.readLine();
|
|
|
2762 |
|
|
|
2763 |
// Byte Order Mark (BOM) - The Unicode Standard, version 3.0, page 324
|
|
|
2764 |
// http://www.unicode.org/faq/utf_bom.html
|
|
|
2765 |
|
|
|
2766 |
// 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:
|
|
|
2767 |
// http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4508058
|
|
|
2768 |
if (line && line.length() && line.charAt(0) === 0xfeff) {
|
|
|
2769 |
// Eat the BOM, since we've already found the encoding on this file,
|
|
|
2770 |
// and we plan to concatenating this buffer with others; the BOM should
|
|
|
2771 |
// only appear at the top of a file.
|
|
|
2772 |
line = line.substring(1);
|
|
|
2773 |
}
|
|
|
2774 |
while (line !== null) {
|
|
|
2775 |
stringBuffer.append(line);
|
|
|
2776 |
stringBuffer.append(file.lineSeparator);
|
|
|
2777 |
line = input.readLine();
|
|
|
2778 |
}
|
|
|
2779 |
//Make sure we return a JavaScript string and not a Java string.
|
|
|
2780 |
return String(stringBuffer.toString()); //String
|
|
|
2781 |
} finally {
|
|
|
2782 |
input.close();
|
|
|
2783 |
}
|
|
|
2784 |
},
|
|
|
2785 |
|
|
|
2786 |
saveUtf8File: function (/*String*/fileName, /*String*/fileContents) {
|
|
|
2787 |
//summary: saves a file using UTF-8 encoding.
|
|
|
2788 |
file.saveFile(fileName, fileContents, "utf-8");
|
|
|
2789 |
},
|
|
|
2790 |
|
|
|
2791 |
saveFile: function (/*String*/fileName, /*String*/fileContents, /*String?*/encoding) {
|
|
|
2792 |
//summary: saves a file.
|
|
|
2793 |
var outFile = new java.io.File(fileName), outWriter, parentDir, os;
|
|
|
2794 |
|
|
|
2795 |
parentDir = outFile.getAbsoluteFile().getParentFile();
|
|
|
2796 |
if (!parentDir.exists()) {
|
|
|
2797 |
if (!parentDir.mkdirs()) {
|
|
|
2798 |
throw "Could not create directory: " + parentDir.getAbsolutePath();
|
|
|
2799 |
}
|
|
|
2800 |
}
|
|
|
2801 |
|
|
|
2802 |
if (encoding) {
|
|
|
2803 |
outWriter = new java.io.OutputStreamWriter(new java.io.FileOutputStream(outFile), encoding);
|
|
|
2804 |
} else {
|
|
|
2805 |
outWriter = new java.io.OutputStreamWriter(new java.io.FileOutputStream(outFile));
|
|
|
2806 |
}
|
|
|
2807 |
|
|
|
2808 |
os = new java.io.BufferedWriter(outWriter);
|
|
|
2809 |
try {
|
|
|
2810 |
os.write(fileContents);
|
|
|
2811 |
} finally {
|
|
|
2812 |
os.close();
|
|
|
2813 |
}
|
|
|
2814 |
},
|
|
|
2815 |
|
|
|
2816 |
deleteFile: function (/*String*/fileName) {
|
|
|
2817 |
//summary: deletes a file or directory if it exists.
|
|
|
2818 |
var fileObj = new java.io.File(fileName), files, i;
|
|
|
2819 |
if (fileObj.exists()) {
|
|
|
2820 |
if (fileObj.isDirectory()) {
|
|
|
2821 |
files = fileObj.listFiles();
|
|
|
2822 |
for (i = 0; i < files.length; i++) {
|
|
|
2823 |
this.deleteFile(files[i]);
|
|
|
2824 |
}
|
|
|
2825 |
}
|
|
|
2826 |
fileObj["delete"]();
|
|
|
2827 |
}
|
|
|
2828 |
}
|
|
|
2829 |
};
|
|
|
2830 |
|
|
|
2831 |
return file;
|
|
|
2832 |
});
|
|
|
2833 |
|
|
|
2834 |
}
|
|
|
2835 |
/**
|
|
|
2836 |
* @license Copyright (c) 2010-2011, The Dojo Foundation All Rights Reserved.
|
|
|
2837 |
* Available via the MIT or new BSD license.
|
|
|
2838 |
* see: http://github.com/jrburke/requirejs for details
|
|
|
2839 |
*/
|
|
|
2840 |
|
|
|
2841 |
/*jslint plusplus: false, strict: false */
|
|
|
2842 |
/*global define: false */
|
|
|
2843 |
|
|
|
2844 |
define('lang', function () {
|
|
|
2845 |
var lang = {
|
|
|
2846 |
backSlashRegExp: /\\/g,
|
|
|
2847 |
ostring: Object.prototype.toString,
|
|
|
2848 |
|
|
|
2849 |
isArray: Array.isArray ? Array.isArray : function (it) {
|
|
|
2850 |
return lang.ostring.call(it) === "[object Array]";
|
|
|
2851 |
},
|
|
|
2852 |
|
|
|
2853 |
/**
|
|
|
2854 |
* Simple function to mix in properties from source into target,
|
|
|
2855 |
* but only if target does not already have a property of the same name.
|
|
|
2856 |
*/
|
|
|
2857 |
mixin: function (target, source, override) {
|
|
|
2858 |
//Use an empty object to avoid other bad JS code that modifies
|
|
|
2859 |
//Object.prototype.
|
|
|
2860 |
var empty = {}, prop;
|
|
|
2861 |
for (prop in source) {
|
|
|
2862 |
if (override || !(prop in target)) {
|
|
|
2863 |
target[prop] = source[prop];
|
|
|
2864 |
}
|
|
|
2865 |
}
|
|
|
2866 |
},
|
|
|
2867 |
|
|
|
2868 |
delegate: (function () {
|
|
|
2869 |
// boodman/crockford delegation w/ cornford optimization
|
|
|
2870 |
function TMP() {}
|
|
|
2871 |
return function (obj, props) {
|
|
|
2872 |
TMP.prototype = obj;
|
|
|
2873 |
var tmp = new TMP();
|
|
|
2874 |
TMP.prototype = null;
|
|
|
2875 |
if (props) {
|
|
|
2876 |
lang.mixin(tmp, props);
|
|
|
2877 |
}
|
|
|
2878 |
return tmp; // Object
|
|
|
2879 |
};
|
|
|
2880 |
}())
|
|
|
2881 |
};
|
|
|
2882 |
return lang;
|
|
|
2883 |
});
|
|
|
2884 |
|
|
|
2885 |
if(env === 'node') {
|
|
|
2886 |
/**
|
|
|
2887 |
* @license RequireJS Copyright (c) 2010-2011, The Dojo Foundation All Rights Reserved.
|
|
|
2888 |
* Available via the MIT or new BSD license.
|
|
|
2889 |
* see: http://github.com/jrburke/requirejs for details
|
|
|
2890 |
*/
|
|
|
2891 |
|
|
|
2892 |
/*jslint strict: false */
|
|
|
2893 |
/*global define: false, console: false */
|
|
|
2894 |
|
|
|
2895 |
define('node/print', function () {
|
|
|
2896 |
function print(msg) {
|
|
|
2897 |
console.log(msg);
|
|
|
2898 |
}
|
|
|
2899 |
|
|
|
2900 |
return print;
|
|
|
2901 |
});
|
|
|
2902 |
|
|
|
2903 |
}
|
|
|
2904 |
|
|
|
2905 |
if(env === 'rhino') {
|
|
|
2906 |
/**
|
|
|
2907 |
* @license RequireJS Copyright (c) 2010-2011, The Dojo Foundation All Rights Reserved.
|
|
|
2908 |
* Available via the MIT or new BSD license.
|
|
|
2909 |
* see: http://github.com/jrburke/requirejs for details
|
|
|
2910 |
*/
|
|
|
2911 |
|
|
|
2912 |
/*jslint strict: false */
|
|
|
2913 |
/*global define: false, print: false */
|
|
|
2914 |
|
|
|
2915 |
define('rhino/print', function () {
|
|
|
2916 |
return print;
|
|
|
2917 |
});
|
|
|
2918 |
|
|
|
2919 |
}
|
|
|
2920 |
/**
|
|
|
2921 |
* @license RequireJS Copyright (c) 2010-2011, The Dojo Foundation All Rights Reserved.
|
|
|
2922 |
* Available via the MIT or new BSD license.
|
|
|
2923 |
* see: http://github.com/jrburke/requirejs for details
|
|
|
2924 |
*/
|
|
|
2925 |
|
|
|
2926 |
/*jslint nomen: false, strict: false */
|
|
|
2927 |
/*global define: false */
|
|
|
2928 |
|
|
|
2929 |
define('logger', ['env!env/print'], function (print) {
|
|
|
2930 |
var logger = {
|
|
|
2931 |
TRACE: 0,
|
|
|
2932 |
INFO: 1,
|
|
|
2933 |
WARN: 2,
|
|
|
2934 |
ERROR: 3,
|
|
|
2935 |
SILENT: 4,
|
|
|
2936 |
level: 0,
|
|
|
2937 |
logPrefix: "",
|
|
|
2938 |
|
|
|
2939 |
logLevel: function( level ) {
|
|
|
2940 |
this.level = level;
|
|
|
2941 |
},
|
|
|
2942 |
|
|
|
2943 |
trace: function (message) {
|
|
|
2944 |
if (this.level <= this.TRACE) {
|
|
|
2945 |
this._print(message);
|
|
|
2946 |
}
|
|
|
2947 |
},
|
|
|
2948 |
|
|
|
2949 |
info: function (message) {
|
|
|
2950 |
if (this.level <= this.INFO) {
|
|
|
2951 |
this._print(message);
|
|
|
2952 |
}
|
|
|
2953 |
},
|
|
|
2954 |
|
|
|
2955 |
warn: function (message) {
|
|
|
2956 |
if (this.level <= this.WARN) {
|
|
|
2957 |
this._print(message);
|
|
|
2958 |
}
|
|
|
2959 |
},
|
|
|
2960 |
|
|
|
2961 |
error: function (message) {
|
|
|
2962 |
if (this.level <= this.ERROR) {
|
|
|
2963 |
this._print(message);
|
|
|
2964 |
}
|
|
|
2965 |
},
|
|
|
2966 |
|
|
|
2967 |
_print: function (message) {
|
|
|
2968 |
this._sysPrint((this.logPrefix ? (this.logPrefix + " ") : "") + message);
|
|
|
2969 |
},
|
|
|
2970 |
|
|
|
2971 |
_sysPrint: function (message) {
|
|
|
2972 |
print(message);
|
|
|
2973 |
}
|
|
|
2974 |
};
|
|
|
2975 |
|
|
|
2976 |
return logger;
|
|
|
2977 |
});
|
|
|
2978 |
//Just a blank file to use when building the optimizer with the optimizer,
|
|
|
2979 |
//so that the build does not attempt to inline some env modules,
|
|
|
2980 |
//like Node's fs and path.
|
|
|
2981 |
|
|
|
2982 |
//Just a blank file to use when building the optimizer with the optimizer,
|
|
|
2983 |
//so that the build does not attempt to inline some env modules,
|
|
|
2984 |
//like Node's fs and path.
|
|
|
2985 |
|
|
|
2986 |
define('uglifyjs/parse-js', ["require", "exports", "module"], function(require, exports, module) {
|
|
|
2987 |
|
|
|
2988 |
/***********************************************************************
|
|
|
2989 |
|
|
|
2990 |
A JavaScript tokenizer / parser / beautifier / compressor.
|
|
|
2991 |
|
|
|
2992 |
This version is suitable for Node.js. With minimal changes (the
|
|
|
2993 |
exports stuff) it should work on any JS platform.
|
|
|
2994 |
|
|
|
2995 |
This file contains the tokenizer/parser. It is a port to JavaScript
|
|
|
2996 |
of parse-js [1], a JavaScript parser library written in Common Lisp
|
|
|
2997 |
by Marijn Haverbeke. Thank you Marijn!
|
|
|
2998 |
|
|
|
2999 |
[1] http://marijn.haverbeke.nl/parse-js/
|
|
|
3000 |
|
|
|
3001 |
Exported functions:
|
|
|
3002 |
|
|
|
3003 |
- tokenizer(code) -- returns a function. Call the returned
|
|
|
3004 |
function to fetch the next token.
|
|
|
3005 |
|
|
|
3006 |
- parse(code) -- returns an AST of the given JavaScript code.
|
|
|
3007 |
|
|
|
3008 |
-------------------------------- (C) ---------------------------------
|
|
|
3009 |
|
|
|
3010 |
Author: Mihai Bazon
|
|
|
3011 |
<mihai.bazon@gmail.com>
|
|
|
3012 |
http://mihai.bazon.net/blog
|
|
|
3013 |
|
|
|
3014 |
Distributed under the BSD license:
|
|
|
3015 |
|
|
|
3016 |
Copyright 2010 (c) Mihai Bazon <mihai.bazon@gmail.com>
|
|
|
3017 |
Based on parse-js (http://marijn.haverbeke.nl/parse-js/).
|
|
|
3018 |
|
|
|
3019 |
Redistribution and use in source and binary forms, with or without
|
|
|
3020 |
modification, are permitted provided that the following conditions
|
|
|
3021 |
are met:
|
|
|
3022 |
|
|
|
3023 |
* Redistributions of source code must retain the above
|
|
|
3024 |
copyright notice, this list of conditions and the following
|
|
|
3025 |
disclaimer.
|
|
|
3026 |
|
|
|
3027 |
* Redistributions in binary form must reproduce the above
|
|
|
3028 |
copyright notice, this list of conditions and the following
|
|
|
3029 |
disclaimer in the documentation and/or other materials
|
|
|
3030 |
provided with the distribution.
|
|
|
3031 |
|
|
|
3032 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
|
|
|
3033 |
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
3034 |
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
3035 |
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
|
|
|
3036 |
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
|
|
3037 |
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
3038 |
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
3039 |
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
3040 |
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
|
|
|
3041 |
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
|
|
3042 |
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
3043 |
SUCH DAMAGE.
|
|
|
3044 |
|
|
|
3045 |
***********************************************************************/
|
|
|
3046 |
|
|
|
3047 |
/* -----[ Tokenizer (constants) ]----- */
|
|
|
3048 |
|
|
|
3049 |
var KEYWORDS = array_to_hash([
|
|
|
3050 |
"break",
|
|
|
3051 |
"case",
|
|
|
3052 |
"catch",
|
|
|
3053 |
"const",
|
|
|
3054 |
"continue",
|
|
|
3055 |
"default",
|
|
|
3056 |
"delete",
|
|
|
3057 |
"do",
|
|
|
3058 |
"else",
|
|
|
3059 |
"finally",
|
|
|
3060 |
"for",
|
|
|
3061 |
"function",
|
|
|
3062 |
"if",
|
|
|
3063 |
"in",
|
|
|
3064 |
"instanceof",
|
|
|
3065 |
"new",
|
|
|
3066 |
"return",
|
|
|
3067 |
"switch",
|
|
|
3068 |
"throw",
|
|
|
3069 |
"try",
|
|
|
3070 |
"typeof",
|
|
|
3071 |
"var",
|
|
|
3072 |
"void",
|
|
|
3073 |
"while",
|
|
|
3074 |
"with"
|
|
|
3075 |
]);
|
|
|
3076 |
|
|
|
3077 |
var RESERVED_WORDS = array_to_hash([
|
|
|
3078 |
"abstract",
|
|
|
3079 |
"boolean",
|
|
|
3080 |
"byte",
|
|
|
3081 |
"char",
|
|
|
3082 |
"class",
|
|
|
3083 |
"debugger",
|
|
|
3084 |
"double",
|
|
|
3085 |
"enum",
|
|
|
3086 |
"export",
|
|
|
3087 |
"extends",
|
|
|
3088 |
"final",
|
|
|
3089 |
"float",
|
|
|
3090 |
"goto",
|
|
|
3091 |
"implements",
|
|
|
3092 |
"import",
|
|
|
3093 |
"int",
|
|
|
3094 |
"interface",
|
|
|
3095 |
"long",
|
|
|
3096 |
"native",
|
|
|
3097 |
"package",
|
|
|
3098 |
"private",
|
|
|
3099 |
"protected",
|
|
|
3100 |
"public",
|
|
|
3101 |
"short",
|
|
|
3102 |
"static",
|
|
|
3103 |
"super",
|
|
|
3104 |
"synchronized",
|
|
|
3105 |
"throws",
|
|
|
3106 |
"transient",
|
|
|
3107 |
"volatile"
|
|
|
3108 |
]);
|
|
|
3109 |
|
|
|
3110 |
var KEYWORDS_BEFORE_EXPRESSION = array_to_hash([
|
|
|
3111 |
"return",
|
|
|
3112 |
"new",
|
|
|
3113 |
"delete",
|
|
|
3114 |
"throw",
|
|
|
3115 |
"else",
|
|
|
3116 |
"case"
|
|
|
3117 |
]);
|
|
|
3118 |
|
|
|
3119 |
var KEYWORDS_ATOM = array_to_hash([
|
|
|
3120 |
"false",
|
|
|
3121 |
"null",
|
|
|
3122 |
"true",
|
|
|
3123 |
"undefined"
|
|
|
3124 |
]);
|
|
|
3125 |
|
|
|
3126 |
var OPERATOR_CHARS = array_to_hash(characters("+-*&%=<>!?|~^"));
|
|
|
3127 |
|
|
|
3128 |
var RE_HEX_NUMBER = /^0x[0-9a-f]+$/i;
|
|
|
3129 |
var RE_OCT_NUMBER = /^0[0-7]+$/;
|
|
|
3130 |
var RE_DEC_NUMBER = /^\d*\.?\d*(?:e[+-]?\d*(?:\d\.?|\.?\d)\d*)?$/i;
|
|
|
3131 |
|
|
|
3132 |
var OPERATORS = array_to_hash([
|
|
|
3133 |
"in",
|
|
|
3134 |
"instanceof",
|
|
|
3135 |
"typeof",
|
|
|
3136 |
"new",
|
|
|
3137 |
"void",
|
|
|
3138 |
"delete",
|
|
|
3139 |
"++",
|
|
|
3140 |
"--",
|
|
|
3141 |
"+",
|
|
|
3142 |
"-",
|
|
|
3143 |
"!",
|
|
|
3144 |
"~",
|
|
|
3145 |
"&",
|
|
|
3146 |
"|",
|
|
|
3147 |
"^",
|
|
|
3148 |
"*",
|
|
|
3149 |
"/",
|
|
|
3150 |
"%",
|
|
|
3151 |
">>",
|
|
|
3152 |
"<<",
|
|
|
3153 |
">>>",
|
|
|
3154 |
"<",
|
|
|
3155 |
">",
|
|
|
3156 |
"<=",
|
|
|
3157 |
">=",
|
|
|
3158 |
"==",
|
|
|
3159 |
"===",
|
|
|
3160 |
"!=",
|
|
|
3161 |
"!==",
|
|
|
3162 |
"?",
|
|
|
3163 |
"=",
|
|
|
3164 |
"+=",
|
|
|
3165 |
"-=",
|
|
|
3166 |
"/=",
|
|
|
3167 |
"*=",
|
|
|
3168 |
"%=",
|
|
|
3169 |
">>=",
|
|
|
3170 |
"<<=",
|
|
|
3171 |
">>>=",
|
|
|
3172 |
"|=",
|
|
|
3173 |
"^=",
|
|
|
3174 |
"&=",
|
|
|
3175 |
"&&",
|
|
|
3176 |
"||"
|
|
|
3177 |
]);
|
|
|
3178 |
|
|
|
3179 |
var WHITESPACE_CHARS = array_to_hash(characters(" \u00a0\n\r\t\f\u000b\u200b\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000"));
|
|
|
3180 |
|
|
|
3181 |
var PUNC_BEFORE_EXPRESSION = array_to_hash(characters("[{}(,.;:"));
|
|
|
3182 |
|
|
|
3183 |
var PUNC_CHARS = array_to_hash(characters("[]{}(),;:"));
|
|
|
3184 |
|
|
|
3185 |
var REGEXP_MODIFIERS = array_to_hash(characters("gmsiy"));
|
|
|
3186 |
|
|
|
3187 |
/* -----[ Tokenizer ]----- */
|
|
|
3188 |
|
|
|
3189 |
// regexps adapted from http://xregexp.com/plugins/#unicode
|
|
|
3190 |
var UNICODE = {
|
|
|
3191 |
letter: new RegExp("[\\u0041-\\u005A\\u0061-\\u007A\\u00AA\\u00B5\\u00BA\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02C1\\u02C6-\\u02D1\\u02E0-\\u02E4\\u02EC\\u02EE\\u0370-\\u0374\\u0376\\u0377\\u037A-\\u037D\\u0386\\u0388-\\u038A\\u038C\\u038E-\\u03A1\\u03A3-\\u03F5\\u03F7-\\u0481\\u048A-\\u0523\\u0531-\\u0556\\u0559\\u0561-\\u0587\\u05D0-\\u05EA\\u05F0-\\u05F2\\u0621-\\u064A\\u066E\\u066F\\u0671-\\u06D3\\u06D5\\u06E5\\u06E6\\u06EE\\u06EF\\u06FA-\\u06FC\\u06FF\\u0710\\u0712-\\u072F\\u074D-\\u07A5\\u07B1\\u07CA-\\u07EA\\u07F4\\u07F5\\u07FA\\u0904-\\u0939\\u093D\\u0950\\u0958-\\u0961\\u0971\\u0972\\u097B-\\u097F\\u0985-\\u098C\\u098F\\u0990\\u0993-\\u09A8\\u09AA-\\u09B0\\u09B2\\u09B6-\\u09B9\\u09BD\\u09CE\\u09DC\\u09DD\\u09DF-\\u09E1\\u09F0\\u09F1\\u0A05-\\u0A0A\\u0A0F\\u0A10\\u0A13-\\u0A28\\u0A2A-\\u0A30\\u0A32\\u0A33\\u0A35\\u0A36\\u0A38\\u0A39\\u0A59-\\u0A5C\\u0A5E\\u0A72-\\u0A74\\u0A85-\\u0A8D\\u0A8F-\\u0A91\\u0A93-\\u0AA8\\u0AAA-\\u0AB0\\u0AB2\\u0AB3\\u0AB5-\\u0AB9\\u0ABD\\u0AD0\\u0AE0\\u0AE1\\u0B05-\\u0B0C\\u0B0F\\u0B10\\u0B13-\\u0B28\\u0B2A-\\u0B30\\u0B32\\u0B33\\u0B35-\\u0B39\\u0B3D\\u0B5C\\u0B5D\\u0B5F-\\u0B61\\u0B71\\u0B83\\u0B85-\\u0B8A\\u0B8E-\\u0B90\\u0B92-\\u0B95\\u0B99\\u0B9A\\u0B9C\\u0B9E\\u0B9F\\u0BA3\\u0BA4\\u0BA8-\\u0BAA\\u0BAE-\\u0BB9\\u0BD0\\u0C05-\\u0C0C\\u0C0E-\\u0C10\\u0C12-\\u0C28\\u0C2A-\\u0C33\\u0C35-\\u0C39\\u0C3D\\u0C58\\u0C59\\u0C60\\u0C61\\u0C85-\\u0C8C\\u0C8E-\\u0C90\\u0C92-\\u0CA8\\u0CAA-\\u0CB3\\u0CB5-\\u0CB9\\u0CBD\\u0CDE\\u0CE0\\u0CE1\\u0D05-\\u0D0C\\u0D0E-\\u0D10\\u0D12-\\u0D28\\u0D2A-\\u0D39\\u0D3D\\u0D60\\u0D61\\u0D7A-\\u0D7F\\u0D85-\\u0D96\\u0D9A-\\u0DB1\\u0DB3-\\u0DBB\\u0DBD\\u0DC0-\\u0DC6\\u0E01-\\u0E30\\u0E32\\u0E33\\u0E40-\\u0E46\\u0E81\\u0E82\\u0E84\\u0E87\\u0E88\\u0E8A\\u0E8D\\u0E94-\\u0E97\\u0E99-\\u0E9F\\u0EA1-\\u0EA3\\u0EA5\\u0EA7\\u0EAA\\u0EAB\\u0EAD-\\u0EB0\\u0EB2\\u0EB3\\u0EBD\\u0EC0-\\u0EC4\\u0EC6\\u0EDC\\u0EDD\\u0F00\\u0F40-\\u0F47\\u0F49-\\u0F6C\\u0F88-\\u0F8B\\u1000-\\u102A\\u103F\\u1050-\\u1055\\u105A-\\u105D\\u1061\\u1065\\u1066\\u106E-\\u1070\\u1075-\\u1081\\u108E\\u10A0-\\u10C5\\u10D0-\\u10FA\\u10FC\\u1100-\\u1159\\u115F-\\u11A2\\u11A8-\\u11F9\\u1200-\\u1248\\u124A-\\u124D\\u1250-\\u1256\\u1258\\u125A-\\u125D\\u1260-\\u1288\\u128A-\\u128D\\u1290-\\u12B0\\u12B2-\\u12B5\\u12B8-\\u12BE\\u12C0\\u12C2-\\u12C5\\u12C8-\\u12D6\\u12D8-\\u1310\\u1312-\\u1315\\u1318-\\u135A\\u1380-\\u138F\\u13A0-\\u13F4\\u1401-\\u166C\\u166F-\\u1676\\u1681-\\u169A\\u16A0-\\u16EA\\u1700-\\u170C\\u170E-\\u1711\\u1720-\\u1731\\u1740-\\u1751\\u1760-\\u176C\\u176E-\\u1770\\u1780-\\u17B3\\u17D7\\u17DC\\u1820-\\u1877\\u1880-\\u18A8\\u18AA\\u1900-\\u191C\\u1950-\\u196D\\u1970-\\u1974\\u1980-\\u19A9\\u19C1-\\u19C7\\u1A00-\\u1A16\\u1B05-\\u1B33\\u1B45-\\u1B4B\\u1B83-\\u1BA0\\u1BAE\\u1BAF\\u1C00-\\u1C23\\u1C4D-\\u1C4F\\u1C5A-\\u1C7D\\u1D00-\\u1DBF\\u1E00-\\u1F15\\u1F18-\\u1F1D\\u1F20-\\u1F45\\u1F48-\\u1F4D\\u1F50-\\u1F57\\u1F59\\u1F5B\\u1F5D\\u1F5F-\\u1F7D\\u1F80-\\u1FB4\\u1FB6-\\u1FBC\\u1FBE\\u1FC2-\\u1FC4\\u1FC6-\\u1FCC\\u1FD0-\\u1FD3\\u1FD6-\\u1FDB\\u1FE0-\\u1FEC\\u1FF2-\\u1FF4\\u1FF6-\\u1FFC\\u2071\\u207F\\u2090-\\u2094\\u2102\\u2107\\u210A-\\u2113\\u2115\\u2119-\\u211D\\u2124\\u2126\\u2128\\u212A-\\u212D\\u212F-\\u2139\\u213C-\\u213F\\u2145-\\u2149\\u214E\\u2183\\u2184\\u2C00-\\u2C2E\\u2C30-\\u2C5E\\u2C60-\\u2C6F\\u2C71-\\u2C7D\\u2C80-\\u2CE4\\u2D00-\\u2D25\\u2D30-\\u2D65\\u2D6F\\u2D80-\\u2D96\\u2DA0-\\u2DA6\\u2DA8-\\u2DAE\\u2DB0-\\u2DB6\\u2DB8-\\u2DBE\\u2DC0-\\u2DC6\\u2DC8-\\u2DCE\\u2DD0-\\u2DD6\\u2DD8-\\u2DDE\\u2E2F\\u3005\\u3006\\u3031-\\u3035\\u303B\\u303C\\u3041-\\u3096\\u309D-\\u309F\\u30A1-\\u30FA\\u30FC-\\u30FF\\u3105-\\u312D\\u3131-\\u318E\\u31A0-\\u31B7\\u31F0-\\u31FF\\u3400\\u4DB5\\u4E00\\u9FC3\\uA000-\\uA48C\\uA500-\\uA60C\\uA610-\\uA61F\\uA62A\\uA62B\\uA640-\\uA65F\\uA662-\\uA66E\\uA67F-\\uA697\\uA717-\\uA71F\\uA722-\\uA788\\uA78B\\uA78C\\uA7FB-\\uA801\\uA803-\\uA805\\uA807-\\uA80A\\uA80C-\\uA822\\uA840-\\uA873\\uA882-\\uA8B3\\uA90A-\\uA925\\uA930-\\uA946\\uAA00-\\uAA28\\uAA40-\\uAA42\\uAA44-\\uAA4B\\uAC00\\uD7A3\\uF900-\\uFA2D\\uFA30-\\uFA6A\\uFA70-\\uFAD9\\uFB00-\\uFB06\\uFB13-\\uFB17\\uFB1D\\uFB1F-\\uFB28\\uFB2A-\\uFB36\\uFB38-\\uFB3C\\uFB3E\\uFB40\\uFB41\\uFB43\\uFB44\\uFB46-\\uFBB1\\uFBD3-\\uFD3D\\uFD50-\\uFD8F\\uFD92-\\uFDC7\\uFDF0-\\uFDFB\\uFE70-\\uFE74\\uFE76-\\uFEFC\\uFF21-\\uFF3A\\uFF41-\\uFF5A\\uFF66-\\uFFBE\\uFFC2-\\uFFC7\\uFFCA-\\uFFCF\\uFFD2-\\uFFD7\\uFFDA-\\uFFDC]"),
|
|
|
3192 |
non_spacing_mark: new RegExp("[\\u0300-\\u036F\\u0483-\\u0487\\u0591-\\u05BD\\u05BF\\u05C1\\u05C2\\u05C4\\u05C5\\u05C7\\u0610-\\u061A\\u064B-\\u065E\\u0670\\u06D6-\\u06DC\\u06DF-\\u06E4\\u06E7\\u06E8\\u06EA-\\u06ED\\u0711\\u0730-\\u074A\\u07A6-\\u07B0\\u07EB-\\u07F3\\u0816-\\u0819\\u081B-\\u0823\\u0825-\\u0827\\u0829-\\u082D\\u0900-\\u0902\\u093C\\u0941-\\u0948\\u094D\\u0951-\\u0955\\u0962\\u0963\\u0981\\u09BC\\u09C1-\\u09C4\\u09CD\\u09E2\\u09E3\\u0A01\\u0A02\\u0A3C\\u0A41\\u0A42\\u0A47\\u0A48\\u0A4B-\\u0A4D\\u0A51\\u0A70\\u0A71\\u0A75\\u0A81\\u0A82\\u0ABC\\u0AC1-\\u0AC5\\u0AC7\\u0AC8\\u0ACD\\u0AE2\\u0AE3\\u0B01\\u0B3C\\u0B3F\\u0B41-\\u0B44\\u0B4D\\u0B56\\u0B62\\u0B63\\u0B82\\u0BC0\\u0BCD\\u0C3E-\\u0C40\\u0C46-\\u0C48\\u0C4A-\\u0C4D\\u0C55\\u0C56\\u0C62\\u0C63\\u0CBC\\u0CBF\\u0CC6\\u0CCC\\u0CCD\\u0CE2\\u0CE3\\u0D41-\\u0D44\\u0D4D\\u0D62\\u0D63\\u0DCA\\u0DD2-\\u0DD4\\u0DD6\\u0E31\\u0E34-\\u0E3A\\u0E47-\\u0E4E\\u0EB1\\u0EB4-\\u0EB9\\u0EBB\\u0EBC\\u0EC8-\\u0ECD\\u0F18\\u0F19\\u0F35\\u0F37\\u0F39\\u0F71-\\u0F7E\\u0F80-\\u0F84\\u0F86\\u0F87\\u0F90-\\u0F97\\u0F99-\\u0FBC\\u0FC6\\u102D-\\u1030\\u1032-\\u1037\\u1039\\u103A\\u103D\\u103E\\u1058\\u1059\\u105E-\\u1060\\u1071-\\u1074\\u1082\\u1085\\u1086\\u108D\\u109D\\u135F\\u1712-\\u1714\\u1732-\\u1734\\u1752\\u1753\\u1772\\u1773\\u17B7-\\u17BD\\u17C6\\u17C9-\\u17D3\\u17DD\\u180B-\\u180D\\u18A9\\u1920-\\u1922\\u1927\\u1928\\u1932\\u1939-\\u193B\\u1A17\\u1A18\\u1A56\\u1A58-\\u1A5E\\u1A60\\u1A62\\u1A65-\\u1A6C\\u1A73-\\u1A7C\\u1A7F\\u1B00-\\u1B03\\u1B34\\u1B36-\\u1B3A\\u1B3C\\u1B42\\u1B6B-\\u1B73\\u1B80\\u1B81\\u1BA2-\\u1BA5\\u1BA8\\u1BA9\\u1C2C-\\u1C33\\u1C36\\u1C37\\u1CD0-\\u1CD2\\u1CD4-\\u1CE0\\u1CE2-\\u1CE8\\u1CED\\u1DC0-\\u1DE6\\u1DFD-\\u1DFF\\u20D0-\\u20DC\\u20E1\\u20E5-\\u20F0\\u2CEF-\\u2CF1\\u2DE0-\\u2DFF\\u302A-\\u302F\\u3099\\u309A\\uA66F\\uA67C\\uA67D\\uA6F0\\uA6F1\\uA802\\uA806\\uA80B\\uA825\\uA826\\uA8C4\\uA8E0-\\uA8F1\\uA926-\\uA92D\\uA947-\\uA951\\uA980-\\uA982\\uA9B3\\uA9B6-\\uA9B9\\uA9BC\\uAA29-\\uAA2E\\uAA31\\uAA32\\uAA35\\uAA36\\uAA43\\uAA4C\\uAAB0\\uAAB2-\\uAAB4\\uAAB7\\uAAB8\\uAABE\\uAABF\\uAAC1\\uABE5\\uABE8\\uABED\\uFB1E\\uFE00-\\uFE0F\\uFE20-\\uFE26]"),
|
|
|
3193 |
space_combining_mark: new RegExp("[\\u0903\\u093E-\\u0940\\u0949-\\u094C\\u094E\\u0982\\u0983\\u09BE-\\u09C0\\u09C7\\u09C8\\u09CB\\u09CC\\u09D7\\u0A03\\u0A3E-\\u0A40\\u0A83\\u0ABE-\\u0AC0\\u0AC9\\u0ACB\\u0ACC\\u0B02\\u0B03\\u0B3E\\u0B40\\u0B47\\u0B48\\u0B4B\\u0B4C\\u0B57\\u0BBE\\u0BBF\\u0BC1\\u0BC2\\u0BC6-\\u0BC8\\u0BCA-\\u0BCC\\u0BD7\\u0C01-\\u0C03\\u0C41-\\u0C44\\u0C82\\u0C83\\u0CBE\\u0CC0-\\u0CC4\\u0CC7\\u0CC8\\u0CCA\\u0CCB\\u0CD5\\u0CD6\\u0D02\\u0D03\\u0D3E-\\u0D40\\u0D46-\\u0D48\\u0D4A-\\u0D4C\\u0D57\\u0D82\\u0D83\\u0DCF-\\u0DD1\\u0DD8-\\u0DDF\\u0DF2\\u0DF3\\u0F3E\\u0F3F\\u0F7F\\u102B\\u102C\\u1031\\u1038\\u103B\\u103C\\u1056\\u1057\\u1062-\\u1064\\u1067-\\u106D\\u1083\\u1084\\u1087-\\u108C\\u108F\\u109A-\\u109C\\u17B6\\u17BE-\\u17C5\\u17C7\\u17C8\\u1923-\\u1926\\u1929-\\u192B\\u1930\\u1931\\u1933-\\u1938\\u19B0-\\u19C0\\u19C8\\u19C9\\u1A19-\\u1A1B\\u1A55\\u1A57\\u1A61\\u1A63\\u1A64\\u1A6D-\\u1A72\\u1B04\\u1B35\\u1B3B\\u1B3D-\\u1B41\\u1B43\\u1B44\\u1B82\\u1BA1\\u1BA6\\u1BA7\\u1BAA\\u1C24-\\u1C2B\\u1C34\\u1C35\\u1CE1\\u1CF2\\uA823\\uA824\\uA827\\uA880\\uA881\\uA8B4-\\uA8C3\\uA952\\uA953\\uA983\\uA9B4\\uA9B5\\uA9BA\\uA9BB\\uA9BD-\\uA9C0\\uAA2F\\uAA30\\uAA33\\uAA34\\uAA4D\\uAA7B\\uABE3\\uABE4\\uABE6\\uABE7\\uABE9\\uABEA\\uABEC]"),
|
|
|
3194 |
connector_punctuation: new RegExp("[\\u005F\\u203F\\u2040\\u2054\\uFE33\\uFE34\\uFE4D-\\uFE4F\\uFF3F]")
|
|
|
3195 |
};
|
|
|
3196 |
|
|
|
3197 |
function is_letter(ch) {
|
|
|
3198 |
return UNICODE.letter.test(ch);
|
|
|
3199 |
};
|
|
|
3200 |
|
|
|
3201 |
function is_digit(ch) {
|
|
|
3202 |
ch = ch.charCodeAt(0);
|
|
|
3203 |
return ch >= 48 && ch <= 57; //XXX: find out if "UnicodeDigit" means something else than 0..9
|
|
|
3204 |
};
|
|
|
3205 |
|
|
|
3206 |
function is_alphanumeric_char(ch) {
|
|
|
3207 |
return is_digit(ch) || is_letter(ch);
|
|
|
3208 |
};
|
|
|
3209 |
|
|
|
3210 |
function is_unicode_combining_mark(ch) {
|
|
|
3211 |
return UNICODE.non_spacing_mark.test(ch) || UNICODE.space_combining_mark.test(ch);
|
|
|
3212 |
};
|
|
|
3213 |
|
|
|
3214 |
function is_unicode_connector_punctuation(ch) {
|
|
|
3215 |
return UNICODE.connector_punctuation.test(ch);
|
|
|
3216 |
};
|
|
|
3217 |
|
|
|
3218 |
function is_identifier_start(ch) {
|
|
|
3219 |
return ch == "$" || ch == "_" || is_letter(ch);
|
|
|
3220 |
};
|
|
|
3221 |
|
|
|
3222 |
function is_identifier_char(ch) {
|
|
|
3223 |
return is_identifier_start(ch)
|
|
|
3224 |
|| is_unicode_combining_mark(ch)
|
|
|
3225 |
|| is_digit(ch)
|
|
|
3226 |
|| is_unicode_connector_punctuation(ch)
|
|
|
3227 |
|| ch == "\u200c" // zero-width non-joiner <ZWNJ>
|
|
|
3228 |
|| ch == "\u200d" // zero-width joiner <ZWJ> (in my ECMA-262 PDF, this is also 200c)
|
|
|
3229 |
;
|
|
|
3230 |
};
|
|
|
3231 |
|
|
|
3232 |
function parse_js_number(num) {
|
|
|
3233 |
if (RE_HEX_NUMBER.test(num)) {
|
|
|
3234 |
return parseInt(num.substr(2), 16);
|
|
|
3235 |
} else if (RE_OCT_NUMBER.test(num)) {
|
|
|
3236 |
return parseInt(num.substr(1), 8);
|
|
|
3237 |
} else if (RE_DEC_NUMBER.test(num)) {
|
|
|
3238 |
return parseFloat(num);
|
|
|
3239 |
}
|
|
|
3240 |
};
|
|
|
3241 |
|
|
|
3242 |
function JS_Parse_Error(message, line, col, pos) {
|
|
|
3243 |
this.message = message;
|
|
|
3244 |
this.line = line;
|
|
|
3245 |
this.col = col;
|
|
|
3246 |
this.pos = pos;
|
|
|
3247 |
this.stack = new Error().stack;
|
|
|
3248 |
};
|
|
|
3249 |
|
|
|
3250 |
JS_Parse_Error.prototype.toString = function() {
|
|
|
3251 |
return this.message + " (line: " + this.line + ", col: " + this.col + ", pos: " + this.pos + ")" + "\n\n" + this.stack;
|
|
|
3252 |
};
|
|
|
3253 |
|
|
|
3254 |
function js_error(message, line, col, pos) {
|
|
|
3255 |
throw new JS_Parse_Error(message, line, col, pos);
|
|
|
3256 |
};
|
|
|
3257 |
|
|
|
3258 |
function is_token(token, type, val) {
|
|
|
3259 |
return token.type == type && (val == null || token.value == val);
|
|
|
3260 |
};
|
|
|
3261 |
|
|
|
3262 |
var EX_EOF = {};
|
|
|
3263 |
|
|
|
3264 |
function tokenizer($TEXT) {
|
|
|
3265 |
|
|
|
3266 |
var S = {
|
|
|
3267 |
text : $TEXT.replace(/\r\n?|[\n\u2028\u2029]/g, "\n").replace(/^\uFEFF/, ''),
|
|
|
3268 |
pos : 0,
|
|
|
3269 |
tokpos : 0,
|
|
|
3270 |
line : 0,
|
|
|
3271 |
tokline : 0,
|
|
|
3272 |
col : 0,
|
|
|
3273 |
tokcol : 0,
|
|
|
3274 |
newline_before : false,
|
|
|
3275 |
regex_allowed : false,
|
|
|
3276 |
comments_before : []
|
|
|
3277 |
};
|
|
|
3278 |
|
|
|
3279 |
function peek() { return S.text.charAt(S.pos); };
|
|
|
3280 |
|
|
|
3281 |
function next(signal_eof) {
|
|
|
3282 |
var ch = S.text.charAt(S.pos++);
|
|
|
3283 |
if (signal_eof && !ch)
|
|
|
3284 |
throw EX_EOF;
|
|
|
3285 |
if (ch == "\n") {
|
|
|
3286 |
S.newline_before = true;
|
|
|
3287 |
++S.line;
|
|
|
3288 |
S.col = 0;
|
|
|
3289 |
} else {
|
|
|
3290 |
++S.col;
|
|
|
3291 |
}
|
|
|
3292 |
return ch;
|
|
|
3293 |
};
|
|
|
3294 |
|
|
|
3295 |
function eof() {
|
|
|
3296 |
return !S.peek();
|
|
|
3297 |
};
|
|
|
3298 |
|
|
|
3299 |
function find(what, signal_eof) {
|
|
|
3300 |
var pos = S.text.indexOf(what, S.pos);
|
|
|
3301 |
if (signal_eof && pos == -1) throw EX_EOF;
|
|
|
3302 |
return pos;
|
|
|
3303 |
};
|
|
|
3304 |
|
|
|
3305 |
function start_token() {
|
|
|
3306 |
S.tokline = S.line;
|
|
|
3307 |
S.tokcol = S.col;
|
|
|
3308 |
S.tokpos = S.pos;
|
|
|
3309 |
};
|
|
|
3310 |
|
|
|
3311 |
function token(type, value, is_comment) {
|
|
|
3312 |
S.regex_allowed = ((type == "operator" && !HOP(UNARY_POSTFIX, value)) ||
|
|
|
3313 |
(type == "keyword" && HOP(KEYWORDS_BEFORE_EXPRESSION, value)) ||
|
|
|
3314 |
(type == "punc" && HOP(PUNC_BEFORE_EXPRESSION, value)));
|
|
|
3315 |
var ret = {
|
|
|
3316 |
type : type,
|
|
|
3317 |
value : value,
|
|
|
3318 |
line : S.tokline,
|
|
|
3319 |
col : S.tokcol,
|
|
|
3320 |
pos : S.tokpos,
|
|
|
3321 |
nlb : S.newline_before
|
|
|
3322 |
};
|
|
|
3323 |
if (!is_comment) {
|
|
|
3324 |
ret.comments_before = S.comments_before;
|
|
|
3325 |
S.comments_before = [];
|
|
|
3326 |
}
|
|
|
3327 |
S.newline_before = false;
|
|
|
3328 |
return ret;
|
|
|
3329 |
};
|
|
|
3330 |
|
|
|
3331 |
function skip_whitespace() {
|
|
|
3332 |
while (HOP(WHITESPACE_CHARS, peek()))
|
|
|
3333 |
next();
|
|
|
3334 |
};
|
|
|
3335 |
|
|
|
3336 |
function read_while(pred) {
|
|
|
3337 |
var ret = "", ch = peek(), i = 0;
|
|
|
3338 |
while (ch && pred(ch, i++)) {
|
|
|
3339 |
ret += next();
|
|
|
3340 |
ch = peek();
|
|
|
3341 |
}
|
|
|
3342 |
return ret;
|
|
|
3343 |
};
|
|
|
3344 |
|
|
|
3345 |
function parse_error(err) {
|
|
|
3346 |
js_error(err, S.tokline, S.tokcol, S.tokpos);
|
|
|
3347 |
};
|
|
|
3348 |
|
|
|
3349 |
function read_num(prefix) {
|
|
|
3350 |
var has_e = false, after_e = false, has_x = false, has_dot = prefix == ".";
|
|
|
3351 |
var num = read_while(function(ch, i){
|
|
|
3352 |
if (ch == "x" || ch == "X") {
|
|
|
3353 |
if (has_x) return false;
|
|
|
3354 |
return has_x = true;
|
|
|
3355 |
}
|
|
|
3356 |
if (!has_x && (ch == "E" || ch == "e")) {
|
|
|
3357 |
if (has_e) return false;
|
|
|
3358 |
return has_e = after_e = true;
|
|
|
3359 |
}
|
|
|
3360 |
if (ch == "-") {
|
|
|
3361 |
if (after_e || (i == 0 && !prefix)) return true;
|
|
|
3362 |
return false;
|
|
|
3363 |
}
|
|
|
3364 |
if (ch == "+") return after_e;
|
|
|
3365 |
after_e = false;
|
|
|
3366 |
if (ch == ".") {
|
|
|
3367 |
if (!has_dot && !has_x)
|
|
|
3368 |
return has_dot = true;
|
|
|
3369 |
return false;
|
|
|
3370 |
}
|
|
|
3371 |
return is_alphanumeric_char(ch);
|
|
|
3372 |
});
|
|
|
3373 |
if (prefix)
|
|
|
3374 |
num = prefix + num;
|
|
|
3375 |
var valid = parse_js_number(num);
|
|
|
3376 |
if (!isNaN(valid)) {
|
|
|
3377 |
return token("num", valid);
|
|
|
3378 |
} else {
|
|
|
3379 |
parse_error("Invalid syntax: " + num);
|
|
|
3380 |
}
|
|
|
3381 |
};
|
|
|
3382 |
|
|
|
3383 |
function read_escaped_char() {
|
|
|
3384 |
var ch = next(true);
|
|
|
3385 |
switch (ch) {
|
|
|
3386 |
case "n" : return "\n";
|
|
|
3387 |
case "r" : return "\r";
|
|
|
3388 |
case "t" : return "\t";
|
|
|
3389 |
case "b" : return "\b";
|
|
|
3390 |
case "v" : return "\u000b";
|
|
|
3391 |
case "f" : return "\f";
|
|
|
3392 |
case "0" : return "\0";
|
|
|
3393 |
case "x" : return String.fromCharCode(hex_bytes(2));
|
|
|
3394 |
case "u" : return String.fromCharCode(hex_bytes(4));
|
|
|
3395 |
case "\n": return "";
|
|
|
3396 |
default : return ch;
|
|
|
3397 |
}
|
|
|
3398 |
};
|
|
|
3399 |
|
|
|
3400 |
function hex_bytes(n) {
|
|
|
3401 |
var num = 0;
|
|
|
3402 |
for (; n > 0; --n) {
|
|
|
3403 |
var digit = parseInt(next(true), 16);
|
|
|
3404 |
if (isNaN(digit))
|
|
|
3405 |
parse_error("Invalid hex-character pattern in string");
|
|
|
3406 |
num = (num << 4) | digit;
|
|
|
3407 |
}
|
|
|
3408 |
return num;
|
|
|
3409 |
};
|
|
|
3410 |
|
|
|
3411 |
function read_string() {
|
|
|
3412 |
return with_eof_error("Unterminated string constant", function(){
|
|
|
3413 |
var quote = next(), ret = "";
|
|
|
3414 |
for (;;) {
|
|
|
3415 |
var ch = next(true);
|
|
|
3416 |
if (ch == "\\") {
|
|
|
3417 |
// read OctalEscapeSequence (XXX: deprecated if "strict mode")
|
|
|
3418 |
// https://github.com/mishoo/UglifyJS/issues/178
|
|
|
3419 |
var octal_len = 0, first = null;
|
|
|
3420 |
ch = read_while(function(ch){
|
|
|
3421 |
if (ch >= "0" && ch <= "7") {
|
|
|
3422 |
if (!first) {
|
|
|
3423 |
first = ch;
|
|
|
3424 |
return ++octal_len;
|
|
|
3425 |
}
|
|
|
3426 |
else if (first <= "3" && octal_len <= 2) return ++octal_len;
|
|
|
3427 |
else if (first >= "4" && octal_len <= 1) return ++octal_len;
|
|
|
3428 |
}
|
|
|
3429 |
return false;
|
|
|
3430 |
});
|
|
|
3431 |
if (octal_len > 0) ch = String.fromCharCode(parseInt(ch, 8));
|
|
|
3432 |
else ch = read_escaped_char();
|
|
|
3433 |
}
|
|
|
3434 |
else if (ch == quote) break;
|
|
|
3435 |
ret += ch;
|
|
|
3436 |
}
|
|
|
3437 |
return token("string", ret);
|
|
|
3438 |
});
|
|
|
3439 |
};
|
|
|
3440 |
|
|
|
3441 |
function read_line_comment() {
|
|
|
3442 |
next();
|
|
|
3443 |
var i = find("\n"), ret;
|
|
|
3444 |
if (i == -1) {
|
|
|
3445 |
ret = S.text.substr(S.pos);
|
|
|
3446 |
S.pos = S.text.length;
|
|
|
3447 |
} else {
|
|
|
3448 |
ret = S.text.substring(S.pos, i);
|
|
|
3449 |
S.pos = i;
|
|
|
3450 |
}
|
|
|
3451 |
return token("comment1", ret, true);
|
|
|
3452 |
};
|
|
|
3453 |
|
|
|
3454 |
function read_multiline_comment() {
|
|
|
3455 |
next();
|
|
|
3456 |
return with_eof_error("Unterminated multiline comment", function(){
|
|
|
3457 |
var i = find("*/", true),
|
|
|
3458 |
text = S.text.substring(S.pos, i),
|
|
|
3459 |
tok = token("comment2", text, true);
|
|
|
3460 |
S.pos = i + 2;
|
|
|
3461 |
S.line += text.split("\n").length - 1;
|
|
|
3462 |
S.newline_before = text.indexOf("\n") >= 0;
|
|
|
3463 |
|
|
|
3464 |
// https://github.com/mishoo/UglifyJS/issues/#issue/100
|
|
|
3465 |
if (/^@cc_on/i.test(text)) {
|
|
|
3466 |
warn("WARNING: at line " + S.line);
|
|
|
3467 |
warn("*** Found \"conditional comment\": " + text);
|
|
|
3468 |
warn("*** UglifyJS DISCARDS ALL COMMENTS. This means your code might no longer work properly in Internet Explorer.");
|
|
|
3469 |
}
|
|
|
3470 |
|
|
|
3471 |
return tok;
|
|
|
3472 |
});
|
|
|
3473 |
};
|
|
|
3474 |
|
|
|
3475 |
function read_name() {
|
|
|
3476 |
var backslash = false, name = "", ch;
|
|
|
3477 |
while ((ch = peek()) != null) {
|
|
|
3478 |
if (!backslash) {
|
|
|
3479 |
if (ch == "\\") backslash = true, next();
|
|
|
3480 |
else if (is_identifier_char(ch)) name += next();
|
|
|
3481 |
else break;
|
|
|
3482 |
}
|
|
|
3483 |
else {
|
|
|
3484 |
if (ch != "u") parse_error("Expecting UnicodeEscapeSequence -- uXXXX");
|
|
|
3485 |
ch = read_escaped_char();
|
|
|
3486 |
if (!is_identifier_char(ch)) parse_error("Unicode char: " + ch.charCodeAt(0) + " is not valid in identifier");
|
|
|
3487 |
name += ch;
|
|
|
3488 |
backslash = false;
|
|
|
3489 |
}
|
|
|
3490 |
}
|
|
|
3491 |
return name;
|
|
|
3492 |
};
|
|
|
3493 |
|
|
|
3494 |
function read_regexp(regexp) {
|
|
|
3495 |
return with_eof_error("Unterminated regular expression", function(){
|
|
|
3496 |
var prev_backslash = false, ch, in_class = false;
|
|
|
3497 |
while ((ch = next(true))) if (prev_backslash) {
|
|
|
3498 |
regexp += "\\" + ch;
|
|
|
3499 |
prev_backslash = false;
|
|
|
3500 |
} else if (ch == "[") {
|
|
|
3501 |
in_class = true;
|
|
|
3502 |
regexp += ch;
|
|
|
3503 |
} else if (ch == "]" && in_class) {
|
|
|
3504 |
in_class = false;
|
|
|
3505 |
regexp += ch;
|
|
|
3506 |
} else if (ch == "/" && !in_class) {
|
|
|
3507 |
break;
|
|
|
3508 |
} else if (ch == "\\") {
|
|
|
3509 |
prev_backslash = true;
|
|
|
3510 |
} else {
|
|
|
3511 |
regexp += ch;
|
|
|
3512 |
}
|
|
|
3513 |
var mods = read_name();
|
|
|
3514 |
return token("regexp", [ regexp, mods ]);
|
|
|
3515 |
});
|
|
|
3516 |
};
|
|
|
3517 |
|
|
|
3518 |
function read_operator(prefix) {
|
|
|
3519 |
function grow(op) {
|
|
|
3520 |
if (!peek()) return op;
|
|
|
3521 |
var bigger = op + peek();
|
|
|
3522 |
if (HOP(OPERATORS, bigger)) {
|
|
|
3523 |
next();
|
|
|
3524 |
return grow(bigger);
|
|
|
3525 |
} else {
|
|
|
3526 |
return op;
|
|
|
3527 |
}
|
|
|
3528 |
};
|
|
|
3529 |
return token("operator", grow(prefix || next()));
|
|
|
3530 |
};
|
|
|
3531 |
|
|
|
3532 |
function handle_slash() {
|
|
|
3533 |
next();
|
|
|
3534 |
var regex_allowed = S.regex_allowed;
|
|
|
3535 |
switch (peek()) {
|
|
|
3536 |
case "/":
|
|
|
3537 |
S.comments_before.push(read_line_comment());
|
|
|
3538 |
S.regex_allowed = regex_allowed;
|
|
|
3539 |
return next_token();
|
|
|
3540 |
case "*":
|
|
|
3541 |
S.comments_before.push(read_multiline_comment());
|
|
|
3542 |
S.regex_allowed = regex_allowed;
|
|
|
3543 |
return next_token();
|
|
|
3544 |
}
|
|
|
3545 |
return S.regex_allowed ? read_regexp("") : read_operator("/");
|
|
|
3546 |
};
|
|
|
3547 |
|
|
|
3548 |
function handle_dot() {
|
|
|
3549 |
next();
|
|
|
3550 |
return is_digit(peek())
|
|
|
3551 |
? read_num(".")
|
|
|
3552 |
: token("punc", ".");
|
|
|
3553 |
};
|
|
|
3554 |
|
|
|
3555 |
function read_word() {
|
|
|
3556 |
var word = read_name();
|
|
|
3557 |
return !HOP(KEYWORDS, word)
|
|
|
3558 |
? token("name", word)
|
|
|
3559 |
: HOP(OPERATORS, word)
|
|
|
3560 |
? token("operator", word)
|
|
|
3561 |
: HOP(KEYWORDS_ATOM, word)
|
|
|
3562 |
? token("atom", word)
|
|
|
3563 |
: token("keyword", word);
|
|
|
3564 |
};
|
|
|
3565 |
|
|
|
3566 |
function with_eof_error(eof_error, cont) {
|
|
|
3567 |
try {
|
|
|
3568 |
return cont();
|
|
|
3569 |
} catch(ex) {
|
|
|
3570 |
if (ex === EX_EOF) parse_error(eof_error);
|
|
|
3571 |
else throw ex;
|
|
|
3572 |
}
|
|
|
3573 |
};
|
|
|
3574 |
|
|
|
3575 |
function next_token(force_regexp) {
|
|
|
3576 |
if (force_regexp != null)
|
|
|
3577 |
return read_regexp(force_regexp);
|
|
|
3578 |
skip_whitespace();
|
|
|
3579 |
start_token();
|
|
|
3580 |
var ch = peek();
|
|
|
3581 |
if (!ch) return token("eof");
|
|
|
3582 |
if (is_digit(ch)) return read_num();
|
|
|
3583 |
if (ch == '"' || ch == "'") return read_string();
|
|
|
3584 |
if (HOP(PUNC_CHARS, ch)) return token("punc", next());
|
|
|
3585 |
if (ch == ".") return handle_dot();
|
|
|
3586 |
if (ch == "/") return handle_slash();
|
|
|
3587 |
if (HOP(OPERATOR_CHARS, ch)) return read_operator();
|
|
|
3588 |
if (ch == "\\" || is_identifier_start(ch)) return read_word();
|
|
|
3589 |
parse_error("Unexpected character '" + ch + "'");
|
|
|
3590 |
};
|
|
|
3591 |
|
|
|
3592 |
next_token.context = function(nc) {
|
|
|
3593 |
if (nc) S = nc;
|
|
|
3594 |
return S;
|
|
|
3595 |
};
|
|
|
3596 |
|
|
|
3597 |
return next_token;
|
|
|
3598 |
|
|
|
3599 |
};
|
|
|
3600 |
|
|
|
3601 |
/* -----[ Parser (constants) ]----- */
|
|
|
3602 |
|
|
|
3603 |
var UNARY_PREFIX = array_to_hash([
|
|
|
3604 |
"typeof",
|
|
|
3605 |
"void",
|
|
|
3606 |
"delete",
|
|
|
3607 |
"--",
|
|
|
3608 |
"++",
|
|
|
3609 |
"!",
|
|
|
3610 |
"~",
|
|
|
3611 |
"-",
|
|
|
3612 |
"+"
|
|
|
3613 |
]);
|
|
|
3614 |
|
|
|
3615 |
var UNARY_POSTFIX = array_to_hash([ "--", "++" ]);
|
|
|
3616 |
|
|
|
3617 |
var ASSIGNMENT = (function(a, ret, i){
|
|
|
3618 |
while (i < a.length) {
|
|
|
3619 |
ret[a[i]] = a[i].substr(0, a[i].length - 1);
|
|
|
3620 |
i++;
|
|
|
3621 |
}
|
|
|
3622 |
return ret;
|
|
|
3623 |
})(
|
|
|
3624 |
["+=", "-=", "/=", "*=", "%=", ">>=", "<<=", ">>>=", "|=", "^=", "&="],
|
|
|
3625 |
{ "=": true },
|
|
|
3626 |
0
|
|
|
3627 |
);
|
|
|
3628 |
|
|
|
3629 |
var PRECEDENCE = (function(a, ret){
|
|
|
3630 |
for (var i = 0, n = 1; i < a.length; ++i, ++n) {
|
|
|
3631 |
var b = a[i];
|
|
|
3632 |
for (var j = 0; j < b.length; ++j) {
|
|
|
3633 |
ret[b[j]] = n;
|
|
|
3634 |
}
|
|
|
3635 |
}
|
|
|
3636 |
return ret;
|
|
|
3637 |
})(
|
|
|
3638 |
[
|
|
|
3639 |
["||"],
|
|
|
3640 |
["&&"],
|
|
|
3641 |
["|"],
|
|
|
3642 |
["^"],
|
|
|
3643 |
["&"],
|
|
|
3644 |
["==", "===", "!=", "!=="],
|
|
|
3645 |
["<", ">", "<=", ">=", "in", "instanceof"],
|
|
|
3646 |
[">>", "<<", ">>>"],
|
|
|
3647 |
["+", "-"],
|
|
|
3648 |
["*", "/", "%"]
|
|
|
3649 |
],
|
|
|
3650 |
{}
|
|
|
3651 |
);
|
|
|
3652 |
|
|
|
3653 |
var STATEMENTS_WITH_LABELS = array_to_hash([ "for", "do", "while", "switch" ]);
|
|
|
3654 |
|
|
|
3655 |
var ATOMIC_START_TOKEN = array_to_hash([ "atom", "num", "string", "regexp", "name" ]);
|
|
|
3656 |
|
|
|
3657 |
/* -----[ Parser ]----- */
|
|
|
3658 |
|
|
|
3659 |
function NodeWithToken(str, start, end) {
|
|
|
3660 |
this.name = str;
|
|
|
3661 |
this.start = start;
|
|
|
3662 |
this.end = end;
|
|
|
3663 |
};
|
|
|
3664 |
|
|
|
3665 |
NodeWithToken.prototype.toString = function() { return this.name; };
|
|
|
3666 |
|
|
|
3667 |
function parse($TEXT, exigent_mode, embed_tokens) {
|
|
|
3668 |
|
|
|
3669 |
var S = {
|
|
|
3670 |
input : typeof $TEXT == "string" ? tokenizer($TEXT, true) : $TEXT,
|
|
|
3671 |
token : null,
|
|
|
3672 |
prev : null,
|
|
|
3673 |
peeked : null,
|
|
|
3674 |
in_function : 0,
|
|
|
3675 |
in_loop : 0,
|
|
|
3676 |
labels : []
|
|
|
3677 |
};
|
|
|
3678 |
|
|
|
3679 |
S.token = next();
|
|
|
3680 |
|
|
|
3681 |
function is(type, value) {
|
|
|
3682 |
return is_token(S.token, type, value);
|
|
|
3683 |
};
|
|
|
3684 |
|
|
|
3685 |
function peek() { return S.peeked || (S.peeked = S.input()); };
|
|
|
3686 |
|
|
|
3687 |
function next() {
|
|
|
3688 |
S.prev = S.token;
|
|
|
3689 |
if (S.peeked) {
|
|
|
3690 |
S.token = S.peeked;
|
|
|
3691 |
S.peeked = null;
|
|
|
3692 |
} else {
|
|
|
3693 |
S.token = S.input();
|
|
|
3694 |
}
|
|
|
3695 |
return S.token;
|
|
|
3696 |
};
|
|
|
3697 |
|
|
|
3698 |
function prev() {
|
|
|
3699 |
return S.prev;
|
|
|
3700 |
};
|
|
|
3701 |
|
|
|
3702 |
function croak(msg, line, col, pos) {
|
|
|
3703 |
var ctx = S.input.context();
|
|
|
3704 |
js_error(msg,
|
|
|
3705 |
line != null ? line : ctx.tokline,
|
|
|
3706 |
col != null ? col : ctx.tokcol,
|
|
|
3707 |
pos != null ? pos : ctx.tokpos);
|
|
|
3708 |
};
|
|
|
3709 |
|
|
|
3710 |
function token_error(token, msg) {
|
|
|
3711 |
croak(msg, token.line, token.col);
|
|
|
3712 |
};
|
|
|
3713 |
|
|
|
3714 |
function unexpected(token) {
|
|
|
3715 |
if (token == null)
|
|
|
3716 |
token = S.token;
|
|
|
3717 |
token_error(token, "Unexpected token: " + token.type + " (" + token.value + ")");
|
|
|
3718 |
};
|
|
|
3719 |
|
|
|
3720 |
function expect_token(type, val) {
|
|
|
3721 |
if (is(type, val)) {
|
|
|
3722 |
return next();
|
|
|
3723 |
}
|
|
|
3724 |
token_error(S.token, "Unexpected token " + S.token.type + ", expected " + type);
|
|
|
3725 |
};
|
|
|
3726 |
|
|
|
3727 |
function expect(punc) { return expect_token("punc", punc); };
|
|
|
3728 |
|
|
|
3729 |
function can_insert_semicolon() {
|
|
|
3730 |
return !exigent_mode && (
|
|
|
3731 |
S.token.nlb || is("eof") || is("punc", "}")
|
|
|
3732 |
);
|
|
|
3733 |
};
|
|
|
3734 |
|
|
|
3735 |
function semicolon() {
|
|
|
3736 |
if (is("punc", ";")) next();
|
|
|
3737 |
else if (!can_insert_semicolon()) unexpected();
|
|
|
3738 |
};
|
|
|
3739 |
|
|
|
3740 |
function as() {
|
|
|
3741 |
return slice(arguments);
|
|
|
3742 |
};
|
|
|
3743 |
|
|
|
3744 |
function parenthesised() {
|
|
|
3745 |
expect("(");
|
|
|
3746 |
var ex = expression();
|
|
|
3747 |
expect(")");
|
|
|
3748 |
return ex;
|
|
|
3749 |
};
|
|
|
3750 |
|
|
|
3751 |
function add_tokens(str, start, end) {
|
|
|
3752 |
return str instanceof NodeWithToken ? str : new NodeWithToken(str, start, end);
|
|
|
3753 |
};
|
|
|
3754 |
|
|
|
3755 |
function maybe_embed_tokens(parser) {
|
|
|
3756 |
if (embed_tokens) return function() {
|
|
|
3757 |
var start = S.token;
|
|
|
3758 |
var ast = parser.apply(this, arguments);
|
|
|
3759 |
ast[0] = add_tokens(ast[0], start, prev());
|
|
|
3760 |
return ast;
|
|
|
3761 |
};
|
|
|
3762 |
else return parser;
|
|
|
3763 |
};
|
|
|
3764 |
|
|
|
3765 |
var statement = maybe_embed_tokens(function() {
|
|
|
3766 |
if (is("operator", "/") || is("operator", "/=")) {
|
|
|
3767 |
S.peeked = null;
|
|
|
3768 |
S.token = S.input(S.token.value.substr(1)); // force regexp
|
|
|
3769 |
}
|
|
|
3770 |
switch (S.token.type) {
|
|
|
3771 |
case "num":
|
|
|
3772 |
case "string":
|
|
|
3773 |
case "regexp":
|
|
|
3774 |
case "operator":
|
|
|
3775 |
case "atom":
|
|
|
3776 |
return simple_statement();
|
|
|
3777 |
|
|
|
3778 |
case "name":
|
|
|
3779 |
return is_token(peek(), "punc", ":")
|
|
|
3780 |
? labeled_statement(prog1(S.token.value, next, next))
|
|
|
3781 |
: simple_statement();
|
|
|
3782 |
|
|
|
3783 |
case "punc":
|
|
|
3784 |
switch (S.token.value) {
|
|
|
3785 |
case "{":
|
|
|
3786 |
return as("block", block_());
|
|
|
3787 |
case "[":
|
|
|
3788 |
case "(":
|
|
|
3789 |
return simple_statement();
|
|
|
3790 |
case ";":
|
|
|
3791 |
next();
|
|
|
3792 |
return as("block");
|
|
|
3793 |
default:
|
|
|
3794 |
unexpected();
|
|
|
3795 |
}
|
|
|
3796 |
|
|
|
3797 |
case "keyword":
|
|
|
3798 |
switch (prog1(S.token.value, next)) {
|
|
|
3799 |
case "break":
|
|
|
3800 |
return break_cont("break");
|
|
|
3801 |
|
|
|
3802 |
case "continue":
|
|
|
3803 |
return break_cont("continue");
|
|
|
3804 |
|
|
|
3805 |
case "debugger":
|
|
|
3806 |
semicolon();
|
|
|
3807 |
return as("debugger");
|
|
|
3808 |
|
|
|
3809 |
case "do":
|
|
|
3810 |
return (function(body){
|
|
|
3811 |
expect_token("keyword", "while");
|
|
|
3812 |
return as("do", prog1(parenthesised, semicolon), body);
|
|
|
3813 |
})(in_loop(statement));
|
|
|
3814 |
|
|
|
3815 |
case "for":
|
|
|
3816 |
return for_();
|
|
|
3817 |
|
|
|
3818 |
case "function":
|
|
|
3819 |
return function_(true);
|
|
|
3820 |
|
|
|
3821 |
case "if":
|
|
|
3822 |
return if_();
|
|
|
3823 |
|
|
|
3824 |
case "return":
|
|
|
3825 |
if (S.in_function == 0)
|
|
|
3826 |
croak("'return' outside of function");
|
|
|
3827 |
return as("return",
|
|
|
3828 |
is("punc", ";")
|
|
|
3829 |
? (next(), null)
|
|
|
3830 |
: can_insert_semicolon()
|
|
|
3831 |
? null
|
|
|
3832 |
: prog1(expression, semicolon));
|
|
|
3833 |
|
|
|
3834 |
case "switch":
|
|
|
3835 |
return as("switch", parenthesised(), switch_block_());
|
|
|
3836 |
|
|
|
3837 |
case "throw":
|
|
|
3838 |
if (S.token.nlb)
|
|
|
3839 |
croak("Illegal newline after 'throw'");
|
|
|
3840 |
return as("throw", prog1(expression, semicolon));
|
|
|
3841 |
|
|
|
3842 |
case "try":
|
|
|
3843 |
return try_();
|
|
|
3844 |
|
|
|
3845 |
case "var":
|
|
|
3846 |
return prog1(var_, semicolon);
|
|
|
3847 |
|
|
|
3848 |
case "const":
|
|
|
3849 |
return prog1(const_, semicolon);
|
|
|
3850 |
|
|
|
3851 |
case "while":
|
|
|
3852 |
return as("while", parenthesised(), in_loop(statement));
|
|
|
3853 |
|
|
|
3854 |
case "with":
|
|
|
3855 |
return as("with", parenthesised(), statement());
|
|
|
3856 |
|
|
|
3857 |
default:
|
|
|
3858 |
unexpected();
|
|
|
3859 |
}
|
|
|
3860 |
}
|
|
|
3861 |
});
|
|
|
3862 |
|
|
|
3863 |
function labeled_statement(label) {
|
|
|
3864 |
S.labels.push(label);
|
|
|
3865 |
var start = S.token, stat = statement();
|
|
|
3866 |
if (exigent_mode && !HOP(STATEMENTS_WITH_LABELS, stat[0]))
|
|
|
3867 |
unexpected(start);
|
|
|
3868 |
S.labels.pop();
|
|
|
3869 |
return as("label", label, stat);
|
|
|
3870 |
};
|
|
|
3871 |
|
|
|
3872 |
function simple_statement() {
|
|
|
3873 |
return as("stat", prog1(expression, semicolon));
|
|
|
3874 |
};
|
|
|
3875 |
|
|
|
3876 |
function break_cont(type) {
|
|
|
3877 |
var name;
|
|
|
3878 |
if (!can_insert_semicolon()) {
|
|
|
3879 |
name = is("name") ? S.token.value : null;
|
|
|
3880 |
}
|
|
|
3881 |
if (name != null) {
|
|
|
3882 |
next();
|
|
|
3883 |
if (!member(name, S.labels))
|
|
|
3884 |
croak("Label " + name + " without matching loop or statement");
|
|
|
3885 |
}
|
|
|
3886 |
else if (S.in_loop == 0)
|
|
|
3887 |
croak(type + " not inside a loop or switch");
|
|
|
3888 |
semicolon();
|
|
|
3889 |
return as(type, name);
|
|
|
3890 |
};
|
|
|
3891 |
|
|
|
3892 |
function for_() {
|
|
|
3893 |
expect("(");
|
|
|
3894 |
var init = null;
|
|
|
3895 |
if (!is("punc", ";")) {
|
|
|
3896 |
init = is("keyword", "var")
|
|
|
3897 |
? (next(), var_(true))
|
|
|
3898 |
: expression(true, true);
|
|
|
3899 |
if (is("operator", "in"))
|
|
|
3900 |
return for_in(init);
|
|
|
3901 |
}
|
|
|
3902 |
return regular_for(init);
|
|
|
3903 |
};
|
|
|
3904 |
|
|
|
3905 |
function regular_for(init) {
|
|
|
3906 |
expect(";");
|
|
|
3907 |
var test = is("punc", ";") ? null : expression();
|
|
|
3908 |
expect(";");
|
|
|
3909 |
var step = is("punc", ")") ? null : expression();
|
|
|
3910 |
expect(")");
|
|
|
3911 |
return as("for", init, test, step, in_loop(statement));
|
|
|
3912 |
};
|
|
|
3913 |
|
|
|
3914 |
function for_in(init) {
|
|
|
3915 |
var lhs = init[0] == "var" ? as("name", init[1][0]) : init;
|
|
|
3916 |
next();
|
|
|
3917 |
var obj = expression();
|
|
|
3918 |
expect(")");
|
|
|
3919 |
return as("for-in", init, lhs, obj, in_loop(statement));
|
|
|
3920 |
};
|
|
|
3921 |
|
|
|
3922 |
var function_ = maybe_embed_tokens(function(in_statement) {
|
|
|
3923 |
var name = is("name") ? prog1(S.token.value, next) : null;
|
|
|
3924 |
if (in_statement && !name)
|
|
|
3925 |
unexpected();
|
|
|
3926 |
expect("(");
|
|
|
3927 |
return as(in_statement ? "defun" : "function",
|
|
|
3928 |
name,
|
|
|
3929 |
// arguments
|
|
|
3930 |
(function(first, a){
|
|
|
3931 |
while (!is("punc", ")")) {
|
|
|
3932 |
if (first) first = false; else expect(",");
|
|
|
3933 |
if (!is("name")) unexpected();
|
|
|
3934 |
a.push(S.token.value);
|
|
|
3935 |
next();
|
|
|
3936 |
}
|
|
|
3937 |
next();
|
|
|
3938 |
return a;
|
|
|
3939 |
})(true, []),
|
|
|
3940 |
// body
|
|
|
3941 |
(function(){
|
|
|
3942 |
++S.in_function;
|
|
|
3943 |
var loop = S.in_loop;
|
|
|
3944 |
S.in_loop = 0;
|
|
|
3945 |
var a = block_();
|
|
|
3946 |
--S.in_function;
|
|
|
3947 |
S.in_loop = loop;
|
|
|
3948 |
return a;
|
|
|
3949 |
})());
|
|
|
3950 |
});
|
|
|
3951 |
|
|
|
3952 |
function if_() {
|
|
|
3953 |
var cond = parenthesised(), body = statement(), belse;
|
|
|
3954 |
if (is("keyword", "else")) {
|
|
|
3955 |
next();
|
|
|
3956 |
belse = statement();
|
|
|
3957 |
}
|
|
|
3958 |
return as("if", cond, body, belse);
|
|
|
3959 |
};
|
|
|
3960 |
|
|
|
3961 |
function block_() {
|
|
|
3962 |
expect("{");
|
|
|
3963 |
var a = [];
|
|
|
3964 |
while (!is("punc", "}")) {
|
|
|
3965 |
if (is("eof")) unexpected();
|
|
|
3966 |
a.push(statement());
|
|
|
3967 |
}
|
|
|
3968 |
next();
|
|
|
3969 |
return a;
|
|
|
3970 |
};
|
|
|
3971 |
|
|
|
3972 |
var switch_block_ = curry(in_loop, function(){
|
|
|
3973 |
expect("{");
|
|
|
3974 |
var a = [], cur = null;
|
|
|
3975 |
while (!is("punc", "}")) {
|
|
|
3976 |
if (is("eof")) unexpected();
|
|
|
3977 |
if (is("keyword", "case")) {
|
|
|
3978 |
next();
|
|
|
3979 |
cur = [];
|
|
|
3980 |
a.push([ expression(), cur ]);
|
|
|
3981 |
expect(":");
|
|
|
3982 |
}
|
|
|
3983 |
else if (is("keyword", "default")) {
|
|
|
3984 |
next();
|
|
|
3985 |
expect(":");
|
|
|
3986 |
cur = [];
|
|
|
3987 |
a.push([ null, cur ]);
|
|
|
3988 |
}
|
|
|
3989 |
else {
|
|
|
3990 |
if (!cur) unexpected();
|
|
|
3991 |
cur.push(statement());
|
|
|
3992 |
}
|
|
|
3993 |
}
|
|
|
3994 |
next();
|
|
|
3995 |
return a;
|
|
|
3996 |
});
|
|
|
3997 |
|
|
|
3998 |
function try_() {
|
|
|
3999 |
var body = block_(), bcatch, bfinally;
|
|
|
4000 |
if (is("keyword", "catch")) {
|
|
|
4001 |
next();
|
|
|
4002 |
expect("(");
|
|
|
4003 |
if (!is("name"))
|
|
|
4004 |
croak("Name expected");
|
|
|
4005 |
var name = S.token.value;
|
|
|
4006 |
next();
|
|
|
4007 |
expect(")");
|
|
|
4008 |
bcatch = [ name, block_() ];
|
|
|
4009 |
}
|
|
|
4010 |
if (is("keyword", "finally")) {
|
|
|
4011 |
next();
|
|
|
4012 |
bfinally = block_();
|
|
|
4013 |
}
|
|
|
4014 |
if (!bcatch && !bfinally)
|
|
|
4015 |
croak("Missing catch/finally blocks");
|
|
|
4016 |
return as("try", body, bcatch, bfinally);
|
|
|
4017 |
};
|
|
|
4018 |
|
|
|
4019 |
function vardefs(no_in) {
|
|
|
4020 |
var a = [];
|
|
|
4021 |
for (;;) {
|
|
|
4022 |
if (!is("name"))
|
|
|
4023 |
unexpected();
|
|
|
4024 |
var name = S.token.value;
|
|
|
4025 |
next();
|
|
|
4026 |
if (is("operator", "=")) {
|
|
|
4027 |
next();
|
|
|
4028 |
a.push([ name, expression(false, no_in) ]);
|
|
|
4029 |
} else {
|
|
|
4030 |
a.push([ name ]);
|
|
|
4031 |
}
|
|
|
4032 |
if (!is("punc", ","))
|
|
|
4033 |
break;
|
|
|
4034 |
next();
|
|
|
4035 |
}
|
|
|
4036 |
return a;
|
|
|
4037 |
};
|
|
|
4038 |
|
|
|
4039 |
function var_(no_in) {
|
|
|
4040 |
return as("var", vardefs(no_in));
|
|
|
4041 |
};
|
|
|
4042 |
|
|
|
4043 |
function const_() {
|
|
|
4044 |
return as("const", vardefs());
|
|
|
4045 |
};
|
|
|
4046 |
|
|
|
4047 |
function new_() {
|
|
|
4048 |
var newexp = expr_atom(false), args;
|
|
|
4049 |
if (is("punc", "(")) {
|
|
|
4050 |
next();
|
|
|
4051 |
args = expr_list(")");
|
|
|
4052 |
} else {
|
|
|
4053 |
args = [];
|
|
|
4054 |
}
|
|
|
4055 |
return subscripts(as("new", newexp, args), true);
|
|
|
4056 |
};
|
|
|
4057 |
|
|
|
4058 |
var expr_atom = maybe_embed_tokens(function(allow_calls) {
|
|
|
4059 |
if (is("operator", "new")) {
|
|
|
4060 |
next();
|
|
|
4061 |
return new_();
|
|
|
4062 |
}
|
|
|
4063 |
if (is("punc")) {
|
|
|
4064 |
switch (S.token.value) {
|
|
|
4065 |
case "(":
|
|
|
4066 |
next();
|
|
|
4067 |
return subscripts(prog1(expression, curry(expect, ")")), allow_calls);
|
|
|
4068 |
case "[":
|
|
|
4069 |
next();
|
|
|
4070 |
return subscripts(array_(), allow_calls);
|
|
|
4071 |
case "{":
|
|
|
4072 |
next();
|
|
|
4073 |
return subscripts(object_(), allow_calls);
|
|
|
4074 |
}
|
|
|
4075 |
unexpected();
|
|
|
4076 |
}
|
|
|
4077 |
if (is("keyword", "function")) {
|
|
|
4078 |
next();
|
|
|
4079 |
return subscripts(function_(false), allow_calls);
|
|
|
4080 |
}
|
|
|
4081 |
if (HOP(ATOMIC_START_TOKEN, S.token.type)) {
|
|
|
4082 |
var atom = S.token.type == "regexp"
|
|
|
4083 |
? as("regexp", S.token.value[0], S.token.value[1])
|
|
|
4084 |
: as(S.token.type, S.token.value);
|
|
|
4085 |
return subscripts(prog1(atom, next), allow_calls);
|
|
|
4086 |
}
|
|
|
4087 |
unexpected();
|
|
|
4088 |
});
|
|
|
4089 |
|
|
|
4090 |
function expr_list(closing, allow_trailing_comma, allow_empty) {
|
|
|
4091 |
var first = true, a = [];
|
|
|
4092 |
while (!is("punc", closing)) {
|
|
|
4093 |
if (first) first = false; else expect(",");
|
|
|
4094 |
if (allow_trailing_comma && is("punc", closing)) break;
|
|
|
4095 |
if (is("punc", ",") && allow_empty) {
|
|
|
4096 |
a.push([ "atom", "undefined" ]);
|
|
|
4097 |
} else {
|
|
|
4098 |
a.push(expression(false));
|
|
|
4099 |
}
|
|
|
4100 |
}
|
|
|
4101 |
next();
|
|
|
4102 |
return a;
|
|
|
4103 |
};
|
|
|
4104 |
|
|
|
4105 |
function array_() {
|
|
|
4106 |
return as("array", expr_list("]", !exigent_mode, true));
|
|
|
4107 |
};
|
|
|
4108 |
|
|
|
4109 |
function object_() {
|
|
|
4110 |
var first = true, a = [];
|
|
|
4111 |
while (!is("punc", "}")) {
|
|
|
4112 |
if (first) first = false; else expect(",");
|
|
|
4113 |
if (!exigent_mode && is("punc", "}"))
|
|
|
4114 |
// allow trailing comma
|
|
|
4115 |
break;
|
|
|
4116 |
var type = S.token.type;
|
|
|
4117 |
var name = as_property_name();
|
|
|
4118 |
if (type == "name" && (name == "get" || name == "set") && !is("punc", ":")) {
|
|
|
4119 |
a.push([ as_name(), function_(false), name ]);
|
|
|
4120 |
} else {
|
|
|
4121 |
expect(":");
|
|
|
4122 |
a.push([ name, expression(false) ]);
|
|
|
4123 |
}
|
|
|
4124 |
}
|
|
|
4125 |
next();
|
|
|
4126 |
return as("object", a);
|
|
|
4127 |
};
|
|
|
4128 |
|
|
|
4129 |
function as_property_name() {
|
|
|
4130 |
switch (S.token.type) {
|
|
|
4131 |
case "num":
|
|
|
4132 |
case "string":
|
|
|
4133 |
return prog1(S.token.value, next);
|
|
|
4134 |
}
|
|
|
4135 |
return as_name();
|
|
|
4136 |
};
|
|
|
4137 |
|
|
|
4138 |
function as_name() {
|
|
|
4139 |
switch (S.token.type) {
|
|
|
4140 |
case "name":
|
|
|
4141 |
case "operator":
|
|
|
4142 |
case "keyword":
|
|
|
4143 |
case "atom":
|
|
|
4144 |
return prog1(S.token.value, next);
|
|
|
4145 |
default:
|
|
|
4146 |
unexpected();
|
|
|
4147 |
}
|
|
|
4148 |
};
|
|
|
4149 |
|
|
|
4150 |
function subscripts(expr, allow_calls) {
|
|
|
4151 |
if (is("punc", ".")) {
|
|
|
4152 |
next();
|
|
|
4153 |
return subscripts(as("dot", expr, as_name()), allow_calls);
|
|
|
4154 |
}
|
|
|
4155 |
if (is("punc", "[")) {
|
|
|
4156 |
next();
|
|
|
4157 |
return subscripts(as("sub", expr, prog1(expression, curry(expect, "]"))), allow_calls);
|
|
|
4158 |
}
|
|
|
4159 |
if (allow_calls && is("punc", "(")) {
|
|
|
4160 |
next();
|
|
|
4161 |
return subscripts(as("call", expr, expr_list(")")), true);
|
|
|
4162 |
}
|
|
|
4163 |
return expr;
|
|
|
4164 |
};
|
|
|
4165 |
|
|
|
4166 |
function maybe_unary(allow_calls) {
|
|
|
4167 |
if (is("operator") && HOP(UNARY_PREFIX, S.token.value)) {
|
|
|
4168 |
return make_unary("unary-prefix",
|
|
|
4169 |
prog1(S.token.value, next),
|
|
|
4170 |
maybe_unary(allow_calls));
|
|
|
4171 |
}
|
|
|
4172 |
var val = expr_atom(allow_calls);
|
|
|
4173 |
while (is("operator") && HOP(UNARY_POSTFIX, S.token.value) && !S.token.nlb) {
|
|
|
4174 |
val = make_unary("unary-postfix", S.token.value, val);
|
|
|
4175 |
next();
|
|
|
4176 |
}
|
|
|
4177 |
return val;
|
|
|
4178 |
};
|
|
|
4179 |
|
|
|
4180 |
function make_unary(tag, op, expr) {
|
|
|
4181 |
if ((op == "++" || op == "--") && !is_assignable(expr))
|
|
|
4182 |
croak("Invalid use of " + op + " operator");
|
|
|
4183 |
return as(tag, op, expr);
|
|
|
4184 |
};
|
|
|
4185 |
|
|
|
4186 |
function expr_op(left, min_prec, no_in) {
|
|
|
4187 |
var op = is("operator") ? S.token.value : null;
|
|
|
4188 |
if (op && op == "in" && no_in) op = null;
|
|
|
4189 |
var prec = op != null ? PRECEDENCE[op] : null;
|
|
|
4190 |
if (prec != null && prec > min_prec) {
|
|
|
4191 |
next();
|
|
|
4192 |
var right = expr_op(maybe_unary(true), prec, no_in);
|
|
|
4193 |
return expr_op(as("binary", op, left, right), min_prec, no_in);
|
|
|
4194 |
}
|
|
|
4195 |
return left;
|
|
|
4196 |
};
|
|
|
4197 |
|
|
|
4198 |
function expr_ops(no_in) {
|
|
|
4199 |
return expr_op(maybe_unary(true), 0, no_in);
|
|
|
4200 |
};
|
|
|
4201 |
|
|
|
4202 |
function maybe_conditional(no_in) {
|
|
|
4203 |
var expr = expr_ops(no_in);
|
|
|
4204 |
if (is("operator", "?")) {
|
|
|
4205 |
next();
|
|
|
4206 |
var yes = expression(false);
|
|
|
4207 |
expect(":");
|
|
|
4208 |
return as("conditional", expr, yes, expression(false, no_in));
|
|
|
4209 |
}
|
|
|
4210 |
return expr;
|
|
|
4211 |
};
|
|
|
4212 |
|
|
|
4213 |
function is_assignable(expr) {
|
|
|
4214 |
if (!exigent_mode) return true;
|
|
|
4215 |
switch (expr[0]+"") {
|
|
|
4216 |
case "dot":
|
|
|
4217 |
case "sub":
|
|
|
4218 |
case "new":
|
|
|
4219 |
case "call":
|
|
|
4220 |
return true;
|
|
|
4221 |
case "name":
|
|
|
4222 |
return expr[1] != "this";
|
|
|
4223 |
}
|
|
|
4224 |
};
|
|
|
4225 |
|
|
|
4226 |
function maybe_assign(no_in) {
|
|
|
4227 |
var left = maybe_conditional(no_in), val = S.token.value;
|
|
|
4228 |
if (is("operator") && HOP(ASSIGNMENT, val)) {
|
|
|
4229 |
if (is_assignable(left)) {
|
|
|
4230 |
next();
|
|
|
4231 |
return as("assign", ASSIGNMENT[val], left, maybe_assign(no_in));
|
|
|
4232 |
}
|
|
|
4233 |
croak("Invalid assignment");
|
|
|
4234 |
}
|
|
|
4235 |
return left;
|
|
|
4236 |
};
|
|
|
4237 |
|
|
|
4238 |
var expression = maybe_embed_tokens(function(commas, no_in) {
|
|
|
4239 |
if (arguments.length == 0)
|
|
|
4240 |
commas = true;
|
|
|
4241 |
var expr = maybe_assign(no_in);
|
|
|
4242 |
if (commas && is("punc", ",")) {
|
|
|
4243 |
next();
|
|
|
4244 |
return as("seq", expr, expression(true, no_in));
|
|
|
4245 |
}
|
|
|
4246 |
return expr;
|
|
|
4247 |
});
|
|
|
4248 |
|
|
|
4249 |
function in_loop(cont) {
|
|
|
4250 |
try {
|
|
|
4251 |
++S.in_loop;
|
|
|
4252 |
return cont();
|
|
|
4253 |
} finally {
|
|
|
4254 |
--S.in_loop;
|
|
|
4255 |
}
|
|
|
4256 |
};
|
|
|
4257 |
|
|
|
4258 |
return as("toplevel", (function(a){
|
|
|
4259 |
while (!is("eof"))
|
|
|
4260 |
a.push(statement());
|
|
|
4261 |
return a;
|
|
|
4262 |
})([]));
|
|
|
4263 |
|
|
|
4264 |
};
|
|
|
4265 |
|
|
|
4266 |
/* -----[ Utilities ]----- */
|
|
|
4267 |
|
|
|
4268 |
function curry(f) {
|
|
|
4269 |
var args = slice(arguments, 1);
|
|
|
4270 |
return function() { return f.apply(this, args.concat(slice(arguments))); };
|
|
|
4271 |
};
|
|
|
4272 |
|
|
|
4273 |
function prog1(ret) {
|
|
|
4274 |
if (ret instanceof Function)
|
|
|
4275 |
ret = ret();
|
|
|
4276 |
for (var i = 1, n = arguments.length; --n > 0; ++i)
|
|
|
4277 |
arguments[i]();
|
|
|
4278 |
return ret;
|
|
|
4279 |
};
|
|
|
4280 |
|
|
|
4281 |
function array_to_hash(a) {
|
|
|
4282 |
var ret = {};
|
|
|
4283 |
for (var i = 0; i < a.length; ++i)
|
|
|
4284 |
ret[a[i]] = true;
|
|
|
4285 |
return ret;
|
|
|
4286 |
};
|
|
|
4287 |
|
|
|
4288 |
function slice(a, start) {
|
|
|
4289 |
return Array.prototype.slice.call(a, start || 0);
|
|
|
4290 |
};
|
|
|
4291 |
|
|
|
4292 |
function characters(str) {
|
|
|
4293 |
return str.split("");
|
|
|
4294 |
};
|
|
|
4295 |
|
|
|
4296 |
function member(name, array) {
|
|
|
4297 |
for (var i = array.length; --i >= 0;)
|
|
|
4298 |
if (array[i] === name)
|
|
|
4299 |
return true;
|
|
|
4300 |
return false;
|
|
|
4301 |
};
|
|
|
4302 |
|
|
|
4303 |
function HOP(obj, prop) {
|
|
|
4304 |
return Object.prototype.hasOwnProperty.call(obj, prop);
|
|
|
4305 |
};
|
|
|
4306 |
|
|
|
4307 |
var warn = function() {};
|
|
|
4308 |
|
|
|
4309 |
/* -----[ Exports ]----- */
|
|
|
4310 |
|
|
|
4311 |
exports.tokenizer = tokenizer;
|
|
|
4312 |
exports.parse = parse;
|
|
|
4313 |
exports.slice = slice;
|
|
|
4314 |
exports.curry = curry;
|
|
|
4315 |
exports.member = member;
|
|
|
4316 |
exports.array_to_hash = array_to_hash;
|
|
|
4317 |
exports.PRECEDENCE = PRECEDENCE;
|
|
|
4318 |
exports.KEYWORDS_ATOM = KEYWORDS_ATOM;
|
|
|
4319 |
exports.RESERVED_WORDS = RESERVED_WORDS;
|
|
|
4320 |
exports.KEYWORDS = KEYWORDS;
|
|
|
4321 |
exports.ATOMIC_START_TOKEN = ATOMIC_START_TOKEN;
|
|
|
4322 |
exports.OPERATORS = OPERATORS;
|
|
|
4323 |
exports.is_alphanumeric_char = is_alphanumeric_char;
|
|
|
4324 |
exports.set_logger = function(logger) {
|
|
|
4325 |
warn = logger;
|
|
|
4326 |
};
|
|
|
4327 |
|
|
|
4328 |
});define('uglifyjs/squeeze-more', ["require", "exports", "module", "./parse-js", "./process"], function(require, exports, module) {
|
|
|
4329 |
|
|
|
4330 |
var jsp = require("./parse-js"),
|
|
|
4331 |
pro = require("./process"),
|
|
|
4332 |
slice = jsp.slice,
|
|
|
4333 |
member = jsp.member,
|
|
|
4334 |
curry = jsp.curry,
|
|
|
4335 |
MAP = pro.MAP,
|
|
|
4336 |
PRECEDENCE = jsp.PRECEDENCE,
|
|
|
4337 |
OPERATORS = jsp.OPERATORS;
|
|
|
4338 |
|
|
|
4339 |
function ast_squeeze_more(ast) {
|
|
|
4340 |
var w = pro.ast_walker(), walk = w.walk, scope;
|
|
|
4341 |
function with_scope(s, cont) {
|
|
|
4342 |
var save = scope, ret;
|
|
|
4343 |
scope = s;
|
|
|
4344 |
ret = cont();
|
|
|
4345 |
scope = save;
|
|
|
4346 |
return ret;
|
|
|
4347 |
};
|
|
|
4348 |
function _lambda(name, args, body) {
|
|
|
4349 |
return [ this[0], name, args, with_scope(body.scope, curry(MAP, body, walk)) ];
|
|
|
4350 |
};
|
|
|
4351 |
return w.with_walkers({
|
|
|
4352 |
"toplevel": function(body) {
|
|
|
4353 |
return [ this[0], with_scope(this.scope, curry(MAP, body, walk)) ];
|
|
|
4354 |
},
|
|
|
4355 |
"function": _lambda,
|
|
|
4356 |
"defun": _lambda,
|
|
|
4357 |
"new": function(ctor, args) {
|
|
|
4358 |
if (ctor[0] == "name" && ctor[1] == "Array" && !scope.has("Array")) {
|
|
|
4359 |
if (args.length != 1) {
|
|
|
4360 |
return [ "array", args ];
|
|
|
4361 |
} else {
|
|
|
4362 |
return walk([ "call", [ "name", "Array" ], args ]);
|
|
|
4363 |
}
|
|
|
4364 |
}
|
|
|
4365 |
},
|
|
|
4366 |
"call": function(expr, args) {
|
|
|
4367 |
if (expr[0] == "dot" && expr[2] == "toString" && args.length == 0) {
|
|
|
4368 |
// foo.toString() ==> foo+""
|
|
|
4369 |
return [ "binary", "+", expr[1], [ "string", "" ]];
|
|
|
4370 |
}
|
|
|
4371 |
if (expr[0] == "name" && expr[1] == "Array" && args.length != 1 && !scope.has("Array")) {
|
|
|
4372 |
return [ "array", args ];
|
|
|
4373 |
}
|
|
|
4374 |
}
|
|
|
4375 |
}, function() {
|
|
|
4376 |
return walk(pro.ast_add_scope(ast));
|
|
|
4377 |
});
|
|
|
4378 |
};
|
|
|
4379 |
|
|
|
4380 |
exports.ast_squeeze_more = ast_squeeze_more;
|
|
|
4381 |
|
|
|
4382 |
});define('uglifyjs/process', ["require", "exports", "module", "./parse-js", "./squeeze-more"], function(require, exports, module) {
|
|
|
4383 |
|
|
|
4384 |
/***********************************************************************
|
|
|
4385 |
|
|
|
4386 |
A JavaScript tokenizer / parser / beautifier / compressor.
|
|
|
4387 |
|
|
|
4388 |
This version is suitable for Node.js. With minimal changes (the
|
|
|
4389 |
exports stuff) it should work on any JS platform.
|
|
|
4390 |
|
|
|
4391 |
This file implements some AST processors. They work on data built
|
|
|
4392 |
by parse-js.
|
|
|
4393 |
|
|
|
4394 |
Exported functions:
|
|
|
4395 |
|
|
|
4396 |
- ast_mangle(ast, options) -- mangles the variable/function names
|
|
|
4397 |
in the AST. Returns an AST.
|
|
|
4398 |
|
|
|
4399 |
- ast_squeeze(ast) -- employs various optimizations to make the
|
|
|
4400 |
final generated code even smaller. Returns an AST.
|
|
|
4401 |
|
|
|
4402 |
- gen_code(ast, options) -- generates JS code from the AST. Pass
|
|
|
4403 |
true (or an object, see the code for some options) as second
|
|
|
4404 |
argument to get "pretty" (indented) code.
|
|
|
4405 |
|
|
|
4406 |
-------------------------------- (C) ---------------------------------
|
|
|
4407 |
|
|
|
4408 |
Author: Mihai Bazon
|
|
|
4409 |
<mihai.bazon@gmail.com>
|
|
|
4410 |
http://mihai.bazon.net/blog
|
|
|
4411 |
|
|
|
4412 |
Distributed under the BSD license:
|
|
|
4413 |
|
|
|
4414 |
Copyright 2010 (c) Mihai Bazon <mihai.bazon@gmail.com>
|
|
|
4415 |
|
|
|
4416 |
Redistribution and use in source and binary forms, with or without
|
|
|
4417 |
modification, are permitted provided that the following conditions
|
|
|
4418 |
are met:
|
|
|
4419 |
|
|
|
4420 |
* Redistributions of source code must retain the above
|
|
|
4421 |
copyright notice, this list of conditions and the following
|
|
|
4422 |
disclaimer.
|
|
|
4423 |
|
|
|
4424 |
* Redistributions in binary form must reproduce the above
|
|
|
4425 |
copyright notice, this list of conditions and the following
|
|
|
4426 |
disclaimer in the documentation and/or other materials
|
|
|
4427 |
provided with the distribution.
|
|
|
4428 |
|
|
|
4429 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
|
|
|
4430 |
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
4431 |
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
4432 |
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
|
|
|
4433 |
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
|
|
4434 |
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
4435 |
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
4436 |
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
4437 |
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
|
|
|
4438 |
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
|
|
4439 |
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
4440 |
SUCH DAMAGE.
|
|
|
4441 |
|
|
|
4442 |
***********************************************************************/
|
|
|
4443 |
|
|
|
4444 |
var jsp = require("./parse-js"),
|
|
|
4445 |
slice = jsp.slice,
|
|
|
4446 |
member = jsp.member,
|
|
|
4447 |
PRECEDENCE = jsp.PRECEDENCE,
|
|
|
4448 |
OPERATORS = jsp.OPERATORS;
|
|
|
4449 |
|
|
|
4450 |
/* -----[ helper for AST traversal ]----- */
|
|
|
4451 |
|
|
|
4452 |
function ast_walker() {
|
|
|
4453 |
function _vardefs(defs) {
|
|
|
4454 |
return [ this[0], MAP(defs, function(def){
|
|
|
4455 |
var a = [ def[0] ];
|
|
|
4456 |
if (def.length > 1)
|
|
|
4457 |
a[1] = walk(def[1]);
|
|
|
4458 |
return a;
|
|
|
4459 |
}) ];
|
|
|
4460 |
};
|
|
|
4461 |
function _block(statements) {
|
|
|
4462 |
var out = [ this[0] ];
|
|
|
4463 |
if (statements != null)
|
|
|
4464 |
out.push(MAP(statements, walk));
|
|
|
4465 |
return out;
|
|
|
4466 |
};
|
|
|
4467 |
var walkers = {
|
|
|
4468 |
"string": function(str) {
|
|
|
4469 |
return [ this[0], str ];
|
|
|
4470 |
},
|
|
|
4471 |
"num": function(num) {
|
|
|
4472 |
return [ this[0], num ];
|
|
|
4473 |
},
|
|
|
4474 |
"name": function(name) {
|
|
|
4475 |
return [ this[0], name ];
|
|
|
4476 |
},
|
|
|
4477 |
"toplevel": function(statements) {
|
|
|
4478 |
return [ this[0], MAP(statements, walk) ];
|
|
|
4479 |
},
|
|
|
4480 |
"block": _block,
|
|
|
4481 |
"splice": _block,
|
|
|
4482 |
"var": _vardefs,
|
|
|
4483 |
"const": _vardefs,
|
|
|
4484 |
"try": function(t, c, f) {
|
|
|
4485 |
return [
|
|
|
4486 |
this[0],
|
|
|
4487 |
MAP(t, walk),
|
|
|
4488 |
c != null ? [ c[0], MAP(c[1], walk) ] : null,
|
|
|
4489 |
f != null ? MAP(f, walk) : null
|
|
|
4490 |
];
|
|
|
4491 |
},
|
|
|
4492 |
"throw": function(expr) {
|
|
|
4493 |
return [ this[0], walk(expr) ];
|
|
|
4494 |
},
|
|
|
4495 |
"new": function(ctor, args) {
|
|
|
4496 |
return [ this[0], walk(ctor), MAP(args, walk) ];
|
|
|
4497 |
},
|
|
|
4498 |
"switch": function(expr, body) {
|
|
|
4499 |
return [ this[0], walk(expr), MAP(body, function(branch){
|
|
|
4500 |
return [ branch[0] ? walk(branch[0]) : null,
|
|
|
4501 |
MAP(branch[1], walk) ];
|
|
|
4502 |
}) ];
|
|
|
4503 |
},
|
|
|
4504 |
"break": function(label) {
|
|
|
4505 |
return [ this[0], label ];
|
|
|
4506 |
},
|
|
|
4507 |
"continue": function(label) {
|
|
|
4508 |
return [ this[0], label ];
|
|
|
4509 |
},
|
|
|
4510 |
"conditional": function(cond, t, e) {
|
|
|
4511 |
return [ this[0], walk(cond), walk(t), walk(e) ];
|
|
|
4512 |
},
|
|
|
4513 |
"assign": function(op, lvalue, rvalue) {
|
|
|
4514 |
return [ this[0], op, walk(lvalue), walk(rvalue) ];
|
|
|
4515 |
},
|
|
|
4516 |
"dot": function(expr) {
|
|
|
4517 |
return [ this[0], walk(expr) ].concat(slice(arguments, 1));
|
|
|
4518 |
},
|
|
|
4519 |
"call": function(expr, args) {
|
|
|
4520 |
return [ this[0], walk(expr), MAP(args, walk) ];
|
|
|
4521 |
},
|
|
|
4522 |
"function": function(name, args, body) {
|
|
|
4523 |
return [ this[0], name, args.slice(), MAP(body, walk) ];
|
|
|
4524 |
},
|
|
|
4525 |
"defun": function(name, args, body) {
|
|
|
4526 |
return [ this[0], name, args.slice(), MAP(body, walk) ];
|
|
|
4527 |
},
|
|
|
4528 |
"if": function(conditional, t, e) {
|
|
|
4529 |
return [ this[0], walk(conditional), walk(t), walk(e) ];
|
|
|
4530 |
},
|
|
|
4531 |
"for": function(init, cond, step, block) {
|
|
|
4532 |
return [ this[0], walk(init), walk(cond), walk(step), walk(block) ];
|
|
|
4533 |
},
|
|
|
4534 |
"for-in": function(vvar, key, hash, block) {
|
|
|
4535 |
return [ this[0], walk(vvar), walk(key), walk(hash), walk(block) ];
|
|
|
4536 |
},
|
|
|
4537 |
"while": function(cond, block) {
|
|
|
4538 |
return [ this[0], walk(cond), walk(block) ];
|
|
|
4539 |
},
|
|
|
4540 |
"do": function(cond, block) {
|
|
|
4541 |
return [ this[0], walk(cond), walk(block) ];
|
|
|
4542 |
},
|
|
|
4543 |
"return": function(expr) {
|
|
|
4544 |
return [ this[0], walk(expr) ];
|
|
|
4545 |
},
|
|
|
4546 |
"binary": function(op, left, right) {
|
|
|
4547 |
return [ this[0], op, walk(left), walk(right) ];
|
|
|
4548 |
},
|
|
|
4549 |
"unary-prefix": function(op, expr) {
|
|
|
4550 |
return [ this[0], op, walk(expr) ];
|
|
|
4551 |
},
|
|
|
4552 |
"unary-postfix": function(op, expr) {
|
|
|
4553 |
return [ this[0], op, walk(expr) ];
|
|
|
4554 |
},
|
|
|
4555 |
"sub": function(expr, subscript) {
|
|
|
4556 |
return [ this[0], walk(expr), walk(subscript) ];
|
|
|
4557 |
},
|
|
|
4558 |
"object": function(props) {
|
|
|
4559 |
return [ this[0], MAP(props, function(p){
|
|
|
4560 |
return p.length == 2
|
|
|
4561 |
? [ p[0], walk(p[1]) ]
|
|
|
4562 |
: [ p[0], walk(p[1]), p[2] ]; // get/set-ter
|
|
|
4563 |
}) ];
|
|
|
4564 |
},
|
|
|
4565 |
"regexp": function(rx, mods) {
|
|
|
4566 |
return [ this[0], rx, mods ];
|
|
|
4567 |
},
|
|
|
4568 |
"array": function(elements) {
|
|
|
4569 |
return [ this[0], MAP(elements, walk) ];
|
|
|
4570 |
},
|
|
|
4571 |
"stat": function(stat) {
|
|
|
4572 |
return [ this[0], walk(stat) ];
|
|
|
4573 |
},
|
|
|
4574 |
"seq": function() {
|
|
|
4575 |
return [ this[0] ].concat(MAP(slice(arguments), walk));
|
|
|
4576 |
},
|
|
|
4577 |
"label": function(name, block) {
|
|
|
4578 |
return [ this[0], name, walk(block) ];
|
|
|
4579 |
},
|
|
|
4580 |
"with": function(expr, block) {
|
|
|
4581 |
return [ this[0], walk(expr), walk(block) ];
|
|
|
4582 |
},
|
|
|
4583 |
"atom": function(name) {
|
|
|
4584 |
return [ this[0], name ];
|
|
|
4585 |
}
|
|
|
4586 |
};
|
|
|
4587 |
|
|
|
4588 |
var user = {};
|
|
|
4589 |
var stack = [];
|
|
|
4590 |
function walk(ast) {
|
|
|
4591 |
if (ast == null)
|
|
|
4592 |
return null;
|
|
|
4593 |
try {
|
|
|
4594 |
stack.push(ast);
|
|
|
4595 |
var type = ast[0];
|
|
|
4596 |
var gen = user[type];
|
|
|
4597 |
if (gen) {
|
|
|
4598 |
var ret = gen.apply(ast, ast.slice(1));
|
|
|
4599 |
if (ret != null)
|
|
|
4600 |
return ret;
|
|
|
4601 |
}
|
|
|
4602 |
gen = walkers[type];
|
|
|
4603 |
return gen.apply(ast, ast.slice(1));
|
|
|
4604 |
} finally {
|
|
|
4605 |
stack.pop();
|
|
|
4606 |
}
|
|
|
4607 |
};
|
|
|
4608 |
|
|
|
4609 |
function dive(ast) {
|
|
|
4610 |
if (ast == null)
|
|
|
4611 |
return null;
|
|
|
4612 |
try {
|
|
|
4613 |
stack.push(ast);
|
|
|
4614 |
return walkers[ast[0]].apply(ast, ast.slice(1));
|
|
|
4615 |
} finally {
|
|
|
4616 |
stack.pop();
|
|
|
4617 |
}
|
|
|
4618 |
};
|
|
|
4619 |
|
|
|
4620 |
function with_walkers(walkers, cont){
|
|
|
4621 |
var save = {}, i;
|
|
|
4622 |
for (i in walkers) if (HOP(walkers, i)) {
|
|
|
4623 |
save[i] = user[i];
|
|
|
4624 |
user[i] = walkers[i];
|
|
|
4625 |
}
|
|
|
4626 |
var ret = cont();
|
|
|
4627 |
for (i in save) if (HOP(save, i)) {
|
|
|
4628 |
if (!save[i]) delete user[i];
|
|
|
4629 |
else user[i] = save[i];
|
|
|
4630 |
}
|
|
|
4631 |
return ret;
|
|
|
4632 |
};
|
|
|
4633 |
|
|
|
4634 |
return {
|
|
|
4635 |
walk: walk,
|
|
|
4636 |
dive: dive,
|
|
|
4637 |
with_walkers: with_walkers,
|
|
|
4638 |
parent: function() {
|
|
|
4639 |
return stack[stack.length - 2]; // last one is current node
|
|
|
4640 |
},
|
|
|
4641 |
stack: function() {
|
|
|
4642 |
return stack;
|
|
|
4643 |
}
|
|
|
4644 |
};
|
|
|
4645 |
};
|
|
|
4646 |
|
|
|
4647 |
/* -----[ Scope and mangling ]----- */
|
|
|
4648 |
|
|
|
4649 |
function Scope(parent) {
|
|
|
4650 |
this.names = {}; // names defined in this scope
|
|
|
4651 |
this.mangled = {}; // mangled names (orig.name => mangled)
|
|
|
4652 |
this.rev_mangled = {}; // reverse lookup (mangled => orig.name)
|
|
|
4653 |
this.cname = -1; // current mangled name
|
|
|
4654 |
this.refs = {}; // names referenced from this scope
|
|
|
4655 |
this.uses_with = false; // will become TRUE if with() is detected in this or any subscopes
|
|
|
4656 |
this.uses_eval = false; // will become TRUE if eval() is detected in this or any subscopes
|
|
|
4657 |
this.parent = parent; // parent scope
|
|
|
4658 |
this.children = []; // sub-scopes
|
|
|
4659 |
if (parent) {
|
|
|
4660 |
this.level = parent.level + 1;
|
|
|
4661 |
parent.children.push(this);
|
|
|
4662 |
} else {
|
|
|
4663 |
this.level = 0;
|
|
|
4664 |
}
|
|
|
4665 |
};
|
|
|
4666 |
|
|
|
4667 |
var base54 = (function(){
|
|
|
4668 |
var DIGITS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_";
|
|
|
4669 |
return function(num) {
|
|
|
4670 |
var ret = "";
|
|
|
4671 |
do {
|
|
|
4672 |
ret = DIGITS.charAt(num % 54) + ret;
|
|
|
4673 |
num = Math.floor(num / 54);
|
|
|
4674 |
} while (num > 0);
|
|
|
4675 |
return ret;
|
|
|
4676 |
};
|
|
|
4677 |
})();
|
|
|
4678 |
|
|
|
4679 |
Scope.prototype = {
|
|
|
4680 |
has: function(name) {
|
|
|
4681 |
for (var s = this; s; s = s.parent)
|
|
|
4682 |
if (HOP(s.names, name))
|
|
|
4683 |
return s;
|
|
|
4684 |
},
|
|
|
4685 |
has_mangled: function(mname) {
|
|
|
4686 |
for (var s = this; s; s = s.parent)
|
|
|
4687 |
if (HOP(s.rev_mangled, mname))
|
|
|
4688 |
return s;
|
|
|
4689 |
},
|
|
|
4690 |
toJSON: function() {
|
|
|
4691 |
return {
|
|
|
4692 |
names: this.names,
|
|
|
4693 |
uses_eval: this.uses_eval,
|
|
|
4694 |
uses_with: this.uses_with
|
|
|
4695 |
};
|
|
|
4696 |
},
|
|
|
4697 |
|
|
|
4698 |
next_mangled: function() {
|
|
|
4699 |
// we must be careful that the new mangled name:
|
|
|
4700 |
//
|
|
|
4701 |
// 1. doesn't shadow a mangled name from a parent
|
|
|
4702 |
// scope, unless we don't reference the original
|
|
|
4703 |
// name from this scope OR from any sub-scopes!
|
|
|
4704 |
// This will get slow.
|
|
|
4705 |
//
|
|
|
4706 |
// 2. doesn't shadow an original name from a parent
|
|
|
4707 |
// scope, in the event that the name is not mangled
|
|
|
4708 |
// in the parent scope and we reference that name
|
|
|
4709 |
// here OR IN ANY SUBSCOPES!
|
|
|
4710 |
//
|
|
|
4711 |
// 3. doesn't shadow a name that is referenced but not
|
|
|
4712 |
// defined (possibly global defined elsewhere).
|
|
|
4713 |
for (;;) {
|
|
|
4714 |
var m = base54(++this.cname), prior;
|
|
|
4715 |
|
|
|
4716 |
// case 1.
|
|
|
4717 |
prior = this.has_mangled(m);
|
|
|
4718 |
if (prior && this.refs[prior.rev_mangled[m]] === prior)
|
|
|
4719 |
continue;
|
|
|
4720 |
|
|
|
4721 |
// case 2.
|
|
|
4722 |
prior = this.has(m);
|
|
|
4723 |
if (prior && prior !== this && this.refs[m] === prior && !prior.has_mangled(m))
|
|
|
4724 |
continue;
|
|
|
4725 |
|
|
|
4726 |
// case 3.
|
|
|
4727 |
if (HOP(this.refs, m) && this.refs[m] == null)
|
|
|
4728 |
continue;
|
|
|
4729 |
|
|
|
4730 |
// I got "do" once. :-/
|
|
|
4731 |
if (!is_identifier(m))
|
|
|
4732 |
continue;
|
|
|
4733 |
|
|
|
4734 |
return m;
|
|
|
4735 |
}
|
|
|
4736 |
},
|
|
|
4737 |
set_mangle: function(name, m) {
|
|
|
4738 |
this.rev_mangled[m] = name;
|
|
|
4739 |
return this.mangled[name] = m;
|
|
|
4740 |
},
|
|
|
4741 |
get_mangled: function(name, newMangle) {
|
|
|
4742 |
if (this.uses_eval || this.uses_with) return name; // no mangle if eval or with is in use
|
|
|
4743 |
var s = this.has(name);
|
|
|
4744 |
if (!s) return name; // not in visible scope, no mangle
|
|
|
4745 |
if (HOP(s.mangled, name)) return s.mangled[name]; // already mangled in this scope
|
|
|
4746 |
if (!newMangle) return name; // not found and no mangling requested
|
|
|
4747 |
return s.set_mangle(name, s.next_mangled());
|
|
|
4748 |
},
|
|
|
4749 |
references: function(name) {
|
|
|
4750 |
return name && !this.parent || this.uses_with || this.uses_eval || this.refs[name];
|
|
|
4751 |
},
|
|
|
4752 |
define: function(name, type) {
|
|
|
4753 |
if (name != null) {
|
|
|
4754 |
if (type == "var" || !HOP(this.names, name))
|
|
|
4755 |
this.names[name] = type || "var";
|
|
|
4756 |
return name;
|
|
|
4757 |
}
|
|
|
4758 |
}
|
|
|
4759 |
};
|
|
|
4760 |
|
|
|
4761 |
function ast_add_scope(ast) {
|
|
|
4762 |
|
|
|
4763 |
var current_scope = null;
|
|
|
4764 |
var w = ast_walker(), walk = w.walk;
|
|
|
4765 |
var having_eval = [];
|
|
|
4766 |
|
|
|
4767 |
function with_new_scope(cont) {
|
|
|
4768 |
current_scope = new Scope(current_scope);
|
|
|
4769 |
var ret = current_scope.body = cont();
|
|
|
4770 |
ret.scope = current_scope;
|
|
|
4771 |
current_scope = current_scope.parent;
|
|
|
4772 |
return ret;
|
|
|
4773 |
};
|
|
|
4774 |
|
|
|
4775 |
function define(name, type) {
|
|
|
4776 |
return current_scope.define(name, type);
|
|
|
4777 |
};
|
|
|
4778 |
|
|
|
4779 |
function reference(name) {
|
|
|
4780 |
current_scope.refs[name] = true;
|
|
|
4781 |
};
|
|
|
4782 |
|
|
|
4783 |
function _lambda(name, args, body) {
|
|
|
4784 |
var is_defun = this[0] == "defun";
|
|
|
4785 |
return [ this[0], is_defun ? define(name, "defun") : name, args, with_new_scope(function(){
|
|
|
4786 |
if (!is_defun) define(name, "lambda");
|
|
|
4787 |
MAP(args, function(name){ define(name, "arg") });
|
|
|
4788 |
return MAP(body, walk);
|
|
|
4789 |
})];
|
|
|
4790 |
};
|
|
|
4791 |
|
|
|
4792 |
function _vardefs(type) {
|
|
|
4793 |
return function(defs) {
|
|
|
4794 |
MAP(defs, function(d){
|
|
|
4795 |
define(d[0], type);
|
|
|
4796 |
if (d[1]) reference(d[0]);
|
|
|
4797 |
});
|
|
|
4798 |
};
|
|
|
4799 |
};
|
|
|
4800 |
|
|
|
4801 |
return with_new_scope(function(){
|
|
|
4802 |
// process AST
|
|
|
4803 |
var ret = w.with_walkers({
|
|
|
4804 |
"function": _lambda,
|
|
|
4805 |
"defun": _lambda,
|
|
|
4806 |
"label": function(name, stat) { define(name, "label") },
|
|
|
4807 |
"break": function(label) { if (label) reference(label) },
|
|
|
4808 |
"continue": function(label) { if (label) reference(label) },
|
|
|
4809 |
"with": function(expr, block) {
|
|
|
4810 |
for (var s = current_scope; s; s = s.parent)
|
|
|
4811 |
s.uses_with = true;
|
|
|
4812 |
},
|
|
|
4813 |
"var": _vardefs("var"),
|
|
|
4814 |
"const": _vardefs("const"),
|
|
|
4815 |
"try": function(t, c, f) {
|
|
|
4816 |
if (c != null) return [
|
|
|
4817 |
this[0],
|
|
|
4818 |
MAP(t, walk),
|
|
|
4819 |
[ define(c[0], "catch"), MAP(c[1], walk) ],
|
|
|
4820 |
f != null ? MAP(f, walk) : null
|
|
|
4821 |
];
|
|
|
4822 |
},
|
|
|
4823 |
"name": function(name) {
|
|
|
4824 |
if (name == "eval")
|
|
|
4825 |
having_eval.push(current_scope);
|
|
|
4826 |
reference(name);
|
|
|
4827 |
}
|
|
|
4828 |
}, function(){
|
|
|
4829 |
return walk(ast);
|
|
|
4830 |
});
|
|
|
4831 |
|
|
|
4832 |
// the reason why we need an additional pass here is
|
|
|
4833 |
// that names can be used prior to their definition.
|
|
|
4834 |
|
|
|
4835 |
// scopes where eval was detected and their parents
|
|
|
4836 |
// are marked with uses_eval, unless they define the
|
|
|
4837 |
// "eval" name.
|
|
|
4838 |
MAP(having_eval, function(scope){
|
|
|
4839 |
if (!scope.has("eval")) while (scope) {
|
|
|
4840 |
scope.uses_eval = true;
|
|
|
4841 |
scope = scope.parent;
|
|
|
4842 |
}
|
|
|
4843 |
});
|
|
|
4844 |
|
|
|
4845 |
// for referenced names it might be useful to know
|
|
|
4846 |
// their origin scope. current_scope here is the
|
|
|
4847 |
// toplevel one.
|
|
|
4848 |
function fixrefs(scope, i) {
|
|
|
4849 |
// do children first; order shouldn't matter
|
|
|
4850 |
for (i = scope.children.length; --i >= 0;)
|
|
|
4851 |
fixrefs(scope.children[i]);
|
|
|
4852 |
for (i in scope.refs) if (HOP(scope.refs, i)) {
|
|
|
4853 |
// find origin scope and propagate the reference to origin
|
|
|
4854 |
for (var origin = scope.has(i), s = scope; s; s = s.parent) {
|
|
|
4855 |
s.refs[i] = origin;
|
|
|
4856 |
if (s === origin) break;
|
|
|
4857 |
}
|
|
|
4858 |
}
|
|
|
4859 |
};
|
|
|
4860 |
fixrefs(current_scope);
|
|
|
4861 |
|
|
|
4862 |
return ret;
|
|
|
4863 |
});
|
|
|
4864 |
|
|
|
4865 |
};
|
|
|
4866 |
|
|
|
4867 |
/* -----[ mangle names ]----- */
|
|
|
4868 |
|
|
|
4869 |
function ast_mangle(ast, options) {
|
|
|
4870 |
var w = ast_walker(), walk = w.walk, scope;
|
|
|
4871 |
options = options || {};
|
|
|
4872 |
|
|
|
4873 |
function get_mangled(name, newMangle) {
|
|
|
4874 |
if (!options.toplevel && !scope.parent) return name; // don't mangle toplevel
|
|
|
4875 |
if (options.except && member(name, options.except))
|
|
|
4876 |
return name;
|
|
|
4877 |
return scope.get_mangled(name, newMangle);
|
|
|
4878 |
};
|
|
|
4879 |
|
|
|
4880 |
function get_define(name) {
|
|
|
4881 |
if (options.defines) {
|
|
|
4882 |
// we always lookup a defined symbol for the current scope FIRST, so declared
|
|
|
4883 |
// vars trump a DEFINE symbol, but if no such var is found, then match a DEFINE value
|
|
|
4884 |
if (!scope.has(name)) {
|
|
|
4885 |
if (HOP(options.defines, name)) {
|
|
|
4886 |
return options.defines[name];
|
|
|
4887 |
}
|
|
|
4888 |
}
|
|
|
4889 |
return null;
|
|
|
4890 |
}
|
|
|
4891 |
};
|
|
|
4892 |
|
|
|
4893 |
function _lambda(name, args, body) {
|
|
|
4894 |
var is_defun = this[0] == "defun", extra;
|
|
|
4895 |
if (name) {
|
|
|
4896 |
if (is_defun) name = get_mangled(name);
|
|
|
4897 |
else {
|
|
|
4898 |
extra = {};
|
|
|
4899 |
if (!(scope.uses_eval || scope.uses_with))
|
|
|
4900 |
name = extra[name] = scope.next_mangled();
|
|
|
4901 |
else
|
|
|
4902 |
extra[name] = name;
|
|
|
4903 |
}
|
|
|
4904 |
}
|
|
|
4905 |
body = with_scope(body.scope, function(){
|
|
|
4906 |
args = MAP(args, function(name){ return get_mangled(name) });
|
|
|
4907 |
return MAP(body, walk);
|
|
|
4908 |
}, extra);
|
|
|
4909 |
return [ this[0], name, args, body ];
|
|
|
4910 |
};
|
|
|
4911 |
|
|
|
4912 |
function with_scope(s, cont, extra) {
|
|
|
4913 |
var _scope = scope;
|
|
|
4914 |
scope = s;
|
|
|
4915 |
if (extra) for (var i in extra) if (HOP(extra, i)) {
|
|
|
4916 |
s.set_mangle(i, extra[i]);
|
|
|
4917 |
}
|
|
|
4918 |
for (var i in s.names) if (HOP(s.names, i)) {
|
|
|
4919 |
get_mangled(i, true);
|
|
|
4920 |
}
|
|
|
4921 |
var ret = cont();
|
|
|
4922 |
ret.scope = s;
|
|
|
4923 |
scope = _scope;
|
|
|
4924 |
return ret;
|
|
|
4925 |
};
|
|
|
4926 |
|
|
|
4927 |
function _vardefs(defs) {
|
|
|
4928 |
return [ this[0], MAP(defs, function(d){
|
|
|
4929 |
return [ get_mangled(d[0]), walk(d[1]) ];
|
|
|
4930 |
}) ];
|
|
|
4931 |
};
|
|
|
4932 |
|
|
|
4933 |
return w.with_walkers({
|
|
|
4934 |
"function": _lambda,
|
|
|
4935 |
"defun": function() {
|
|
|
4936 |
// move function declarations to the top when
|
|
|
4937 |
// they are not in some block.
|
|
|
4938 |
var ast = _lambda.apply(this, arguments);
|
|
|
4939 |
switch (w.parent()[0]) {
|
|
|
4940 |
case "toplevel":
|
|
|
4941 |
case "function":
|
|
|
4942 |
case "defun":
|
|
|
4943 |
return MAP.at_top(ast);
|
|
|
4944 |
}
|
|
|
4945 |
return ast;
|
|
|
4946 |
},
|
|
|
4947 |
"label": function(label, stat) { return [ this[0], get_mangled(label), walk(stat) ] },
|
|
|
4948 |
"break": function(label) { if (label) return [ this[0], get_mangled(label) ] },
|
|
|
4949 |
"continue": function(label) { if (label) return [ this[0], get_mangled(label) ] },
|
|
|
4950 |
"var": _vardefs,
|
|
|
4951 |
"const": _vardefs,
|
|
|
4952 |
"name": function(name) {
|
|
|
4953 |
return get_define(name) || [ this[0], get_mangled(name) ];
|
|
|
4954 |
},
|
|
|
4955 |
"try": function(t, c, f) {
|
|
|
4956 |
return [ this[0],
|
|
|
4957 |
MAP(t, walk),
|
|
|
4958 |
c != null ? [ get_mangled(c[0]), MAP(c[1], walk) ] : null,
|
|
|
4959 |
f != null ? MAP(f, walk) : null ];
|
|
|
4960 |
},
|
|
|
4961 |
"toplevel": function(body) {
|
|
|
4962 |
var self = this;
|
|
|
4963 |
return with_scope(self.scope, function(){
|
|
|
4964 |
return [ self[0], MAP(body, walk) ];
|
|
|
4965 |
});
|
|
|
4966 |
}
|
|
|
4967 |
}, function() {
|
|
|
4968 |
return walk(ast_add_scope(ast));
|
|
|
4969 |
});
|
|
|
4970 |
};
|
|
|
4971 |
|
|
|
4972 |
/* -----[
|
|
|
4973 |
- compress foo["bar"] into foo.bar,
|
|
|
4974 |
- remove block brackets {} where possible
|
|
|
4975 |
- join consecutive var declarations
|
|
|
4976 |
- various optimizations for IFs:
|
|
|
4977 |
- if (cond) foo(); else bar(); ==> cond?foo():bar();
|
|
|
4978 |
- if (cond) foo(); ==> cond&&foo();
|
|
|
4979 |
- if (foo) return bar(); else return baz(); ==> return foo?bar():baz(); // also for throw
|
|
|
4980 |
- if (foo) return bar(); else something(); ==> {if(foo)return bar();something()}
|
|
|
4981 |
]----- */
|
|
|
4982 |
|
|
|
4983 |
var warn = function(){};
|
|
|
4984 |
|
|
|
4985 |
function best_of(ast1, ast2) {
|
|
|
4986 |
return gen_code(ast1).length > gen_code(ast2[0] == "stat" ? ast2[1] : ast2).length ? ast2 : ast1;
|
|
|
4987 |
};
|
|
|
4988 |
|
|
|
4989 |
function last_stat(b) {
|
|
|
4990 |
if (b[0] == "block" && b[1] && b[1].length > 0)
|
|
|
4991 |
return b[1][b[1].length - 1];
|
|
|
4992 |
return b;
|
|
|
4993 |
}
|
|
|
4994 |
|
|
|
4995 |
function aborts(t) {
|
|
|
4996 |
if (t) switch (last_stat(t)[0]) {
|
|
|
4997 |
case "return":
|
|
|
4998 |
case "break":
|
|
|
4999 |
case "continue":
|
|
|
5000 |
case "throw":
|
|
|
5001 |
return true;
|
|
|
5002 |
}
|
|
|
5003 |
};
|
|
|
5004 |
|
|
|
5005 |
function boolean_expr(expr) {
|
|
|
5006 |
return ( (expr[0] == "unary-prefix"
|
|
|
5007 |
&& member(expr[1], [ "!", "delete" ])) ||
|
|
|
5008 |
|
|
|
5009 |
(expr[0] == "binary"
|
|
|
5010 |
&& member(expr[1], [ "in", "instanceof", "==", "!=", "===", "!==", "<", "<=", ">=", ">" ])) ||
|
|
|
5011 |
|
|
|
5012 |
(expr[0] == "binary"
|
|
|
5013 |
&& member(expr[1], [ "&&", "||" ])
|
|
|
5014 |
&& boolean_expr(expr[2])
|
|
|
5015 |
&& boolean_expr(expr[3])) ||
|
|
|
5016 |
|
|
|
5017 |
(expr[0] == "conditional"
|
|
|
5018 |
&& boolean_expr(expr[2])
|
|
|
5019 |
&& boolean_expr(expr[3])) ||
|
|
|
5020 |
|
|
|
5021 |
(expr[0] == "assign"
|
|
|
5022 |
&& expr[1] === true
|
|
|
5023 |
&& boolean_expr(expr[3])) ||
|
|
|
5024 |
|
|
|
5025 |
(expr[0] == "seq"
|
|
|
5026 |
&& boolean_expr(expr[expr.length - 1]))
|
|
|
5027 |
);
|
|
|
5028 |
};
|
|
|
5029 |
|
|
|
5030 |
function empty(b) {
|
|
|
5031 |
return !b || (b[0] == "block" && (!b[1] || b[1].length == 0));
|
|
|
5032 |
};
|
|
|
5033 |
|
|
|
5034 |
function is_string(node) {
|
|
|
5035 |
return (node[0] == "string" ||
|
|
|
5036 |
node[0] == "unary-prefix" && node[1] == "typeof" ||
|
|
|
5037 |
node[0] == "binary" && node[1] == "+" &&
|
|
|
5038 |
(is_string(node[2]) || is_string(node[3])));
|
|
|
5039 |
};
|
|
|
5040 |
|
|
|
5041 |
var when_constant = (function(){
|
|
|
5042 |
|
|
|
5043 |
var $NOT_CONSTANT = {};
|
|
|
5044 |
|
|
|
5045 |
// this can only evaluate constant expressions. If it finds anything
|
|
|
5046 |
// not constant, it throws $NOT_CONSTANT.
|
|
|
5047 |
function evaluate(expr) {
|
|
|
5048 |
switch (expr[0]) {
|
|
|
5049 |
case "string":
|
|
|
5050 |
case "num":
|
|
|
5051 |
return expr[1];
|
|
|
5052 |
case "name":
|
|
|
5053 |
case "atom":
|
|
|
5054 |
switch (expr[1]) {
|
|
|
5055 |
case "true": return true;
|
|
|
5056 |
case "false": return false;
|
|
|
5057 |
case "null": return null;
|
|
|
5058 |
}
|
|
|
5059 |
break;
|
|
|
5060 |
case "unary-prefix":
|
|
|
5061 |
switch (expr[1]) {
|
|
|
5062 |
case "!": return !evaluate(expr[2]);
|
|
|
5063 |
case "typeof": return typeof evaluate(expr[2]);
|
|
|
5064 |
case "~": return ~evaluate(expr[2]);
|
|
|
5065 |
case "-": return -evaluate(expr[2]);
|
|
|
5066 |
case "+": return +evaluate(expr[2]);
|
|
|
5067 |
}
|
|
|
5068 |
break;
|
|
|
5069 |
case "binary":
|
|
|
5070 |
var left = expr[2], right = expr[3];
|
|
|
5071 |
switch (expr[1]) {
|
|
|
5072 |
case "&&" : return evaluate(left) && evaluate(right);
|
|
|
5073 |
case "||" : return evaluate(left) || evaluate(right);
|
|
|
5074 |
case "|" : return evaluate(left) | evaluate(right);
|
|
|
5075 |
case "&" : return evaluate(left) & evaluate(right);
|
|
|
5076 |
case "^" : return evaluate(left) ^ evaluate(right);
|
|
|
5077 |
case "+" : return evaluate(left) + evaluate(right);
|
|
|
5078 |
case "*" : return evaluate(left) * evaluate(right);
|
|
|
5079 |
case "/" : return evaluate(left) / evaluate(right);
|
|
|
5080 |
case "%" : return evaluate(left) % evaluate(right);
|
|
|
5081 |
case "-" : return evaluate(left) - evaluate(right);
|
|
|
5082 |
case "<<" : return evaluate(left) << evaluate(right);
|
|
|
5083 |
case ">>" : return evaluate(left) >> evaluate(right);
|
|
|
5084 |
case ">>>" : return evaluate(left) >>> evaluate(right);
|
|
|
5085 |
case "==" : return evaluate(left) == evaluate(right);
|
|
|
5086 |
case "===" : return evaluate(left) === evaluate(right);
|
|
|
5087 |
case "!=" : return evaluate(left) != evaluate(right);
|
|
|
5088 |
case "!==" : return evaluate(left) !== evaluate(right);
|
|
|
5089 |
case "<" : return evaluate(left) < evaluate(right);
|
|
|
5090 |
case "<=" : return evaluate(left) <= evaluate(right);
|
|
|
5091 |
case ">" : return evaluate(left) > evaluate(right);
|
|
|
5092 |
case ">=" : return evaluate(left) >= evaluate(right);
|
|
|
5093 |
case "in" : return evaluate(left) in evaluate(right);
|
|
|
5094 |
case "instanceof" : return evaluate(left) instanceof evaluate(right);
|
|
|
5095 |
}
|
|
|
5096 |
}
|
|
|
5097 |
throw $NOT_CONSTANT;
|
|
|
5098 |
};
|
|
|
5099 |
|
|
|
5100 |
return function(expr, yes, no) {
|
|
|
5101 |
try {
|
|
|
5102 |
var val = evaluate(expr), ast;
|
|
|
5103 |
switch (typeof val) {
|
|
|
5104 |
case "string": ast = [ "string", val ]; break;
|
|
|
5105 |
case "number": ast = [ "num", val ]; break;
|
|
|
5106 |
case "boolean": ast = [ "name", String(val) ]; break;
|
|
|
5107 |
default: throw new Error("Can't handle constant of type: " + (typeof val));
|
|
|
5108 |
}
|
|
|
5109 |
return yes.call(expr, ast, val);
|
|
|
5110 |
} catch(ex) {
|
|
|
5111 |
if (ex === $NOT_CONSTANT) {
|
|
|
5112 |
if (expr[0] == "binary"
|
|
|
5113 |
&& (expr[1] == "===" || expr[1] == "!==")
|
|
|
5114 |
&& ((is_string(expr[2]) && is_string(expr[3]))
|
|
|
5115 |
|| (boolean_expr(expr[2]) && boolean_expr(expr[3])))) {
|
|
|
5116 |
expr[1] = expr[1].substr(0, 2);
|
|
|
5117 |
}
|
|
|
5118 |
else if (no && expr[0] == "binary"
|
|
|
5119 |
&& (expr[1] == "||" || expr[1] == "&&")) {
|
|
|
5120 |
// the whole expression is not constant but the lval may be...
|
|
|
5121 |
try {
|
|
|
5122 |
var lval = evaluate(expr[2]);
|
|
|
5123 |
expr = ((expr[1] == "&&" && (lval ? expr[3] : lval)) ||
|
|
|
5124 |
(expr[1] == "||" && (lval ? lval : expr[3])) ||
|
|
|
5125 |
expr);
|
|
|
5126 |
} catch(ex2) {
|
|
|
5127 |
// IGNORE... lval is not constant
|
|
|
5128 |
}
|
|
|
5129 |
}
|
|
|
5130 |
return no ? no.call(expr, expr) : null;
|
|
|
5131 |
}
|
|
|
5132 |
else throw ex;
|
|
|
5133 |
}
|
|
|
5134 |
};
|
|
|
5135 |
|
|
|
5136 |
})();
|
|
|
5137 |
|
|
|
5138 |
function warn_unreachable(ast) {
|
|
|
5139 |
if (!empty(ast))
|
|
|
5140 |
warn("Dropping unreachable code: " + gen_code(ast, true));
|
|
|
5141 |
};
|
|
|
5142 |
|
|
|
5143 |
function prepare_ifs(ast) {
|
|
|
5144 |
var w = ast_walker(), walk = w.walk;
|
|
|
5145 |
// In this first pass, we rewrite ifs which abort with no else with an
|
|
|
5146 |
// if-else. For example:
|
|
|
5147 |
//
|
|
|
5148 |
// if (x) {
|
|
|
5149 |
// blah();
|
|
|
5150 |
// return y;
|
|
|
5151 |
// }
|
|
|
5152 |
// foobar();
|
|
|
5153 |
//
|
|
|
5154 |
// is rewritten into:
|
|
|
5155 |
//
|
|
|
5156 |
// if (x) {
|
|
|
5157 |
// blah();
|
|
|
5158 |
// return y;
|
|
|
5159 |
// } else {
|
|
|
5160 |
// foobar();
|
|
|
5161 |
// }
|
|
|
5162 |
function redo_if(statements) {
|
|
|
5163 |
statements = MAP(statements, walk);
|
|
|
5164 |
|
|
|
5165 |
for (var i = 0; i < statements.length; ++i) {
|
|
|
5166 |
var fi = statements[i];
|
|
|
5167 |
if (fi[0] != "if") continue;
|
|
|
5168 |
|
|
|
5169 |
if (fi[3] && walk(fi[3])) continue;
|
|
|
5170 |
|
|
|
5171 |
var t = walk(fi[2]);
|
|
|
5172 |
if (!aborts(t)) continue;
|
|
|
5173 |
|
|
|
5174 |
var conditional = walk(fi[1]);
|
|
|
5175 |
|
|
|
5176 |
var e_body = statements.slice(i + 1);
|
|
|
5177 |
var e = e_body.length == 1 ? e_body[0] : [ "block", e_body ];
|
|
|
5178 |
|
|
|
5179 |
var ret = statements.slice(0, i).concat([ [
|
|
|
5180 |
fi[0], // "if"
|
|
|
5181 |
conditional, // conditional
|
|
|
5182 |
t, // then
|
|
|
5183 |
e // else
|
|
|
5184 |
] ]);
|
|
|
5185 |
|
|
|
5186 |
return redo_if(ret);
|
|
|
5187 |
}
|
|
|
5188 |
|
|
|
5189 |
return statements;
|
|
|
5190 |
};
|
|
|
5191 |
|
|
|
5192 |
function redo_if_lambda(name, args, body) {
|
|
|
5193 |
body = redo_if(body);
|
|
|
5194 |
return [ this[0], name, args, body ];
|
|
|
5195 |
};
|
|
|
5196 |
|
|
|
5197 |
function redo_if_block(statements) {
|
|
|
5198 |
return [ this[0], statements != null ? redo_if(statements) : null ];
|
|
|
5199 |
};
|
|
|
5200 |
|
|
|
5201 |
return w.with_walkers({
|
|
|
5202 |
"defun": redo_if_lambda,
|
|
|
5203 |
"function": redo_if_lambda,
|
|
|
5204 |
"block": redo_if_block,
|
|
|
5205 |
"splice": redo_if_block,
|
|
|
5206 |
"toplevel": function(statements) {
|
|
|
5207 |
return [ this[0], redo_if(statements) ];
|
|
|
5208 |
},
|
|
|
5209 |
"try": function(t, c, f) {
|
|
|
5210 |
return [
|
|
|
5211 |
this[0],
|
|
|
5212 |
redo_if(t),
|
|
|
5213 |
c != null ? [ c[0], redo_if(c[1]) ] : null,
|
|
|
5214 |
f != null ? redo_if(f) : null
|
|
|
5215 |
];
|
|
|
5216 |
}
|
|
|
5217 |
}, function() {
|
|
|
5218 |
return walk(ast);
|
|
|
5219 |
});
|
|
|
5220 |
};
|
|
|
5221 |
|
|
|
5222 |
function for_side_effects(ast, handler) {
|
|
|
5223 |
var w = ast_walker(), walk = w.walk;
|
|
|
5224 |
var $stop = {}, $restart = {};
|
|
|
5225 |
function stop() { throw $stop };
|
|
|
5226 |
function restart() { throw $restart };
|
|
|
5227 |
function found(){ return handler.call(this, this, w, stop, restart) };
|
|
|
5228 |
function unary(op) {
|
|
|
5229 |
if (op == "++" || op == "--")
|
|
|
5230 |
return found.apply(this, arguments);
|
|
|
5231 |
};
|
|
|
5232 |
return w.with_walkers({
|
|
|
5233 |
"try": found,
|
|
|
5234 |
"throw": found,
|
|
|
5235 |
"return": found,
|
|
|
5236 |
"new": found,
|
|
|
5237 |
"switch": found,
|
|
|
5238 |
"break": found,
|
|
|
5239 |
"continue": found,
|
|
|
5240 |
"assign": found,
|
|
|
5241 |
"call": found,
|
|
|
5242 |
"if": found,
|
|
|
5243 |
"for": found,
|
|
|
5244 |
"for-in": found,
|
|
|
5245 |
"while": found,
|
|
|
5246 |
"do": found,
|
|
|
5247 |
"return": found,
|
|
|
5248 |
"unary-prefix": unary,
|
|
|
5249 |
"unary-postfix": unary,
|
|
|
5250 |
"defun": found
|
|
|
5251 |
}, function(){
|
|
|
5252 |
while (true) try {
|
|
|
5253 |
walk(ast);
|
|
|
5254 |
break;
|
|
|
5255 |
} catch(ex) {
|
|
|
5256 |
if (ex === $stop) break;
|
|
|
5257 |
if (ex === $restart) continue;
|
|
|
5258 |
throw ex;
|
|
|
5259 |
}
|
|
|
5260 |
});
|
|
|
5261 |
};
|
|
|
5262 |
|
|
|
5263 |
function ast_lift_variables(ast) {
|
|
|
5264 |
var w = ast_walker(), walk = w.walk, scope;
|
|
|
5265 |
function do_body(body, env) {
|
|
|
5266 |
var _scope = scope;
|
|
|
5267 |
scope = env;
|
|
|
5268 |
body = MAP(body, walk);
|
|
|
5269 |
var hash = {}, names = MAP(env.names, function(type, name){
|
|
|
5270 |
if (type != "var") return MAP.skip;
|
|
|
5271 |
if (!env.references(name)) return MAP.skip;
|
|
|
5272 |
hash[name] = true;
|
|
|
5273 |
return [ name ];
|
|
|
5274 |
});
|
|
|
5275 |
if (names.length > 0) {
|
|
|
5276 |
// looking for assignments to any of these variables.
|
|
|
5277 |
// we can save considerable space by moving the definitions
|
|
|
5278 |
// in the var declaration.
|
|
|
5279 |
for_side_effects([ "block", body ], function(ast, walker, stop, restart) {
|
|
|
5280 |
if (ast[0] == "assign"
|
|
|
5281 |
&& ast[1] === true
|
|
|
5282 |
&& ast[2][0] == "name"
|
|
|
5283 |
&& HOP(hash, ast[2][1])) {
|
|
|
5284 |
// insert the definition into the var declaration
|
|
|
5285 |
for (var i = names.length; --i >= 0;) {
|
|
|
5286 |
if (names[i][0] == ast[2][1]) {
|
|
|
5287 |
if (names[i][1]) // this name already defined, we must stop
|
|
|
5288 |
stop();
|
|
|
5289 |
names[i][1] = ast[3]; // definition
|
|
|
5290 |
names.push(names.splice(i, 1)[0]);
|
|
|
5291 |
break;
|
|
|
5292 |
}
|
|
|
5293 |
}
|
|
|
5294 |
// remove this assignment from the AST.
|
|
|
5295 |
var p = walker.parent();
|
|
|
5296 |
if (p[0] == "seq") {
|
|
|
5297 |
var a = p[2];
|
|
|
5298 |
a.unshift(0, p.length);
|
|
|
5299 |
p.splice.apply(p, a);
|
|
|
5300 |
}
|
|
|
5301 |
else if (p[0] == "stat") {
|
|
|
5302 |
p.splice(0, p.length, "block"); // empty statement
|
|
|
5303 |
}
|
|
|
5304 |
else {
|
|
|
5305 |
stop();
|
|
|
5306 |
}
|
|
|
5307 |
restart();
|
|
|
5308 |
}
|
|
|
5309 |
stop();
|
|
|
5310 |
});
|
|
|
5311 |
body.unshift([ "var", names ]);
|
|
|
5312 |
}
|
|
|
5313 |
scope = _scope;
|
|
|
5314 |
return body;
|
|
|
5315 |
};
|
|
|
5316 |
function _vardefs(defs) {
|
|
|
5317 |
var ret = null;
|
|
|
5318 |
for (var i = defs.length; --i >= 0;) {
|
|
|
5319 |
var d = defs[i];
|
|
|
5320 |
if (!d[1]) continue;
|
|
|
5321 |
d = [ "assign", true, [ "name", d[0] ], d[1] ];
|
|
|
5322 |
if (ret == null) ret = d;
|
|
|
5323 |
else ret = [ "seq", d, ret ];
|
|
|
5324 |
}
|
|
|
5325 |
if (ret == null) {
|
|
|
5326 |
if (w.parent()[0] == "for-in")
|
|
|
5327 |
return [ "name", defs[0][0] ];
|
|
|
5328 |
return MAP.skip;
|
|
|
5329 |
}
|
|
|
5330 |
return [ "stat", ret ];
|
|
|
5331 |
};
|
|
|
5332 |
function _toplevel(body) {
|
|
|
5333 |
return [ this[0], do_body(body, this.scope) ];
|
|
|
5334 |
};
|
|
|
5335 |
return w.with_walkers({
|
|
|
5336 |
"function": function(name, args, body){
|
|
|
5337 |
for (var i = args.length; --i >= 0 && !body.scope.references(args[i]);)
|
|
|
5338 |
args.pop();
|
|
|
5339 |
if (!body.scope.references(name)) name = null;
|
|
|
5340 |
return [ this[0], name, args, do_body(body, body.scope) ];
|
|
|
5341 |
},
|
|
|
5342 |
"defun": function(name, args, body){
|
|
|
5343 |
if (!scope.references(name)) return MAP.skip;
|
|
|
5344 |
for (var i = args.length; --i >= 0 && !body.scope.references(args[i]);)
|
|
|
5345 |
args.pop();
|
|
|
5346 |
return [ this[0], name, args, do_body(body, body.scope) ];
|
|
|
5347 |
},
|
|
|
5348 |
"var": _vardefs,
|
|
|
5349 |
"toplevel": _toplevel
|
|
|
5350 |
}, function(){
|
|
|
5351 |
return walk(ast_add_scope(ast));
|
|
|
5352 |
});
|
|
|
5353 |
};
|
|
|
5354 |
|
|
|
5355 |
function ast_squeeze(ast, options) {
|
|
|
5356 |
options = defaults(options, {
|
|
|
5357 |
make_seqs : true,
|
|
|
5358 |
dead_code : true,
|
|
|
5359 |
no_warnings : false
|
|
|
5360 |
});
|
|
|
5361 |
|
|
|
5362 |
var w = ast_walker(), walk = w.walk, scope;
|
|
|
5363 |
|
|
|
5364 |
function negate(c) {
|
|
|
5365 |
var not_c = [ "unary-prefix", "!", c ];
|
|
|
5366 |
switch (c[0]) {
|
|
|
5367 |
case "unary-prefix":
|
|
|
5368 |
return c[1] == "!" && boolean_expr(c[2]) ? c[2] : not_c;
|
|
|
5369 |
case "seq":
|
|
|
5370 |
c = slice(c);
|
|
|
5371 |
c[c.length - 1] = negate(c[c.length - 1]);
|
|
|
5372 |
return c;
|
|
|
5373 |
case "conditional":
|
|
|
5374 |
return best_of(not_c, [ "conditional", c[1], negate(c[2]), negate(c[3]) ]);
|
|
|
5375 |
case "binary":
|
|
|
5376 |
var op = c[1], left = c[2], right = c[3];
|
|
|
5377 |
switch (op) {
|
|
|
5378 |
case "<=" : return [ "binary", ">", left, right ];
|
|
|
5379 |
case "<" : return [ "binary", ">=", left, right ];
|
|
|
5380 |
case ">=" : return [ "binary", "<", left, right ];
|
|
|
5381 |
case ">" : return [ "binary", "<=", left, right ];
|
|
|
5382 |
case "==" : return [ "binary", "!=", left, right ];
|
|
|
5383 |
case "!=" : return [ "binary", "==", left, right ];
|
|
|
5384 |
case "===" : return [ "binary", "!==", left, right ];
|
|
|
5385 |
case "!==" : return [ "binary", "===", left, right ];
|
|
|
5386 |
case "&&" : return best_of(not_c, [ "binary", "||", negate(left), negate(right) ]);
|
|
|
5387 |
case "||" : return best_of(not_c, [ "binary", "&&", negate(left), negate(right) ]);
|
|
|
5388 |
}
|
|
|
5389 |
break;
|
|
|
5390 |
}
|
|
|
5391 |
return not_c;
|
|
|
5392 |
};
|
|
|
5393 |
|
|
|
5394 |
function make_conditional(c, t, e) {
|
|
|
5395 |
var make_real_conditional = function() {
|
|
|
5396 |
if (c[0] == "unary-prefix" && c[1] == "!") {
|
|
|
5397 |
return e ? [ "conditional", c[2], e, t ] : [ "binary", "||", c[2], t ];
|
|
|
5398 |
} else {
|
|
|
5399 |
return e ? best_of(
|
|
|
5400 |
[ "conditional", c, t, e ],
|
|
|
5401 |
[ "conditional", negate(c), e, t ]
|
|
|
5402 |
) : [ "binary", "&&", c, t ];
|
|
|
5403 |
}
|
|
|
5404 |
};
|
|
|
5405 |
// shortcut the conditional if the expression has a constant value
|
|
|
5406 |
return when_constant(c, function(ast, val){
|
|
|
5407 |
warn_unreachable(val ? e : t);
|
|
|
5408 |
return (val ? t : e);
|
|
|
5409 |
}, make_real_conditional);
|
|
|
5410 |
};
|
|
|
5411 |
|
|
|
5412 |
function with_scope(s, cont) {
|
|
|
5413 |
var _scope = scope;
|
|
|
5414 |
scope = s;
|
|
|
5415 |
var ret = cont();
|
|
|
5416 |
ret.scope = s;
|
|
|
5417 |
scope = _scope;
|
|
|
5418 |
return ret;
|
|
|
5419 |
};
|
|
|
5420 |
|
|
|
5421 |
function rmblock(block) {
|
|
|
5422 |
if (block != null && block[0] == "block" && block[1]) {
|
|
|
5423 |
if (block[1].length == 1)
|
|
|
5424 |
block = block[1][0];
|
|
|
5425 |
else if (block[1].length == 0)
|
|
|
5426 |
block = [ "block" ];
|
|
|
5427 |
}
|
|
|
5428 |
return block;
|
|
|
5429 |
};
|
|
|
5430 |
|
|
|
5431 |
function _lambda(name, args, body) {
|
|
|
5432 |
var is_defun = this[0] == "defun";
|
|
|
5433 |
body = with_scope(body.scope, function(){
|
|
|
5434 |
var ret = tighten(body, "lambda");
|
|
|
5435 |
if (!is_defun && name && !scope.references(name))
|
|
|
5436 |
name = null;
|
|
|
5437 |
return ret;
|
|
|
5438 |
});
|
|
|
5439 |
return [ this[0], name, args, body ];
|
|
|
5440 |
};
|
|
|
5441 |
|
|
|
5442 |
// this function does a few things:
|
|
|
5443 |
// 1. discard useless blocks
|
|
|
5444 |
// 2. join consecutive var declarations
|
|
|
5445 |
// 3. remove obviously dead code
|
|
|
5446 |
// 4. transform consecutive statements using the comma operator
|
|
|
5447 |
// 5. if block_type == "lambda" and it detects constructs like if(foo) return ... - rewrite like if (!foo) { ... }
|
|
|
5448 |
function tighten(statements, block_type) {
|
|
|
5449 |
statements = MAP(statements, walk);
|
|
|
5450 |
|
|
|
5451 |
statements = statements.reduce(function(a, stat){
|
|
|
5452 |
if (stat[0] == "block") {
|
|
|
5453 |
if (stat[1]) {
|
|
|
5454 |
a.push.apply(a, stat[1]);
|
|
|
5455 |
}
|
|
|
5456 |
} else {
|
|
|
5457 |
a.push(stat);
|
|
|
5458 |
}
|
|
|
5459 |
return a;
|
|
|
5460 |
}, []);
|
|
|
5461 |
|
|
|
5462 |
statements = (function(a, prev){
|
|
|
5463 |
statements.forEach(function(cur){
|
|
|
5464 |
if (prev && ((cur[0] == "var" && prev[0] == "var") ||
|
|
|
5465 |
(cur[0] == "const" && prev[0] == "const"))) {
|
|
|
5466 |
prev[1] = prev[1].concat(cur[1]);
|
|
|
5467 |
} else {
|
|
|
5468 |
a.push(cur);
|
|
|
5469 |
prev = cur;
|
|
|
5470 |
}
|
|
|
5471 |
});
|
|
|
5472 |
return a;
|
|
|
5473 |
})([]);
|
|
|
5474 |
|
|
|
5475 |
if (options.dead_code) statements = (function(a, has_quit){
|
|
|
5476 |
statements.forEach(function(st){
|
|
|
5477 |
if (has_quit) {
|
|
|
5478 |
if (st[0] == "function" || st[0] == "defun") {
|
|
|
5479 |
a.push(st);
|
|
|
5480 |
}
|
|
|
5481 |
else if (st[0] == "var" || st[0] == "const") {
|
|
|
5482 |
if (!options.no_warnings)
|
|
|
5483 |
warn("Variables declared in unreachable code");
|
|
|
5484 |
st[1] = MAP(st[1], function(def){
|
|
|
5485 |
if (def[1] && !options.no_warnings)
|
|
|
5486 |
warn_unreachable([ "assign", true, [ "name", def[0] ], def[1] ]);
|
|
|
5487 |
return [ def[0] ];
|
|
|
5488 |
});
|
|
|
5489 |
a.push(st);
|
|
|
5490 |
}
|
|
|
5491 |
else if (!options.no_warnings)
|
|
|
5492 |
warn_unreachable(st);
|
|
|
5493 |
}
|
|
|
5494 |
else {
|
|
|
5495 |
a.push(st);
|
|
|
5496 |
if (member(st[0], [ "return", "throw", "break", "continue" ]))
|
|
|
5497 |
has_quit = true;
|
|
|
5498 |
}
|
|
|
5499 |
});
|
|
|
5500 |
return a;
|
|
|
5501 |
})([]);
|
|
|
5502 |
|
|
|
5503 |
if (options.make_seqs) statements = (function(a, prev) {
|
|
|
5504 |
statements.forEach(function(cur){
|
|
|
5505 |
if (prev && prev[0] == "stat" && cur[0] == "stat") {
|
|
|
5506 |
prev[1] = [ "seq", prev[1], cur[1] ];
|
|
|
5507 |
} else {
|
|
|
5508 |
a.push(cur);
|
|
|
5509 |
prev = cur;
|
|
|
5510 |
}
|
|
|
5511 |
});
|
|
|
5512 |
if (a.length >= 2
|
|
|
5513 |
&& a[a.length-2][0] == "stat"
|
|
|
5514 |
&& (a[a.length-1][0] == "return" || a[a.length-1][0] == "throw")
|
|
|
5515 |
&& a[a.length-1][1])
|
|
|
5516 |
{
|
|
|
5517 |
a.splice(a.length - 2, 2,
|
|
|
5518 |
[ a[a.length-1][0],
|
|
|
5519 |
[ "seq", a[a.length-2][1], a[a.length-1][1] ]]);
|
|
|
5520 |
}
|
|
|
5521 |
return a;
|
|
|
5522 |
})([]);
|
|
|
5523 |
|
|
|
5524 |
// this increases jQuery by 1K. Probably not such a good idea after all..
|
|
|
5525 |
// part of this is done in prepare_ifs anyway.
|
|
|
5526 |
// if (block_type == "lambda") statements = (function(i, a, stat){
|
|
|
5527 |
// while (i < statements.length) {
|
|
|
5528 |
// stat = statements[i++];
|
|
|
5529 |
// if (stat[0] == "if" && !stat[3]) {
|
|
|
5530 |
// if (stat[2][0] == "return" && stat[2][1] == null) {
|
|
|
5531 |
// a.push(make_if(negate(stat[1]), [ "block", statements.slice(i) ]));
|
|
|
5532 |
// break;
|
|
|
5533 |
// }
|
|
|
5534 |
// var last = last_stat(stat[2]);
|
|
|
5535 |
// if (last[0] == "return" && last[1] == null) {
|
|
|
5536 |
// a.push(make_if(stat[1], [ "block", stat[2][1].slice(0, -1) ], [ "block", statements.slice(i) ]));
|
|
|
5537 |
// break;
|
|
|
5538 |
// }
|
|
|
5539 |
// }
|
|
|
5540 |
// a.push(stat);
|
|
|
5541 |
// }
|
|
|
5542 |
// return a;
|
|
|
5543 |
// })(0, []);
|
|
|
5544 |
|
|
|
5545 |
return statements;
|
|
|
5546 |
};
|
|
|
5547 |
|
|
|
5548 |
function make_if(c, t, e) {
|
|
|
5549 |
return when_constant(c, function(ast, val){
|
|
|
5550 |
if (val) {
|
|
|
5551 |
t = walk(t);
|
|
|
5552 |
warn_unreachable(e);
|
|
|
5553 |
return t || [ "block" ];
|
|
|
5554 |
} else {
|
|
|
5555 |
e = walk(e);
|
|
|
5556 |
warn_unreachable(t);
|
|
|
5557 |
return e || [ "block" ];
|
|
|
5558 |
}
|
|
|
5559 |
}, function() {
|
|
|
5560 |
return make_real_if(c, t, e);
|
|
|
5561 |
});
|
|
|
5562 |
};
|
|
|
5563 |
|
|
|
5564 |
function make_real_if(c, t, e) {
|
|
|
5565 |
c = walk(c);
|
|
|
5566 |
t = walk(t);
|
|
|
5567 |
e = walk(e);
|
|
|
5568 |
|
|
|
5569 |
if (empty(t)) {
|
|
|
5570 |
c = negate(c);
|
|
|
5571 |
t = e;
|
|
|
5572 |
e = null;
|
|
|
5573 |
} else if (empty(e)) {
|
|
|
5574 |
e = null;
|
|
|
5575 |
} else {
|
|
|
5576 |
// if we have both else and then, maybe it makes sense to switch them?
|
|
|
5577 |
(function(){
|
|
|
5578 |
var a = gen_code(c);
|
|
|
5579 |
var n = negate(c);
|
|
|
5580 |
var b = gen_code(n);
|
|
|
5581 |
if (b.length < a.length) {
|
|
|
5582 |
var tmp = t;
|
|
|
5583 |
t = e;
|
|
|
5584 |
e = tmp;
|
|
|
5585 |
c = n;
|
|
|
5586 |
}
|
|
|
5587 |
})();
|
|
|
5588 |
}
|
|
|
5589 |
if (empty(e) && empty(t))
|
|
|
5590 |
return [ "stat", c ];
|
|
|
5591 |
var ret = [ "if", c, t, e ];
|
|
|
5592 |
if (t[0] == "if" && empty(t[3]) && empty(e)) {
|
|
|
5593 |
ret = best_of(ret, walk([ "if", [ "binary", "&&", c, t[1] ], t[2] ]));
|
|
|
5594 |
}
|
|
|
5595 |
else if (t[0] == "stat") {
|
|
|
5596 |
if (e) {
|
|
|
5597 |
if (e[0] == "stat") {
|
|
|
5598 |
ret = best_of(ret, [ "stat", make_conditional(c, t[1], e[1]) ]);
|
|
|
5599 |
}
|
|
|
5600 |
}
|
|
|
5601 |
else {
|
|
|
5602 |
ret = best_of(ret, [ "stat", make_conditional(c, t[1]) ]);
|
|
|
5603 |
}
|
|
|
5604 |
}
|
|
|
5605 |
else if (e && t[0] == e[0] && (t[0] == "return" || t[0] == "throw") && t[1] && e[1]) {
|
|
|
5606 |
ret = best_of(ret, [ t[0], make_conditional(c, t[1], e[1] ) ]);
|
|
|
5607 |
}
|
|
|
5608 |
else if (e && aborts(t)) {
|
|
|
5609 |
ret = [ [ "if", c, t ] ];
|
|
|
5610 |
if (e[0] == "block") {
|
|
|
5611 |
if (e[1]) ret = ret.concat(e[1]);
|
|
|
5612 |
}
|
|
|
5613 |
else {
|
|
|
5614 |
ret.push(e);
|
|
|
5615 |
}
|
|
|
5616 |
ret = walk([ "block", ret ]);
|
|
|
5617 |
}
|
|
|
5618 |
else if (t && aborts(e)) {
|
|
|
5619 |
ret = [ [ "if", negate(c), e ] ];
|
|
|
5620 |
if (t[0] == "block") {
|
|
|
5621 |
if (t[1]) ret = ret.concat(t[1]);
|
|
|
5622 |
} else {
|
|
|
5623 |
ret.push(t);
|
|
|
5624 |
}
|
|
|
5625 |
ret = walk([ "block", ret ]);
|
|
|
5626 |
}
|
|
|
5627 |
return ret;
|
|
|
5628 |
};
|
|
|
5629 |
|
|
|
5630 |
function _do_while(cond, body) {
|
|
|
5631 |
return when_constant(cond, function(cond, val){
|
|
|
5632 |
if (!val) {
|
|
|
5633 |
warn_unreachable(body);
|
|
|
5634 |
return [ "block" ];
|
|
|
5635 |
} else {
|
|
|
5636 |
return [ "for", null, null, null, walk(body) ];
|
|
|
5637 |
}
|
|
|
5638 |
});
|
|
|
5639 |
};
|
|
|
5640 |
|
|
|
5641 |
return w.with_walkers({
|
|
|
5642 |
"sub": function(expr, subscript) {
|
|
|
5643 |
if (subscript[0] == "string") {
|
|
|
5644 |
var name = subscript[1];
|
|
|
5645 |
if (is_identifier(name))
|
|
|
5646 |
return [ "dot", walk(expr), name ];
|
|
|
5647 |
else if (/^[1-9][0-9]*$/.test(name) || name === "0")
|
|
|
5648 |
return [ "sub", walk(expr), [ "num", parseInt(name, 10) ] ];
|
|
|
5649 |
}
|
|
|
5650 |
},
|
|
|
5651 |
"if": make_if,
|
|
|
5652 |
"toplevel": function(body) {
|
|
|
5653 |
return [ "toplevel", with_scope(this.scope, function(){
|
|
|
5654 |
return tighten(body);
|
|
|
5655 |
}) ];
|
|
|
5656 |
},
|
|
|
5657 |
"switch": function(expr, body) {
|
|
|
5658 |
var last = body.length - 1;
|
|
|
5659 |
return [ "switch", walk(expr), MAP(body, function(branch, i){
|
|
|
5660 |
var block = tighten(branch[1]);
|
|
|
5661 |
if (i == last && block.length > 0) {
|
|
|
5662 |
var node = block[block.length - 1];
|
|
|
5663 |
if (node[0] == "break" && !node[1])
|
|
|
5664 |
block.pop();
|
|
|
5665 |
}
|
|
|
5666 |
return [ branch[0] ? walk(branch[0]) : null, block ];
|
|
|
5667 |
}) ];
|
|
|
5668 |
},
|
|
|
5669 |
"function": _lambda,
|
|
|
5670 |
"defun": _lambda,
|
|
|
5671 |
"block": function(body) {
|
|
|
5672 |
if (body) return rmblock([ "block", tighten(body) ]);
|
|
|
5673 |
},
|
|
|
5674 |
"binary": function(op, left, right) {
|
|
|
5675 |
return when_constant([ "binary", op, walk(left), walk(right) ], function yes(c){
|
|
|
5676 |
return best_of(walk(c), this);
|
|
|
5677 |
}, function no() {
|
|
|
5678 |
return function(){
|
|
|
5679 |
if(op != "==" && op != "!=") return;
|
|
|
5680 |
var l = walk(left), r = walk(right);
|
|
|
5681 |
if(l && l[0] == "unary-prefix" && l[1] == "!" && l[2][0] == "num")
|
|
|
5682 |
left = ['num', +!l[2][1]];
|
|
|
5683 |
else if (r && r[0] == "unary-prefix" && r[1] == "!" && r[2][0] == "num")
|
|
|
5684 |
right = ['num', +!r[2][1]];
|
|
|
5685 |
return ["binary", op, left, right];
|
|
|
5686 |
}() || this;
|
|
|
5687 |
});
|
|
|
5688 |
},
|
|
|
5689 |
"conditional": function(c, t, e) {
|
|
|
5690 |
return make_conditional(walk(c), walk(t), walk(e));
|
|
|
5691 |
},
|
|
|
5692 |
"try": function(t, c, f) {
|
|
|
5693 |
return [
|
|
|
5694 |
"try",
|
|
|
5695 |
tighten(t),
|
|
|
5696 |
c != null ? [ c[0], tighten(c[1]) ] : null,
|
|
|
5697 |
f != null ? tighten(f) : null
|
|
|
5698 |
];
|
|
|
5699 |
},
|
|
|
5700 |
"unary-prefix": function(op, expr) {
|
|
|
5701 |
expr = walk(expr);
|
|
|
5702 |
var ret = [ "unary-prefix", op, expr ];
|
|
|
5703 |
if (op == "!")
|
|
|
5704 |
ret = best_of(ret, negate(expr));
|
|
|
5705 |
return when_constant(ret, function(ast, val){
|
|
|
5706 |
return walk(ast); // it's either true or false, so minifies to !0 or !1
|
|
|
5707 |
}, function() { return ret });
|
|
|
5708 |
},
|
|
|
5709 |
"name": function(name) {
|
|
|
5710 |
switch (name) {
|
|
|
5711 |
case "true": return [ "unary-prefix", "!", [ "num", 0 ]];
|
|
|
5712 |
case "false": return [ "unary-prefix", "!", [ "num", 1 ]];
|
|
|
5713 |
}
|
|
|
5714 |
},
|
|
|
5715 |
"while": _do_while,
|
|
|
5716 |
"assign": function(op, lvalue, rvalue) {
|
|
|
5717 |
lvalue = walk(lvalue);
|
|
|
5718 |
rvalue = walk(rvalue);
|
|
|
5719 |
var okOps = [ '+', '-', '/', '*', '%', '>>', '<<', '>>>', '|', '^', '&' ];
|
|
|
5720 |
if (op === true && lvalue[0] === "name" && rvalue[0] === "binary" &&
|
|
|
5721 |
~okOps.indexOf(rvalue[1]) && rvalue[2][0] === "name" &&
|
|
|
5722 |
rvalue[2][1] === lvalue[1]) {
|
|
|
5723 |
return [ this[0], rvalue[1], lvalue, rvalue[3] ]
|
|
|
5724 |
}
|
|
|
5725 |
return [ this[0], op, lvalue, rvalue ];
|
|
|
5726 |
}
|
|
|
5727 |
}, function() {
|
|
|
5728 |
for (var i = 0; i < 2; ++i) {
|
|
|
5729 |
ast = prepare_ifs(ast);
|
|
|
5730 |
ast = ast_add_scope(ast);
|
|
|
5731 |
ast = walk(ast);
|
|
|
5732 |
}
|
|
|
5733 |
return ast;
|
|
|
5734 |
});
|
|
|
5735 |
};
|
|
|
5736 |
|
|
|
5737 |
/* -----[ re-generate code from the AST ]----- */
|
|
|
5738 |
|
|
|
5739 |
var DOT_CALL_NO_PARENS = jsp.array_to_hash([
|
|
|
5740 |
"name",
|
|
|
5741 |
"array",
|
|
|
5742 |
"object",
|
|
|
5743 |
"string",
|
|
|
5744 |
"dot",
|
|
|
5745 |
"sub",
|
|
|
5746 |
"call",
|
|
|
5747 |
"regexp"
|
|
|
5748 |
]);
|
|
|
5749 |
|
|
|
5750 |
function make_string(str, ascii_only) {
|
|
|
5751 |
var dq = 0, sq = 0;
|
|
|
5752 |
str = str.replace(/[\\\b\f\n\r\t\x22\x27\u2028\u2029\0]/g, function(s){
|
|
|
5753 |
switch (s) {
|
|
|
5754 |
case "\\": return "\\\\";
|
|
|
5755 |
case "\b": return "\\b";
|
|
|
5756 |
case "\f": return "\\f";
|
|
|
5757 |
case "\n": return "\\n";
|
|
|
5758 |
case "\r": return "\\r";
|
|
|
5759 |
case "\t": return "\\t";
|
|
|
5760 |
case "\u2028": return "\\u2028";
|
|
|
5761 |
case "\u2029": return "\\u2029";
|
|
|
5762 |
case '"': ++dq; return '"';
|
|
|
5763 |
case "'": ++sq; return "'";
|
|
|
5764 |
case "\0": return "\\0";
|
|
|
5765 |
}
|
|
|
5766 |
return s;
|
|
|
5767 |
});
|
|
|
5768 |
if (ascii_only) str = to_ascii(str);
|
|
|
5769 |
if (dq > sq) return "'" + str.replace(/\x27/g, "\\'") + "'";
|
|
|
5770 |
else return '"' + str.replace(/\x22/g, '\\"') + '"';
|
|
|
5771 |
};
|
|
|
5772 |
|
|
|
5773 |
function to_ascii(str) {
|
|
|
5774 |
return str.replace(/[\u0080-\uffff]/g, function(ch) {
|
|
|
5775 |
var code = ch.charCodeAt(0).toString(16);
|
|
|
5776 |
while (code.length < 4) code = "0" + code;
|
|
|
5777 |
return "\\u" + code;
|
|
|
5778 |
});
|
|
|
5779 |
};
|
|
|
5780 |
|
|
|
5781 |
var SPLICE_NEEDS_BRACKETS = jsp.array_to_hash([ "if", "while", "do", "for", "for-in", "with" ]);
|
|
|
5782 |
|
|
|
5783 |
function gen_code(ast, options) {
|
|
|
5784 |
options = defaults(options, {
|
|
|
5785 |
indent_start : 0,
|
|
|
5786 |
indent_level : 4,
|
|
|
5787 |
quote_keys : false,
|
|
|
5788 |
space_colon : false,
|
|
|
5789 |
beautify : false,
|
|
|
5790 |
ascii_only : false,
|
|
|
5791 |
inline_script: false
|
|
|
5792 |
});
|
|
|
5793 |
var beautify = !!options.beautify;
|
|
|
5794 |
var indentation = 0,
|
|
|
5795 |
newline = beautify ? "\n" : "",
|
|
|
5796 |
space = beautify ? " " : "";
|
|
|
5797 |
|
|
|
5798 |
function encode_string(str) {
|
|
|
5799 |
var ret = make_string(str, options.ascii_only);
|
|
|
5800 |
if (options.inline_script)
|
|
|
5801 |
ret = ret.replace(/<\x2fscript([>/\t\n\f\r ])/gi, "<\\/script$1");
|
|
|
5802 |
return ret;
|
|
|
5803 |
};
|
|
|
5804 |
|
|
|
5805 |
function make_name(name) {
|
|
|
5806 |
name = name.toString();
|
|
|
5807 |
if (options.ascii_only)
|
|
|
5808 |
name = to_ascii(name);
|
|
|
5809 |
return name;
|
|
|
5810 |
};
|
|
|
5811 |
|
|
|
5812 |
function indent(line) {
|
|
|
5813 |
if (line == null)
|
|
|
5814 |
line = "";
|
|
|
5815 |
if (beautify)
|
|
|
5816 |
line = repeat_string(" ", options.indent_start + indentation * options.indent_level) + line;
|
|
|
5817 |
return line;
|
|
|
5818 |
};
|
|
|
5819 |
|
|
|
5820 |
function with_indent(cont, incr) {
|
|
|
5821 |
if (incr == null) incr = 1;
|
|
|
5822 |
indentation += incr;
|
|
|
5823 |
try { return cont.apply(null, slice(arguments, 1)); }
|
|
|
5824 |
finally { indentation -= incr; }
|
|
|
5825 |
};
|
|
|
5826 |
|
|
|
5827 |
function add_spaces(a) {
|
|
|
5828 |
if (beautify)
|
|
|
5829 |
return a.join(" ");
|
|
|
5830 |
var b = [];
|
|
|
5831 |
for (var i = 0; i < a.length; ++i) {
|
|
|
5832 |
var next = a[i + 1];
|
|
|
5833 |
b.push(a[i]);
|
|
|
5834 |
if (next &&
|
|
|
5835 |
((/[a-z0-9_\x24]$/i.test(a[i].toString()) && /^[a-z0-9_\x24]/i.test(next.toString())) ||
|
|
|
5836 |
(/[\+\-]$/.test(a[i].toString()) && /^[\+\-]/.test(next.toString())))) {
|
|
|
5837 |
b.push(" ");
|
|
|
5838 |
}
|
|
|
5839 |
}
|
|
|
5840 |
return b.join("");
|
|
|
5841 |
};
|
|
|
5842 |
|
|
|
5843 |
function add_commas(a) {
|
|
|
5844 |
return a.join("," + space);
|
|
|
5845 |
};
|
|
|
5846 |
|
|
|
5847 |
function parenthesize(expr) {
|
|
|
5848 |
var gen = make(expr);
|
|
|
5849 |
for (var i = 1; i < arguments.length; ++i) {
|
|
|
5850 |
var el = arguments[i];
|
|
|
5851 |
if ((el instanceof Function && el(expr)) || expr[0] == el)
|
|
|
5852 |
return "(" + gen + ")";
|
|
|
5853 |
}
|
|
|
5854 |
return gen;
|
|
|
5855 |
};
|
|
|
5856 |
|
|
|
5857 |
function best_of(a) {
|
|
|
5858 |
if (a.length == 1) {
|
|
|
5859 |
return a[0];
|
|
|
5860 |
}
|
|
|
5861 |
if (a.length == 2) {
|
|
|
5862 |
var b = a[1];
|
|
|
5863 |
a = a[0];
|
|
|
5864 |
return a.length <= b.length ? a : b;
|
|
|
5865 |
}
|
|
|
5866 |
return best_of([ a[0], best_of(a.slice(1)) ]);
|
|
|
5867 |
};
|
|
|
5868 |
|
|
|
5869 |
function needs_parens(expr) {
|
|
|
5870 |
if (expr[0] == "function" || expr[0] == "object") {
|
|
|
5871 |
// dot/call on a literal function requires the
|
|
|
5872 |
// function literal itself to be parenthesized
|
|
|
5873 |
// only if it's the first "thing" in a
|
|
|
5874 |
// statement. This means that the parent is
|
|
|
5875 |
// "stat", but it could also be a "seq" and
|
|
|
5876 |
// we're the first in this "seq" and the
|
|
|
5877 |
// parent is "stat", and so on. Messy stuff,
|
|
|
5878 |
// but it worths the trouble.
|
|
|
5879 |
var a = slice(w.stack()), self = a.pop(), p = a.pop();
|
|
|
5880 |
while (p) {
|
|
|
5881 |
if (p[0] == "stat") return true;
|
|
|
5882 |
if (((p[0] == "seq" || p[0] == "call" || p[0] == "dot" || p[0] == "sub" || p[0] == "conditional") && p[1] === self) ||
|
|
|
5883 |
((p[0] == "binary" || p[0] == "assign" || p[0] == "unary-postfix") && p[2] === self)) {
|
|
|
5884 |
self = p;
|
|
|
5885 |
p = a.pop();
|
|
|
5886 |
} else {
|
|
|
5887 |
return false;
|
|
|
5888 |
}
|
|
|
5889 |
}
|
|
|
5890 |
}
|
|
|
5891 |
return !HOP(DOT_CALL_NO_PARENS, expr[0]);
|
|
|
5892 |
};
|
|
|
5893 |
|
|
|
5894 |
function make_num(num) {
|
|
|
5895 |
var str = num.toString(10), a = [ str.replace(/^0\./, ".") ], m;
|
|
|
5896 |
if (Math.floor(num) === num) {
|
|
|
5897 |
if (num >= 0) {
|
|
|
5898 |
a.push("0x" + num.toString(16).toLowerCase(), // probably pointless
|
|
|
5899 |
"0" + num.toString(8)); // same.
|
|
|
5900 |
} else {
|
|
|
5901 |
a.push("-0x" + (-num).toString(16).toLowerCase(), // probably pointless
|
|
|
5902 |
"-0" + (-num).toString(8)); // same.
|
|
|
5903 |
}
|
|
|
5904 |
if ((m = /^(.*?)(0+)$/.exec(num))) {
|
|
|
5905 |
a.push(m[1] + "e" + m[2].length);
|
|
|
5906 |
}
|
|
|
5907 |
} else if ((m = /^0?\.(0+)(.*)$/.exec(num))) {
|
|
|
5908 |
a.push(m[2] + "e-" + (m[1].length + m[2].length),
|
|
|
5909 |
str.substr(str.indexOf(".")));
|
|
|
5910 |
}
|
|
|
5911 |
return best_of(a);
|
|
|
5912 |
};
|
|
|
5913 |
|
|
|
5914 |
var w = ast_walker();
|
|
|
5915 |
var make = w.walk;
|
|
|
5916 |
return w.with_walkers({
|
|
|
5917 |
"string": encode_string,
|
|
|
5918 |
"num": make_num,
|
|
|
5919 |
"name": make_name,
|
|
|
5920 |
"toplevel": function(statements) {
|
|
|
5921 |
return make_block_statements(statements)
|
|
|
5922 |
.join(newline + newline);
|
|
|
5923 |
},
|
|
|
5924 |
"splice": function(statements) {
|
|
|
5925 |
var parent = w.parent();
|
|
|
5926 |
if (HOP(SPLICE_NEEDS_BRACKETS, parent)) {
|
|
|
5927 |
// we need block brackets in this case
|
|
|
5928 |
return make_block.apply(this, arguments);
|
|
|
5929 |
} else {
|
|
|
5930 |
return MAP(make_block_statements(statements, true),
|
|
|
5931 |
function(line, i) {
|
|
|
5932 |
// the first line is already indented
|
|
|
5933 |
return i > 0 ? indent(line) : line;
|
|
|
5934 |
}).join(newline);
|
|
|
5935 |
}
|
|
|
5936 |
},
|
|
|
5937 |
"block": make_block,
|
|
|
5938 |
"var": function(defs) {
|
|
|
5939 |
return "var " + add_commas(MAP(defs, make_1vardef)) + ";";
|
|
|
5940 |
},
|
|
|
5941 |
"const": function(defs) {
|
|
|
5942 |
return "const " + add_commas(MAP(defs, make_1vardef)) + ";";
|
|
|
5943 |
},
|
|
|
5944 |
"try": function(tr, ca, fi) {
|
|
|
5945 |
var out = [ "try", make_block(tr) ];
|
|
|
5946 |
if (ca) out.push("catch", "(" + ca[0] + ")", make_block(ca[1]));
|
|
|
5947 |
if (fi) out.push("finally", make_block(fi));
|
|
|
5948 |
return add_spaces(out);
|
|
|
5949 |
},
|
|
|
5950 |
"throw": function(expr) {
|
|
|
5951 |
return add_spaces([ "throw", make(expr) ]) + ";";
|
|
|
5952 |
},
|
|
|
5953 |
"new": function(ctor, args) {
|
|
|
5954 |
args = args.length > 0 ? "(" + add_commas(MAP(args, function(expr){
|
|
|
5955 |
return parenthesize(expr, "seq");
|
|
|
5956 |
})) + ")" : "";
|
|
|
5957 |
return add_spaces([ "new", parenthesize(ctor, "seq", "binary", "conditional", "assign", function(expr){
|
|
|
5958 |
var w = ast_walker(), has_call = {};
|
|
|
5959 |
try {
|
|
|
5960 |
w.with_walkers({
|
|
|
5961 |
"call": function() { throw has_call },
|
|
|
5962 |
"function": function() { return this }
|
|
|
5963 |
}, function(){
|
|
|
5964 |
w.walk(expr);
|
|
|
5965 |
});
|
|
|
5966 |
} catch(ex) {
|
|
|
5967 |
if (ex === has_call)
|
|
|
5968 |
return true;
|
|
|
5969 |
throw ex;
|
|
|
5970 |
}
|
|
|
5971 |
}) + args ]);
|
|
|
5972 |
},
|
|
|
5973 |
"switch": function(expr, body) {
|
|
|
5974 |
return add_spaces([ "switch", "(" + make(expr) + ")", make_switch_block(body) ]);
|
|
|
5975 |
},
|
|
|
5976 |
"break": function(label) {
|
|
|
5977 |
var out = "break";
|
|
|
5978 |
if (label != null)
|
|
|
5979 |
out += " " + make_name(label);
|
|
|
5980 |
return out + ";";
|
|
|
5981 |
},
|
|
|
5982 |
"continue": function(label) {
|
|
|
5983 |
var out = "continue";
|
|
|
5984 |
if (label != null)
|
|
|
5985 |
out += " " + make_name(label);
|
|
|
5986 |
return out + ";";
|
|
|
5987 |
},
|
|
|
5988 |
"conditional": function(co, th, el) {
|
|
|
5989 |
return add_spaces([ parenthesize(co, "assign", "seq", "conditional"), "?",
|
|
|
5990 |
parenthesize(th, "seq"), ":",
|
|
|
5991 |
parenthesize(el, "seq") ]);
|
|
|
5992 |
},
|
|
|
5993 |
"assign": function(op, lvalue, rvalue) {
|
|
|
5994 |
if (op && op !== true) op += "=";
|
|
|
5995 |
else op = "=";
|
|
|
5996 |
return add_spaces([ make(lvalue), op, parenthesize(rvalue, "seq") ]);
|
|
|
5997 |
},
|
|
|
5998 |
"dot": function(expr) {
|
|
|
5999 |
var out = make(expr), i = 1;
|
|
|
6000 |
if (expr[0] == "num") {
|
|
|
6001 |
if (!/\./.test(expr[1]))
|
|
|
6002 |
out += ".";
|
|
|
6003 |
} else if (needs_parens(expr))
|
|
|
6004 |
out = "(" + out + ")";
|
|
|
6005 |
while (i < arguments.length)
|
|
|
6006 |
out += "." + make_name(arguments[i++]);
|
|
|
6007 |
return out;
|
|
|
6008 |
},
|
|
|
6009 |
"call": function(func, args) {
|
|
|
6010 |
var f = make(func);
|
|
|
6011 |
if (needs_parens(func))
|
|
|
6012 |
f = "(" + f + ")";
|
|
|
6013 |
return f + "(" + add_commas(MAP(args, function(expr){
|
|
|
6014 |
return parenthesize(expr, "seq");
|
|
|
6015 |
})) + ")";
|
|
|
6016 |
},
|
|
|
6017 |
"function": make_function,
|
|
|
6018 |
"defun": make_function,
|
|
|
6019 |
"if": function(co, th, el) {
|
|
|
6020 |
var out = [ "if", "(" + make(co) + ")", el ? make_then(th) : make(th) ];
|
|
|
6021 |
if (el) {
|
|
|
6022 |
out.push("else", make(el));
|
|
|
6023 |
}
|
|
|
6024 |
return add_spaces(out);
|
|
|
6025 |
},
|
|
|
6026 |
"for": function(init, cond, step, block) {
|
|
|
6027 |
var out = [ "for" ];
|
|
|
6028 |
init = (init != null ? make(init) : "").replace(/;*\s*$/, ";" + space);
|
|
|
6029 |
cond = (cond != null ? make(cond) : "").replace(/;*\s*$/, ";" + space);
|
|
|
6030 |
step = (step != null ? make(step) : "").replace(/;*\s*$/, "");
|
|
|
6031 |
var args = init + cond + step;
|
|
|
6032 |
if (args == "; ; ") args = ";;";
|
|
|
6033 |
out.push("(" + args + ")", make(block));
|
|
|
6034 |
return add_spaces(out);
|
|
|
6035 |
},
|
|
|
6036 |
"for-in": function(vvar, key, hash, block) {
|
|
|
6037 |
return add_spaces([ "for", "(" +
|
|
|
6038 |
(vvar ? make(vvar).replace(/;+$/, "") : make(key)),
|
|
|
6039 |
"in",
|
|
|
6040 |
make(hash) + ")", make(block) ]);
|
|
|
6041 |
},
|
|
|
6042 |
"while": function(condition, block) {
|
|
|
6043 |
return add_spaces([ "while", "(" + make(condition) + ")", make(block) ]);
|
|
|
6044 |
},
|
|
|
6045 |
"do": function(condition, block) {
|
|
|
6046 |
return add_spaces([ "do", make(block), "while", "(" + make(condition) + ")" ]) + ";";
|
|
|
6047 |
},
|
|
|
6048 |
"return": function(expr) {
|
|
|
6049 |
var out = [ "return" ];
|
|
|
6050 |
if (expr != null) out.push(make(expr));
|
|
|
6051 |
return add_spaces(out) + ";";
|
|
|
6052 |
},
|
|
|
6053 |
"binary": function(operator, lvalue, rvalue) {
|
|
|
6054 |
var left = make(lvalue), right = make(rvalue);
|
|
|
6055 |
// XXX: I'm pretty sure other cases will bite here.
|
|
|
6056 |
// we need to be smarter.
|
|
|
6057 |
// adding parens all the time is the safest bet.
|
|
|
6058 |
if (member(lvalue[0], [ "assign", "conditional", "seq" ]) ||
|
|
|
6059 |
lvalue[0] == "binary" && PRECEDENCE[operator] > PRECEDENCE[lvalue[1]] ||
|
|
|
6060 |
lvalue[0] == "function" && needs_parens(this)) {
|
|
|
6061 |
left = "(" + left + ")";
|
|
|
6062 |
}
|
|
|
6063 |
if (member(rvalue[0], [ "assign", "conditional", "seq" ]) ||
|
|
|
6064 |
rvalue[0] == "binary" && PRECEDENCE[operator] >= PRECEDENCE[rvalue[1]] &&
|
|
|
6065 |
!(rvalue[1] == operator && member(operator, [ "&&", "||", "*" ]))) {
|
|
|
6066 |
right = "(" + right + ")";
|
|
|
6067 |
}
|
|
|
6068 |
else if (!beautify && options.inline_script && (operator == "<" || operator == "<<")
|
|
|
6069 |
&& rvalue[0] == "regexp" && /^script/i.test(rvalue[1])) {
|
|
|
6070 |
right = " " + right;
|
|
|
6071 |
}
|
|
|
6072 |
return add_spaces([ left, operator, right ]);
|
|
|
6073 |
},
|
|
|
6074 |
"unary-prefix": function(operator, expr) {
|
|
|
6075 |
var val = make(expr);
|
|
|
6076 |
if (!(expr[0] == "num" || (expr[0] == "unary-prefix" && !HOP(OPERATORS, operator + expr[1])) || !needs_parens(expr)))
|
|
|
6077 |
val = "(" + val + ")";
|
|
|
6078 |
return operator + (jsp.is_alphanumeric_char(operator.charAt(0)) ? " " : "") + val;
|
|
|
6079 |
},
|
|
|
6080 |
"unary-postfix": function(operator, expr) {
|
|
|
6081 |
var val = make(expr);
|
|
|
6082 |
if (!(expr[0] == "num" || (expr[0] == "unary-postfix" && !HOP(OPERATORS, operator + expr[1])) || !needs_parens(expr)))
|
|
|
6083 |
val = "(" + val + ")";
|
|
|
6084 |
return val + operator;
|
|
|
6085 |
},
|
|
|
6086 |
"sub": function(expr, subscript) {
|
|
|
6087 |
var hash = make(expr);
|
|
|
6088 |
if (needs_parens(expr))
|
|
|
6089 |
hash = "(" + hash + ")";
|
|
|
6090 |
return hash + "[" + make(subscript) + "]";
|
|
|
6091 |
},
|
|
|
6092 |
"object": function(props) {
|
|
|
6093 |
var obj_needs_parens = needs_parens(this);
|
|
|
6094 |
if (props.length == 0)
|
|
|
6095 |
return obj_needs_parens ? "({})" : "{}";
|
|
|
6096 |
var out = "{" + newline + with_indent(function(){
|
|
|
6097 |
return MAP(props, function(p){
|
|
|
6098 |
if (p.length == 3) {
|
|
|
6099 |
// getter/setter. The name is in p[0], the arg.list in p[1][2], the
|
|
|
6100 |
// body in p[1][3] and type ("get" / "set") in p[2].
|
|
|
6101 |
return indent(make_function(p[0], p[1][2], p[1][3], p[2]));
|
|
|
6102 |
}
|
|
|
6103 |
var key = p[0], val = parenthesize(p[1], "seq");
|
|
|
6104 |
if (options.quote_keys) {
|
|
|
6105 |
key = encode_string(key);
|
|
|
6106 |
} else if ((typeof key == "number" || !beautify && +key + "" == key)
|
|
|
6107 |
&& parseFloat(key) >= 0) {
|
|
|
6108 |
key = make_num(+key);
|
|
|
6109 |
} else if (!is_identifier(key)) {
|
|
|
6110 |
key = encode_string(key);
|
|
|
6111 |
}
|
|
|
6112 |
return indent(add_spaces(beautify && options.space_colon
|
|
|
6113 |
? [ key, ":", val ]
|
|
|
6114 |
: [ key + ":", val ]));
|
|
|
6115 |
}).join("," + newline);
|
|
|
6116 |
}) + newline + indent("}");
|
|
|
6117 |
return obj_needs_parens ? "(" + out + ")" : out;
|
|
|
6118 |
},
|
|
|
6119 |
"regexp": function(rx, mods) {
|
|
|
6120 |
return "/" + rx + "/" + mods;
|
|
|
6121 |
},
|
|
|
6122 |
"array": function(elements) {
|
|
|
6123 |
if (elements.length == 0) return "[]";
|
|
|
6124 |
return add_spaces([ "[", add_commas(MAP(elements, function(el, i){
|
|
|
6125 |
if (!beautify && el[0] == "atom" && el[1] == "undefined") return i === elements.length - 1 ? "," : "";
|
|
|
6126 |
return parenthesize(el, "seq");
|
|
|
6127 |
})), "]" ]);
|
|
|
6128 |
},
|
|
|
6129 |
"stat": function(stmt) {
|
|
|
6130 |
return make(stmt).replace(/;*\s*$/, ";");
|
|
|
6131 |
},
|
|
|
6132 |
"seq": function() {
|
|
|
6133 |
return add_commas(MAP(slice(arguments), make));
|
|
|
6134 |
},
|
|
|
6135 |
"label": function(name, block) {
|
|
|
6136 |
return add_spaces([ make_name(name), ":", make(block) ]);
|
|
|
6137 |
},
|
|
|
6138 |
"with": function(expr, block) {
|
|
|
6139 |
return add_spaces([ "with", "(" + make(expr) + ")", make(block) ]);
|
|
|
6140 |
},
|
|
|
6141 |
"atom": function(name) {
|
|
|
6142 |
return make_name(name);
|
|
|
6143 |
}
|
|
|
6144 |
}, function(){ return make(ast) });
|
|
|
6145 |
|
|
|
6146 |
// The squeezer replaces "block"-s that contain only a single
|
|
|
6147 |
// statement with the statement itself; technically, the AST
|
|
|
6148 |
// is correct, but this can create problems when we output an
|
|
|
6149 |
// IF having an ELSE clause where the THEN clause ends in an
|
|
|
6150 |
// IF *without* an ELSE block (then the outer ELSE would refer
|
|
|
6151 |
// to the inner IF). This function checks for this case and
|
|
|
6152 |
// adds the block brackets if needed.
|
|
|
6153 |
function make_then(th) {
|
|
|
6154 |
if (th == null) return ";";
|
|
|
6155 |
if (th[0] == "do") {
|
|
|
6156 |
// https://github.com/mishoo/UglifyJS/issues/#issue/57
|
|
|
6157 |
// IE croaks with "syntax error" on code like this:
|
|
|
6158 |
// if (foo) do ... while(cond); else ...
|
|
|
6159 |
// we need block brackets around do/while
|
|
|
6160 |
return make_block([ th ]);
|
|
|
6161 |
}
|
|
|
6162 |
var b = th;
|
|
|
6163 |
while (true) {
|
|
|
6164 |
var type = b[0];
|
|
|
6165 |
if (type == "if") {
|
|
|
6166 |
if (!b[3])
|
|
|
6167 |
// no else, we must add the block
|
|
|
6168 |
return make([ "block", [ th ]]);
|
|
|
6169 |
b = b[3];
|
|
|
6170 |
}
|
|
|
6171 |
else if (type == "while" || type == "do") b = b[2];
|
|
|
6172 |
else if (type == "for" || type == "for-in") b = b[4];
|
|
|
6173 |
else break;
|
|
|
6174 |
}
|
|
|
6175 |
return make(th);
|
|
|
6176 |
};
|
|
|
6177 |
|
|
|
6178 |
function make_function(name, args, body, keyword) {
|
|
|
6179 |
var out = keyword || "function";
|
|
|
6180 |
if (name) {
|
|
|
6181 |
out += " " + make_name(name);
|
|
|
6182 |
}
|
|
|
6183 |
out += "(" + add_commas(MAP(args, make_name)) + ")";
|
|
|
6184 |
return add_spaces([ out, make_block(body) ]);
|
|
|
6185 |
};
|
|
|
6186 |
|
|
|
6187 |
function must_has_semicolon(node) {
|
|
|
6188 |
switch (node[0]) {
|
|
|
6189 |
case "with":
|
|
|
6190 |
case "while":
|
|
|
6191 |
return empty(node[2]); // `with' or `while' with empty body?
|
|
|
6192 |
case "for":
|
|
|
6193 |
case "for-in":
|
|
|
6194 |
return empty(node[4]); // `for' with empty body?
|
|
|
6195 |
case "if":
|
|
|
6196 |
if (empty(node[2]) && !node[3]) return true; // `if' with empty `then' and no `else'
|
|
|
6197 |
if (node[3]) {
|
|
|
6198 |
if (empty(node[3])) return true; // `else' present but empty
|
|
|
6199 |
return must_has_semicolon(node[3]); // dive into the `else' branch
|
|
|
6200 |
}
|
|
|
6201 |
return must_has_semicolon(node[2]); // dive into the `then' branch
|
|
|
6202 |
}
|
|
|
6203 |
};
|
|
|
6204 |
|
|
|
6205 |
function make_block_statements(statements, noindent) {
|
|
|
6206 |
for (var a = [], last = statements.length - 1, i = 0; i <= last; ++i) {
|
|
|
6207 |
var stat = statements[i];
|
|
|
6208 |
var code = make(stat);
|
|
|
6209 |
if (code != ";") {
|
|
|
6210 |
if (!beautify && i == last && !must_has_semicolon(stat)) {
|
|
|
6211 |
code = code.replace(/;+\s*$/, "");
|
|
|
6212 |
}
|
|
|
6213 |
a.push(code);
|
|
|
6214 |
}
|
|
|
6215 |
}
|
|
|
6216 |
return noindent ? a : MAP(a, indent);
|
|
|
6217 |
};
|
|
|
6218 |
|
|
|
6219 |
function make_switch_block(body) {
|
|
|
6220 |
var n = body.length;
|
|
|
6221 |
if (n == 0) return "{}";
|
|
|
6222 |
return "{" + newline + MAP(body, function(branch, i){
|
|
|
6223 |
var has_body = branch[1].length > 0, code = with_indent(function(){
|
|
|
6224 |
return indent(branch[0]
|
|
|
6225 |
? add_spaces([ "case", make(branch[0]) + ":" ])
|
|
|
6226 |
: "default:");
|
|
|
6227 |
}, 0.5) + (has_body ? newline + with_indent(function(){
|
|
|
6228 |
return make_block_statements(branch[1]).join(newline);
|
|
|
6229 |
}) : "");
|
|
|
6230 |
if (!beautify && has_body && i < n - 1)
|
|
|
6231 |
code += ";";
|
|
|
6232 |
return code;
|
|
|
6233 |
}).join(newline) + newline + indent("}");
|
|
|
6234 |
};
|
|
|
6235 |
|
|
|
6236 |
function make_block(statements) {
|
|
|
6237 |
if (!statements) return ";";
|
|
|
6238 |
if (statements.length == 0) return "{}";
|
|
|
6239 |
return "{" + newline + with_indent(function(){
|
|
|
6240 |
return make_block_statements(statements).join(newline);
|
|
|
6241 |
}) + newline + indent("}");
|
|
|
6242 |
};
|
|
|
6243 |
|
|
|
6244 |
function make_1vardef(def) {
|
|
|
6245 |
var name = def[0], val = def[1];
|
|
|
6246 |
if (val != null)
|
|
|
6247 |
name = add_spaces([ make_name(name), "=", parenthesize(val, "seq") ]);
|
|
|
6248 |
return name;
|
|
|
6249 |
};
|
|
|
6250 |
|
|
|
6251 |
};
|
|
|
6252 |
|
|
|
6253 |
function split_lines(code, max_line_length) {
|
|
|
6254 |
var splits = [ 0 ];
|
|
|
6255 |
jsp.parse(function(){
|
|
|
6256 |
var next_token = jsp.tokenizer(code);
|
|
|
6257 |
var last_split = 0;
|
|
|
6258 |
var prev_token;
|
|
|
6259 |
function current_length(tok) {
|
|
|
6260 |
return tok.pos - last_split;
|
|
|
6261 |
};
|
|
|
6262 |
function split_here(tok) {
|
|
|
6263 |
last_split = tok.pos;
|
|
|
6264 |
splits.push(last_split);
|
|
|
6265 |
};
|
|
|
6266 |
function custom(){
|
|
|
6267 |
var tok = next_token.apply(this, arguments);
|
|
|
6268 |
out: {
|
|
|
6269 |
if (prev_token) {
|
|
|
6270 |
if (prev_token.type == "keyword") break out;
|
|
|
6271 |
}
|
|
|
6272 |
if (current_length(tok) > max_line_length) {
|
|
|
6273 |
switch (tok.type) {
|
|
|
6274 |
case "keyword":
|
|
|
6275 |
case "atom":
|
|
|
6276 |
case "name":
|
|
|
6277 |
case "punc":
|
|
|
6278 |
split_here(tok);
|
|
|
6279 |
break out;
|
|
|
6280 |
}
|
|
|
6281 |
}
|
|
|
6282 |
}
|
|
|
6283 |
prev_token = tok;
|
|
|
6284 |
return tok;
|
|
|
6285 |
};
|
|
|
6286 |
custom.context = function() {
|
|
|
6287 |
return next_token.context.apply(this, arguments);
|
|
|
6288 |
};
|
|
|
6289 |
return custom;
|
|
|
6290 |
}());
|
|
|
6291 |
return splits.map(function(pos, i){
|
|
|
6292 |
return code.substring(pos, splits[i + 1] || code.length);
|
|
|
6293 |
}).join("\n");
|
|
|
6294 |
};
|
|
|
6295 |
|
|
|
6296 |
/* -----[ Utilities ]----- */
|
|
|
6297 |
|
|
|
6298 |
function repeat_string(str, i) {
|
|
|
6299 |
if (i <= 0) return "";
|
|
|
6300 |
if (i == 1) return str;
|
|
|
6301 |
var d = repeat_string(str, i >> 1);
|
|
|
6302 |
d += d;
|
|
|
6303 |
if (i & 1) d += str;
|
|
|
6304 |
return d;
|
|
|
6305 |
};
|
|
|
6306 |
|
|
|
6307 |
function defaults(args, defs) {
|
|
|
6308 |
var ret = {};
|
|
|
6309 |
if (args === true)
|
|
|
6310 |
args = {};
|
|
|
6311 |
for (var i in defs) if (HOP(defs, i)) {
|
|
|
6312 |
ret[i] = (args && HOP(args, i)) ? args[i] : defs[i];
|
|
|
6313 |
}
|
|
|
6314 |
return ret;
|
|
|
6315 |
};
|
|
|
6316 |
|
|
|
6317 |
function is_identifier(name) {
|
|
|
6318 |
return /^[a-z_$][a-z0-9_$]*$/i.test(name)
|
|
|
6319 |
&& name != "this"
|
|
|
6320 |
&& !HOP(jsp.KEYWORDS_ATOM, name)
|
|
|
6321 |
&& !HOP(jsp.RESERVED_WORDS, name)
|
|
|
6322 |
&& !HOP(jsp.KEYWORDS, name);
|
|
|
6323 |
};
|
|
|
6324 |
|
|
|
6325 |
function HOP(obj, prop) {
|
|
|
6326 |
return Object.prototype.hasOwnProperty.call(obj, prop);
|
|
|
6327 |
};
|
|
|
6328 |
|
|
|
6329 |
// some utilities
|
|
|
6330 |
|
|
|
6331 |
var MAP;
|
|
|
6332 |
|
|
|
6333 |
(function(){
|
|
|
6334 |
MAP = function(a, f, o) {
|
|
|
6335 |
var ret = [], top = [], i;
|
|
|
6336 |
function doit() {
|
|
|
6337 |
var val = f.call(o, a[i], i);
|
|
|
6338 |
if (val instanceof AtTop) {
|
|
|
6339 |
val = val.v;
|
|
|
6340 |
if (val instanceof Splice) {
|
|
|
6341 |
top.push.apply(top, val.v);
|
|
|
6342 |
} else {
|
|
|
6343 |
top.push(val);
|
|
|
6344 |
}
|
|
|
6345 |
}
|
|
|
6346 |
else if (val != skip) {
|
|
|
6347 |
if (val instanceof Splice) {
|
|
|
6348 |
ret.push.apply(ret, val.v);
|
|
|
6349 |
} else {
|
|
|
6350 |
ret.push(val);
|
|
|
6351 |
}
|
|
|
6352 |
}
|
|
|
6353 |
};
|
|
|
6354 |
if (a instanceof Array) for (i = 0; i < a.length; ++i) doit();
|
|
|
6355 |
else for (i in a) if (HOP(a, i)) doit();
|
|
|
6356 |
return top.concat(ret);
|
|
|
6357 |
};
|
|
|
6358 |
MAP.at_top = function(val) { return new AtTop(val) };
|
|
|
6359 |
MAP.splice = function(val) { return new Splice(val) };
|
|
|
6360 |
var skip = MAP.skip = {};
|
|
|
6361 |
function AtTop(val) { this.v = val };
|
|
|
6362 |
function Splice(val) { this.v = val };
|
|
|
6363 |
})();
|
|
|
6364 |
|
|
|
6365 |
/* -----[ Exports ]----- */
|
|
|
6366 |
|
|
|
6367 |
exports.ast_walker = ast_walker;
|
|
|
6368 |
exports.ast_mangle = ast_mangle;
|
|
|
6369 |
exports.ast_squeeze = ast_squeeze;
|
|
|
6370 |
exports.ast_lift_variables = ast_lift_variables;
|
|
|
6371 |
exports.gen_code = gen_code;
|
|
|
6372 |
exports.ast_add_scope = ast_add_scope;
|
|
|
6373 |
exports.set_logger = function(logger) { warn = logger };
|
|
|
6374 |
exports.make_string = make_string;
|
|
|
6375 |
exports.split_lines = split_lines;
|
|
|
6376 |
exports.MAP = MAP;
|
|
|
6377 |
|
|
|
6378 |
// keep this last!
|
|
|
6379 |
exports.ast_squeeze_more = require("./squeeze-more").ast_squeeze_more;
|
|
|
6380 |
|
|
|
6381 |
});define('uglifyjs/index', ["require", "exports", "module", "./parse-js", "./process"], function(require, exports, module) {
|
|
|
6382 |
|
|
|
6383 |
//convienence function(src, [options]);
|
|
|
6384 |
function uglify(orig_code, options){
|
|
|
6385 |
options || (options = {});
|
|
|
6386 |
var jsp = uglify.parser;
|
|
|
6387 |
var pro = uglify.uglify;
|
|
|
6388 |
|
|
|
6389 |
var ast = jsp.parse(orig_code, options.strict_semicolons); // parse code and get the initial AST
|
|
|
6390 |
ast = pro.ast_mangle(ast, options.mangle_options); // get a new AST with mangled names
|
|
|
6391 |
ast = pro.ast_squeeze(ast, options.squeeze_options); // get an AST with compression optimizations
|
|
|
6392 |
var final_code = pro.gen_code(ast, options.gen_options); // compressed code here
|
|
|
6393 |
return final_code;
|
|
|
6394 |
};
|
|
|
6395 |
|
|
|
6396 |
uglify.parser = require("./parse-js");
|
|
|
6397 |
uglify.uglify = require("./process");
|
|
|
6398 |
|
|
|
6399 |
module.exports = uglify
|
|
|
6400 |
|
|
|
6401 |
});/**
|
|
|
6402 |
* @license Copyright (c) 2010-2011, The Dojo Foundation All Rights Reserved.
|
|
|
6403 |
* Available via the MIT or new BSD license.
|
|
|
6404 |
* see: http://github.com/jrburke/requirejs for details
|
|
|
6405 |
*/
|
|
|
6406 |
|
|
|
6407 |
/*jslint plusplus: false, strict: false */
|
|
|
6408 |
/*global define: false */
|
|
|
6409 |
|
|
|
6410 |
define('parse', ['uglifyjs/index'], function (uglify) {
|
|
|
6411 |
var parser = uglify.parser,
|
|
|
6412 |
processor = uglify.uglify,
|
|
|
6413 |
ostring = Object.prototype.toString,
|
|
|
6414 |
isArray;
|
|
|
6415 |
|
|
|
6416 |
if (Array.isArray) {
|
|
|
6417 |
isArray = Array.isArray;
|
|
|
6418 |
} else {
|
|
|
6419 |
isArray = function (it) {
|
|
|
6420 |
return ostring.call(it) === "[object Array]";
|
|
|
6421 |
};
|
|
|
6422 |
}
|
|
|
6423 |
|
|
|
6424 |
/**
|
|
|
6425 |
* Determines if the AST node is an array literal
|
|
|
6426 |
*/
|
|
|
6427 |
function isArrayLiteral(node) {
|
|
|
6428 |
return node[0] === 'array';
|
|
|
6429 |
}
|
|
|
6430 |
|
|
|
6431 |
/**
|
|
|
6432 |
* Determines if the AST node is an object literal
|
|
|
6433 |
*/
|
|
|
6434 |
function isObjectLiteral(node) {
|
|
|
6435 |
return node[0] === 'object';
|
|
|
6436 |
}
|
|
|
6437 |
|
|
|
6438 |
/**
|
|
|
6439 |
* Converts a regular JS array of strings to an AST node that
|
|
|
6440 |
* represents that array.
|
|
|
6441 |
* @param {Array} ary
|
|
|
6442 |
* @param {Node} an AST node that represents an array of strings.
|
|
|
6443 |
*/
|
|
|
6444 |
function toAstArray(ary) {
|
|
|
6445 |
var output = [
|
|
|
6446 |
'array',
|
|
|
6447 |
[]
|
|
|
6448 |
],
|
|
|
6449 |
i, item;
|
|
|
6450 |
|
|
|
6451 |
for (i = 0; (item = ary[i]); i++) {
|
|
|
6452 |
output[1].push([
|
|
|
6453 |
'string',
|
|
|
6454 |
item
|
|
|
6455 |
]);
|
|
|
6456 |
}
|
|
|
6457 |
|
|
|
6458 |
return output;
|
|
|
6459 |
}
|
|
|
6460 |
|
|
|
6461 |
/**
|
|
|
6462 |
* Validates a node as being an object literal (like for i18n bundles)
|
|
|
6463 |
* or an array literal with just string members. If an array literal,
|
|
|
6464 |
* only return array members that are full strings. So the caller of
|
|
|
6465 |
* this function should use the return value as the new value for the
|
|
|
6466 |
* node.
|
|
|
6467 |
*
|
|
|
6468 |
* This function does not need to worry about comments, they are not
|
|
|
6469 |
* present in this AST.
|
|
|
6470 |
*
|
|
|
6471 |
* @param {Node} node an AST node.
|
|
|
6472 |
*
|
|
|
6473 |
* @returns {Node} an AST node to use for the valid dependencies.
|
|
|
6474 |
* If null is returned, then it means the input node was not a valid
|
|
|
6475 |
* dependency.
|
|
|
6476 |
*/
|
|
|
6477 |
function validateDeps(node) {
|
|
|
6478 |
var newDeps = ['array', []],
|
|
|
6479 |
arrayArgs, i, dep;
|
|
|
6480 |
|
|
|
6481 |
if (!node) {
|
|
|
6482 |
return null;
|
|
|
6483 |
}
|
|
|
6484 |
|
|
|
6485 |
if (isObjectLiteral(node) || node[0] === 'function') {
|
|
|
6486 |
return node;
|
|
|
6487 |
}
|
|
|
6488 |
|
|
|
6489 |
//Dependencies can be an object literal or an array.
|
|
|
6490 |
if (!isArrayLiteral(node)) {
|
|
|
6491 |
return null;
|
|
|
6492 |
}
|
|
|
6493 |
|
|
|
6494 |
arrayArgs = node[1];
|
|
|
6495 |
|
|
|
6496 |
for (i = 0; i < arrayArgs.length; i++) {
|
|
|
6497 |
dep = arrayArgs[i];
|
|
|
6498 |
if (dep[0] === 'string') {
|
|
|
6499 |
newDeps[1].push(dep);
|
|
|
6500 |
}
|
|
|
6501 |
}
|
|
|
6502 |
return newDeps[1].length ? newDeps : null;
|
|
|
6503 |
}
|
|
|
6504 |
|
|
|
6505 |
/**
|
|
|
6506 |
* Gets dependencies from a node, but only if it is an array literal,
|
|
|
6507 |
* and only if the dependency is a string literal.
|
|
|
6508 |
*
|
|
|
6509 |
* This function does not need to worry about comments, they are not
|
|
|
6510 |
* present in this AST.
|
|
|
6511 |
*
|
|
|
6512 |
* @param {Node} node an AST node.
|
|
|
6513 |
*
|
|
|
6514 |
* @returns {Array} of valid dependencies.
|
|
|
6515 |
* If null is returned, then it means the input node was not a valid
|
|
|
6516 |
* array literal, or did not have any string literals..
|
|
|
6517 |
*/
|
|
|
6518 |
function getValidDeps(node) {
|
|
|
6519 |
var newDeps = [],
|
|
|
6520 |
arrayArgs, i, dep;
|
|
|
6521 |
|
|
|
6522 |
if (!node) {
|
|
|
6523 |
return null;
|
|
|
6524 |
}
|
|
|
6525 |
|
|
|
6526 |
if (isObjectLiteral(node) || node[0] === 'function') {
|
|
|
6527 |
return null;
|
|
|
6528 |
}
|
|
|
6529 |
|
|
|
6530 |
//Dependencies can be an object literal or an array.
|
|
|
6531 |
if (!isArrayLiteral(node)) {
|
|
|
6532 |
return null;
|
|
|
6533 |
}
|
|
|
6534 |
|
|
|
6535 |
arrayArgs = node[1];
|
|
|
6536 |
|
|
|
6537 |
for (i = 0; i < arrayArgs.length; i++) {
|
|
|
6538 |
dep = arrayArgs[i];
|
|
|
6539 |
if (dep[0] === 'string') {
|
|
|
6540 |
newDeps.push(dep[1]);
|
|
|
6541 |
}
|
|
|
6542 |
}
|
|
|
6543 |
return newDeps.length ? newDeps : null;
|
|
|
6544 |
}
|
|
|
6545 |
|
|
|
6546 |
/**
|
|
|
6547 |
* Main parse function. Returns a string of any valid require or define/require.def
|
|
|
6548 |
* calls as part of one JavaScript source string.
|
|
|
6549 |
* @param {String} moduleName the module name that represents this file.
|
|
|
6550 |
* It is used to create a default define if there is not one already for the file.
|
|
|
6551 |
* This allows properly tracing dependencies for builds. Otherwise, if
|
|
|
6552 |
* the file just has a require() call, the file dependencies will not be
|
|
|
6553 |
* properly reflected: the file will come before its dependencies.
|
|
|
6554 |
* @param {String} moduleName
|
|
|
6555 |
* @param {String} fileName
|
|
|
6556 |
* @param {String} fileContents
|
|
|
6557 |
* @param {Object} options optional options. insertNeedsDefine: true will
|
|
|
6558 |
* add calls to require.needsDefine() if appropriate.
|
|
|
6559 |
* @returns {String} JS source string or null, if no require or define/require.def
|
|
|
6560 |
* calls are found.
|
|
|
6561 |
*/
|
|
|
6562 |
function parse(moduleName, fileName, fileContents, options) {
|
|
|
6563 |
options = options || {};
|
|
|
6564 |
|
|
|
6565 |
//Set up source input
|
|
|
6566 |
var moduleDeps = [],
|
|
|
6567 |
result = '',
|
|
|
6568 |
moduleList = [],
|
|
|
6569 |
needsDefine = true,
|
|
|
6570 |
astRoot = parser.parse(fileContents),
|
|
|
6571 |
i, moduleCall, depString;
|
|
|
6572 |
|
|
|
6573 |
parse.recurse(astRoot, function (callName, config, name, deps) {
|
|
|
6574 |
//If name is an array, it means it is an anonymous module,
|
|
|
6575 |
//so adjust args appropriately. An anonymous module could
|
|
|
6576 |
//have a FUNCTION as the name type, but just ignore those
|
|
|
6577 |
//since we just want to find dependencies.
|
|
|
6578 |
if (name && isArrayLiteral(name)) {
|
|
|
6579 |
deps = name;
|
|
|
6580 |
name = null;
|
|
|
6581 |
}
|
|
|
6582 |
|
|
|
6583 |
if (!(deps = getValidDeps(deps))) {
|
|
|
6584 |
deps = [];
|
|
|
6585 |
}
|
|
|
6586 |
|
|
|
6587 |
//Get the name as a string literal, if it is available.
|
|
|
6588 |
if (name && name[0] === 'string') {
|
|
|
6589 |
name = name[1];
|
|
|
6590 |
} else {
|
|
|
6591 |
name = null;
|
|
|
6592 |
}
|
|
|
6593 |
|
|
|
6594 |
if (callName === 'define' && (!name || name === moduleName)) {
|
|
|
6595 |
needsDefine = false;
|
|
|
6596 |
}
|
|
|
6597 |
|
|
|
6598 |
if (!name) {
|
|
|
6599 |
//If there is no module name, the dependencies are for
|
|
|
6600 |
//this file/default module name.
|
|
|
6601 |
moduleDeps = moduleDeps.concat(deps);
|
|
|
6602 |
} else {
|
|
|
6603 |
moduleList.push({
|
|
|
6604 |
name: name,
|
|
|
6605 |
deps: deps
|
|
|
6606 |
});
|
|
|
6607 |
}
|
|
|
6608 |
});
|
|
|
6609 |
|
|
|
6610 |
if (options.insertNeedsDefine && needsDefine) {
|
|
|
6611 |
result += 'require.needsDefine("' + moduleName + '");';
|
|
|
6612 |
}
|
|
|
6613 |
|
|
|
6614 |
if (moduleDeps.length || moduleList.length) {
|
|
|
6615 |
for (i = 0; (moduleCall = moduleList[i]); i++) {
|
|
|
6616 |
if (result) {
|
|
|
6617 |
result += '\n';
|
|
|
6618 |
}
|
|
|
6619 |
depString = moduleCall.deps.length ? '["' + moduleCall.deps.join('","') + '"]' : '[]';
|
|
|
6620 |
result += 'define("' + moduleCall.name + '",' + depString + ');';
|
|
|
6621 |
}
|
|
|
6622 |
if (moduleDeps.length) {
|
|
|
6623 |
if (result) {
|
|
|
6624 |
result += '\n';
|
|
|
6625 |
}
|
|
|
6626 |
depString = moduleDeps.length ? '["' + moduleDeps.join('","') + '"]' : '[]';
|
|
|
6627 |
result += 'define("' + moduleName + '",' + depString + ');';
|
|
|
6628 |
}
|
|
|
6629 |
}
|
|
|
6630 |
|
|
|
6631 |
return result ? result : null;
|
|
|
6632 |
}
|
|
|
6633 |
|
|
|
6634 |
//Add some private methods to object for use in derived objects.
|
|
|
6635 |
parse.isArray = isArray;
|
|
|
6636 |
parse.isObjectLiteral = isObjectLiteral;
|
|
|
6637 |
parse.isArrayLiteral = isArrayLiteral;
|
|
|
6638 |
|
|
|
6639 |
/**
|
|
|
6640 |
* Handles parsing a file recursively for require calls.
|
|
|
6641 |
* @param {Array} parentNode the AST node to start with.
|
|
|
6642 |
* @param {Function} onMatch function to call on a parse match.
|
|
|
6643 |
*/
|
|
|
6644 |
parse.recurse = function (parentNode, onMatch) {
|
|
|
6645 |
var i, node;
|
|
|
6646 |
if (isArray(parentNode)) {
|
|
|
6647 |
for (i = 0; i < parentNode.length; i++) {
|
|
|
6648 |
node = parentNode[i];
|
|
|
6649 |
if (isArray(node)) {
|
|
|
6650 |
this.parseNode(node, onMatch);
|
|
|
6651 |
this.recurse(node, onMatch);
|
|
|
6652 |
}
|
|
|
6653 |
}
|
|
|
6654 |
}
|
|
|
6655 |
};
|
|
|
6656 |
|
|
|
6657 |
/**
|
|
|
6658 |
* Determines if the file defines require().
|
|
|
6659 |
* @param {String} fileName
|
|
|
6660 |
* @param {String} fileContents
|
|
|
6661 |
* @returns {Boolean}
|
|
|
6662 |
*/
|
|
|
6663 |
parse.definesRequire = function (fileName, fileContents) {
|
|
|
6664 |
var astRoot = parser.parse(fileContents);
|
|
|
6665 |
return this.nodeHasRequire(astRoot);
|
|
|
6666 |
};
|
|
|
6667 |
|
|
|
6668 |
/**
|
|
|
6669 |
* Finds require("") calls inside a CommonJS anonymous module wrapped in a
|
|
|
6670 |
* define(function(require, exports, module){}) wrapper. These dependencies
|
|
|
6671 |
* will be added to a modified define() call that lists the dependencies
|
|
|
6672 |
* on the outside of the function.
|
|
|
6673 |
* @param {String} fileName
|
|
|
6674 |
* @param {String} fileContents
|
|
|
6675 |
* @returns {Array} an array of module names that are dependencies. Always
|
|
|
6676 |
* returns an array, but could be of length zero.
|
|
|
6677 |
*/
|
|
|
6678 |
parse.getAnonDeps = function (fileName, fileContents) {
|
|
|
6679 |
var astRoot = parser.parse(fileContents),
|
|
|
6680 |
defFunc = this.findAnonDefineFactory(astRoot);
|
|
|
6681 |
|
|
|
6682 |
return parse.getAnonDepsFromNode(defFunc);
|
|
|
6683 |
};
|
|
|
6684 |
|
|
|
6685 |
/**
|
|
|
6686 |
* Finds require("") calls inside a CommonJS anonymous module wrapped
|
|
|
6687 |
* in a define function, given an AST node for the definition function.
|
|
|
6688 |
* @param {Node} node the AST node for the definition function.
|
|
|
6689 |
* @returns {Array} and array of dependency names. Can be of zero length.
|
|
|
6690 |
*/
|
|
|
6691 |
parse.getAnonDepsFromNode = function (node) {
|
|
|
6692 |
var deps = [],
|
|
|
6693 |
funcArgLength;
|
|
|
6694 |
|
|
|
6695 |
if (node) {
|
|
|
6696 |
this.findRequireDepNames(node, deps);
|
|
|
6697 |
|
|
|
6698 |
//If no deps, still add the standard CommonJS require, exports, module,
|
|
|
6699 |
//in that order, to the deps, but only if specified as function args.
|
|
|
6700 |
//In particular, if exports is used, it is favored over the return
|
|
|
6701 |
//value of the function, so only add it if asked.
|
|
|
6702 |
funcArgLength = node[2] && node[2].length;
|
|
|
6703 |
if (funcArgLength) {
|
|
|
6704 |
deps = (funcArgLength > 1 ? ["require", "exports", "module"] :
|
|
|
6705 |
["require"]).concat(deps);
|
|
|
6706 |
}
|
|
|
6707 |
}
|
|
|
6708 |
return deps;
|
|
|
6709 |
};
|
|
|
6710 |
|
|
|
6711 |
/**
|
|
|
6712 |
* Finds the function in define(function (require, exports, module){});
|
|
|
6713 |
* @param {Array} node
|
|
|
6714 |
* @returns {Boolean}
|
|
|
6715 |
*/
|
|
|
6716 |
parse.findAnonDefineFactory = function (node) {
|
|
|
6717 |
var callback, i, n, call, args;
|
|
|
6718 |
|
|
|
6719 |
if (isArray(node)) {
|
|
|
6720 |
if (node[0] === 'call') {
|
|
|
6721 |
call = node[1];
|
|
|
6722 |
args = node[2];
|
|
|
6723 |
if ((call[0] === 'name' && call[1] === 'define') ||
|
|
|
6724 |
(call[0] === 'dot' && call[1][1] === 'require' && call[2] === 'def')) {
|
|
|
6725 |
|
|
|
6726 |
//There should only be one argument and it should be a function,
|
|
|
6727 |
//or a named module with function as second arg
|
|
|
6728 |
if (args.length === 1 && args[0][0] === 'function') {
|
|
|
6729 |
return args[0];
|
|
|
6730 |
} else if (args.length === 2 && args[0][0] === 'string' &&
|
|
|
6731 |
args[1][0] === 'function') {
|
|
|
6732 |
return args[1];
|
|
|
6733 |
}
|
|
|
6734 |
}
|
|
|
6735 |
}
|
|
|
6736 |
|
|
|
6737 |
//Check child nodes
|
|
|
6738 |
for (i = 0; i < node.length; i++) {
|
|
|
6739 |
n = node[i];
|
|
|
6740 |
if ((callback = this.findAnonDefineFactory(n))) {
|
|
|
6741 |
return callback;
|
|
|
6742 |
}
|
|
|
6743 |
}
|
|
|
6744 |
}
|
|
|
6745 |
|
|
|
6746 |
return null;
|
|
|
6747 |
};
|
|
|
6748 |
|
|
|
6749 |
/**
|
|
|
6750 |
* Finds all dependencies specified in dependency arrays and inside
|
|
|
6751 |
* simplified commonjs wrappers.
|
|
|
6752 |
* @param {String} fileName
|
|
|
6753 |
* @param {String} fileContents
|
|
|
6754 |
*
|
|
|
6755 |
* @returns {Array} an array of dependency strings. The dependencies
|
|
|
6756 |
* have not been normalized, they may be relative IDs.
|
|
|
6757 |
*/
|
|
|
6758 |
parse.findDependencies = function (fileName, fileContents) {
|
|
|
6759 |
//This is a litle bit inefficient, it ends up with two uglifyjs parser
|
|
|
6760 |
//calls. Can revisit later, but trying to build out larger functional
|
|
|
6761 |
//pieces first.
|
|
|
6762 |
var dependencies = parse.getAnonDeps(fileName, fileContents),
|
|
|
6763 |
astRoot = parser.parse(fileContents);
|
|
|
6764 |
|
|
|
6765 |
parse.recurse(astRoot, function (callName, config, name, deps) {
|
|
|
6766 |
//Normalize the input args.
|
|
|
6767 |
if (name && isArrayLiteral(name)) {
|
|
|
6768 |
deps = name;
|
|
|
6769 |
name = null;
|
|
|
6770 |
}
|
|
|
6771 |
|
|
|
6772 |
if ((deps = getValidDeps(deps))) {
|
|
|
6773 |
dependencies = dependencies.concat(deps);
|
|
|
6774 |
}
|
|
|
6775 |
});
|
|
|
6776 |
|
|
|
6777 |
return dependencies;
|
|
|
6778 |
};
|
|
|
6779 |
|
|
|
6780 |
parse.findRequireDepNames = function (node, deps) {
|
|
|
6781 |
var moduleName, i, n, call, args;
|
|
|
6782 |
|
|
|
6783 |
if (isArray(node)) {
|
|
|
6784 |
if (node[0] === 'call') {
|
|
|
6785 |
call = node[1];
|
|
|
6786 |
args = node[2];
|
|
|
6787 |
|
|
|
6788 |
if (call[0] === 'name' && call[1] === 'require') {
|
|
|
6789 |
moduleName = args[0];
|
|
|
6790 |
if (moduleName[0] === 'string') {
|
|
|
6791 |
deps.push(moduleName[1]);
|
|
|
6792 |
}
|
|
|
6793 |
}
|
|
|
6794 |
|
|
|
6795 |
|
|
|
6796 |
}
|
|
|
6797 |
|
|
|
6798 |
//Check child nodes
|
|
|
6799 |
for (i = 0; i < node.length; i++) {
|
|
|
6800 |
n = node[i];
|
|
|
6801 |
this.findRequireDepNames(n, deps);
|
|
|
6802 |
}
|
|
|
6803 |
}
|
|
|
6804 |
};
|
|
|
6805 |
|
|
|
6806 |
/**
|
|
|
6807 |
* Determines if a given node contains a require() definition.
|
|
|
6808 |
* @param {Array} node
|
|
|
6809 |
* @returns {Boolean}
|
|
|
6810 |
*/
|
|
|
6811 |
parse.nodeHasRequire = function (node) {
|
|
|
6812 |
if (this.isDefineNode(node)) {
|
|
|
6813 |
return true;
|
|
|
6814 |
}
|
|
|
6815 |
|
|
|
6816 |
if (isArray(node)) {
|
|
|
6817 |
for (var i = 0, n; i < node.length; i++) {
|
|
|
6818 |
n = node[i];
|
|
|
6819 |
if (this.nodeHasRequire(n)) {
|
|
|
6820 |
return true;
|
|
|
6821 |
}
|
|
|
6822 |
}
|
|
|
6823 |
}
|
|
|
6824 |
|
|
|
6825 |
return false;
|
|
|
6826 |
};
|
|
|
6827 |
|
|
|
6828 |
/**
|
|
|
6829 |
* Is the given node the actual definition of define(). Actually uses
|
|
|
6830 |
* the definition of define.amd to find require.
|
|
|
6831 |
* @param {Array} node
|
|
|
6832 |
* @returns {Boolean}
|
|
|
6833 |
*/
|
|
|
6834 |
parse.isDefineNode = function (node) {
|
|
|
6835 |
//Actually look for the define.amd = assignment, since
|
|
|
6836 |
//that is more indicative of RequireJS vs a plain require definition.
|
|
|
6837 |
var assign;
|
|
|
6838 |
if (!node) {
|
|
|
6839 |
return null;
|
|
|
6840 |
}
|
|
|
6841 |
|
|
|
6842 |
if (node[0] === 'assign' && node[1] === true) {
|
|
|
6843 |
assign = node[2];
|
|
|
6844 |
if (assign[0] === 'dot' && assign[1][0] === 'name' &&
|
|
|
6845 |
assign[1][1] === 'define' && assign[2] === 'amd') {
|
|
|
6846 |
return true;
|
|
|
6847 |
}
|
|
|
6848 |
}
|
|
|
6849 |
return false;
|
|
|
6850 |
};
|
|
|
6851 |
|
|
|
6852 |
/**
|
|
|
6853 |
* Determines if a specific node is a valid require or define/require.def call.
|
|
|
6854 |
* @param {Array} node
|
|
|
6855 |
* @param {Function} onMatch a function to call when a match is found.
|
|
|
6856 |
* It is passed the match name, and the config, name, deps possible args.
|
|
|
6857 |
* The config, name and deps args are not normalized.
|
|
|
6858 |
*
|
|
|
6859 |
* @returns {String} a JS source string with the valid require/define call.
|
|
|
6860 |
* Otherwise null.
|
|
|
6861 |
*/
|
|
|
6862 |
parse.parseNode = function (node, onMatch) {
|
|
|
6863 |
var call, name, config, deps, args, cjsDeps;
|
|
|
6864 |
|
|
|
6865 |
if (!isArray(node)) {
|
|
|
6866 |
return null;
|
|
|
6867 |
}
|
|
|
6868 |
|
|
|
6869 |
if (node[0] === 'call') {
|
|
|
6870 |
call = node[1];
|
|
|
6871 |
args = node[2];
|
|
|
6872 |
|
|
|
6873 |
if (call) {
|
|
|
6874 |
if (call[0] === 'name' && call[1] === 'require') {
|
|
|
6875 |
|
|
|
6876 |
//It is a plain require() call.
|
|
|
6877 |
config = args[0];
|
|
|
6878 |
deps = args[1];
|
|
|
6879 |
if (isArrayLiteral(config)) {
|
|
|
6880 |
deps = config;
|
|
|
6881 |
config = null;
|
|
|
6882 |
}
|
|
|
6883 |
|
|
|
6884 |
if (!(deps = validateDeps(deps))) {
|
|
|
6885 |
return null;
|
|
|
6886 |
}
|
|
|
6887 |
|
|
|
6888 |
return onMatch("require", null, null, deps);
|
|
|
6889 |
|
|
|
6890 |
} else if (call[0] === 'name' && call[1] === 'define') {
|
|
|
6891 |
|
|
|
6892 |
//A define call
|
|
|
6893 |
name = args[0];
|
|
|
6894 |
deps = args[1];
|
|
|
6895 |
//Only allow define calls that match what is expected
|
|
|
6896 |
//in an AMD call:
|
|
|
6897 |
//* first arg should be string, array, function or object
|
|
|
6898 |
//* second arg optional, or array, function or object.
|
|
|
6899 |
//This helps weed out calls to a non-AMD define, but it is
|
|
|
6900 |
//not completely robust. Someone could create a define
|
|
|
6901 |
//function that still matches this shape, but this is the
|
|
|
6902 |
//best that is possible, and at least allows UglifyJS,
|
|
|
6903 |
//which does create its own internal define in one file,
|
|
|
6904 |
//to be inlined.
|
|
|
6905 |
if (((name[0] === 'string' || isArrayLiteral(name) ||
|
|
|
6906 |
name[0] === 'function' || isObjectLiteral(name))) &&
|
|
|
6907 |
(!deps || isArrayLiteral(deps) ||
|
|
|
6908 |
deps[0] === 'function' || isObjectLiteral(deps) ||
|
|
|
6909 |
// allow define(['dep'], factory) pattern
|
|
|
6910 |
(isArrayLiteral(name) && deps[0] === 'name' && args.length === 2))) {
|
|
|
6911 |
|
|
|
6912 |
//If first arg is a function, could be a commonjs wrapper,
|
|
|
6913 |
//look inside for commonjs dependencies.
|
|
|
6914 |
//Also, if deps is a function look for commonjs deps.
|
|
|
6915 |
if (name && name[0] === 'function') {
|
|
|
6916 |
cjsDeps = parse.getAnonDepsFromNode(name);
|
|
|
6917 |
if (cjsDeps.length) {
|
|
|
6918 |
name = toAstArray(cjsDeps);
|
|
|
6919 |
}
|
|
|
6920 |
} else if (deps && deps[0] === 'function') {
|
|
|
6921 |
cjsDeps = parse.getAnonDepsFromNode(deps);
|
|
|
6922 |
if (cjsDeps.length) {
|
|
|
6923 |
deps = toAstArray(cjsDeps);
|
|
|
6924 |
}
|
|
|
6925 |
}
|
|
|
6926 |
|
|
|
6927 |
return onMatch("define", null, name, deps);
|
|
|
6928 |
}
|
|
|
6929 |
}
|
|
|
6930 |
}
|
|
|
6931 |
}
|
|
|
6932 |
|
|
|
6933 |
return null;
|
|
|
6934 |
};
|
|
|
6935 |
|
|
|
6936 |
/**
|
|
|
6937 |
* Converts an AST node into a JS source string. Does not maintain formatting
|
|
|
6938 |
* or even comments from original source, just returns valid JS source.
|
|
|
6939 |
* @param {Array} node
|
|
|
6940 |
* @returns {String} a JS source string.
|
|
|
6941 |
*/
|
|
|
6942 |
parse.nodeToString = function (node) {
|
|
|
6943 |
return processor.gen_code(node, true);
|
|
|
6944 |
};
|
|
|
6945 |
|
|
|
6946 |
return parse;
|
|
|
6947 |
});
|
|
|
6948 |
/**
|
|
|
6949 |
* @license Copyright (c) 2010-2011, The Dojo Foundation All Rights Reserved.
|
|
|
6950 |
* Available via the MIT or new BSD license.
|
|
|
6951 |
* see: http://github.com/jrburke/requirejs for details
|
|
|
6952 |
*/
|
|
|
6953 |
|
|
|
6954 |
/*jslint regexp: false, strict: false, plusplus: false */
|
|
|
6955 |
/*global define: false */
|
|
|
6956 |
|
|
|
6957 |
define('pragma', ['parse', 'logger'], function (parse, logger) {
|
|
|
6958 |
|
|
|
6959 |
function Temp() {}
|
|
|
6960 |
|
|
|
6961 |
function create(obj, mixin) {
|
|
|
6962 |
Temp.prototype = obj;
|
|
|
6963 |
var temp = new Temp(), prop;
|
|
|
6964 |
|
|
|
6965 |
//Avoid any extra memory hanging around
|
|
|
6966 |
Temp.prototype = null;
|
|
|
6967 |
|
|
|
6968 |
if (mixin) {
|
|
|
6969 |
for (prop in mixin) {
|
|
|
6970 |
if (mixin.hasOwnProperty(prop) && !(prop in temp)) {
|
|
|
6971 |
temp[prop] = mixin[prop];
|
|
|
6972 |
}
|
|
|
6973 |
}
|
|
|
6974 |
}
|
|
|
6975 |
|
|
|
6976 |
return temp; // Object
|
|
|
6977 |
}
|
|
|
6978 |
|
|
|
6979 |
var pragma = {
|
|
|
6980 |
conditionalRegExp: /(exclude|include)Start\s*\(\s*["'](\w+)["']\s*,(.*)\)/,
|
|
|
6981 |
useStrictRegExp: /['"]use strict['"];/g,
|
|
|
6982 |
hasRegExp: /has\s*\(\s*['"]([^'"]+)['"]\s*\)/g,
|
|
|
6983 |
nsRegExp: /(^|[^\.])(requirejs|require|define)\s*\(/,
|
|
|
6984 |
nsWrapRegExp: /\/\*requirejs namespace: true \*\//,
|
|
|
6985 |
apiDefRegExp: /var requirejs, require, define;/,
|
|
|
6986 |
defineCheckRegExp: /typeof\s+define\s*===\s*["']function["']\s*&&\s*define\s*\.\s*amd/g,
|
|
|
6987 |
defineJQueryRegExp: /typeof\s+define\s*===\s*["']function["']\s*&&\s*define\s*\.\s*amd\s*&&\s*define\s*\.\s*amd\s*\.\s*jQuery/g,
|
|
|
6988 |
defineTernaryRegExp: /typeof\s+define\s*===\s*['"]function["']\s*&&\s*define\s*\.\s*amd\s*\?\s*define/,
|
|
|
6989 |
|
|
|
6990 |
removeStrict: function (contents, config) {
|
|
|
6991 |
return config.useStrict ? contents : contents.replace(pragma.useStrictRegExp, '');
|
|
|
6992 |
},
|
|
|
6993 |
|
|
|
6994 |
namespace: function (fileContents, ns, onLifecycleName) {
|
|
|
6995 |
if (ns) {
|
|
|
6996 |
//Namespace require/define calls
|
|
|
6997 |
fileContents = fileContents.replace(pragma.nsRegExp, '$1' + ns + '.$2(');
|
|
|
6998 |
|
|
|
6999 |
|
|
|
7000 |
//Namespace define ternary use:
|
|
|
7001 |
fileContents = fileContents.replace(pragma.defineTernaryRegExp,
|
|
|
7002 |
"typeof " + ns + ".define === 'function' && " + ns + ".define.amd ? " + ns + ".define");
|
|
|
7003 |
|
|
|
7004 |
//Namespace define jquery use:
|
|
|
7005 |
fileContents = fileContents.replace(pragma.defineJQueryRegExp,
|
|
|
7006 |
"typeof " + ns + ".define === 'function' && " + ns + ".define.amd && " + ns + ".define.jQuery");
|
|
|
7007 |
|
|
|
7008 |
//Namespace define checks.
|
|
|
7009 |
//Do this one last, since it is a subset of the more specific
|
|
|
7010 |
//checks above.
|
|
|
7011 |
fileContents = fileContents.replace(pragma.defineCheckRegExp,
|
|
|
7012 |
"typeof " + ns + ".define === 'function' && " + ns + ".define.amd");
|
|
|
7013 |
|
|
|
7014 |
//Check for require.js with the require/define definitions
|
|
|
7015 |
if (pragma.apiDefRegExp.test(fileContents) &&
|
|
|
7016 |
fileContents.indexOf("if (typeof " + ns + " === 'undefined')") === -1) {
|
|
|
7017 |
//Wrap the file contents in a typeof check, and a function
|
|
|
7018 |
//to contain the API globals.
|
|
|
7019 |
fileContents = "var " + ns + ";(function () { if (typeof " +
|
|
|
7020 |
ns + " === 'undefined') {\n" +
|
|
|
7021 |
ns + ' = {};\n' +
|
|
|
7022 |
fileContents +
|
|
|
7023 |
"\n" +
|
|
|
7024 |
ns + ".requirejs = requirejs;" +
|
|
|
7025 |
ns + ".require = require;" +
|
|
|
7026 |
ns + ".define = define;\n" +
|
|
|
7027 |
"}\n}());";
|
|
|
7028 |
}
|
|
|
7029 |
|
|
|
7030 |
//Finally, if the file wants a special wrapper because it ties
|
|
|
7031 |
//in to the requirejs internals in a way that would not fit
|
|
|
7032 |
//the above matches, do that. Look for /*requirejs namespace: true*/
|
|
|
7033 |
if (pragma.nsWrapRegExp.test(fileContents)) {
|
|
|
7034 |
//Remove the pragma.
|
|
|
7035 |
fileContents = fileContents.replace(pragma.nsWrapRegExp, '');
|
|
|
7036 |
|
|
|
7037 |
//Alter the contents.
|
|
|
7038 |
fileContents = '(function () {\n' +
|
|
|
7039 |
'var require = ' + ns + '.require,' +
|
|
|
7040 |
'requirejs = ' + ns + '.requirejs,' +
|
|
|
7041 |
'define = ' + ns + '.define;\n' +
|
|
|
7042 |
fileContents +
|
|
|
7043 |
'\n}());'
|
|
|
7044 |
}
|
|
|
7045 |
}
|
|
|
7046 |
|
|
|
7047 |
return fileContents;
|
|
|
7048 |
},
|
|
|
7049 |
|
|
|
7050 |
/**
|
|
|
7051 |
* processes the fileContents for some //>> conditional statements
|
|
|
7052 |
*/
|
|
|
7053 |
process: function (fileName, fileContents, config, onLifecycleName, pluginCollector) {
|
|
|
7054 |
/*jslint evil: true */
|
|
|
7055 |
var foundIndex = -1, startIndex = 0, lineEndIndex, conditionLine,
|
|
|
7056 |
matches, type, marker, condition, isTrue, endRegExp, endMatches,
|
|
|
7057 |
endMarkerIndex, shouldInclude, startLength, lifecycleHas, deps,
|
|
|
7058 |
i, dep, moduleName,
|
|
|
7059 |
lifecyclePragmas, pragmas = config.pragmas, hasConfig = config.has,
|
|
|
7060 |
//Legacy arg defined to help in dojo conversion script. Remove later
|
|
|
7061 |
//when dojo no longer needs conversion:
|
|
|
7062 |
kwArgs = pragmas;
|
|
|
7063 |
|
|
|
7064 |
//Mix in a specific lifecycle scoped object, to allow targeting
|
|
|
7065 |
//some pragmas/has tests to only when files are saved, or at different
|
|
|
7066 |
//lifecycle events. Do not bother with kwArgs in this section, since
|
|
|
7067 |
//the old dojo kwArgs were for all points in the build lifecycle.
|
|
|
7068 |
if (onLifecycleName) {
|
|
|
7069 |
lifecyclePragmas = config['pragmas' + onLifecycleName];
|
|
|
7070 |
lifecycleHas = config['has' + onLifecycleName];
|
|
|
7071 |
|
|
|
7072 |
if (lifecyclePragmas) {
|
|
|
7073 |
pragmas = create(pragmas || {}, lifecyclePragmas);
|
|
|
7074 |
}
|
|
|
7075 |
|
|
|
7076 |
if (lifecycleHas) {
|
|
|
7077 |
hasConfig = create(hasConfig || {}, lifecycleHas);
|
|
|
7078 |
}
|
|
|
7079 |
}
|
|
|
7080 |
|
|
|
7081 |
//Replace has references if desired
|
|
|
7082 |
if (hasConfig) {
|
|
|
7083 |
fileContents = fileContents.replace(pragma.hasRegExp, function (match, test) {
|
|
|
7084 |
if (test in hasConfig) {
|
|
|
7085 |
return !!hasConfig[test];
|
|
|
7086 |
}
|
|
|
7087 |
return match;
|
|
|
7088 |
});
|
|
|
7089 |
}
|
|
|
7090 |
|
|
|
7091 |
if (!config.skipPragmas) {
|
|
|
7092 |
|
|
|
7093 |
while ((foundIndex = fileContents.indexOf("//>>", startIndex)) !== -1) {
|
|
|
7094 |
//Found a conditional. Get the conditional line.
|
|
|
7095 |
lineEndIndex = fileContents.indexOf("\n", foundIndex);
|
|
|
7096 |
if (lineEndIndex === -1) {
|
|
|
7097 |
lineEndIndex = fileContents.length - 1;
|
|
|
7098 |
}
|
|
|
7099 |
|
|
|
7100 |
//Increment startIndex past the line so the next conditional search can be done.
|
|
|
7101 |
startIndex = lineEndIndex + 1;
|
|
|
7102 |
|
|
|
7103 |
//Break apart the conditional.
|
|
|
7104 |
conditionLine = fileContents.substring(foundIndex, lineEndIndex + 1);
|
|
|
7105 |
matches = conditionLine.match(pragma.conditionalRegExp);
|
|
|
7106 |
if (matches) {
|
|
|
7107 |
type = matches[1];
|
|
|
7108 |
marker = matches[2];
|
|
|
7109 |
condition = matches[3];
|
|
|
7110 |
isTrue = false;
|
|
|
7111 |
//See if the condition is true.
|
|
|
7112 |
try {
|
|
|
7113 |
isTrue = !!eval("(" + condition + ")");
|
|
|
7114 |
} catch (e) {
|
|
|
7115 |
throw "Error in file: " +
|
|
|
7116 |
fileName +
|
|
|
7117 |
". Conditional comment: " +
|
|
|
7118 |
conditionLine +
|
|
|
7119 |
" failed with this error: " + e;
|
|
|
7120 |
}
|
|
|
7121 |
|
|
|
7122 |
//Find the endpoint marker.
|
|
|
7123 |
endRegExp = new RegExp('\\/\\/\\>\\>\\s*' + type + 'End\\(\\s*[\'"]' + marker + '[\'"]\\s*\\)', "g");
|
|
|
7124 |
endMatches = endRegExp.exec(fileContents.substring(startIndex, fileContents.length));
|
|
|
7125 |
if (endMatches) {
|
|
|
7126 |
endMarkerIndex = startIndex + endRegExp.lastIndex - endMatches[0].length;
|
|
|
7127 |
|
|
|
7128 |
//Find the next line return based on the match position.
|
|
|
7129 |
lineEndIndex = fileContents.indexOf("\n", endMarkerIndex);
|
|
|
7130 |
if (lineEndIndex === -1) {
|
|
|
7131 |
lineEndIndex = fileContents.length - 1;
|
|
|
7132 |
}
|
|
|
7133 |
|
|
|
7134 |
//Should we include the segment?
|
|
|
7135 |
shouldInclude = ((type === "exclude" && !isTrue) || (type === "include" && isTrue));
|
|
|
7136 |
|
|
|
7137 |
//Remove the conditional comments, and optionally remove the content inside
|
|
|
7138 |
//the conditional comments.
|
|
|
7139 |
startLength = startIndex - foundIndex;
|
|
|
7140 |
fileContents = fileContents.substring(0, foundIndex) +
|
|
|
7141 |
(shouldInclude ? fileContents.substring(startIndex, endMarkerIndex) : "") +
|
|
|
7142 |
fileContents.substring(lineEndIndex + 1, fileContents.length);
|
|
|
7143 |
|
|
|
7144 |
//Move startIndex to foundIndex, since that is the new position in the file
|
|
|
7145 |
//where we need to look for more conditionals in the next while loop pass.
|
|
|
7146 |
startIndex = foundIndex;
|
|
|
7147 |
} else {
|
|
|
7148 |
throw "Error in file: " +
|
|
|
7149 |
fileName +
|
|
|
7150 |
". Cannot find end marker for conditional comment: " +
|
|
|
7151 |
conditionLine;
|
|
|
7152 |
|
|
|
7153 |
}
|
|
|
7154 |
}
|
|
|
7155 |
}
|
|
|
7156 |
}
|
|
|
7157 |
|
|
|
7158 |
//If need to find all plugin resources to optimize, do that now,
|
|
|
7159 |
//before namespacing, since the namespacing will change the API
|
|
|
7160 |
//names.
|
|
|
7161 |
//If there is a plugin collector, scan the file for plugin resources.
|
|
|
7162 |
if (config.optimizeAllPluginResources && pluginCollector) {
|
|
|
7163 |
try {
|
|
|
7164 |
deps = parse.findDependencies(fileName, fileContents);
|
|
|
7165 |
if (deps.length) {
|
|
|
7166 |
for (i = 0; (dep = deps[i]); i++) {
|
|
|
7167 |
if (dep.indexOf('!') !== -1) {
|
|
|
7168 |
(pluginCollector[moduleName] ||
|
|
|
7169 |
(pluginCollector[moduleName] = [])).push(dep);
|
|
|
7170 |
}
|
|
|
7171 |
}
|
|
|
7172 |
}
|
|
|
7173 |
} catch (eDep) {
|
|
|
7174 |
logger.error('Parse error looking for plugin resources in ' +
|
|
|
7175 |
fileName + ', skipping.');
|
|
|
7176 |
}
|
|
|
7177 |
}
|
|
|
7178 |
|
|
|
7179 |
//Do namespacing
|
|
|
7180 |
if (onLifecycleName === 'OnSave' && config.namespace) {
|
|
|
7181 |
fileContents = pragma.namespace(fileContents, config.namespace, onLifecycleName);
|
|
|
7182 |
}
|
|
|
7183 |
|
|
|
7184 |
|
|
|
7185 |
return pragma.removeStrict(fileContents, config);
|
|
|
7186 |
}
|
|
|
7187 |
};
|
|
|
7188 |
|
|
|
7189 |
return pragma;
|
|
|
7190 |
});
|
|
|
7191 |
if(env === 'node') {
|
|
|
7192 |
/**
|
|
|
7193 |
* @license Copyright (c) 2010-2011, The Dojo Foundation All Rights Reserved.
|
|
|
7194 |
* Available via the MIT or new BSD license.
|
|
|
7195 |
* see: http://github.com/jrburke/requirejs for details
|
|
|
7196 |
*/
|
|
|
7197 |
|
|
|
7198 |
/*jslint strict: false */
|
|
|
7199 |
/*global define: false */
|
|
|
7200 |
|
|
|
7201 |
define('node/optimize', {});
|
|
|
7202 |
|
|
|
7203 |
}
|
|
|
7204 |
|
|
|
7205 |
if(env === 'rhino') {
|
|
|
7206 |
/**
|
|
|
7207 |
* @license Copyright (c) 2010-2011, The Dojo Foundation All Rights Reserved.
|
|
|
7208 |
* Available via the MIT or new BSD license.
|
|
|
7209 |
* see: http://github.com/jrburke/requirejs for details
|
|
|
7210 |
*/
|
|
|
7211 |
|
|
|
7212 |
/*jslint strict: false, plusplus: false */
|
|
|
7213 |
/*global define: false, java: false, Packages: false */
|
|
|
7214 |
|
|
|
7215 |
define('rhino/optimize', ['logger'], function (logger) {
|
|
|
7216 |
|
|
|
7217 |
//Add .reduce to Rhino so UglifyJS can run in Rhino,
|
|
|
7218 |
//inspired by https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/reduce
|
|
|
7219 |
//but rewritten for brevity, and to be good enough for use by UglifyJS.
|
|
|
7220 |
if (!Array.prototype.reduce) {
|
|
|
7221 |
Array.prototype.reduce = function (fn /*, initialValue */) {
|
|
|
7222 |
var i = 0,
|
|
|
7223 |
length = this.length,
|
|
|
7224 |
accumulator;
|
|
|
7225 |
|
|
|
7226 |
if (arguments.length >= 2) {
|
|
|
7227 |
accumulator = arguments[1];
|
|
|
7228 |
} else {
|
|
|
7229 |
do {
|
|
|
7230 |
if (i in this) {
|
|
|
7231 |
accumulator = this[i++];
|
|
|
7232 |
break;
|
|
|
7233 |
}
|
|
|
7234 |
}
|
|
|
7235 |
while (true);
|
|
|
7236 |
}
|
|
|
7237 |
|
|
|
7238 |
for (; i < length; i++) {
|
|
|
7239 |
if (i in this) {
|
|
|
7240 |
accumulator = fn.call(undefined, accumulator, this[i], i, this);
|
|
|
7241 |
}
|
|
|
7242 |
}
|
|
|
7243 |
|
|
|
7244 |
return accumulator;
|
|
|
7245 |
};
|
|
|
7246 |
}
|
|
|
7247 |
|
|
|
7248 |
var JSSourceFilefromCode, optimize;
|
|
|
7249 |
|
|
|
7250 |
//Bind to Closure compiler, but if it is not available, do not sweat it.
|
|
|
7251 |
try {
|
|
|
7252 |
JSSourceFilefromCode = java.lang.Class.forName('com.google.javascript.jscomp.JSSourceFile').getMethod('fromCode', [java.lang.String, java.lang.String]);
|
|
|
7253 |
} catch (e) {}
|
|
|
7254 |
|
|
|
7255 |
//Helper for closure compiler, because of weird Java-JavaScript interactions.
|
|
|
7256 |
function closurefromCode(filename, content) {
|
|
|
7257 |
return JSSourceFilefromCode.invoke(null, [filename, content]);
|
|
|
7258 |
}
|
|
|
7259 |
|
|
|
7260 |
optimize = {
|
|
|
7261 |
closure: function (fileName, fileContents, keepLines, config) {
|
|
|
7262 |
config = config || {};
|
|
|
7263 |
var jscomp = Packages.com.google.javascript.jscomp,
|
|
|
7264 |
flags = Packages.com.google.common.flags,
|
|
|
7265 |
//Fake extern
|
|
|
7266 |
externSourceFile = closurefromCode("fakeextern.js", " "),
|
|
|
7267 |
//Set up source input
|
|
|
7268 |
jsSourceFile = closurefromCode(String(fileName), String(fileContents)),
|
|
|
7269 |
options, option, FLAG_compilation_level, compiler,
|
|
|
7270 |
Compiler = Packages.com.google.javascript.jscomp.Compiler,
|
|
|
7271 |
result;
|
|
|
7272 |
|
|
|
7273 |
logger.trace("Minifying file: " + fileName);
|
|
|
7274 |
|
|
|
7275 |
//Set up options
|
|
|
7276 |
options = new jscomp.CompilerOptions();
|
|
|
7277 |
for (option in config.CompilerOptions) {
|
|
|
7278 |
// options are false by default and jslint wanted an if statement in this for loop
|
|
|
7279 |
if (config.CompilerOptions[option]) {
|
|
|
7280 |
options[option] = config.CompilerOptions[option];
|
|
|
7281 |
}
|
|
|
7282 |
|
|
|
7283 |
}
|
|
|
7284 |
options.prettyPrint = keepLines || options.prettyPrint;
|
|
|
7285 |
|
|
|
7286 |
FLAG_compilation_level = jscomp.CompilationLevel[config.CompilationLevel || 'SIMPLE_OPTIMIZATIONS'];
|
|
|
7287 |
FLAG_compilation_level.setOptionsForCompilationLevel(options);
|
|
|
7288 |
|
|
|
7289 |
//Trigger the compiler
|
|
|
7290 |
Compiler.setLoggingLevel(Packages.java.util.logging.Level[config.loggingLevel || 'WARNING']);
|
|
|
7291 |
compiler = new Compiler();
|
|
|
7292 |
|
|
|
7293 |
result = compiler.compile(externSourceFile, jsSourceFile, options);
|
|
|
7294 |
if (!result.success) {
|
|
|
7295 |
logger.error('Cannot closure compile file: ' + fileName + '. Skipping it.');
|
|
|
7296 |
} else {
|
|
|
7297 |
fileContents = compiler.toSource();
|
|
|
7298 |
}
|
|
|
7299 |
|
|
|
7300 |
return fileContents;
|
|
|
7301 |
}
|
|
|
7302 |
};
|
|
|
7303 |
|
|
|
7304 |
return optimize;
|
|
|
7305 |
});
|
|
|
7306 |
}
|
|
|
7307 |
/**
|
|
|
7308 |
* @license Copyright (c) 2010-2011, The Dojo Foundation All Rights Reserved.
|
|
|
7309 |
* Available via the MIT or new BSD license.
|
|
|
7310 |
* see: http://github.com/jrburke/requirejs for details
|
|
|
7311 |
*/
|
|
|
7312 |
|
|
|
7313 |
/*jslint plusplus: false, nomen: false, regexp: false, strict: false */
|
|
|
7314 |
/*global define: false */
|
|
|
7315 |
|
|
|
7316 |
define('optimize', [ 'lang', 'logger', 'env!env/optimize', 'env!env/file', 'parse',
|
|
|
7317 |
'pragma', 'uglifyjs/index'],
|
|
|
7318 |
function (lang, logger, envOptimize, file, parse,
|
|
|
7319 |
pragma, uglify) {
|
|
|
7320 |
|
|
|
7321 |
var optimize,
|
|
|
7322 |
cssImportRegExp = /\@import\s+(url\()?\s*([^);]+)\s*(\))?([\w, ]*)(;)?/g,
|
|
|
7323 |
cssUrlRegExp = /\url\(\s*([^\)]+)\s*\)?/g;
|
|
|
7324 |
|
|
|
7325 |
/**
|
|
|
7326 |
* If an URL from a CSS url value contains start/end quotes, remove them.
|
|
|
7327 |
* This is not done in the regexp, since my regexp fu is not that strong,
|
|
|
7328 |
* and the CSS spec allows for ' and " in the URL if they are backslash escaped.
|
|
|
7329 |
* @param {String} url
|
|
|
7330 |
*/
|
|
|
7331 |
function cleanCssUrlQuotes(url) {
|
|
|
7332 |
//Make sure we are not ending in whitespace.
|
|
|
7333 |
//Not very confident of the css regexps above that there will not be ending
|
|
|
7334 |
//whitespace.
|
|
|
7335 |
url = url.replace(/\s+$/, "");
|
|
|
7336 |
|
|
|
7337 |
if (url.charAt(0) === "'" || url.charAt(0) === "\"") {
|
|
|
7338 |
url = url.substring(1, url.length - 1);
|
|
|
7339 |
}
|
|
|
7340 |
|
|
|
7341 |
return url;
|
|
|
7342 |
}
|
|
|
7343 |
|
|
|
7344 |
/**
|
|
|
7345 |
* Inlines nested stylesheets that have @import calls in them.
|
|
|
7346 |
* @param {String} fileName
|
|
|
7347 |
* @param {String} fileContents
|
|
|
7348 |
* @param {String} [cssImportIgnore]
|
|
|
7349 |
*/
|
|
|
7350 |
function flattenCss(fileName, fileContents, cssImportIgnore) {
|
|
|
7351 |
//Find the last slash in the name.
|
|
|
7352 |
fileName = fileName.replace(lang.backSlashRegExp, "/");
|
|
|
7353 |
var endIndex = fileName.lastIndexOf("/"),
|
|
|
7354 |
//Make a file path based on the last slash.
|
|
|
7355 |
//If no slash, so must be just a file name. Use empty string then.
|
|
|
7356 |
filePath = (endIndex !== -1) ? fileName.substring(0, endIndex + 1) : "";
|
|
|
7357 |
|
|
|
7358 |
//Make sure we have a delimited ignore list to make matching faster
|
|
|
7359 |
if (cssImportIgnore && cssImportIgnore.charAt(cssImportIgnore.length - 1) !== ",") {
|
|
|
7360 |
cssImportIgnore += ",";
|
|
|
7361 |
}
|
|
|
7362 |
|
|
|
7363 |
return fileContents.replace(cssImportRegExp, function (fullMatch, urlStart, importFileName, urlEnd, mediaTypes) {
|
|
|
7364 |
//Only process media type "all" or empty media type rules.
|
|
|
7365 |
if (mediaTypes && ((mediaTypes.replace(/^\s\s*/, '').replace(/\s\s*$/, '')) !== "all")) {
|
|
|
7366 |
return fullMatch;
|
|
|
7367 |
}
|
|
|
7368 |
|
|
|
7369 |
importFileName = cleanCssUrlQuotes(importFileName);
|
|
|
7370 |
|
|
|
7371 |
//Ignore the file import if it is part of an ignore list.
|
|
|
7372 |
if (cssImportIgnore && cssImportIgnore.indexOf(importFileName + ",") !== -1) {
|
|
|
7373 |
return fullMatch;
|
|
|
7374 |
}
|
|
|
7375 |
|
|
|
7376 |
//Make sure we have a unix path for the rest of the operation.
|
|
|
7377 |
importFileName = importFileName.replace(lang.backSlashRegExp, "/");
|
|
|
7378 |
|
|
|
7379 |
try {
|
|
|
7380 |
//if a relative path, then tack on the filePath.
|
|
|
7381 |
//If it is not a relative path, then the readFile below will fail,
|
|
|
7382 |
//and we will just skip that import.
|
|
|
7383 |
var fullImportFileName = importFileName.charAt(0) === "/" ? importFileName : filePath + importFileName,
|
|
|
7384 |
importContents = file.readFile(fullImportFileName), i,
|
|
|
7385 |
importEndIndex, importPath, fixedUrlMatch, colonIndex, parts;
|
|
|
7386 |
|
|
|
7387 |
//Make sure to flatten any nested imports.
|
|
|
7388 |
importContents = flattenCss(fullImportFileName, importContents);
|
|
|
7389 |
|
|
|
7390 |
//Make the full import path
|
|
|
7391 |
importEndIndex = importFileName.lastIndexOf("/");
|
|
|
7392 |
|
|
|
7393 |
//Make a file path based on the last slash.
|
|
|
7394 |
//If no slash, so must be just a file name. Use empty string then.
|
|
|
7395 |
importPath = (importEndIndex !== -1) ? importFileName.substring(0, importEndIndex + 1) : "";
|
|
|
7396 |
|
|
|
7397 |
//Modify URL paths to match the path represented by this file.
|
|
|
7398 |
importContents = importContents.replace(cssUrlRegExp, function (fullMatch, urlMatch) {
|
|
|
7399 |
fixedUrlMatch = cleanCssUrlQuotes(urlMatch);
|
|
|
7400 |
fixedUrlMatch = fixedUrlMatch.replace(lang.backSlashRegExp, "/");
|
|
|
7401 |
|
|
|
7402 |
//Only do the work for relative URLs. Skip things that start with / or have
|
|
|
7403 |
//a protocol.
|
|
|
7404 |
colonIndex = fixedUrlMatch.indexOf(":");
|
|
|
7405 |
if (fixedUrlMatch.charAt(0) !== "/" && (colonIndex === -1 || colonIndex > fixedUrlMatch.indexOf("/"))) {
|
|
|
7406 |
//It is a relative URL, tack on the path prefix
|
|
|
7407 |
urlMatch = importPath + fixedUrlMatch;
|
|
|
7408 |
} else {
|
|
|
7409 |
logger.trace(importFileName + "\n URL not a relative URL, skipping: " + urlMatch);
|
|
|
7410 |
}
|
|
|
7411 |
|
|
|
7412 |
//Collapse .. and .
|
|
|
7413 |
parts = urlMatch.split("/");
|
|
|
7414 |
for (i = parts.length - 1; i > 0; i--) {
|
|
|
7415 |
if (parts[i] === ".") {
|
|
|
7416 |
parts.splice(i, 1);
|
|
|
7417 |
} else if (parts[i] === "..") {
|
|
|
7418 |
if (i !== 0 && parts[i - 1] !== "..") {
|
|
|
7419 |
parts.splice(i - 1, 2);
|
|
|
7420 |
i -= 1;
|
|
|
7421 |
}
|
|
|
7422 |
}
|
|
|
7423 |
}
|
|
|
7424 |
|
|
|
7425 |
return "url(" + parts.join("/") + ")";
|
|
|
7426 |
});
|
|
|
7427 |
|
|
|
7428 |
return importContents;
|
|
|
7429 |
} catch (e) {
|
|
|
7430 |
logger.trace(fileName + "\n Cannot inline css import, skipping: " + importFileName);
|
|
|
7431 |
return fullMatch;
|
|
|
7432 |
}
|
|
|
7433 |
});
|
|
|
7434 |
}
|
|
|
7435 |
|
|
|
7436 |
optimize = {
|
|
|
7437 |
/**
|
|
|
7438 |
* Optimizes a file that contains JavaScript content. Optionally collects
|
|
|
7439 |
* plugin resources mentioned in a file, and then passes the content
|
|
|
7440 |
* through an minifier if one is specified via config.optimize.
|
|
|
7441 |
*
|
|
|
7442 |
* @param {String} fileName the name of the file to optimize
|
|
|
7443 |
* @param {String} outFileName the name of the file to use for the
|
|
|
7444 |
* saved optimized content.
|
|
|
7445 |
* @param {Object} config the build config object.
|
|
|
7446 |
* @param {String} [moduleName] the module name to use for the file.
|
|
|
7447 |
* Used for plugin resource collection.
|
|
|
7448 |
* @param {Array} [pluginCollector] storage for any plugin resources
|
|
|
7449 |
* found.
|
|
|
7450 |
*/
|
|
|
7451 |
jsFile: function (fileName, outFileName, config, moduleName, pluginCollector) {
|
|
|
7452 |
var parts = (config.optimize + "").split('.'),
|
|
|
7453 |
optimizerName = parts[0],
|
|
|
7454 |
keepLines = parts[1] === 'keepLines',
|
|
|
7455 |
fileContents, optFunc, deps, i, dep;
|
|
|
7456 |
|
|
|
7457 |
fileContents = file.readFile(fileName);
|
|
|
7458 |
|
|
|
7459 |
//Apply pragmas/namespace renaming
|
|
|
7460 |
fileContents = pragma.process(fileName, fileContents, config, 'OnSave', pluginCollector);
|
|
|
7461 |
|
|
|
7462 |
//Optimize the JS files if asked.
|
|
|
7463 |
if (optimizerName && optimizerName !== 'none') {
|
|
|
7464 |
optFunc = envOptimize[optimizerName] || optimize.optimizers[optimizerName];
|
|
|
7465 |
if (!optFunc) {
|
|
|
7466 |
throw new Error('optimizer with name of "' +
|
|
|
7467 |
optimizerName +
|
|
|
7468 |
'" not found for this environment');
|
|
|
7469 |
}
|
|
|
7470 |
fileContents = optFunc(fileName, fileContents, keepLines,
|
|
|
7471 |
config[optimizerName]);
|
|
|
7472 |
}
|
|
|
7473 |
|
|
|
7474 |
file.saveUtf8File(outFileName, fileContents);
|
|
|
7475 |
},
|
|
|
7476 |
|
|
|
7477 |
/**
|
|
|
7478 |
* Optimizes one CSS file, inlining @import calls, stripping comments, and
|
|
|
7479 |
* optionally removes line returns.
|
|
|
7480 |
* @param {String} fileName the path to the CSS file to optimize
|
|
|
7481 |
* @param {String} outFileName the path to save the optimized file.
|
|
|
7482 |
* @param {Object} config the config object with the optimizeCss and
|
|
|
7483 |
* cssImportIgnore options.
|
|
|
7484 |
*/
|
|
|
7485 |
cssFile: function (fileName, outFileName, config) {
|
|
|
7486 |
//Read in the file. Make sure we have a JS string.
|
|
|
7487 |
var originalFileContents = file.readFile(fileName),
|
|
|
7488 |
fileContents = flattenCss(fileName, originalFileContents, config.cssImportIgnore),
|
|
|
7489 |
startIndex, endIndex;
|
|
|
7490 |
|
|
|
7491 |
//Do comment removal.
|
|
|
7492 |
try {
|
|
|
7493 |
startIndex = -1;
|
|
|
7494 |
//Get rid of comments.
|
|
|
7495 |
while ((startIndex = fileContents.indexOf("/*")) !== -1) {
|
|
|
7496 |
endIndex = fileContents.indexOf("*/", startIndex + 2);
|
|
|
7497 |
if (endIndex === -1) {
|
|
|
7498 |
throw "Improper comment in CSS file: " + fileName;
|
|
|
7499 |
}
|
|
|
7500 |
fileContents = fileContents.substring(0, startIndex) + fileContents.substring(endIndex + 2, fileContents.length);
|
|
|
7501 |
}
|
|
|
7502 |
//Get rid of newlines.
|
|
|
7503 |
if (config.optimizeCss.indexOf(".keepLines") === -1) {
|
|
|
7504 |
fileContents = fileContents.replace(/[\r\n]/g, "");
|
|
|
7505 |
fileContents = fileContents.replace(/\s+/g, " ");
|
|
|
7506 |
fileContents = fileContents.replace(/\{\s/g, "{");
|
|
|
7507 |
fileContents = fileContents.replace(/\s\}/g, "}");
|
|
|
7508 |
} else {
|
|
|
7509 |
//Remove multiple empty lines.
|
|
|
7510 |
fileContents = fileContents.replace(/(\r\n)+/g, "\r\n");
|
|
|
7511 |
fileContents = fileContents.replace(/(\n)+/g, "\n");
|
|
|
7512 |
}
|
|
|
7513 |
} catch (e) {
|
|
|
7514 |
fileContents = originalFileContents;
|
|
|
7515 |
logger.error("Could not optimized CSS file: " + fileName + ", error: " + e);
|
|
|
7516 |
}
|
|
|
7517 |
|
|
|
7518 |
file.saveUtf8File(outFileName, fileContents);
|
|
|
7519 |
},
|
|
|
7520 |
|
|
|
7521 |
/**
|
|
|
7522 |
* Optimizes CSS files, inlining @import calls, stripping comments, and
|
|
|
7523 |
* optionally removes line returns.
|
|
|
7524 |
* @param {String} startDir the path to the top level directory
|
|
|
7525 |
* @param {Object} config the config object with the optimizeCss and
|
|
|
7526 |
* cssImportIgnore options.
|
|
|
7527 |
*/
|
|
|
7528 |
css: function (startDir, config) {
|
|
|
7529 |
if (config.optimizeCss.indexOf("standard") !== -1) {
|
|
|
7530 |
var i, fileName,
|
|
|
7531 |
fileList = file.getFilteredFileList(startDir, /\.css$/, true);
|
|
|
7532 |
if (fileList) {
|
|
|
7533 |
for (i = 0; i < fileList.length; i++) {
|
|
|
7534 |
fileName = fileList[i];
|
|
|
7535 |
logger.trace("Optimizing (" + config.optimizeCss + ") CSS file: " + fileName);
|
|
|
7536 |
optimize.cssFile(fileName, fileName, config);
|
|
|
7537 |
}
|
|
|
7538 |
}
|
|
|
7539 |
}
|
|
|
7540 |
},
|
|
|
7541 |
|
|
|
7542 |
optimizers: {
|
|
|
7543 |
uglify: function (fileName, fileContents, keepLines, config) {
|
|
|
7544 |
var parser = uglify.parser,
|
|
|
7545 |
processor = uglify.uglify,
|
|
|
7546 |
ast, genCodeConfig;
|
|
|
7547 |
|
|
|
7548 |
config = config || {};
|
|
|
7549 |
genCodeConfig = config.gen_codeOptions || keepLines;
|
|
|
7550 |
|
|
|
7551 |
logger.trace("Uglifying file: " + fileName);
|
|
|
7552 |
|
|
|
7553 |
try {
|
|
|
7554 |
ast = parser.parse(fileContents, config.strict_semicolons);
|
|
|
7555 |
ast = processor.ast_mangle(ast, config.do_toplevel);
|
|
|
7556 |
ast = processor.ast_squeeze(ast, config.ast_squeezeOptions);
|
|
|
7557 |
|
|
|
7558 |
fileContents = processor.gen_code(ast, genCodeConfig);
|
|
|
7559 |
} catch (e) {
|
|
|
7560 |
logger.error('Cannot uglify file: ' + fileName + '. Skipping it. Error is:\n' + e.toString());
|
|
|
7561 |
}
|
|
|
7562 |
return fileContents;
|
|
|
7563 |
}
|
|
|
7564 |
}
|
|
|
7565 |
};
|
|
|
7566 |
|
|
|
7567 |
return optimize;
|
|
|
7568 |
});/**
|
|
|
7569 |
* @license RequireJS Copyright (c) 2010-2011, The Dojo Foundation All Rights Reserved.
|
|
|
7570 |
* Available via the MIT or new BSD license.
|
|
|
7571 |
* see: http://github.com/jrburke/requirejs for details
|
|
|
7572 |
*/
|
|
|
7573 |
/*
|
|
|
7574 |
* This file patches require.js to communicate with the build system.
|
|
|
7575 |
*/
|
|
|
7576 |
|
|
|
7577 |
/*jslint nomen: false, plusplus: false, regexp: false, strict: false */
|
|
|
7578 |
/*global require: false, define: true */
|
|
|
7579 |
|
|
|
7580 |
//NOT asking for require as a dependency since the goal is to modify the
|
|
|
7581 |
//global require below
|
|
|
7582 |
define('requirePatch', [ 'env!env/file', 'pragma', 'parse'],
|
|
|
7583 |
function (file, pragma, parse) {
|
|
|
7584 |
|
|
|
7585 |
var allowRun = true;
|
|
|
7586 |
|
|
|
7587 |
//This method should be called when the patches to require should take hold.
|
|
|
7588 |
return function () {
|
|
|
7589 |
if (!allowRun) {
|
|
|
7590 |
return;
|
|
|
7591 |
}
|
|
|
7592 |
allowRun = false;
|
|
|
7593 |
|
|
|
7594 |
var layer,
|
|
|
7595 |
pluginBuilderRegExp = /(["']?)pluginBuilder(["']?)\s*[=\:]\s*["']([^'"\s]+)["']/,
|
|
|
7596 |
oldDef,
|
|
|
7597 |
cachedFileContents = {};
|
|
|
7598 |
|
|
|
7599 |
|
|
|
7600 |
/** Print out some extrs info about the module tree that caused the error. **/
|
|
|
7601 |
require.onError = function (err) {
|
|
|
7602 |
|
|
|
7603 |
var msg = '\nIn module tree:\n',
|
|
|
7604 |
standardIndent = ' ',
|
|
|
7605 |
tree = err.moduleTree,
|
|
|
7606 |
i, j, mod;
|
|
|
7607 |
|
|
|
7608 |
if (tree && tree.length > 0) {
|
|
|
7609 |
for (i = tree.length - 1; i > -1 && (mod = tree[i]); i--) {
|
|
|
7610 |
for (j = tree.length - i; j > -1; j--) {
|
|
|
7611 |
msg += standardIndent;
|
|
|
7612 |
}
|
|
|
7613 |
msg += mod + '\n';
|
|
|
7614 |
}
|
|
|
7615 |
|
|
|
7616 |
err = new Error(err.toString() + msg);
|
|
|
7617 |
}
|
|
|
7618 |
|
|
|
7619 |
throw err;
|
|
|
7620 |
};
|
|
|
7621 |
|
|
|
7622 |
|
|
|
7623 |
/** Reset state for each build layer pass. */
|
|
|
7624 |
require._buildReset = function () {
|
|
|
7625 |
var oldContext = require.s.contexts._;
|
|
|
7626 |
|
|
|
7627 |
//Clear up the existing context.
|
|
|
7628 |
delete require.s.contexts._;
|
|
|
7629 |
|
|
|
7630 |
//Set up new context, so the layer object can hold onto it.
|
|
|
7631 |
require({});
|
|
|
7632 |
|
|
|
7633 |
layer = require._layer = {
|
|
|
7634 |
buildPathMap: {},
|
|
|
7635 |
buildFileToModule: {},
|
|
|
7636 |
buildFilePaths: [],
|
|
|
7637 |
pathAdded: {},
|
|
|
7638 |
modulesWithNames: {},
|
|
|
7639 |
needsDefine: {},
|
|
|
7640 |
existingRequireUrl: "",
|
|
|
7641 |
context: require.s.contexts._
|
|
|
7642 |
};
|
|
|
7643 |
|
|
|
7644 |
//Return the previous context in case it is needed, like for
|
|
|
7645 |
//the basic config object.
|
|
|
7646 |
return oldContext;
|
|
|
7647 |
};
|
|
|
7648 |
|
|
|
7649 |
require._buildReset();
|
|
|
7650 |
|
|
|
7651 |
/**
|
|
|
7652 |
* Makes sure the URL is something that can be supported by the
|
|
|
7653 |
* optimization tool.
|
|
|
7654 |
* @param {String} url
|
|
|
7655 |
* @returns {Boolean}
|
|
|
7656 |
*/
|
|
|
7657 |
require._isSupportedBuildUrl = function (url) {
|
|
|
7658 |
//Ignore URLs with protocols or question marks, means either network
|
|
|
7659 |
//access is needed to fetch it or it is too dynamic. Note that
|
|
|
7660 |
//on Windows, full paths are used for some urls, which include
|
|
|
7661 |
//the drive, like c:/something, so need to test for something other
|
|
|
7662 |
//than just a colon.
|
|
|
7663 |
return url.indexOf("://") === -1 && url.indexOf("?") === -1 &&
|
|
|
7664 |
url.indexOf('empty:') !== 0;
|
|
|
7665 |
};
|
|
|
7666 |
|
|
|
7667 |
//Override define() to catch modules that just define an object, so that
|
|
|
7668 |
//a dummy define call is not put in the build file for them. They do
|
|
|
7669 |
//not end up getting defined via require.execCb, so we need to catch them
|
|
|
7670 |
//at the define call.
|
|
|
7671 |
oldDef = define;
|
|
|
7672 |
|
|
|
7673 |
//This function signature does not have to be exact, just match what we
|
|
|
7674 |
//are looking for.
|
|
|
7675 |
define = function (name, obj) {
|
|
|
7676 |
if (typeof name === "string" && !layer.needsDefine[name]) {
|
|
|
7677 |
layer.modulesWithNames[name] = true;
|
|
|
7678 |
}
|
|
|
7679 |
return oldDef.apply(require, arguments);
|
|
|
7680 |
};
|
|
|
7681 |
|
|
|
7682 |
//Add some utilities for plugins
|
|
|
7683 |
require._readFile = file.readFile;
|
|
|
7684 |
require._fileExists = function (path) {
|
|
|
7685 |
return file.exists(path);
|
|
|
7686 |
};
|
|
|
7687 |
|
|
|
7688 |
function normalizeUrlWithBase(context, moduleName, url) {
|
|
|
7689 |
//Adjust the URL if it was not transformed to use baseUrl.
|
|
|
7690 |
if (require.jsExtRegExp.test(moduleName)) {
|
|
|
7691 |
url = (context.config.dir || context.config.dirBaseUrl) + url;
|
|
|
7692 |
}
|
|
|
7693 |
return url;
|
|
|
7694 |
}
|
|
|
7695 |
|
|
|
7696 |
//Override load so that the file paths can be collected.
|
|
|
7697 |
require.load = function (context, moduleName, url) {
|
|
|
7698 |
/*jslint evil: true */
|
|
|
7699 |
var contents, pluginBuilderMatch, builderName;
|
|
|
7700 |
|
|
|
7701 |
//Adjust the URL if it was not transformed to use baseUrl.
|
|
|
7702 |
url = normalizeUrlWithBase(context, moduleName, url);
|
|
|
7703 |
|
|
|
7704 |
context.scriptCount += 1;
|
|
|
7705 |
|
|
|
7706 |
//Only handle urls that can be inlined, so that means avoiding some
|
|
|
7707 |
//URLs like ones that require network access or may be too dynamic,
|
|
|
7708 |
//like JSONP
|
|
|
7709 |
if (require._isSupportedBuildUrl(url)) {
|
|
|
7710 |
//Save the module name to path and path to module name mappings.
|
|
|
7711 |
layer.buildPathMap[moduleName] = url;
|
|
|
7712 |
layer.buildFileToModule[url] = moduleName;
|
|
|
7713 |
|
|
|
7714 |
if (moduleName in context.plugins) {
|
|
|
7715 |
//plugins need to have their source evaled as-is.
|
|
|
7716 |
context.needFullExec[moduleName] = true;
|
|
|
7717 |
}
|
|
|
7718 |
|
|
|
7719 |
try {
|
|
|
7720 |
if (url in cachedFileContents &&
|
|
|
7721 |
(!context.needFullExec[moduleName] || context.fullExec[moduleName])) {
|
|
|
7722 |
contents = cachedFileContents[url];
|
|
|
7723 |
} else {
|
|
|
7724 |
//Load the file contents, process for conditionals, then
|
|
|
7725 |
//evaluate it.
|
|
|
7726 |
contents = file.readFile(url);
|
|
|
7727 |
contents = pragma.process(url, contents, context.config, 'OnExecute');
|
|
|
7728 |
|
|
|
7729 |
//Find out if the file contains a require() definition. Need to know
|
|
|
7730 |
//this so we can inject plugins right after it, but before they are needed,
|
|
|
7731 |
//and to make sure this file is first, so that define calls work.
|
|
|
7732 |
//This situation mainly occurs when the build is done on top of the output
|
|
|
7733 |
//of another build, where the first build may include require somewhere in it.
|
|
|
7734 |
if (!layer.existingRequireUrl && parse.definesRequire(url, contents)) {
|
|
|
7735 |
layer.existingRequireUrl = url;
|
|
|
7736 |
}
|
|
|
7737 |
|
|
|
7738 |
if (moduleName in context.plugins) {
|
|
|
7739 |
//This is a loader plugin, check to see if it has a build extension,
|
|
|
7740 |
//otherwise the plugin will act as the plugin builder too.
|
|
|
7741 |
pluginBuilderMatch = pluginBuilderRegExp.exec(contents);
|
|
|
7742 |
if (pluginBuilderMatch) {
|
|
|
7743 |
//Load the plugin builder for the plugin contents.
|
|
|
7744 |
builderName = context.normalize(pluginBuilderMatch[3], moduleName);
|
|
|
7745 |
contents = file.readFile(context.nameToUrl(builderName));
|
|
|
7746 |
}
|
|
|
7747 |
}
|
|
|
7748 |
|
|
|
7749 |
//Parse out the require and define calls.
|
|
|
7750 |
//Do this even for plugins in case they have their own
|
|
|
7751 |
//dependencies that may be separate to how the pluginBuilder works.
|
|
|
7752 |
if (!context.needFullExec[moduleName]) {
|
|
|
7753 |
contents = parse(moduleName, url, contents, {
|
|
|
7754 |
insertNeedsDefine: true
|
|
|
7755 |
});
|
|
|
7756 |
}
|
|
|
7757 |
|
|
|
7758 |
cachedFileContents[url] = contents;
|
|
|
7759 |
}
|
|
|
7760 |
|
|
|
7761 |
if (contents) {
|
|
|
7762 |
eval(contents);
|
|
|
7763 |
}
|
|
|
7764 |
|
|
|
7765 |
//Need to close out completion of this module
|
|
|
7766 |
//so that listeners will get notified that it is available.
|
|
|
7767 |
try {
|
|
|
7768 |
context.completeLoad(moduleName);
|
|
|
7769 |
} catch (e) {
|
|
|
7770 |
//Track which module could not complete loading.
|
|
|
7771 |
(e.moduleTree || (e.moduleTree = [])).push(moduleName);
|
|
|
7772 |
throw e;
|
|
|
7773 |
}
|
|
|
7774 |
|
|
|
7775 |
} catch (eOuter) {
|
|
|
7776 |
if (!eOuter.fileName) {
|
|
|
7777 |
eOuter.fileName = url;
|
|
|
7778 |
}
|
|
|
7779 |
throw eOuter;
|
|
|
7780 |
}
|
|
|
7781 |
} else {
|
|
|
7782 |
//With unsupported URLs still need to call completeLoad to
|
|
|
7783 |
//finish loading.
|
|
|
7784 |
context.completeLoad(moduleName);
|
|
|
7785 |
}
|
|
|
7786 |
|
|
|
7787 |
//Mark the module loaded.
|
|
|
7788 |
context.loaded[moduleName] = true;
|
|
|
7789 |
};
|
|
|
7790 |
|
|
|
7791 |
|
|
|
7792 |
//Called when execManager runs for a dependency. Used to figure out
|
|
|
7793 |
//what order of execution.
|
|
|
7794 |
require.onResourceLoad = function (context, map) {
|
|
|
7795 |
var fullName = map.fullName,
|
|
|
7796 |
url;
|
|
|
7797 |
|
|
|
7798 |
//Ignore "fake" modules, usually generated by plugin code, since
|
|
|
7799 |
//they do not map back to a real file to include in the optimizer,
|
|
|
7800 |
//or it will be included, but in a different form.
|
|
|
7801 |
if (context.fake[fullName]) {
|
|
|
7802 |
return;
|
|
|
7803 |
}
|
|
|
7804 |
|
|
|
7805 |
//A plugin.
|
|
|
7806 |
if (map.prefix) {
|
|
|
7807 |
if (!layer.pathAdded[fullName]) {
|
|
|
7808 |
layer.buildFilePaths.push(fullName);
|
|
|
7809 |
//For plugins the real path is not knowable, use the name
|
|
|
7810 |
//for both module to file and file to module mappings.
|
|
|
7811 |
layer.buildPathMap[fullName] = fullName;
|
|
|
7812 |
layer.buildFileToModule[fullName] = fullName;
|
|
|
7813 |
layer.modulesWithNames[fullName] = true;
|
|
|
7814 |
layer.pathAdded[fullName] = true;
|
|
|
7815 |
}
|
|
|
7816 |
} else if (map.url && require._isSupportedBuildUrl(map.url)) {
|
|
|
7817 |
//If the url has not been added to the layer yet, and it
|
|
|
7818 |
//is from an actual file that was loaded, add it now.
|
|
|
7819 |
if (!layer.pathAdded[url] && layer.buildPathMap[fullName]) {
|
|
|
7820 |
url = normalizeUrlWithBase(context, map.fullName, map.url);
|
|
|
7821 |
//Remember the list of dependencies for this layer.
|
|
|
7822 |
layer.buildFilePaths.push(url);
|
|
|
7823 |
layer.pathAdded[url] = true;
|
|
|
7824 |
}
|
|
|
7825 |
}
|
|
|
7826 |
};
|
|
|
7827 |
|
|
|
7828 |
//Called by output of the parse() function, when a file does not
|
|
|
7829 |
//explicitly call define, probably just require, but the parse()
|
|
|
7830 |
//function normalizes on define() for dependency mapping and file
|
|
|
7831 |
//ordering works correctly.
|
|
|
7832 |
require.needsDefine = function (moduleName) {
|
|
|
7833 |
layer.needsDefine[moduleName] = true;
|
|
|
7834 |
};
|
|
|
7835 |
|
|
|
7836 |
//Marks module has having a name, and optionally executes the
|
|
|
7837 |
//callback, but only if it meets certain criteria.
|
|
|
7838 |
require.execCb = function (name, cb, args, exports) {
|
|
|
7839 |
if (!layer.needsDefine[name]) {
|
|
|
7840 |
layer.modulesWithNames[name] = true;
|
|
|
7841 |
}
|
|
|
7842 |
if (cb.__requireJsBuild || layer.context.needFullExec[name]) {
|
|
|
7843 |
return cb.apply(exports, args);
|
|
|
7844 |
}
|
|
|
7845 |
return undefined;
|
|
|
7846 |
};
|
|
|
7847 |
};
|
|
|
7848 |
});
|
|
|
7849 |
/**
|
|
|
7850 |
* @license RequireJS Copyright (c) 2010-2011, The Dojo Foundation All Rights Reserved.
|
|
|
7851 |
* Available via the MIT or new BSD license.
|
|
|
7852 |
* see: http://github.com/jrburke/requirejs for details
|
|
|
7853 |
*/
|
|
|
7854 |
|
|
|
7855 |
/*jslint plusplus: false, regexp: false, strict: false */
|
|
|
7856 |
/*global define: false, console: false */
|
|
|
7857 |
|
|
|
7858 |
define('commonJs', ['env!env/file', 'uglifyjs/index'], function (file, uglify) {
|
|
|
7859 |
var commonJs = {
|
|
|
7860 |
depRegExp: /require\s*\(\s*["']([\w-_\.\/]+)["']\s*\)/g,
|
|
|
7861 |
|
|
|
7862 |
//Set this to false in non-rhino environments. If rhino, then it uses
|
|
|
7863 |
//rhino's decompiler to remove comments before looking for require() calls,
|
|
|
7864 |
//otherwise, it will use a crude regexp approach to remove comments. The
|
|
|
7865 |
//rhino way is more robust, but he regexp is more portable across environments.
|
|
|
7866 |
useRhino: true,
|
|
|
7867 |
|
|
|
7868 |
//Set to false if you do not want this file to log. Useful in environments
|
|
|
7869 |
//like node where you want the work to happen without noise.
|
|
|
7870 |
useLog: true,
|
|
|
7871 |
|
|
|
7872 |
convertDir: function (commonJsPath, savePath) {
|
|
|
7873 |
var fileList, i,
|
|
|
7874 |
jsFileRegExp = /\.js$/,
|
|
|
7875 |
fileName, convertedFileName, fileContents;
|
|
|
7876 |
|
|
|
7877 |
//Get list of files to convert.
|
|
|
7878 |
fileList = file.getFilteredFileList(commonJsPath, /\w/, true);
|
|
|
7879 |
|
|
|
7880 |
//Normalize on front slashes and make sure the paths do not end in a slash.
|
|
|
7881 |
commonJsPath = commonJsPath.replace(/\\/g, "/");
|
|
|
7882 |
savePath = savePath.replace(/\\/g, "/");
|
|
|
7883 |
if (commonJsPath.charAt(commonJsPath.length - 1) === "/") {
|
|
|
7884 |
commonJsPath = commonJsPath.substring(0, commonJsPath.length - 1);
|
|
|
7885 |
}
|
|
|
7886 |
if (savePath.charAt(savePath.length - 1) === "/") {
|
|
|
7887 |
savePath = savePath.substring(0, savePath.length - 1);
|
|
|
7888 |
}
|
|
|
7889 |
|
|
|
7890 |
//Cycle through all the JS files and convert them.
|
|
|
7891 |
if (!fileList || !fileList.length) {
|
|
|
7892 |
if (commonJs.useLog) {
|
|
|
7893 |
if (commonJsPath === "convert") {
|
|
|
7894 |
//A request just to convert one file.
|
|
|
7895 |
console.log('\n\n' + commonJs.convert(savePath, file.readFile(savePath)));
|
|
|
7896 |
} else {
|
|
|
7897 |
console.log("No files to convert in directory: " + commonJsPath);
|
|
|
7898 |
}
|
|
|
7899 |
}
|
|
|
7900 |
} else {
|
|
|
7901 |
for (i = 0; (fileName = fileList[i]); i++) {
|
|
|
7902 |
convertedFileName = fileName.replace(commonJsPath, savePath);
|
|
|
7903 |
|
|
|
7904 |
//Handle JS files.
|
|
|
7905 |
if (jsFileRegExp.test(fileName)) {
|
|
|
7906 |
fileContents = file.readFile(fileName);
|
|
|
7907 |
fileContents = commonJs.convert(fileName, fileContents);
|
|
|
7908 |
file.saveUtf8File(convertedFileName, fileContents);
|
|
|
7909 |
} else {
|
|
|
7910 |
//Just copy the file over.
|
|
|
7911 |
file.copyFile(fileName, convertedFileName, true);
|
|
|
7912 |
}
|
|
|
7913 |
}
|
|
|
7914 |
}
|
|
|
7915 |
},
|
|
|
7916 |
|
|
|
7917 |
/**
|
|
|
7918 |
* Removes the comments from a string.
|
|
|
7919 |
*
|
|
|
7920 |
* @param {String} fileContents
|
|
|
7921 |
* @param {String} fileName mostly used for informative reasons if an error.
|
|
|
7922 |
*
|
|
|
7923 |
* @returns {String} a string of JS with comments removed.
|
|
|
7924 |
*/
|
|
|
7925 |
removeComments: function (fileContents, fileName) {
|
|
|
7926 |
//Uglify's ast generation removes comments, so just convert to ast,
|
|
|
7927 |
//then back to source code to get rid of comments.
|
|
|
7928 |
return uglify.uglify.gen_code(uglify.parser.parse(fileContents), true);
|
|
|
7929 |
},
|
|
|
7930 |
|
|
|
7931 |
/**
|
|
|
7932 |
* Regexp for testing if there is already a require.def call in the file,
|
|
|
7933 |
* in which case do not try to convert it.
|
|
|
7934 |
*/
|
|
|
7935 |
defRegExp: /define\s*\(\s*("|'|\[|function)/,
|
|
|
7936 |
|
|
|
7937 |
/**
|
|
|
7938 |
* Regexp for testing if there is a require([]) or require(function(){})
|
|
|
7939 |
* call, indicating the file is already in requirejs syntax.
|
|
|
7940 |
*/
|
|
|
7941 |
rjsRegExp: /require\s*\(\s*(\[|function)/,
|
|
|
7942 |
|
|
|
7943 |
/**
|
|
|
7944 |
* Does the actual file conversion.
|
|
|
7945 |
*
|
|
|
7946 |
* @param {String} fileName the name of the file.
|
|
|
7947 |
*
|
|
|
7948 |
* @param {String} fileContents the contents of a file :)
|
|
|
7949 |
*
|
|
|
7950 |
* @param {Boolean} skipDeps if true, require("") dependencies
|
|
|
7951 |
* will not be searched, but the contents will just be wrapped in the
|
|
|
7952 |
* standard require, exports, module dependencies. Only usable in sync
|
|
|
7953 |
* environments like Node where the require("") calls can be resolved on
|
|
|
7954 |
* the fly.
|
|
|
7955 |
*
|
|
|
7956 |
* @returns {String} the converted contents
|
|
|
7957 |
*/
|
|
|
7958 |
convert: function (fileName, fileContents, skipDeps) {
|
|
|
7959 |
//Strip out comments.
|
|
|
7960 |
try {
|
|
|
7961 |
var deps = [], depName, match,
|
|
|
7962 |
//Remove comments
|
|
|
7963 |
tempContents = commonJs.removeComments(fileContents, fileName);
|
|
|
7964 |
|
|
|
7965 |
//First see if the module is not already RequireJS-formatted.
|
|
|
7966 |
if (commonJs.defRegExp.test(tempContents) || commonJs.rjsRegExp.test(tempContents)) {
|
|
|
7967 |
return fileContents;
|
|
|
7968 |
}
|
|
|
7969 |
|
|
|
7970 |
//Reset the regexp to start at beginning of file. Do this
|
|
|
7971 |
//since the regexp is reused across files.
|
|
|
7972 |
commonJs.depRegExp.lastIndex = 0;
|
|
|
7973 |
|
|
|
7974 |
if (!skipDeps) {
|
|
|
7975 |
//Find dependencies in the code that was not in comments.
|
|
|
7976 |
while ((match = commonJs.depRegExp.exec(tempContents))) {
|
|
|
7977 |
depName = match[1];
|
|
|
7978 |
if (depName) {
|
|
|
7979 |
deps.push('"' + depName + '"');
|
|
|
7980 |
}
|
|
|
7981 |
}
|
|
|
7982 |
}
|
|
|
7983 |
|
|
|
7984 |
//Construct the wrapper boilerplate.
|
|
|
7985 |
fileContents = 'define(["require", "exports", "module"' +
|
|
|
7986 |
(deps.length ? ', ' + deps.join(",") : '') + '], ' +
|
|
|
7987 |
'function(require, exports, module) {\n' +
|
|
|
7988 |
fileContents +
|
|
|
7989 |
'\n});\n';
|
|
|
7990 |
} catch (e) {
|
|
|
7991 |
console.log("COULD NOT CONVERT: " + fileName + ", so skipping it. Error was: " + e);
|
|
|
7992 |
return fileContents;
|
|
|
7993 |
}
|
|
|
7994 |
|
|
|
7995 |
return fileContents;
|
|
|
7996 |
}
|
|
|
7997 |
};
|
|
|
7998 |
|
|
|
7999 |
return commonJs;
|
|
|
8000 |
});
|
|
|
8001 |
/**
|
|
|
8002 |
* @license Copyright (c) 2010-2011, The Dojo Foundation All Rights Reserved.
|
|
|
8003 |
* Available via the MIT or new BSD license.
|
|
|
8004 |
* see: http://github.com/jrburke/requirejs for details
|
|
|
8005 |
*/
|
|
|
8006 |
|
|
|
8007 |
/*jslint regexp: false, plusplus: false, nomen: false, strict: false */
|
|
|
8008 |
/*global define: false, require: false */
|
|
|
8009 |
|
|
|
8010 |
|
|
|
8011 |
define('build', [ 'lang', 'logger', 'env!env/file', 'parse', 'optimize', 'pragma',
|
|
|
8012 |
'env!env/load', 'requirePatch'],
|
|
|
8013 |
function (lang, logger, file, parse, optimize, pragma,
|
|
|
8014 |
load, requirePatch) {
|
|
|
8015 |
var build, buildBaseConfig,
|
|
|
8016 |
endsWithSemiColonRegExp = /;\s*$/;
|
|
|
8017 |
|
|
|
8018 |
buildBaseConfig = {
|
|
|
8019 |
appDir: "",
|
|
|
8020 |
pragmas: {},
|
|
|
8021 |
paths: {},
|
|
|
8022 |
optimize: "uglify",
|
|
|
8023 |
optimizeCss: "standard.keepLines",
|
|
|
8024 |
inlineText: true,
|
|
|
8025 |
isBuild: true,
|
|
|
8026 |
optimizeAllPluginResources: false,
|
|
|
8027 |
//By default, all files/directories are copied, unless
|
|
|
8028 |
//they match this regexp, by default just excludes .folders
|
|
|
8029 |
dirExclusionRegExp: file.dirExclusionRegExp
|
|
|
8030 |
};
|
|
|
8031 |
|
|
|
8032 |
/**
|
|
|
8033 |
* Some JS may not be valid if concatenated with other JS, in particular
|
|
|
8034 |
* the style of omitting semicolons and rely on ASI. Add a semicolon in
|
|
|
8035 |
* those cases.
|
|
|
8036 |
*/
|
|
|
8037 |
function addSemiColon(text) {
|
|
|
8038 |
if (endsWithSemiColonRegExp.test(text)) {
|
|
|
8039 |
return text;
|
|
|
8040 |
} else {
|
|
|
8041 |
return text + ";";
|
|
|
8042 |
}
|
|
|
8043 |
}
|
|
|
8044 |
|
|
|
8045 |
/**
|
|
|
8046 |
* If the path looks like an URL, throw an error. This is to prevent
|
|
|
8047 |
* people from using URLs with protocols in the build config, since
|
|
|
8048 |
* the optimizer is not set up to do network access. However, be
|
|
|
8049 |
* sure to allow absolute paths on Windows, like C:\directory.
|
|
|
8050 |
*/
|
|
|
8051 |
function disallowUrls(path) {
|
|
|
8052 |
if (path.indexOf('://') !== -1 && path !== 'empty:') {
|
|
|
8053 |
throw new Error('Path is not supported: ' + path +
|
|
|
8054 |
'\nOptimizer can only handle' +
|
|
|
8055 |
' local paths. Download the locally if necessary' +
|
|
|
8056 |
' and update the config to use a local path.\n' +
|
|
|
8057 |
'http://requirejs.org/docs/errors.html#pathnotsupported');
|
|
|
8058 |
}
|
|
|
8059 |
}
|
|
|
8060 |
|
|
|
8061 |
function endsWithSlash(dirName) {
|
|
|
8062 |
if (dirName.charAt(dirName.length - 1) !== "/") {
|
|
|
8063 |
dirName += "/";
|
|
|
8064 |
}
|
|
|
8065 |
disallowUrls(dirName);
|
|
|
8066 |
return dirName;
|
|
|
8067 |
}
|
|
|
8068 |
|
|
|
8069 |
//Method used by plugin writeFile calls, defined up here to avoid
|
|
|
8070 |
//jslint warning about "making a function in a loop".
|
|
|
8071 |
function makeWriteFile(anonDefRegExp, namespaceWithDot, layer) {
|
|
|
8072 |
function writeFile(name, contents) {
|
|
|
8073 |
logger.trace('Saving plugin-optimized file: ' + name);
|
|
|
8074 |
file.saveUtf8File(name, contents);
|
|
|
8075 |
}
|
|
|
8076 |
|
|
|
8077 |
writeFile.asModule = function (moduleName, fileName, contents) {
|
|
|
8078 |
writeFile(fileName,
|
|
|
8079 |
build.toTransport(anonDefRegExp, namespaceWithDot, moduleName, fileName, contents, layer));
|
|
|
8080 |
};
|
|
|
8081 |
|
|
|
8082 |
return writeFile;
|
|
|
8083 |
}
|
|
|
8084 |
|
|
|
8085 |
/**
|
|
|
8086 |
* Main API entry point into the build. The args argument can either be
|
|
|
8087 |
* an array of arguments (like the onese passed on a command-line),
|
|
|
8088 |
* or it can be a JavaScript object that has the format of a build profile
|
|
|
8089 |
* file.
|
|
|
8090 |
*
|
|
|
8091 |
* If it is an object, then in addition to the normal properties allowed in
|
|
|
8092 |
* a build profile file, the object should contain one other property:
|
|
|
8093 |
*
|
|
|
8094 |
* The object could also contain a "buildFile" property, which is a string
|
|
|
8095 |
* that is the file path to a build profile that contains the rest
|
|
|
8096 |
* of the build profile directives.
|
|
|
8097 |
*
|
|
|
8098 |
* This function does not return a status, it should throw an error if
|
|
|
8099 |
* there is a problem completing the build.
|
|
|
8100 |
*/
|
|
|
8101 |
build = function (args) {
|
|
|
8102 |
var buildFile, cmdConfig;
|
|
|
8103 |
|
|
|
8104 |
if (!args || lang.isArray(args)) {
|
|
|
8105 |
if (!args || args.length < 1) {
|
|
|
8106 |
logger.error("build.js buildProfile.js\n" +
|
|
|
8107 |
"where buildProfile.js is the name of the build file (see example.build.js for hints on how to make a build file).");
|
|
|
8108 |
return undefined;
|
|
|
8109 |
}
|
|
|
8110 |
|
|
|
8111 |
//Next args can include a build file path as well as other build args.
|
|
|
8112 |
//build file path comes first. If it does not contain an = then it is
|
|
|
8113 |
//a build file path. Otherwise, just all build args.
|
|
|
8114 |
if (args[0].indexOf("=") === -1) {
|
|
|
8115 |
buildFile = args[0];
|
|
|
8116 |
args.splice(0, 1);
|
|
|
8117 |
}
|
|
|
8118 |
|
|
|
8119 |
//Remaining args are options to the build
|
|
|
8120 |
cmdConfig = build.convertArrayToObject(args);
|
|
|
8121 |
cmdConfig.buildFile = buildFile;
|
|
|
8122 |
} else {
|
|
|
8123 |
cmdConfig = args;
|
|
|
8124 |
}
|
|
|
8125 |
|
|
|
8126 |
return build._run(cmdConfig);
|
|
|
8127 |
};
|
|
|
8128 |
|
|
|
8129 |
build._run = function (cmdConfig) {
|
|
|
8130 |
var buildFileContents = "",
|
|
|
8131 |
pluginCollector = {},
|
|
|
8132 |
buildPaths, fileName, fileNames,
|
|
|
8133 |
prop, paths, i,
|
|
|
8134 |
baseConfig, config,
|
|
|
8135 |
modules, builtModule, srcPath, buildContext,
|
|
|
8136 |
destPath, moduleName, moduleMap, parentModuleMap, context,
|
|
|
8137 |
resources, resource, pluginProcessed = {}, plugin;
|
|
|
8138 |
|
|
|
8139 |
//Can now run the patches to require.js to allow it to be used for
|
|
|
8140 |
//build generation. Do it here instead of at the top of the module
|
|
|
8141 |
//because we want normal require behavior to load the build tool
|
|
|
8142 |
//then want to switch to build mode.
|
|
|
8143 |
requirePatch();
|
|
|
8144 |
|
|
|
8145 |
config = build.createConfig(cmdConfig);
|
|
|
8146 |
paths = config.paths;
|
|
|
8147 |
|
|
|
8148 |
if (config.logLevel) {
|
|
|
8149 |
logger.logLevel(config.logLevel);
|
|
|
8150 |
}
|
|
|
8151 |
|
|
|
8152 |
if (!config.out && !config.cssIn) {
|
|
|
8153 |
//This is not just a one-off file build but a full build profile, with
|
|
|
8154 |
//lots of files to process.
|
|
|
8155 |
|
|
|
8156 |
//First copy all the baseUrl content
|
|
|
8157 |
file.copyDir((config.appDir || config.baseUrl), config.dir, /\w/, true);
|
|
|
8158 |
|
|
|
8159 |
//Adjust baseUrl if config.appDir is in play, and set up build output paths.
|
|
|
8160 |
buildPaths = {};
|
|
|
8161 |
if (config.appDir) {
|
|
|
8162 |
//All the paths should be inside the appDir
|
|
|
8163 |
buildPaths = paths;
|
|
|
8164 |
} else {
|
|
|
8165 |
//If no appDir, then make sure to copy the other paths to this directory.
|
|
|
8166 |
for (prop in paths) {
|
|
|
8167 |
if (paths.hasOwnProperty(prop)) {
|
|
|
8168 |
//Set up build path for each path prefix.
|
|
|
8169 |
buildPaths[prop] = paths[prop] === 'empty:' ? 'empty:' : prop.replace(/\./g, "/");
|
|
|
8170 |
|
|
|
8171 |
//Make sure source path is fully formed with baseUrl,
|
|
|
8172 |
//if it is a relative URL.
|
|
|
8173 |
srcPath = paths[prop];
|
|
|
8174 |
if (srcPath.indexOf('/') !== 0 && srcPath.indexOf(':') === -1) {
|
|
|
8175 |
srcPath = config.baseUrl + srcPath;
|
|
|
8176 |
}
|
|
|
8177 |
|
|
|
8178 |
destPath = config.dirBaseUrl + buildPaths[prop];
|
|
|
8179 |
|
|
|
8180 |
//Skip empty: paths
|
|
|
8181 |
if (srcPath !== 'empty:') {
|
|
|
8182 |
//If the srcPath is a directory, copy the whole directory.
|
|
|
8183 |
if (file.exists(srcPath) && file.isDirectory(srcPath)) {
|
|
|
8184 |
//Copy files to build area. Copy all files (the /\w/ regexp)
|
|
|
8185 |
file.copyDir(srcPath, destPath, /\w/, true);
|
|
|
8186 |
} else {
|
|
|
8187 |
//Try a .js extension
|
|
|
8188 |
srcPath += '.js';
|
|
|
8189 |
destPath += '.js';
|
|
|
8190 |
file.copyFile(srcPath, destPath);
|
|
|
8191 |
}
|
|
|
8192 |
}
|
|
|
8193 |
}
|
|
|
8194 |
}
|
|
|
8195 |
}
|
|
|
8196 |
}
|
|
|
8197 |
|
|
|
8198 |
//Figure out source file location for each module layer. Do this by seeding require
|
|
|
8199 |
//with source area configuration. This is needed so that later the module layers
|
|
|
8200 |
//can be manually copied over to the source area, since the build may be
|
|
|
8201 |
//require multiple times and the above copyDir call only copies newer files.
|
|
|
8202 |
require({
|
|
|
8203 |
baseUrl: config.baseUrl,
|
|
|
8204 |
paths: paths,
|
|
|
8205 |
packagePaths: config.packagePaths,
|
|
|
8206 |
packages: config.packages
|
|
|
8207 |
});
|
|
|
8208 |
buildContext = require.s.contexts._;
|
|
|
8209 |
modules = config.modules;
|
|
|
8210 |
|
|
|
8211 |
if (modules) {
|
|
|
8212 |
modules.forEach(function (module) {
|
|
|
8213 |
if (module.name) {
|
|
|
8214 |
module._sourcePath = buildContext.nameToUrl(module.name);
|
|
|
8215 |
//If the module does not exist, and this is not a "new" module layer,
|
|
|
8216 |
//as indicated by a true "create" property on the module, and
|
|
|
8217 |
//it is not a plugin-loaded resource, then throw an error.
|
|
|
8218 |
if (!file.exists(module._sourcePath) && !module.create &&
|
|
|
8219 |
module.name.indexOf('!') === -1) {
|
|
|
8220 |
throw new Error("ERROR: module path does not exist: " +
|
|
|
8221 |
module._sourcePath + " for module named: " + module.name +
|
|
|
8222 |
". Path is relative to: " + file.absPath('.'));
|
|
|
8223 |
}
|
|
|
8224 |
}
|
|
|
8225 |
});
|
|
|
8226 |
}
|
|
|
8227 |
|
|
|
8228 |
if (config.out) {
|
|
|
8229 |
//Just set up the _buildPath for the module layer.
|
|
|
8230 |
require(config);
|
|
|
8231 |
if (!config.cssIn) {
|
|
|
8232 |
config.modules[0]._buildPath = config.out;
|
|
|
8233 |
}
|
|
|
8234 |
} else if (!config.cssIn) {
|
|
|
8235 |
//Now set up the config for require to use the build area, and calculate the
|
|
|
8236 |
//build file locations. Pass along any config info too.
|
|
|
8237 |
baseConfig = {
|
|
|
8238 |
baseUrl: config.dirBaseUrl,
|
|
|
8239 |
paths: buildPaths
|
|
|
8240 |
};
|
|
|
8241 |
|
|
|
8242 |
lang.mixin(baseConfig, config);
|
|
|
8243 |
require(baseConfig);
|
|
|
8244 |
|
|
|
8245 |
if (modules) {
|
|
|
8246 |
modules.forEach(function (module) {
|
|
|
8247 |
if (module.name) {
|
|
|
8248 |
module._buildPath = buildContext.nameToUrl(module.name, null);
|
|
|
8249 |
if (!module.create) {
|
|
|
8250 |
file.copyFile(module._sourcePath, module._buildPath);
|
|
|
8251 |
}
|
|
|
8252 |
}
|
|
|
8253 |
});
|
|
|
8254 |
}
|
|
|
8255 |
}
|
|
|
8256 |
|
|
|
8257 |
//Run CSS optimizations before doing JS module tracing, to allow
|
|
|
8258 |
//things like text loader plugins loading CSS to get the optimized
|
|
|
8259 |
//CSS.
|
|
|
8260 |
if (config.optimizeCss && config.optimizeCss !== "none" && config.dir) {
|
|
|
8261 |
optimize.css(config.dir, config);
|
|
|
8262 |
}
|
|
|
8263 |
|
|
|
8264 |
if (modules) {
|
|
|
8265 |
//For each module layer, call require to calculate dependencies.
|
|
|
8266 |
modules.forEach(function (module) {
|
|
|
8267 |
module.layer = build.traceDependencies(module, config);
|
|
|
8268 |
});
|
|
|
8269 |
|
|
|
8270 |
//Now build up shadow layers for anything that should be excluded.
|
|
|
8271 |
//Do this after tracing dependencies for each module, in case one
|
|
|
8272 |
//of those modules end up being one of the excluded values.
|
|
|
8273 |
modules.forEach(function (module) {
|
|
|
8274 |
if (module.exclude) {
|
|
|
8275 |
module.excludeLayers = [];
|
|
|
8276 |
module.exclude.forEach(function (exclude, i) {
|
|
|
8277 |
//See if it is already in the list of modules.
|
|
|
8278 |
//If not trace dependencies for it.
|
|
|
8279 |
module.excludeLayers[i] = build.findBuildModule(exclude, modules) ||
|
|
|
8280 |
{layer: build.traceDependencies({name: exclude}, config)};
|
|
|
8281 |
});
|
|
|
8282 |
}
|
|
|
8283 |
});
|
|
|
8284 |
|
|
|
8285 |
modules.forEach(function (module) {
|
|
|
8286 |
if (module.exclude) {
|
|
|
8287 |
//module.exclude is an array of module names. For each one,
|
|
|
8288 |
//get the nested dependencies for it via a matching entry
|
|
|
8289 |
//in the module.excludeLayers array.
|
|
|
8290 |
module.exclude.forEach(function (excludeModule, i) {
|
|
|
8291 |
var excludeLayer = module.excludeLayers[i].layer, map = excludeLayer.buildPathMap, prop;
|
|
|
8292 |
for (prop in map) {
|
|
|
8293 |
if (map.hasOwnProperty(prop)) {
|
|
|
8294 |
build.removeModulePath(prop, map[prop], module.layer);
|
|
|
8295 |
}
|
|
|
8296 |
}
|
|
|
8297 |
});
|
|
|
8298 |
}
|
|
|
8299 |
if (module.excludeShallow) {
|
|
|
8300 |
//module.excludeShallow is an array of module names.
|
|
|
8301 |
//shallow exclusions are just that module itself, and not
|
|
|
8302 |
//its nested dependencies.
|
|
|
8303 |
module.excludeShallow.forEach(function (excludeShallowModule) {
|
|
|
8304 |
var path = module.layer.buildPathMap[excludeShallowModule];
|
|
|
8305 |
if (path) {
|
|
|
8306 |
build.removeModulePath(excludeShallowModule, path, module.layer);
|
|
|
8307 |
}
|
|
|
8308 |
});
|
|
|
8309 |
}
|
|
|
8310 |
|
|
|
8311 |
//Flatten them and collect the build output for each module.
|
|
|
8312 |
builtModule = build.flattenModule(module, module.layer, config);
|
|
|
8313 |
file.saveUtf8File(module._buildPath, builtModule.text);
|
|
|
8314 |
buildFileContents += builtModule.buildText;
|
|
|
8315 |
});
|
|
|
8316 |
}
|
|
|
8317 |
|
|
|
8318 |
//Do other optimizations.
|
|
|
8319 |
if (config.out && !config.cssIn) {
|
|
|
8320 |
//Just need to worry about one JS file.
|
|
|
8321 |
fileName = config.modules[0]._buildPath;
|
|
|
8322 |
optimize.jsFile(fileName, fileName, config);
|
|
|
8323 |
} else if (!config.cssIn) {
|
|
|
8324 |
//Normal optimizations across modules.
|
|
|
8325 |
|
|
|
8326 |
//JS optimizations.
|
|
|
8327 |
fileNames = file.getFilteredFileList(config.dir, /\.js$/, true);
|
|
|
8328 |
for (i = 0; (fileName = fileNames[i]); i++) {
|
|
|
8329 |
//Generate the module name from the config.dir root.
|
|
|
8330 |
moduleName = fileName.replace(config.dir, '');
|
|
|
8331 |
//Get rid of the extension
|
|
|
8332 |
moduleName = moduleName.substring(0, moduleName.length - 3);
|
|
|
8333 |
optimize.jsFile(fileName, fileName, config, moduleName, pluginCollector);
|
|
|
8334 |
}
|
|
|
8335 |
|
|
|
8336 |
//Normalize all the plugin resources.
|
|
|
8337 |
context = require.s.contexts._;
|
|
|
8338 |
|
|
|
8339 |
for (moduleName in pluginCollector) {
|
|
|
8340 |
if (pluginCollector.hasOwnProperty(moduleName)) {
|
|
|
8341 |
parentModuleMap = context.makeModuleMap(moduleName);
|
|
|
8342 |
resources = pluginCollector[moduleName];
|
|
|
8343 |
for (i = 0; (resource = resources[i]); i++) {
|
|
|
8344 |
moduleMap = context.makeModuleMap(resource, parentModuleMap);
|
|
|
8345 |
if (!context.plugins[moduleMap.prefix]) {
|
|
|
8346 |
//Set the value in context.plugins so it
|
|
|
8347 |
//will be evaluated as a full plugin.
|
|
|
8348 |
context.plugins[moduleMap.prefix] = true;
|
|
|
8349 |
|
|
|
8350 |
//Do not bother if the plugin is not available.
|
|
|
8351 |
if (!file.exists(require.toUrl(moduleMap.prefix + '.js'))) {
|
|
|
8352 |
continue;
|
|
|
8353 |
}
|
|
|
8354 |
|
|
|
8355 |
//Rely on the require in the build environment
|
|
|
8356 |
//to be synchronous
|
|
|
8357 |
context.require([moduleMap.prefix]);
|
|
|
8358 |
|
|
|
8359 |
//Now that the plugin is loaded, redo the moduleMap
|
|
|
8360 |
//since the plugin will need to normalize part of the path.
|
|
|
8361 |
moduleMap = context.makeModuleMap(resource, parentModuleMap);
|
|
|
8362 |
}
|
|
|
8363 |
|
|
|
8364 |
//Only bother with plugin resources that can be handled
|
|
|
8365 |
//processed by the plugin, via support of the writeFile
|
|
|
8366 |
//method.
|
|
|
8367 |
if (!pluginProcessed[moduleMap.fullName]) {
|
|
|
8368 |
//Only do the work if the plugin was really loaded.
|
|
|
8369 |
//Using an internal access because the file may
|
|
|
8370 |
//not really be loaded.
|
|
|
8371 |
plugin = context.defined[moduleMap.prefix];
|
|
|
8372 |
if (plugin && plugin.writeFile) {
|
|
|
8373 |
plugin.writeFile(
|
|
|
8374 |
moduleMap.prefix,
|
|
|
8375 |
moduleMap.name,
|
|
|
8376 |
require,
|
|
|
8377 |
makeWriteFile(
|
|
|
8378 |
config.anonDefRegExp,
|
|
|
8379 |
config.namespaceWithDot
|
|
|
8380 |
),
|
|
|
8381 |
context.config
|
|
|
8382 |
);
|
|
|
8383 |
}
|
|
|
8384 |
|
|
|
8385 |
pluginProcessed[moduleMap.fullName] = true;
|
|
|
8386 |
}
|
|
|
8387 |
}
|
|
|
8388 |
|
|
|
8389 |
}
|
|
|
8390 |
}
|
|
|
8391 |
|
|
|
8392 |
//console.log('PLUGIN COLLECTOR: ' + JSON.stringify(pluginCollector, null, " "));
|
|
|
8393 |
|
|
|
8394 |
|
|
|
8395 |
//All module layers are done, write out the build.txt file.
|
|
|
8396 |
file.saveUtf8File(config.dir + "build.txt", buildFileContents);
|
|
|
8397 |
}
|
|
|
8398 |
|
|
|
8399 |
//If just have one CSS file to optimize, do that here.
|
|
|
8400 |
if (config.cssIn) {
|
|
|
8401 |
optimize.cssFile(config.cssIn, config.out, config);
|
|
|
8402 |
}
|
|
|
8403 |
|
|
|
8404 |
//Print out what was built into which layers.
|
|
|
8405 |
if (buildFileContents) {
|
|
|
8406 |
logger.info(buildFileContents);
|
|
|
8407 |
return buildFileContents;
|
|
|
8408 |
}
|
|
|
8409 |
|
|
|
8410 |
return '';
|
|
|
8411 |
};
|
|
|
8412 |
|
|
|
8413 |
/**
|
|
|
8414 |
* Converts command line args like "paths.foo=../some/path"
|
|
|
8415 |
* result.paths = { foo: '../some/path' } where prop = paths,
|
|
|
8416 |
* name = paths.foo and value = ../some/path, so it assumes the
|
|
|
8417 |
* name=value splitting has already happened.
|
|
|
8418 |
*/
|
|
|
8419 |
function stringDotToObj(result, prop, name, value) {
|
|
|
8420 |
if (!result[prop]) {
|
|
|
8421 |
result[prop] = {};
|
|
|
8422 |
}
|
|
|
8423 |
name = name.substring((prop + '.').length, name.length);
|
|
|
8424 |
result[prop][name] = value;
|
|
|
8425 |
}
|
|
|
8426 |
|
|
|
8427 |
/**
|
|
|
8428 |
* Converts an array that has String members of "name=value"
|
|
|
8429 |
* into an object, where the properties on the object are the names in the array.
|
|
|
8430 |
* Also converts the strings "true" and "false" to booleans for the values.
|
|
|
8431 |
* member name/value pairs, and converts some comma-separated lists into
|
|
|
8432 |
* arrays.
|
|
|
8433 |
* @param {Array} ary
|
|
|
8434 |
*/
|
|
|
8435 |
build.convertArrayToObject = function (ary) {
|
|
|
8436 |
var result = {}, i, separatorIndex, prop, value,
|
|
|
8437 |
needArray = {
|
|
|
8438 |
"include": true,
|
|
|
8439 |
"exclude": true,
|
|
|
8440 |
"excludeShallow": true
|
|
|
8441 |
};
|
|
|
8442 |
|
|
|
8443 |
for (i = 0; i < ary.length; i++) {
|
|
|
8444 |
separatorIndex = ary[i].indexOf("=");
|
|
|
8445 |
if (separatorIndex === -1) {
|
|
|
8446 |
throw "Malformed name/value pair: [" + ary[i] + "]. Format should be name=value";
|
|
|
8447 |
}
|
|
|
8448 |
|
|
|
8449 |
value = ary[i].substring(separatorIndex + 1, ary[i].length);
|
|
|
8450 |
if (value === "true") {
|
|
|
8451 |
value = true;
|
|
|
8452 |
} else if (value === "false") {
|
|
|
8453 |
value = false;
|
|
|
8454 |
}
|
|
|
8455 |
|
|
|
8456 |
prop = ary[i].substring(0, separatorIndex);
|
|
|
8457 |
|
|
|
8458 |
//Convert to array if necessary
|
|
|
8459 |
if (needArray[prop]) {
|
|
|
8460 |
value = value.split(",");
|
|
|
8461 |
}
|
|
|
8462 |
|
|
|
8463 |
if (prop.indexOf("paths.") === 0 || prop.indexOf("wrap.") === 0) {
|
|
|
8464 |
stringDotToObj(result, prop.split('.')[0], prop, value);
|
|
|
8465 |
} else {
|
|
|
8466 |
result[prop] = value;
|
|
|
8467 |
}
|
|
|
8468 |
}
|
|
|
8469 |
return result; //Object
|
|
|
8470 |
};
|
|
|
8471 |
|
|
|
8472 |
build.makeAbsPath = function (path, absFilePath) {
|
|
|
8473 |
//Add abspath if necessary. If path starts with a slash or has a colon,
|
|
|
8474 |
//then already is an abolute path.
|
|
|
8475 |
if (path.indexOf('/') !== 0 && path.indexOf(':') === -1) {
|
|
|
8476 |
path = absFilePath +
|
|
|
8477 |
(absFilePath.charAt(absFilePath.length - 1) === '/' ? '' : '/') +
|
|
|
8478 |
path;
|
|
|
8479 |
path = file.normalize(path);
|
|
|
8480 |
}
|
|
|
8481 |
return path.replace(lang.backSlashRegExp, '/');
|
|
|
8482 |
};
|
|
|
8483 |
|
|
|
8484 |
/**
|
|
|
8485 |
* Creates a config object for an optimization build.
|
|
|
8486 |
* It will also read the build profile if it is available, to create
|
|
|
8487 |
* the configuration.
|
|
|
8488 |
*
|
|
|
8489 |
* @param {Object} cfg config options that take priority
|
|
|
8490 |
* over defaults and ones in the build file. These options could
|
|
|
8491 |
* be from a command line, for instance.
|
|
|
8492 |
*
|
|
|
8493 |
* @param {Object} the created config object.
|
|
|
8494 |
*/
|
|
|
8495 |
build.createConfig = function (cfg) {
|
|
|
8496 |
/*jslint evil: true */
|
|
|
8497 |
var config = {}, buildFileContents, buildFileConfig,
|
|
|
8498 |
paths, props, i, prop, buildFile, absFilePath, originalBaseUrl;
|
|
|
8499 |
|
|
|
8500 |
lang.mixin(config, buildBaseConfig);
|
|
|
8501 |
lang.mixin(config, cfg, true);
|
|
|
8502 |
|
|
|
8503 |
absFilePath = file.absPath('.');
|
|
|
8504 |
|
|
|
8505 |
if (config.buildFile) {
|
|
|
8506 |
//A build file exists, load it to get more config.
|
|
|
8507 |
buildFile = file.absPath(config.buildFile);
|
|
|
8508 |
|
|
|
8509 |
//Find the build file, and make sure it exists, if this is a build
|
|
|
8510 |
//that has a build profile, and not just command line args with an in=path
|
|
|
8511 |
if (!file.exists(buildFile)) {
|
|
|
8512 |
throw new Error("ERROR: build file does not exist: " + buildFile);
|
|
|
8513 |
}
|
|
|
8514 |
|
|
|
8515 |
absFilePath = config.baseUrl = file.absPath(file.parent(buildFile));
|
|
|
8516 |
config.dir = config.baseUrl + "/build/";
|
|
|
8517 |
|
|
|
8518 |
//Load build file options.
|
|
|
8519 |
buildFileContents = file.readFile(buildFile);
|
|
|
8520 |
try {
|
|
|
8521 |
buildFileConfig = eval("(" + buildFileContents + ")");
|
|
|
8522 |
} catch (e) {
|
|
|
8523 |
throw new Error("Build file " + buildFile + " is malformed: " + e);
|
|
|
8524 |
}
|
|
|
8525 |
lang.mixin(config, buildFileConfig, true);
|
|
|
8526 |
|
|
|
8527 |
//Re-apply the override config values, things like command line
|
|
|
8528 |
//args should take precedence over build file values.
|
|
|
8529 |
lang.mixin(config, cfg, true);
|
|
|
8530 |
} else if (config.cssIn) {
|
|
|
8531 |
if (!config.out) {
|
|
|
8532 |
throw new Error("ERROR: 'out' option missing.");
|
|
|
8533 |
} else {
|
|
|
8534 |
config.out = config.out.replace(lang.backSlashRegExp, "/");
|
|
|
8535 |
}
|
|
|
8536 |
}
|
|
|
8537 |
|
|
|
8538 |
if (!config.cssIn && !config.baseUrl) {
|
|
|
8539 |
throw new Error("ERROR: 'baseUrl' option missing.");
|
|
|
8540 |
}
|
|
|
8541 |
|
|
|
8542 |
if (config.out && !config.cssIn) {
|
|
|
8543 |
//Just one file to optimize.
|
|
|
8544 |
|
|
|
8545 |
//Set up dummy module layer to build.
|
|
|
8546 |
config.modules = [
|
|
|
8547 |
{
|
|
|
8548 |
name: config.name,
|
|
|
8549 |
out: config.out,
|
|
|
8550 |
include: config.include,
|
|
|
8551 |
exclude: config.exclude,
|
|
|
8552 |
excludeShallow: config.excludeShallow
|
|
|
8553 |
}
|
|
|
8554 |
];
|
|
|
8555 |
|
|
|
8556 |
//Does not have a build file, so set up some defaults.
|
|
|
8557 |
//Optimizing CSS should not be allowed, unless explicitly
|
|
|
8558 |
//asked for on command line. In that case the only task is
|
|
|
8559 |
//to optimize a CSS file.
|
|
|
8560 |
if (!cfg.optimizeCss) {
|
|
|
8561 |
config.optimizeCss = "none";
|
|
|
8562 |
}
|
|
|
8563 |
}
|
|
|
8564 |
|
|
|
8565 |
//Adjust the path properties as appropriate.
|
|
|
8566 |
//First make sure build paths use front slashes and end in a slash,
|
|
|
8567 |
//and make sure they are aboslute paths.
|
|
|
8568 |
props = ["appDir", "dir", "baseUrl"];
|
|
|
8569 |
for (i = 0; (prop = props[i]); i++) {
|
|
|
8570 |
if (config[prop]) {
|
|
|
8571 |
config[prop] = config[prop].replace(lang.backSlashRegExp, "/");
|
|
|
8572 |
|
|
|
8573 |
//Add abspath if necessary.
|
|
|
8574 |
if (prop === "baseUrl") {
|
|
|
8575 |
originalBaseUrl = config.baseUrl;
|
|
|
8576 |
if (config.appDir) {
|
|
|
8577 |
//If baseUrl with an appDir, the baseUrl is relative to
|
|
|
8578 |
//the appDir, *not* the absFilePath. appDir and dir are
|
|
|
8579 |
//made absolute before baseUrl, so this will work.
|
|
|
8580 |
config.baseUrl = build.makeAbsPath(originalBaseUrl, config.appDir);
|
|
|
8581 |
//Set up dir output baseUrl.
|
|
|
8582 |
config.dirBaseUrl = build.makeAbsPath(originalBaseUrl, config.dir);
|
|
|
8583 |
} else {
|
|
|
8584 |
//The dir output baseUrl is same as regular baseUrl, both
|
|
|
8585 |
//relative to the absFilePath.
|
|
|
8586 |
config.baseUrl = build.makeAbsPath(config[prop], absFilePath);
|
|
|
8587 |
config.dirBaseUrl = config.dir || config.baseUrl;
|
|
|
8588 |
}
|
|
|
8589 |
|
|
|
8590 |
//Make sure dirBaseUrl ends in a slash, since it is
|
|
|
8591 |
//concatenated with other strings.
|
|
|
8592 |
config.dirBaseUrl = endsWithSlash(config.dirBaseUrl);
|
|
|
8593 |
} else {
|
|
|
8594 |
config[prop] = build.makeAbsPath(config[prop], absFilePath);
|
|
|
8595 |
}
|
|
|
8596 |
|
|
|
8597 |
config[prop] = endsWithSlash(config[prop]);
|
|
|
8598 |
}
|
|
|
8599 |
}
|
|
|
8600 |
|
|
|
8601 |
//Do not allow URLs for paths resources.
|
|
|
8602 |
if (config.paths) {
|
|
|
8603 |
for (prop in config.paths) {
|
|
|
8604 |
if (config.paths.hasOwnProperty(prop)) {
|
|
|
8605 |
config.paths[prop] = config.paths[prop].replace(lang.backSlashRegExp, "/");
|
|
|
8606 |
disallowUrls(config.paths[prop]);
|
|
|
8607 |
}
|
|
|
8608 |
}
|
|
|
8609 |
}
|
|
|
8610 |
|
|
|
8611 |
//Make sure some other paths are absolute.
|
|
|
8612 |
props = ["out", "cssIn"];
|
|
|
8613 |
for (i = 0; (prop = props[i]); i++) {
|
|
|
8614 |
if (config[prop]) {
|
|
|
8615 |
config[prop] = build.makeAbsPath(config[prop], absFilePath);
|
|
|
8616 |
}
|
|
|
8617 |
}
|
|
|
8618 |
|
|
|
8619 |
//Get any wrap text.
|
|
|
8620 |
try {
|
|
|
8621 |
if (config.wrap) {
|
|
|
8622 |
if (config.wrap === true) {
|
|
|
8623 |
//Use default values.
|
|
|
8624 |
config.wrap = {
|
|
|
8625 |
start: '(function () {',
|
|
|
8626 |
end: '}());'
|
|
|
8627 |
};
|
|
|
8628 |
} else {
|
|
|
8629 |
config.wrap.start = config.wrap.start ||
|
|
|
8630 |
file.readFile(build.makeAbsPath(config.wrap.startFile, absFilePath));
|
|
|
8631 |
config.wrap.end = config.wrap.end ||
|
|
|
8632 |
file.readFile(build.makeAbsPath(config.wrap.endFile, absFilePath));
|
|
|
8633 |
}
|
|
|
8634 |
}
|
|
|
8635 |
} catch (wrapError) {
|
|
|
8636 |
throw new Error('Malformed wrap config: need both start/end or ' +
|
|
|
8637 |
'startFile/endFile: ' + wrapError.toString());
|
|
|
8638 |
}
|
|
|
8639 |
|
|
|
8640 |
|
|
|
8641 |
//Set up proper info for namespaces and using namespaces in transport
|
|
|
8642 |
//wrappings.
|
|
|
8643 |
config.namespaceWithDot = config.namespace ? config.namespace + '.' : '';
|
|
|
8644 |
config.anonDefRegExp = build.makeAnonDefRegExp(config.namespaceWithDot);
|
|
|
8645 |
|
|
|
8646 |
//Do final input verification
|
|
|
8647 |
if (config.context) {
|
|
|
8648 |
throw new Error('The build argument "context" is not supported' +
|
|
|
8649 |
' in a build. It should only be used in web' +
|
|
|
8650 |
' pages.');
|
|
|
8651 |
}
|
|
|
8652 |
|
|
|
8653 |
//Set file.dirExclusionRegExp if desired
|
|
|
8654 |
if ('dirExclusionRegExp' in config) {
|
|
|
8655 |
file.dirExclusionRegExp = config.dirExclusionRegExp;
|
|
|
8656 |
}
|
|
|
8657 |
|
|
|
8658 |
return config;
|
|
|
8659 |
};
|
|
|
8660 |
|
|
|
8661 |
/**
|
|
|
8662 |
* finds the module being built/optimized with the given moduleName,
|
|
|
8663 |
* or returns null.
|
|
|
8664 |
* @param {String} moduleName
|
|
|
8665 |
* @param {Array} modules
|
|
|
8666 |
* @returns {Object} the module object from the build profile, or null.
|
|
|
8667 |
*/
|
|
|
8668 |
build.findBuildModule = function (moduleName, modules) {
|
|
|
8669 |
var i, module;
|
|
|
8670 |
for (i = 0; (module = modules[i]); i++) {
|
|
|
8671 |
if (module.name === moduleName) {
|
|
|
8672 |
return module;
|
|
|
8673 |
}
|
|
|
8674 |
}
|
|
|
8675 |
return null;
|
|
|
8676 |
};
|
|
|
8677 |
|
|
|
8678 |
/**
|
|
|
8679 |
* Removes a module name and path from a layer, if it is supposed to be
|
|
|
8680 |
* excluded from the layer.
|
|
|
8681 |
* @param {String} moduleName the name of the module
|
|
|
8682 |
* @param {String} path the file path for the module
|
|
|
8683 |
* @param {Object} layer the layer to remove the module/path from
|
|
|
8684 |
*/
|
|
|
8685 |
build.removeModulePath = function (module, path, layer) {
|
|
|
8686 |
var index = layer.buildFilePaths.indexOf(path);
|
|
|
8687 |
if (index !== -1) {
|
|
|
8688 |
layer.buildFilePaths.splice(index, 1);
|
|
|
8689 |
}
|
|
|
8690 |
|
|
|
8691 |
//Take it out of the specified modules. Specified modules are mostly
|
|
|
8692 |
//used to find require modifiers.
|
|
|
8693 |
delete layer.specified[module];
|
|
|
8694 |
};
|
|
|
8695 |
|
|
|
8696 |
/**
|
|
|
8697 |
* Uses the module build config object to trace the dependencies for the
|
|
|
8698 |
* given module.
|
|
|
8699 |
*
|
|
|
8700 |
* @param {Object} module the module object from the build config info.
|
|
|
8701 |
* @param {Object} the build config object.
|
|
|
8702 |
*
|
|
|
8703 |
* @returns {Object} layer information about what paths and modules should
|
|
|
8704 |
* be in the flattened module.
|
|
|
8705 |
*/
|
|
|
8706 |
build.traceDependencies = function (module, config) {
|
|
|
8707 |
var include, override, layer, context, baseConfig, oldContext;
|
|
|
8708 |
|
|
|
8709 |
//Reset some state set up in requirePatch.js, and clean up require's
|
|
|
8710 |
//current context.
|
|
|
8711 |
oldContext = require._buildReset();
|
|
|
8712 |
|
|
|
8713 |
//Grab the reset layer and context after the reset, but keep the
|
|
|
8714 |
//old config to reuse in the new context.
|
|
|
8715 |
baseConfig = oldContext.config;
|
|
|
8716 |
layer = require._layer;
|
|
|
8717 |
context = layer.context;
|
|
|
8718 |
|
|
|
8719 |
//Put back basic config, use a fresh object for it.
|
|
|
8720 |
//WARNING: probably not robust for paths and packages/packagePaths,
|
|
|
8721 |
//since those property's objects can be modified. But for basic
|
|
|
8722 |
//config clone it works out.
|
|
|
8723 |
require(lang.delegate(baseConfig));
|
|
|
8724 |
|
|
|
8725 |
logger.trace("\nTracing dependencies for: " + (module.name || module.out));
|
|
|
8726 |
include = module.name && !module.create ? [module.name] : [];
|
|
|
8727 |
if (module.include) {
|
|
|
8728 |
include = include.concat(module.include);
|
|
|
8729 |
}
|
|
|
8730 |
|
|
|
8731 |
//If there are overrides to basic config, set that up now.;
|
|
|
8732 |
if (module.override) {
|
|
|
8733 |
override = lang.delegate(baseConfig);
|
|
|
8734 |
lang.mixin(override, module.override, true);
|
|
|
8735 |
require(override);
|
|
|
8736 |
}
|
|
|
8737 |
|
|
|
8738 |
//Figure out module layer dependencies by calling require to do the work.
|
|
|
8739 |
require(include);
|
|
|
8740 |
|
|
|
8741 |
//Pull out the layer dependencies.
|
|
|
8742 |
layer.specified = context.specified;
|
|
|
8743 |
|
|
|
8744 |
//Reset config
|
|
|
8745 |
if (module.override) {
|
|
|
8746 |
require(baseConfig);
|
|
|
8747 |
}
|
|
|
8748 |
|
|
|
8749 |
return layer;
|
|
|
8750 |
};
|
|
|
8751 |
|
|
|
8752 |
/**
|
|
|
8753 |
* Uses the module build config object to create an flattened version
|
|
|
8754 |
* of the module, with deep dependencies included.
|
|
|
8755 |
*
|
|
|
8756 |
* @param {Object} module the module object from the build config info.
|
|
|
8757 |
*
|
|
|
8758 |
* @param {Object} layer the layer object returned from build.traceDependencies.
|
|
|
8759 |
*
|
|
|
8760 |
* @param {Object} the build config object.
|
|
|
8761 |
*
|
|
|
8762 |
* @returns {Object} with two properties: "text", the text of the flattened
|
|
|
8763 |
* module, and "buildText", a string of text representing which files were
|
|
|
8764 |
* included in the flattened module text.
|
|
|
8765 |
*/
|
|
|
8766 |
build.flattenModule = function (module, layer, config) {
|
|
|
8767 |
var buildFileContents = "",
|
|
|
8768 |
namespace = config.namespace ? config.namespace + '.' : '',
|
|
|
8769 |
context = layer.context,
|
|
|
8770 |
anonDefRegExp = config.anonDefRegExp,
|
|
|
8771 |
path, reqIndex, fileContents, currContents,
|
|
|
8772 |
i, moduleName,
|
|
|
8773 |
parts, builder, writeApi;
|
|
|
8774 |
|
|
|
8775 |
//Use override settings, particularly for pragmas
|
|
|
8776 |
if (module.override) {
|
|
|
8777 |
config = lang.delegate(config);
|
|
|
8778 |
lang.mixin(config, module.override, true);
|
|
|
8779 |
}
|
|
|
8780 |
|
|
|
8781 |
//Start build output for the module.
|
|
|
8782 |
buildFileContents += "\n" +
|
|
|
8783 |
(config.dir ? module._buildPath.replace(config.dir, "") : module._buildPath) +
|
|
|
8784 |
"\n----------------\n";
|
|
|
8785 |
|
|
|
8786 |
//If there was an existing file with require in it, hoist to the top.
|
|
|
8787 |
if (layer.existingRequireUrl) {
|
|
|
8788 |
reqIndex = layer.buildFilePaths.indexOf(layer.existingRequireUrl);
|
|
|
8789 |
if (reqIndex !== -1) {
|
|
|
8790 |
layer.buildFilePaths.splice(reqIndex, 1);
|
|
|
8791 |
layer.buildFilePaths.unshift(layer.existingRequireUrl);
|
|
|
8792 |
}
|
|
|
8793 |
}
|
|
|
8794 |
|
|
|
8795 |
//Write the built module to disk, and build up the build output.
|
|
|
8796 |
fileContents = "";
|
|
|
8797 |
for (i = 0; (path = layer.buildFilePaths[i]); i++) {
|
|
|
8798 |
moduleName = layer.buildFileToModule[path];
|
|
|
8799 |
|
|
|
8800 |
//Figure out if the module is a result of a build plugin, and if so,
|
|
|
8801 |
//then delegate to that plugin.
|
|
|
8802 |
parts = context.makeModuleMap(moduleName);
|
|
|
8803 |
builder = parts.prefix && context.defined[parts.prefix];
|
|
|
8804 |
if (builder) {
|
|
|
8805 |
if (builder.write) {
|
|
|
8806 |
writeApi = function (input) {
|
|
|
8807 |
fileContents += "\n" + addSemiColon(input);
|
|
|
8808 |
};
|
|
|
8809 |
writeApi.asModule = function (moduleName, input) {
|
|
|
8810 |
fileContents += "\n" +
|
|
|
8811 |
addSemiColon(
|
|
|
8812 |
build.toTransport(anonDefRegExp, namespace, moduleName, path, input, layer));
|
|
|
8813 |
};
|
|
|
8814 |
builder.write(parts.prefix, parts.name, writeApi);
|
|
|
8815 |
}
|
|
|
8816 |
} else {
|
|
|
8817 |
currContents = file.readFile(path);
|
|
|
8818 |
|
|
|
8819 |
if (config.namespace) {
|
|
|
8820 |
currContents = pragma.namespace(currContents, config.namespace);
|
|
|
8821 |
}
|
|
|
8822 |
|
|
|
8823 |
currContents = build.toTransport(anonDefRegExp, namespace, moduleName, path, currContents, layer);
|
|
|
8824 |
|
|
|
8825 |
//Semicolon is for files that are not well formed when
|
|
|
8826 |
//concatenated with other content.
|
|
|
8827 |
fileContents += "\n" + addSemiColon(currContents);
|
|
|
8828 |
}
|
|
|
8829 |
|
|
|
8830 |
buildFileContents += path.replace(config.dir, "") + "\n";
|
|
|
8831 |
//Some files may not have declared a require module, and if so,
|
|
|
8832 |
//put in a placeholder call so the require does not try to load them
|
|
|
8833 |
//after the module is processed.
|
|
|
8834 |
//If we have a name, but no defined module, then add in the placeholder.
|
|
|
8835 |
if (moduleName && !layer.modulesWithNames[moduleName] && !config.skipModuleInsertion) {
|
|
|
8836 |
//If including jquery, register the module correctly, otherwise
|
|
|
8837 |
//register an empty function. For jquery, make sure jQuery is
|
|
|
8838 |
//a real object, and perhaps not some other file mapping, like
|
|
|
8839 |
//to zepto.
|
|
|
8840 |
if (moduleName === 'jquery') {
|
|
|
8841 |
fileContents += '\n(function () {\n' +
|
|
|
8842 |
'var jq = typeof jQuery !== "undefined" && jQuery;\n' +
|
|
|
8843 |
namespace +
|
|
|
8844 |
'define("jquery", [], function () { return jq; });\n' +
|
|
|
8845 |
'}());\n';
|
|
|
8846 |
} else {
|
|
|
8847 |
fileContents += '\n' + namespace + 'define("' + moduleName + '", function(){});\n';
|
|
|
8848 |
}
|
|
|
8849 |
}
|
|
|
8850 |
}
|
|
|
8851 |
|
|
|
8852 |
return {
|
|
|
8853 |
text: config.wrap ?
|
|
|
8854 |
config.wrap.start + fileContents + config.wrap.end :
|
|
|
8855 |
fileContents,
|
|
|
8856 |
buildText: buildFileContents
|
|
|
8857 |
};
|
|
|
8858 |
};
|
|
|
8859 |
|
|
|
8860 |
/**
|
|
|
8861 |
* Creates the regexp to find anonymous defines.
|
|
|
8862 |
* @param {String} namespace an optional namespace to use. The namespace
|
|
|
8863 |
* should *include* a trailing dot. So a valid value would be 'foo.'
|
|
|
8864 |
* @returns {RegExp}
|
|
|
8865 |
*/
|
|
|
8866 |
build.makeAnonDefRegExp = function (namespace) {
|
|
|
8867 |
//This regexp is not bullet-proof, and it has one optional part to
|
|
|
8868 |
//avoid issues with some Dojo transition modules that use a
|
|
|
8869 |
//define(\n//begin v1.x content
|
|
|
8870 |
//for a comment.
|
|
|
8871 |
return new RegExp('(^|[^\\.])(' + (namespace || '').replace(/\./g, '\\.') +
|
|
|
8872 |
'define|define)\\s*\\(\\s*(\\/\\/[^\\n\\r]*[\\r\\n])?(\\[|f|\\{|["\']([^"\']+)["\'])(\\s*,\\s*f)?');
|
|
|
8873 |
};
|
|
|
8874 |
|
|
|
8875 |
build.leadingCommaRegExp = /^\s*,/;
|
|
|
8876 |
|
|
|
8877 |
build.toTransport = function (anonDefRegExp, namespace, moduleName, path, contents, layer) {
|
|
|
8878 |
|
|
|
8879 |
//If anonymous module, insert the module name.
|
|
|
8880 |
return contents.replace(anonDefRegExp, function (match, start, callName, possibleComment, suffix, namedModule, namedFuncStart) {
|
|
|
8881 |
//A named module with either listed dependencies or an object
|
|
|
8882 |
//literal for a value. Skip it. If named module, only want ones
|
|
|
8883 |
//whose next argument is a function literal to scan for
|
|
|
8884 |
//require('') dependecies.
|
|
|
8885 |
if (namedModule && !namedFuncStart) {
|
|
|
8886 |
return match;
|
|
|
8887 |
}
|
|
|
8888 |
|
|
|
8889 |
if (layer) {
|
|
|
8890 |
layer.modulesWithNames[moduleName] = true;
|
|
|
8891 |
}
|
|
|
8892 |
|
|
|
8893 |
var deps = null;
|
|
|
8894 |
|
|
|
8895 |
//Look for CommonJS require calls inside the function if this is
|
|
|
8896 |
//an anonymous define call that just has a function registered.
|
|
|
8897 |
//Also look if a named define function but has a factory function
|
|
|
8898 |
//as the second arg that should be scanned for dependencies.
|
|
|
8899 |
if (suffix.indexOf('f') !== -1 || (namedModule)) {
|
|
|
8900 |
deps = parse.getAnonDeps(path, contents);
|
|
|
8901 |
|
|
|
8902 |
if (deps.length) {
|
|
|
8903 |
deps = deps.map(function (dep) {
|
|
|
8904 |
return "'" + dep + "'";
|
|
|
8905 |
});
|
|
|
8906 |
} else {
|
|
|
8907 |
deps = [];
|
|
|
8908 |
}
|
|
|
8909 |
}
|
|
|
8910 |
|
|
|
8911 |
return start + namespace + "define('" + moduleName + "'," +
|
|
|
8912 |
(deps ? ('[' + deps.toString() + '],') : '') +
|
|
|
8913 |
(namedModule ? namedFuncStart.replace(build.leadingCommaRegExp, '') : suffix);
|
|
|
8914 |
});
|
|
|
8915 |
|
|
|
8916 |
};
|
|
|
8917 |
|
|
|
8918 |
return build;
|
|
|
8919 |
});
|
|
|
8920 |
|
|
|
8921 |
}
|
|
|
8922 |
|
|
|
8923 |
|
|
|
8924 |
/**
|
|
|
8925 |
* Sets the default baseUrl for requirejs to be directory of top level
|
|
|
8926 |
* script.
|
|
|
8927 |
*/
|
|
|
8928 |
function setBaseUrl(fileName) {
|
|
|
8929 |
//Use the file name's directory as the baseUrl if available.
|
|
|
8930 |
dir = fileName.replace(/\\/g, '/');
|
|
|
8931 |
if (dir.indexOf('/') !== -1) {
|
|
|
8932 |
dir = dir.split('/');
|
|
|
8933 |
dir.pop();
|
|
|
8934 |
dir = dir.join('/');
|
|
|
8935 |
exec("require({baseUrl: '" + dir + "'});");
|
|
|
8936 |
}
|
|
|
8937 |
}
|
|
|
8938 |
|
|
|
8939 |
//If in Node, and included via a require('requirejs'), just export and
|
|
|
8940 |
//THROW IT ON THE GROUND!
|
|
|
8941 |
if (env === 'node' && reqMain !== module) {
|
|
|
8942 |
setBaseUrl(path.resolve(reqMain ? reqMain.filename : '.'));
|
|
|
8943 |
|
|
|
8944 |
//Create a method that will run the optimzer given an object
|
|
|
8945 |
//config.
|
|
|
8946 |
requirejs.optimize = function (config, callback) {
|
|
|
8947 |
if (!loadedOptimizedLib) {
|
|
|
8948 |
loadLib();
|
|
|
8949 |
loadedOptimizedLib = true;
|
|
|
8950 |
}
|
|
|
8951 |
|
|
|
8952 |
//Create the function that will be called once build modules
|
|
|
8953 |
//have been loaded.
|
|
|
8954 |
var runBuild = function (build, logger) {
|
|
|
8955 |
//Make sure config has a log level, and if not,
|
|
|
8956 |
//make it "silent" by default.
|
|
|
8957 |
config.logLevel = config.hasOwnProperty('logLevel') ?
|
|
|
8958 |
config.logLevel : logger.SILENT;
|
|
|
8959 |
|
|
|
8960 |
var result = build(config);
|
|
|
8961 |
|
|
|
8962 |
//Reset build internals on each run.
|
|
|
8963 |
requirejs._buildReset();
|
|
|
8964 |
|
|
|
8965 |
callback(result);
|
|
|
8966 |
};
|
|
|
8967 |
|
|
|
8968 |
//Enable execution of this callback in a build setting.
|
|
|
8969 |
//Normally, once requirePatch is run, by default it will
|
|
|
8970 |
//not execute callbacks, unless this property is set on
|
|
|
8971 |
//the callback.
|
|
|
8972 |
runBuild.__requireJsBuild = true;
|
|
|
8973 |
|
|
|
8974 |
requirejs({
|
|
|
8975 |
context: 'build'
|
|
|
8976 |
}, ['build', 'logger'], runBuild);
|
|
|
8977 |
};
|
|
|
8978 |
|
|
|
8979 |
requirejs.define = define;
|
|
|
8980 |
|
|
|
8981 |
module.exports = requirejs;
|
|
|
8982 |
return;
|
|
|
8983 |
}
|
|
|
8984 |
|
|
|
8985 |
if (commandOption === 'o') {
|
|
|
8986 |
//Do the optimizer work.
|
|
|
8987 |
loadLib();
|
|
|
8988 |
|
|
|
8989 |
/**
|
|
|
8990 |
* @license Copyright (c) 2010-2011, The Dojo Foundation All Rights Reserved.
|
|
|
8991 |
* Available via the MIT or new BSD license.
|
|
|
8992 |
* see: http://github.com/jrburke/requirejs for details
|
|
|
8993 |
*/
|
|
|
8994 |
|
|
|
8995 |
/*
|
|
|
8996 |
* Create a build.js file that has the build options you want and pass that
|
|
|
8997 |
* build file to this file to do the build. See example.build.js for more information.
|
|
|
8998 |
*/
|
|
|
8999 |
|
|
|
9000 |
/*jslint strict: false, nomen: false */
|
|
|
9001 |
/*global require: false */
|
|
|
9002 |
|
|
|
9003 |
require({
|
|
|
9004 |
baseUrl: require.s.contexts._.config.baseUrl,
|
|
|
9005 |
//Use a separate context than the default context so that the
|
|
|
9006 |
//build can use the default context.
|
|
|
9007 |
context: 'build',
|
|
|
9008 |
catchError: {
|
|
|
9009 |
define: true
|
|
|
9010 |
}
|
|
|
9011 |
}, ['env!env/args', 'build'],
|
|
|
9012 |
function (args, build) {
|
|
|
9013 |
build(args);
|
|
|
9014 |
});
|
|
|
9015 |
|
|
|
9016 |
|
|
|
9017 |
} else if (commandOption === 'v') {
|
|
|
9018 |
console.log('r.js: ' + version + ', RequireJS: ' + this.requirejsVars.require.version);
|
|
|
9019 |
} else if (commandOption === 'convert') {
|
|
|
9020 |
loadLib();
|
|
|
9021 |
|
|
|
9022 |
this.requirejsVars.require(['env!env/args', 'commonJs', 'env!env/print'],
|
|
|
9023 |
function (args, commonJs, print) {
|
|
|
9024 |
|
|
|
9025 |
var srcDir, outDir;
|
|
|
9026 |
srcDir = args[0];
|
|
|
9027 |
outDir = args[1];
|
|
|
9028 |
|
|
|
9029 |
if (!srcDir || !outDir) {
|
|
|
9030 |
print('Usage: path/to/commonjs/modules output/dir');
|
|
|
9031 |
return;
|
|
|
9032 |
}
|
|
|
9033 |
|
|
|
9034 |
commonJs.convertDir(args[0], args[1]);
|
|
|
9035 |
});
|
|
|
9036 |
} else {
|
|
|
9037 |
//Just run an app
|
|
|
9038 |
|
|
|
9039 |
//Load the bundled libraries for use in the app.
|
|
|
9040 |
if (commandOption === 'lib') {
|
|
|
9041 |
loadLib();
|
|
|
9042 |
}
|
|
|
9043 |
|
|
|
9044 |
setBaseUrl(fileName);
|
|
|
9045 |
|
|
|
9046 |
if (exists(fileName)) {
|
|
|
9047 |
exec(readFile(fileName), fileName);
|
|
|
9048 |
} else {
|
|
|
9049 |
showHelp();
|
|
|
9050 |
}
|
|
|
9051 |
}
|
|
|
9052 |
|
|
|
9053 |
}((typeof console !== 'undefined' ? console : undefined),
|
|
|
9054 |
(typeof Packages !== 'undefined' ? Array.prototype.slice.call(arguments, 0) : []),
|
|
|
9055 |
(typeof readFile !== 'undefined' ? readFile : undefined)));
|