Skip to content

Commit

Permalink
Fix compatibility on Node < 6
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmoore committed Oct 20, 2016
1 parent 437be5c commit 42ee8b9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 35 deletions.
2 changes: 1 addition & 1 deletion bin/ts-babel-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
'use strict';

require('..').registerBabel();
require('ts-node/dist/bin');
require('ts-node/dist/_bin');
74 changes: 40 additions & 34 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,59 @@

var babel = require('babel-core');
var sourceMapSupport = require('source-map-support');
var convertSourceMap = require('convert-source-map');

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

var babelOpts = {
presets: [ require('babel-preset-es2015') ],
inputSourceMap: true, // load TS source maps
ast: false,
};

var tsLoader = null;

exports.registerBabel = registerBabel;
function registerBabel() {
overrideSourceMaps();

// In case ts-node has already run...
tsLoader = require.extensions['.ts'];
var tsLoader = wrap(require.extensions['.ts'], hook);

Object.defineProperty(require.extensions, '.ts', {
enumerable: true,

// In case ts-node hasn't run yet...
set: function (newTSLoader) {
tsLoader = newTSLoader;
tsLoader = wrap(newTSLoader, hook);
},
get: function () {
return loadPipeline;
return tsLoader;
},
});
}

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

function hook(base, m, filename) {
m._compile = wrap(m._compile, compile);
base(m, filename);
}

function compile(base, code, filename) {
var sourcemap = convertSourceMap.fromMapFileSource(code, '.').toObject();
convertSourceMap.removeMapFileComments(code);

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

// 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 overrideSourceMaps() {
sourceMapSupport.install({
handleUncaughtExceptions: false,
retrieveFile: function (filename) {
Expand All @@ -40,6 +65,7 @@ function registerBabel() {

if (!map) return null;


return {
url: null,
map: map,
Expand All @@ -50,36 +76,16 @@ function registerBabel() {
overrideRetrieveFile: true,
overrideRetrieveSourceMap: true,
});
}

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

function loadPipeline(m, filename) {
m._compile(compile(filename), filename);
// Prevent ts-node from adding its own lookups.
sourceMapSupport.install = function () { };
}

function compile(filename) {
var tsOutput = mockLoad(tsLoader, filename);
var babelOutput = babel.transform(tsOutput, babelOpts);

// 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 babelOutput.code;
}

function mockLoad(loader, filename) {
var content;
var module = {
_compile: function (_content) {
content = _content;
},
function wrap(base, fn) {
if (!(typeof base === 'function')) return base;
return function () {
var args = Array.prototype.slice.call(arguments, 0);
args.unshift(base);
return fn.apply(this, args);
};

loader(module, filename);
return content;
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"bluebird": "^3.4.0",
"code": "^4.0.0",
"concat-stream": "^1.5.1",
"convert-source-map": "^1.3.0",
"error-stack-parser": "^1.3.6",
"eslint": "^3.8.1",
"lab": "^11.1.0",
Expand Down

0 comments on commit 42ee8b9

Please sign in to comment.