Skip to content

Commit ec08008

Browse files
committed
move babel to main thread
There are many failing tests, but it basically works.
1 parent 5add6ad commit ec08008

File tree

4 files changed

+82
-31
lines changed

4 files changed

+82
-31
lines changed

api.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ var Promise = require('bluebird');
88
var figures = require('figures');
99
var globby = require('globby');
1010
var chalk = require('chalk');
11+
var assign = require('object-assign');
1112
var fork = require('./lib/fork');
1213
var formatter = require('./lib/enhance-assert').formatter();
14+
var precompileTest = require('./lib/test-transformer');
1315

1416
function Api(files, options) {
1517
if (!(this instanceof Api)) {
@@ -40,7 +42,15 @@ util.inherits(Api, EventEmitter);
4042
module.exports = Api;
4143

4244
Api.prototype._runFile = function (file) {
43-
return fork(file, this.options)
45+
var precompiled = {};
46+
precompiled[file.testPath] = {
47+
sourcePath: file.tempPath,
48+
mapPath: file.mapPath
49+
};
50+
var options = assign({}, this.options, {
51+
precompiled: precompiled
52+
});
53+
return fork(file.testPath, options)
4454
.on('stats', this._handleStats)
4555
.on('test', this._handleTest)
4656
.on('unhandledRejections', this._handleRejections)
@@ -129,6 +139,7 @@ Api.prototype.run = function () {
129139
.map(function (file) {
130140
return path.resolve(file);
131141
})
142+
.map(precompileTest)
132143
.then(function (files) {
133144
if (files.length === 0) {
134145
return Promise.reject(new Error('Couldn\'t find any files to test'));

lib/babel.js

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,59 +23,41 @@ var resolveCwd = require('resolve-cwd');
2323

2424
var sourceMapCache = Object.create(null);
2525

26+
var fs = require('fs');
2627
var sourceMapSupport = require('source-map-support');
2728
sourceMapSupport.install({
2829
handleUncaughtExceptions: false,
2930
retrieveSourceMap: function (source) {
3031
if (sourceMapCache[source]) {
3132
return {
3233
url: source,
33-
map: sourceMapCache[source]
34+
map: fs.readFileSync(sourceMapCache[source], 'utf8')
3435
};
3536
}
3637
}
3738
});
3839

39-
var createEspowerPlugin = require('babel-plugin-espower/create');
40-
var requireFromString = require('require-from-string');
4140
var loudRejection = require('loud-rejection/api')(process);
4241
var serializeError = require('serialize-error');
43-
var babel = require('babel-core');
4442
var send = require('./send');
43+
var installPrecompiler = require('require-precompiled');
4544

4645
var testPath = opts.file;
4746

48-
// initialize power-assert
49-
var powerAssert = createEspowerPlugin(babel, {
50-
patterns: require('./enhance-assert').PATTERNS
51-
});
52-
53-
// if generators are not supported, use regenerator
54-
var options = {
55-
presets: [require('babel-preset-stage-2'), require('babel-preset-es2015')],
56-
plugins: [powerAssert, require('babel-plugin-transform-runtime')],
57-
sourceMaps: true
58-
};
59-
6047
// check if test files required ava and show error, when they didn't
6148
exports.avaRequired = false;
6249

63-
// try to load an input source map for the test file, in case the file was
64-
// already compiled once by the user
65-
var inputSourceMap = sourceMapSupport.retrieveSourceMap(testPath);
66-
if (inputSourceMap) {
67-
// source-map-support returns the source map as a json-encoded string, but
68-
// babel requires an actual object
69-
options.inputSourceMap = JSON.parse(inputSourceMap.map);
70-
}
71-
72-
// include test file
73-
var transpiled = babel.transformFileSync(testPath, options);
74-
sourceMapCache[testPath] = transpiled.map;
75-
requireFromString(transpiled.code, testPath, {
76-
appendPaths: module.paths
50+
installPrecompiler(function (filename) {
51+
var precompiled = opts.precompiled[filename];
52+
if (precompiled) {
53+
sourceMapCache[filename] = precompiled.mapPath;
54+
return fs.readFileSync(precompiled.sourcePath, 'utf8');
55+
}
56+
return null;
7757
});
7858

59+
require(testPath);
60+
7961
process.on('uncaughtException', function (exception) {
8062
send('uncaughtException', {exception: serializeError(exception)});
8163
});

lib/test-transformer.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
var createEspowerPlugin = require('babel-plugin-espower/create');
2+
var sourceMapSupport = require('source-map-support');
3+
var babel = require('babel-core');
4+
var Promise = require('bluebird');
5+
var tempWrite = require('temp-write');
6+
var transformFile = Promise.promisify(babel.transformFile);
7+
var enhanceAssert = require('./enhance-assert');
8+
9+
var cache = {};
10+
11+
module.exports = precompile;
12+
13+
function precompile(testPath) {
14+
return cache[testPath] || (cache[testPath] = _precompile(testPath));
15+
}
16+
17+
function _precompile(testPath) {
18+
// initialize power-assert
19+
var powerAssert = createEspowerPlugin(babel, {
20+
patterns: enhanceAssert.PATTERNS
21+
});
22+
23+
// if generators are not supported, use regenerator
24+
var options = {
25+
presets: [require('babel-preset-stage-2'), require('babel-preset-es2015')],
26+
plugins: [powerAssert, require('babel-plugin-transform-runtime')],
27+
sourceMaps: true,
28+
ast: false,
29+
inputSourceMap: null
30+
};
31+
32+
// try to load an input source map for the test file, in case the file was
33+
// already compiled once by the user
34+
var inputSourceMap = sourceMapSupport.retrieveSourceMap(testPath);
35+
if (inputSourceMap) {
36+
// source-map-support returns the source map as a json-encoded string, but
37+
// babel requires an actual object
38+
options.inputSourceMap = JSON.parse(inputSourceMap.map);
39+
}
40+
41+
return transformFile(testPath, options)
42+
.then(function (result) {
43+
return Promise.all([
44+
tempWrite(result.code, testPath),
45+
tempWrite(JSON.stringify(result.map), testPath + '.map')
46+
])
47+
.spread(function (tempPath, mapPath) {
48+
result.mapPath = mapPath;
49+
result.tempPath = tempPath;
50+
result.testPath = testPath;
51+
return result;
52+
});
53+
});
54+
}
55+

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,14 @@
110110
"power-assert-renderers": "^0.1.0",
111111
"pretty-ms": "^2.0.0",
112112
"require-from-string": "^1.1.0",
113+
"require-precompiled": "^0.1.0",
113114
"resolve-cwd": "^1.0.0",
115+
"resolve-from": "^2.0.0",
114116
"serialize-error": "^1.1.0",
115117
"set-immediate-shim": "^1.0.1",
116118
"source-map-support": "^0.4.0",
117119
"squeak": "^1.2.0",
120+
"temp-write": "^2.1.0",
118121
"time-require": "^0.1.2",
119122
"update-notifier": "^0.5.0"
120123
},

0 commit comments

Comments
 (0)