Skip to content

Commit

Permalink
Allow user to specify babel opts through babelrc or param. Fixes #5
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmoore committed Oct 21, 2016
1 parent 9dbb339 commit a3b6665
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 13 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@ $ npm install --save-dev ts-babel-node [email protected]

Since `ts-babel-node` is a wrapper around `ts-node`, anything you can do with `ts-node` works with `ts-babel-node`. See [`ts-node`'s docs](https://github.com/TypeStrong/ts-node/#usage) for more details.

To configure babel, you can pass in an options object to the appropriate register function or use a [babelrc](http://babeljs.io/docs/usage/babelrc/). All babelrc locations are supported. **Note**: if you use a babelrc, the default babel configuration provided by ts-babel-node will not be used. Simply include the `es2015` preset in your config (or don't, if you don't want it).

### Library

`ts-babel-node` exposes two APIs. The first is a wrapper around the `ts-node` API.

```js
// $ node this-file.js

require('ts-babel-node').register(/* ts-node options */);
require('ts-babel-node').register(tsNodeOpts, babelOpts); // both opts are optional
// Or
require('ts-babel-node/register');
```
Expand All @@ -57,7 +59,7 @@ The second API only adds the babel-compilation step. This is useful if your code
```js
// $ ts-node this-file.js

require('ts-babel-node').registerBabel();
require('ts-babel-node').registerBabel(babelOpts); // babelOpts is optional
// Or
require('ts-babel-node/register-babel');
```
Expand All @@ -83,4 +85,4 @@ import 'ts-babel-node/register-babel';
// ...
```

Then use `gulp` normally. Keep in mind that the babel traspiler won't be active in your `gulpfile.ts`, but will be running in all your imports.
Then use `gulp` normally. Keep in mind that the babel traspiler won't be active in your `gulpfile.ts`, but will be running in all your imports.
45 changes: 37 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
'use strict';

var path = require('path');
var babel = require('babel-core');
var buildConfigChain = require('babel-core/lib/transformation/file/options/build-config-chain');
var sourceMapSupport = require('source-map-support');
var convertSourceMap = require('convert-source-map');

var outputs = {}; // filename => { code, map }

var babelOpts = {
var baseBabelOpts = { ast: false };

var defaultBabelOpts = {
presets: [ require('babel-preset-es2015') ],
ast: false,
};

exports.registerBabel = registerBabel;
function registerBabel() {
function registerBabel(babelOpts) {
if (babelOpts) {
Object.assign(baseBabelOpts, babelOpts);
defaultBabelOpts = {};
}

overrideSourceMaps();

// In case ts-node has already run...
Expand All @@ -32,9 +40,9 @@ function registerBabel() {
}

exports.register = register;
function register(opts) {
registerBabel();
require('ts-node').register(opts);
function register(tsNodeOpts, babelOpts) {
registerBabel(babelOpts);
require('ts-node').register(tsNodeOpts);
}

function hook(base, m, filename) {
Expand All @@ -46,14 +54,36 @@ function compile(base, code, filename) {
var sourcemap = convertSourceMap.fromMapFileSource(code, '.').toObject();
convertSourceMap.removeMapFileComments(code);

var babelOutput = babel.transform(code, Object.assign({ inputSourceMap: sourcemap }, babelOpts));
var babelOutput = babel.transform(code, getBabelOpts(filename, sourcemap));

// babelOutput has a bunch of undocumented stuff on it. Just grab what we need to save memory
outputs[filename] = { code: babelOutput.code, map: babelOutput.map };

return base.call(this, babelOutput.code, filename);
}

function getBabelOpts(filename, sourcemap) {
// this function does roughly what OptionsManager.init does, but we add our own defaulting logic

var chain = buildConfigChain(Object.assign({ filename: filename, inputSourceMap: sourcemap }, baseBabelOpts));

var optionsManager = new babel.OptionManager();
chain.forEach(function (c) { optionsManager.mergeOptions(c); });

// custom defaulting logic: If the user doesn't provide a .babelrc (or equivalent), then we supply our default.
if (chain.length < 2) // our base config counts as a config
optionsManager.mergeOptions({
options: defaultBabelOpts,
alias: 'default',
loc: 'default',
dirname: path.dirname(filename),
});

optionsManager.normaliseOptions();

return optionsManager.options;
}

function overrideSourceMaps() {
sourceMapSupport.install({
handleUncaughtExceptions: false,
Expand All @@ -65,7 +95,6 @@ function overrideSourceMaps() {

if (!map) return null;


return {
url: null,
map: map,
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
},
"devDependencies": {
"@types/bluebird": "^3.0.35",
"@types/node": "^6.0.45",
"babel-plugin-transform-remove-console": "^6.8.0",
"babel-polyfill": "^6.9.1",
"bluebird": "^3.4.0",
"code": "^4.0.0",
Expand Down
27 changes: 27 additions & 0 deletions test/babelrc.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';
var exec = require('./exec');
var lab = exports.lab = require('lab').script();
var expect = require('code').expect;

// the .babelrc in the custom-babel folder removes all console.* statements.
// We selectively include this to demonstrate that the .babelrc is picked up.
lab.experiment('When I have set a custom .babelrc', function () {
var result;

lab.before(function () {
return exec('custom-babel/test.ts')
.then(function (_result) {
result = _result;
});
});

lab.test('it runs successfully', function (done) {
expect(result.code).to.equal(0);
done();
});

lab.test('it prints the correct output', function (done) {
expect(result.out).to.equal('this is the only thing that should appear\n');
done();
});
});
3 changes: 3 additions & 0 deletions test/cases/custom-babel/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": [ "transform-remove-console" ]
}
2 changes: 2 additions & 0 deletions test/cases/custom-babel/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
console.log('this should not appear');
process.stdout.write('this is the only thing that should appear\n');
3 changes: 1 addition & 2 deletions test/cases/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"isolatedModules": true
"noEmit": true
}
}

0 comments on commit a3b6665

Please sign in to comment.