From b175d6abed7e2d3db972c3381b2062c195360346 Mon Sep 17 00:00:00 2001 From: Bruno Jouhier Date: Sun, 18 Oct 2015 23:44:15 +0200 Subject: [PATCH] issue #291 - improved previous fix + fixed REPL (was broken too) --- lib/repl.js | 7 +++---- lib/transformSync.js | 33 ++------------------------------- lib/util.js | 11 +++++++++++ 3 files changed, 16 insertions(+), 35 deletions(-) diff --git a/lib/repl.js b/lib/repl.js index 6c2bdef8..ffc0e581 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -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. @@ -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 || {}; @@ -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); } diff --git a/lib/transformSync.js b/lib/transformSync.js index 14c285fd..ff8d9366 100644 --- a/lib/transformSync.js +++ b/lib/transformSync.js @@ -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); @@ -51,7 +22,7 @@ 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 { @@ -59,7 +30,7 @@ exports.transform = function(source, options) { }; } if (!options.quiet) util.log("transforming (" + options.runtime + "): " + path); - return babelTransform(util.removeShebang(source), babelOptions); + return require('babel').transform(util.removeShebang(source), babelOptions); } } diff --git a/lib/util.js b/lib/util.js index a8d97e5e..0256ab65 100644 --- a/lib/util.js +++ b/lib/util.js @@ -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: {