Skip to content

Commit 93ede0e

Browse files
committed
WIP - allow custom pre-processors
This adds custom preprocessor support. No automated test yet. Run the demo like this: ./cli.js --compilers=coffee:cofee-script/register ./test/fixture/custom-processor.coffee Currently this runs Babel on the coffeescript transpiled code. This allows the use of generators, async/await, etc. without users having to manually configure Babel. I am not sure this is the right strategy. While automatically configuring Babel would be cool, I think it will likely end up being better to just disable our internal Babel support as soon as they pass a `--compilers` or `--require` command. Basically, once you've done that, you've taken transpilation on yourself. An alternate possibility would be a flag to manually disable our own Babel processing.
1 parent 3f2e67a commit 93ede0e

File tree

5 files changed

+63
-5
lines changed

5 files changed

+63
-5
lines changed

api.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ Api.prototype._runFile = function (file) {
5050
args.push('--serial');
5151
}
5252

53+
if (this.compilers) {
54+
Object.keys(this.compilers).forEach(function (ext) {
55+
args.push('--compilers=' + ext + ':' + this.compilers[ext]);
56+
}, this);
57+
}
58+
5359
// Forward the `time-require` `--sorted` flag.
5460
// Intended for internal optimization tests only.
5561
if (this._sorted) {
@@ -217,7 +223,7 @@ function handlePaths(files) {
217223
})
218224
.then(flatten)
219225
.filter(function (file) {
220-
return path.extname(file) === '.js' && path.basename(file)[0] !== '_';
226+
return path.extname(file) === '.coffee' || path.extname(file) === '.js' && path.basename(file)[0] !== '_';
221227
});
222228
}
223229

cli.js

+19-2
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,27 @@ if (cli.flags.init) {
6262

6363
log.write();
6464

65-
var api = new Api(cli.input, {
65+
var apiOpts = {
6666
failFast: cli.flags.failFast,
6767
serial: cli.flags.serial
68-
});
68+
};
69+
70+
if (cli.flags.compilers) {
71+
var compilers = cli.flags.compilers;
72+
if (!Array.isArray(compilers)) {
73+
compilers = [compilers];
74+
}
75+
apiOpts.compilers = {};
76+
compilers.forEach(function (compilerDef) {
77+
var colonIdx = compilerDef.indexOf(':');
78+
if (colonIdx === -1) {
79+
throw new Error('compilers must be specified with: --compilers=<ext>:<require-path>');
80+
}
81+
apiOpts.compilers[compilerDef.substring(0, colonIdx)] = compilerDef.substring(colonIdx + 1);
82+
});
83+
}
84+
85+
var api = new Api(cli.input, apiOpts);
6986

7087
api.on('test', function (test) {
7188
if (test.error) {

lib/babel.js

+30-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ var loudRejection = require('loud-rejection/api')(process);
3030
var resolveFrom = require('resolve-from');
3131
var hasGenerator = require('has-generator');
3232
var serializeError = require('serialize-error');
33+
var path = require('path');
34+
var fs = require('fs');
3335
var send = require('./send');
3436

3537
var testPath = process.argv[2];
@@ -55,7 +57,8 @@ var options = {
5557
optional: hasGenerator ? ['asyncToGenerator', 'runtime'] : ['runtime'],
5658
plugins: [powerAssert],
5759
sourceMaps: true,
58-
inputSourceMap: null
60+
inputSourceMap: null,
61+
filename: testPath
5962
};
6063

6164
// check if test files required ava and show error, when they didn't
@@ -74,8 +77,33 @@ if (inputSourceMap) {
7477
options.inputSourceMap = JSON.parse(inputSourceMap.map);
7578
}
7679

80+
// load up custom compilers
81+
process.argv.slice(3).forEach(function (arg) {
82+
var match = /^--compilers=.+?:(.+)$/i.exec(arg);
83+
if (match) {
84+
require(match[1]);
85+
}
86+
});
87+
7788
// include test file
78-
var transpiled = babel.transformFileSync(testPath, options);
89+
var Module = module.constructor;
90+
var source = fs.readFileSync(testPath, 'utf8');
91+
var extension = path.extname(testPath);
92+
93+
var compiler = Module._extensions[extension];
94+
if (compiler) {
95+
compiler(
96+
{
97+
_compile: function (s, f) {
98+
source = s;
99+
testPath = f;
100+
}
101+
},
102+
testPath
103+
);
104+
}
105+
106+
var transpiled = babel.transform(source, options);
79107
sourceMapCache[testPath] = transpiled.map;
80108
requireFromString(transpiled.code, testPath, {
81109
appendPaths: module.paths

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@
115115
"update-notifier": "^0.5.0"
116116
},
117117
"devDependencies": {
118+
"coffee-script": "^1.10.0",
118119
"coveralls": "^2.11.4",
120+
"delay": "^1.1.0",
119121
"signal-exit": "^2.1.2",
120122
"sinon": "^1.17.2",
121123
"tap": "^2.2.1",
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
test = require "../../"
2+
delay = require "delay"
3+
4+
test "peter", (t) ->
5+
yield delay 200

0 commit comments

Comments
 (0)