Skip to content

Commit

Permalink
No longer use vm to load code.
Browse files Browse the repository at this point in the history
Improves performance 2x on node > 0.10.

Ref #636
  • Loading branch information
mishoo committed Sep 24, 2015
1 parent 3352800 commit 99233c4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 31 deletions.
1 change: 0 additions & 1 deletion bin/uglifyjs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ if (ARGS.self) {
}
files = UglifyJS.FILES;
if (!ARGS.wrap) ARGS.wrap = "UglifyJS";
ARGS.export_all = true;
}

var ORIG_MAP = ARGS.in_source_map;
Expand Down
1 change: 1 addition & 0 deletions lib/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ function DEFNODE(type, props, methods, base) {
ctor.DEFMETHOD = function(name, method) {
this.prototype[name] = method;
};
exports["AST_" + type] = ctor;
return ctor;
};

Expand Down
16 changes: 16 additions & 0 deletions tools/exports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
exports["Compressor"] = Compressor;
exports["DefaultsError"] = DefaultsError;
exports["Dictionary"] = Dictionary;
exports["JS_Parse_Error"] = JS_Parse_Error;
exports["MAP"] = MAP;
exports["OutputStream"] = OutputStream;
exports["SourceMap"] = SourceMap;
exports["TreeTransformer"] = TreeTransformer;
exports["TreeWalker"] = TreeWalker;
exports["base54"] = base54;
exports["defaults"] = defaults;
exports["mangle_properties"] = mangle_properties;
exports["merge"] = merge;
exports["parse"] = parse;
exports["push_uniq"] = push_uniq;
exports["string_template"] = string_template;
40 changes: 10 additions & 30 deletions tools/node.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,5 @@
var path = require("path");
var fs = require("fs");
var vm = require("vm");

var UglifyJS = vm.createContext({
console : console,
process : process,
Buffer : Buffer,
MOZ_SourceMap : require("source-map")
});

function load_global(file) {
file = path.resolve(path.dirname(module.filename), file);
try {
var code = fs.readFileSync(file, "utf8");
return vm.runInContext(code, UglifyJS, file);
} catch(ex) {
// XXX: in case of a syntax error, the message is kinda
// useless. (no location information).
console.log("ERROR in file: " + file + " / " + ex);
process.exit(1);
}
};

var FILES = exports.FILES = [
"../lib/utils.js",
Expand All @@ -32,24 +11,25 @@ var FILES = exports.FILES = [
"../lib/compress.js",
"../lib/sourcemap.js",
"../lib/mozilla-ast.js",
"../lib/propmangle.js"
"../lib/propmangle.js",
"./exports.js",
].map(function(file){
return fs.realpathSync(path.join(path.dirname(__filename), file));
});

FILES.forEach(load_global);
var UglifyJS = exports;

new Function("MOZ_SourceMap", "exports", FILES.map(function(file){
return fs.readFileSync(file, "utf8");
}).join("\n\n"))(
require("source-map"),
UglifyJS
);

UglifyJS.AST_Node.warn_function = function(txt) {
console.error("WARN: %s", txt);
};

// XXX: perhaps we shouldn't export everything but heck, I'm lazy.
for (var i in UglifyJS) {
if (UglifyJS.hasOwnProperty(i)) {
exports[i] = UglifyJS[i];
}
}

exports.minify = function(files, options) {
options = UglifyJS.defaults(options, {
spidermonkey : false,
Expand Down

0 comments on commit 99233c4

Please sign in to comment.