Skip to content

Commit

Permalink
issue #291 - improved previous fix + fixed REPL (was broken too)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjouhier committed Oct 18, 2015
1 parent a722c11 commit b175d6a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 35 deletions.
7 changes: 3 additions & 4 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ var babel = require('babel');
var util = require('./util');

exports.run = function(prog, options) {
options = util.getOptions(options);
// register in case we get called directy
require('./register').register(options);

function evaluate(cmd, context, filename, callback) {
// HACK: prevent empty commands (just newlines) from throwing errors
// during transformation. the command itself is wrapped in parens.
Expand Down Expand Up @@ -35,6 +31,7 @@ exports.run = function(prog, options) {
}
var babelOptions = util.babelOptions(options);
var source = vars + babel.transform("(function(_) {" + cmd + "})(__callback);", babelOptions).code;

context.__filename = filename;
// cannot assign context.__ directly in callback - need to investigate why
context.__private = context.__private || {};
Expand All @@ -43,7 +40,9 @@ exports.run = function(prog, options) {
if (!err) context.__private.__ = result;
callback(err, result);
};
context.require = require;
vm.runInContext(source, context, filename);

} catch (ex) {
callback(ex);
}
Expand Down
33 changes: 2 additions & 31 deletions lib/transformSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,6 @@ var fsp = require("path");
var util = require('./util');
var cacheSync = require('./cacheSync');

// babel uses process.cwd() to locate its plugins.
// We have to fool it so that globally installed _node / _coffee can load the streamline plugin.
// Fortunately it caches the result of the first process.cwd() call (see tryRequire implementation)
// So we monkey patch process.cwd, execute a dummy transform, and then restore process.cwd
var dummyTransform = function() {
var cwd = process.cwd;
process.cwd = function() {
return fsp.join(__dirname, '..');
}
try {
require('babel').transform("(function(_) {})", {
plugins: ['streamline'],
extra: {
streamline: {
quiet: true,
}
}
});
} catch (ex) {}
process.cwd = cwd;
dummyTransform = null;
}

function babelTransform(source, babelOptions) {
if (dummyTransform) dummyTransform();
require("babel-plugin-streamline");
return require('babel').transform(source, babelOptions);
}

exports.transform = function(source, options) {
var path = options.filename;
var babelOptions = util.babelOptions(options, path);
Expand All @@ -51,15 +22,15 @@ exports.transform = function(source, options) {
}
babelOptions.inputSourceMap = JSON.parse(decaf.v3SourceMap);
if (!options.quiet) util.log("transforming (" + options.runtime + "): " + path);
return babelTransform(decaf.js, babelOptions);
return require('babel').transform(decaf.js, babelOptions);
} else {
if (options.ignore && path && options.ignore(path)) {
return {
code: source,
};
}
if (!options.quiet) util.log("transforming (" + options.runtime + "): " + path);
return babelTransform(util.removeShebang(source), babelOptions);
return require('babel').transform(util.removeShebang(source), babelOptions);
}
}

Expand Down
11 changes: 11 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,17 @@ exports.babelOptions = function(options, filename) {
if (options.babel[opt] != null) babelOpts[opt] = options.babel[opt];
});
if (babelOpts.plugins.indexOf('streamline') < 0) babelOpts.plugins.push('streamline');
babelOpts.plugins = babelOpts.plugins.map(function(plugin) {
if (typeof plugin === 'string') {
try {
return require('babel-plugin-' + plugin);
} catch (ex) {
return require(plugin);
}
} else {
return plugin;
}
});
// see https://github.com/babel/babel/issues/1833
babelOpts.extra = {
streamline: {
Expand Down

0 comments on commit b175d6a

Please sign in to comment.