Skip to content

Commit

Permalink
Use Babel to minimise whitespace and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
curiousdannii committed Jan 13, 2018
1 parent 003a287 commit dc559dd
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "emscripten",
"version": "1.13.0",
"dependencies": {
"babel-core": "^6.26.0",
"ws": "~0.4.28"
}
}
2 changes: 2 additions & 0 deletions src/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ var OUTLINING_LIMIT = 0; // A function size above which we try to automatically
// emscripten to disable some passes that are incompatible with
// it).

var BABEL_OPTIMIZER = 0; // Whether to use the Babel optimizer instead of the Uglify optimizer

var AGGRESSIVE_VARIABLE_ELIMINATION = 0; // Run aggressiveVariableElimination in js-optimizer.js
var SIMPLIFY_IFS = 1; // Whether to simplify ifs in js-optimizer.js

Expand Down
130 changes: 130 additions & 0 deletions tools/js-optimizer-babel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// -*- Mode: javascript; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 ; js-indent-level : 2 ; js-curly-indent-offset: 0 -*-
// vim: set ts=2 et sw=2:

/*
A version of the JS Optimizer that uses Babel
*/

var babel = require("babel-core");

// *** Environment setup code ***

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
// Note that we pollute the global namespace here, otherwise we break in node
print = function(x) {
process['stdout'].write(x + '\n');
};
printErr = function(x) {
process['stderr'].write(x + '\n');
};

var nodeFS = require('fs');
var nodePath = require('path');

if (!nodeFS.existsSync) {
nodeFS.existsSync = function(path) {
try {
return !!nodeFS.readFileSync(path);
} catch(e) {
return false;
}
}
}

function find(filename) {
var prefixes = [nodePath.join(__dirname, '..', 'src'), process.cwd()];
for (var i = 0; i < prefixes.length; ++i) {
var combined = nodePath.join(prefixes[i], filename);
if (nodeFS.existsSync(combined)) {
return combined;
}
}
return filename;
}

read = function(filename) {
var absolute = find(filename);
return nodeFS['readFileSync'](absolute).toString();
};

load = function(f) {
globalEval(read(f));
};

arguments_ = process['argv'].slice(2);

} else if (ENVIRONMENT_IS_SHELL) {
// Polyfill over SpiderMonkey/V8 differences
if (!this['read']) {
this['read'] = function(f) { snarf(f) };
}

if (typeof scriptArgs != 'undefined') {
arguments_ = scriptArgs;
} else if (typeof arguments != 'undefined') {
arguments_ = arguments;
}

} else if (ENVIRONMENT_IS_WEB) {
this['print'] = printErr = function(x) {
console.log(x);
};

this['read'] = function(url) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, false);
xhr.send(null);
return xhr.responseText;
};

if (this['arguments']) {
arguments_ = arguments;
}
} else if (ENVIRONMENT_IS_WORKER) {
// We can do very little here...

this['load'] = importScripts;

} else {
throw 'Unknown runtime environment. Where are we?';
}

// *** MAIN ***

// Process the incoming arguments

var arguments_ = process['argv'].slice(2);

arguments_ = arguments_.filter(function (arg) {
if (!/^--/.test(arg)) return true;

if (arg === '--debug') debug = true;
else throw new Error('Unrecognized flag: ' + arg);
});

var src = read(arguments_[0]);

function hasArg( arg )
{
return arguments_.indexOf( arg ) > -1;
}

// Run through Babel

var options = {
comments: !hasArg('minifyWhitespace'),
minified: hasArg('minifyWhitespace'),
};

var result = babel.transform(src, options);

print(result.code);
print('\n');
3 changes: 3 additions & 0 deletions tools/js_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,9 @@ def run_on_chunk(command):
raise Exception()

def run_on_js(filename, passes, js_engine, source_map=False, extra_info=None, just_split=False, just_concat=False):
# Load the correct optimizer
JS_OPTIMIZER = path_from_root('tools', 'js-optimizer-babel.js' if shared.Settings.BABEL_OPTIMIZER else 'js-optimizer.js')

with ToolchainProfiler.profile_block('js_optimizer.split_markers'):
if not isinstance(passes, list):
passes = [passes]
Expand Down

0 comments on commit dc559dd

Please sign in to comment.