Skip to content

Commit

Permalink
Merge pull request #400 from jamestalmage/rewrite-runtime
Browse files Browse the repository at this point in the history
Rewrite all `require('babel-runtime/**)` statements to absolute paths
  • Loading branch information
jamestalmage committed Mar 19, 2016
2 parents 8d47119 + 382e50d commit e4ef5f2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
19 changes: 18 additions & 1 deletion lib/caching-precompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ CachingPrecompiler.prototype._factory = function (babelConfig, cacheDir) {
var presetES2015 = require('babel-preset-es2015');
var transformRuntime = require('babel-plugin-transform-runtime');

var rewriteRuntime = this._createRewritePlugin();
var powerAssert = this._createEspowerPlugin(babel);

function buildOptions(filename, code) {
Expand All @@ -49,7 +50,7 @@ CachingPrecompiler.prototype._factory = function (babelConfig, cacheDir) {
});

options.plugins = options.plugins || [];
options.plugins.push(powerAssert, transformRuntime);
options.plugins.push(powerAssert, transformRuntime, rewriteRuntime);

return options;
}
Expand All @@ -64,6 +65,22 @@ CachingPrecompiler.prototype._factory = function (babelConfig, cacheDir) {
};
};

CachingPrecompiler.prototype._createRewritePlugin = function () {
var wrapListener = require('babel-plugin-detective/wrap-listener');

function rewriteRuntimeListener(path) {
if (path.isLiteral() && /^babel-runtime[\\\/]?/.test(path.node.value)) {
path.node.value = require.resolve(path.node.value);
}
}

return wrapListener(rewriteRuntimeListener, 'rewrite-runtime', {
generated: true,
require: true,
import: true
});
};

CachingPrecompiler.prototype._createEspowerPlugin = function (babel) {
var createEspowerPlugin = require('babel-plugin-espower/create');
var enhanceAssert = require('./enhance-assert');
Expand Down
9 changes: 0 additions & 9 deletions lib/test-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,6 @@ installPrecompiler(function (filename) {
return null;
});

// Modules need to be able to find `babel-runtime`, which is nested in our node_modules w/ npm@2
var nodeModulesDir = path.join(__dirname, '../node_modules');
var oldNodeModulesPaths = module.constructor._nodeModulePaths;
module.constructor._nodeModulePaths = function () {
var ret = oldNodeModulesPaths.apply(this, arguments);
ret.push(nodeModulesDir);
return ret;
};

var dependencies = [];
Object.keys(require.extensions).forEach(function (ext) {
var wrappedHandler = require.extensions[ext];
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"arrify": "^1.0.0",
"ava-init": "^0.1.0",
"babel-core": "^6.3.21",
"babel-plugin-detective": "^1.0.2",
"babel-plugin-espower": "^2.1.0",
"babel-plugin-transform-runtime": "^6.3.13",
"babel-preset-es2015": "^6.3.13",
Expand Down
4 changes: 3 additions & 1 deletion test/caching-precompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,13 @@ test('uses babelConfig for babel options when babelConfig is an object', functio
var tempDir = uniqueTempDir();
var customPlugin = sinon.stub().returns({visitor: {}});
var powerAssert = sinon.stub().returns({visitor: {}});
var rewrite = sinon.stub().returns({visitor: {}});
var precompiler = new CachingPrecompiler(tempDir, {
presets: ['stage-2', 'es2015'],
plugins: [customPlugin]
});
sinon.stub(precompiler, '_createEspowerPlugin').returns(powerAssert);
sinon.stub(precompiler, '_createRewritePlugin').returns(rewrite);
babel.transform.reset();

precompiler.precompileFile(fixture('es2015.js'));
Expand All @@ -114,6 +116,6 @@ test('uses babelConfig for babel options when babelConfig is an object', functio
t.true('inputSourceMap' in options);
t.false(options.babelrc);
t.same(options.presets, ['stage-2', 'es2015']);
t.same(options.plugins, [customPlugin, powerAssert, transformRuntime]);
t.same(options.plugins, [customPlugin, powerAssert, transformRuntime, rewrite]);
t.end();
});

0 comments on commit e4ef5f2

Please sign in to comment.