aboutsummaryrefslogtreecommitdiff
path: root/tools/js-optimizer.js
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2012-01-10 19:46:31 -0800
committerAlon Zakai <alonzakai@gmail.com>2012-01-10 19:46:31 -0800
commit5991480ae6dfeaac31d0d4806058783304359afd (patch)
tree4854bcd799cd96987ddb72264d503f93b504c462 /tools/js-optimizer.js
parented02667266b2bb5d11db9c9639427beaa8ee5c88 (diff)
refactor js optimizer to not use fs.* as much as possible
Diffstat (limited to 'tools/js-optimizer.js')
-rw-r--r--tools/js-optimizer.js101
1 files changed, 89 insertions, 12 deletions
diff --git a/tools/js-optimizer.js b/tools/js-optimizer.js
index 7ac2195f..a77e0e09 100644
--- a/tools/js-optimizer.js
+++ b/tools/js-optimizer.js
@@ -5,26 +5,103 @@
// conjunction with closure compiler.
//==============================================================================
-var uglify = require('../tools/eliminator/node_modules/uglify-js');
-var fs = require('fs');
+// *** Environment setup code ***
+var arguments_ = [];
+
+var ENVIRONMENT_IS_NODE = typeof process === 'object';
+var ENVIRONMENT_IS_WEB = typeof window === 'object';
+var ENVIRONMENT_IS_WORKER = typeof importScripts === 'function';
+var ENVIRONMENT_IS_SHELL = !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_NODE && !ENVIRONMENT_IS_WORKER;
+
+if (ENVIRONMENT_IS_NODE) {
+ // Expose functionality in the same simple way that the shells work
+ print = function(x) {
+ process['stdout'].write(x + '\n');
+ };
+ printErr = function(x) {
+ process['stderr'].write(x + '\n');
+ };
+
+ var nodeFS = require('fs');
+
+ read = function(filename) {
+ var ret = nodeFS['readFileSync'](filename).toString();
+ if (!ret && filename[0] != '/') {
+ filename = __dirname.split('/').slice(0, -1).join('/') + '/src/' + filename;
+ ret = nodeFS['readFileSync'](filename).toString();
+ }
+ return ret;
+ };
+
+ arguments_ = process['argv'].slice(2);
+
+} else if (ENVIRONMENT_IS_SHELL) {
+ // Polyfill over SpiderMonkey/V8 differences
+ if (!this['read']) {
+ read = function(f) { snarf(f) };
+ }
+
+ if (!this['arguments']) {
+ arguments_ = scriptArgs;
+ } else {
+ arguments_ = arguments;
+ }
+
+} else if (ENVIRONMENT_IS_WEB) {
+ print = printErr = function(x) {
+ console.log(x);
+ };
-// Make node environment compatible with JS shells
+ read = function(url) {
+ var xhr = new XMLHttpRequest();
+ xhr.open('GET', url, false);
+ xhr.send(null);
+ return xhr.responseText;
+ };
-function print(text) {
- process.stdout.write(text + '\n');
+ if (this['arguments']) {
+ arguments_ = arguments;
+ }
+} else if (ENVIRONMENT_IS_WORKER) {
+ // We can do very little here...
+
+ load = importScripts;
+
+} else {
+ throw 'Unknown runtime environment. Where are we?';
+}
+
+function globalEval(x) {
+ eval.call(null, x);
+}
+
+if (typeof load == 'undefined' && typeof read != 'undefined') {
+ load = function(f) {
+ globalEval(read(f));
+ };
}
-function printErr(text) {
- process.stderr.write(text + '\n');
+
+if (typeof printErr === 'undefined') {
+ printErr = function(){};
+}
+
+if (typeof print === 'undefined') {
+ print = printErr;
}
-function read(filename) {
+// *** Environment setup code ***
+
+// Fix read for our location
+read = function(filename) {
if (filename[0] != '/') filename = __dirname.split('/').slice(0, -1).join('/') + '/src/' + filename;
return fs.readFileSync(filename).toString();
}
-var arguments = process.argv.slice(2);
+
+var uglify = require('../tools/eliminator/node_modules/uglify-js');
+var fs = require('fs');
// Load some modules
-eval(read('utility.js'));
+load('utility.js');
// Utilities
@@ -1030,14 +1107,14 @@ var passes = {
// Main
-var src = fs.readFileSync('/dev/stdin').toString();
+var src = read(arguments_[0]);
var ast = srcToAst(src);
//printErr(JSON.stringify(ast)); throw 1;
var metadata = src.split('\n').filter(function(line) { return line.indexOf('EMSCRIPTEN_GENERATED_FUNCTIONS') >= 0 })[0];
//assert(metadata, 'Must have EMSCRIPTEN_GENERATED_FUNCTIONS metadata');
if (metadata) setGeneratedFunctions(metadata);
-arguments.forEach(function(arg) {
+arguments_.slice(1).forEach(function(arg) {
passes[arg](ast);
});
//printErr('output: ' + dump(ast));