diff options
author | Mihai Bazon <mihai.bazon@gmail.com> | 2015-09-24 17:57:47 +0300 |
---|---|---|
committer | Mihai Bazon <mihai.bazon@gmail.com> | 2015-09-24 17:58:51 +0300 |
commit | 99233c44cc125fa1a96a47b863dbfb3ec0c3fadc (patch) | |
tree | 1cb038e841953084f910fd73411bf2dc89730160 /tools/node.js | |
parent | 33528002b496728457cccd9ddf54d2e50bc7e3f2 (diff) | |
download | tracifyjs-99233c44cc125fa1a96a47b863dbfb3ec0c3fadc.tar.gz tracifyjs-99233c44cc125fa1a96a47b863dbfb3ec0c3fadc.zip |
No longer use `vm` to load code.
Improves performance 2x on node > 0.10.
Ref #636
Diffstat (limited to 'tools/node.js')
-rw-r--r-- | tools/node.js | 40 |
1 files changed, 10 insertions, 30 deletions
diff --git a/tools/node.js b/tools/node.js index eba2bc1d..f6048661 100644 --- a/tools/node.js +++ b/tools/node.js @@ -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", @@ -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, |