Skip to content

Commit

Permalink
Fix nodejs with uglify2
Browse files Browse the repository at this point in the history
  • Loading branch information
labsin committed Jul 6, 2015
1 parent 62e48c3 commit 432f462
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
4 changes: 1 addition & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ var gutil = require('gulp-util');
require(__dirname + '/src/qtcore/qml/QMLBinding.js');
require(__dirname + '/src/qtcore/qml/lib/parser.js');
// JS Parser
require(__dirname + '/src/uglify/utils.js');
require(__dirname + '/src/uglify/ast.js');
require(__dirname + '/src/uglify/parse.js');
require(__dirname + '/src/uglify/node.js');
require(__dirname + '/src/qtcore/qml/lib/jsparser.js');

module.exports = function (opt) {
Expand Down
3 changes: 2 additions & 1 deletion src/qtcore/qml/lib/jsparser.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(function() {
var UglifyJS = require('../../../uglify/node.js');

global.importJavascriptInContext = function (jsData, $context) {
with(qmlEngine.rootContext()) {
Expand All @@ -11,7 +12,7 @@
}

global.jsparse = function (source) {
var AST_Tree = parse(source);
var AST_Tree = UglifyJS.parse(source);
var obj = { exports: [], source: source };

for (var i = 0 ; i < AST_Tree.body.length ; ++i) {
Expand Down
39 changes: 39 additions & 0 deletions src/uglify/node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
var path = require("path");
var fs = require("fs");
var vm = require("vm");

var UglifyJS = vm.createContext({
console : console,
process : process,
Buffer : Buffer,
});

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 = [
"./utils.js",
"./ast.js",
"./parse.js"
].map(function(file){
return fs.realpathSync(path.join(path.dirname(__filename), file));
});

FILES.forEach(load_global);

// 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];
}
}

0 comments on commit 432f462

Please sign in to comment.