Skip to content

Commit

Permalink
Add initial working version
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmoore committed Apr 11, 2016
0 parents commit b9af178
Show file tree
Hide file tree
Showing 20 changed files with 697 additions and 0 deletions.
191 changes: 191 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
---
env:
node: 2
rules:
strict: 2
eqeqeq:
- 2
- "allow-null"
guard-for-in: 2
no-extend-native: 2
comma-dangle:
- 2
- "always-multiline"
no-use-before-define:
- 2
- "nofunc"
no-constant-condition: 2
no-debugger: 2
no-dupe-args: 2
no-dupe-keys: 2
no-duplicate-case: 2
no-empty-character-class: 2
no-empty: 2
no-ex-assign: 2
no-extra-boolean-cast: 2
no-extra-parens: 2
no-extra-semi: 2
no-func-assign: 2
no-inner-declarations:
- 2
- "functions"
no-invalid-regexp: 2
no-irregular-whitespace: 2
no-negated-in-lhs: 2
no-obj-calls: 2
no-sparse-arrays: 2
no-unreachable: 2
use-isnan: 2
valid-typeof: 2
complexity:
- 2
- 10
consistent-return: 2
no-with: 2
radix: 2
yoda:
- 2
- "never"
no-unused-vars: 2
no-undef: 2
max-depth:
- 2
- 5
max-len:
- 2
- 120
- 2
-
ignoreComments: true
ignoreUrls: true
max-params:
- 2
- 5
no-caller: 2
no-sequences: 2
no-throw-literal: 2
new-cap:
- 2
no-this-before-super: 2
no-new-object: 2
dot-location:
- 2
- "property"
dot-notation: 2
no-return-assign: 2
no-useless-call: 2
no-unused-expressions: 2
no-self-compare: 2
wrap-iife:
- 2
- "inside"
no-alert: 2
no-fallthrough: 2
eol-last: 2
quotes:
- 2
- "single"
semi:
- 2
- "always"
array-bracket-spacing:
- 2
- "always"
-
singleValue: true
objectsInArrays: true
arraysInArrays: true
brace-style:
- 2
- "1tbs"
-
allowSingleLine: true
camelcase:
- 2
-
properties: "always"
comma-spacing:
- 2
-
before: false
after: true
comma-style:
- 2
- "last"
computed-property-spacing:
- 2
- "never"
func-style:
- 2
- "declaration"
indent:
- 2
- 2
-
SwitchCase: 1
key-spacing:
- 2
-
beforeColon: false
afterColon: true
new-parens: 2
no-lonely-if: 2
no-multiple-empty-lines:
- 2
-
max: 2
no-mixed-spaces-and-tabs: 2
no-nested-ternary: 2
no-spaced-func: 2
no-trailing-spaces: 2
no-unneeded-ternary: 2
object-curly-spacing:
- 2
- "always"
operator-assignment:
- 2
- "always"
operator-linebreak:
- 2
- "before"
padded-blocks:
- 2
- "never"
quote-props:
- 2
- "as-needed"
semi-spacing:
- 2
-
before: false
after: true
space-before-blocks:
- 2
- "always"
space-before-function-paren:
- 2
-
anonymous: "always"
named: "never"
space-in-parens:
- 2
- "never"
space-infix-ops: 2
space-unary-ops:
- 2
-
words: true
nonwords: false
spaced-comment:
- 2
- "always"
-
markers:
- "*"
block-spacing:
- 2
- "always"
keyword-spacing:
- 2
- before: true
after: true
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.sublime-project
*.sublime-workspace
npm-debug.log
.DS_Store
node_modules
13 changes: 13 additions & 0 deletions .sublimelinterrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"linters": {
"typescript": {
"excludes": [
"node_modules/**",
"dist/**"
]
},
"eslint": {

}
}
}
7 changes: 7 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright (c) 2016 Daniel Moore

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
86 changes: 86 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# TypeScript-Babel Node

This package enables Babel compilation of TypeScript compilation output through a registration function and a Node binary proxy.

### Why do I want this?

Because you want `ts-node` to run `async`/`await` code, but TypeScript will only compile `async`/`await` to ES6 and Node 5.x doesn't support all of ES6 yet. So you need Babel to bridge that gap.

`ts-babel-node` wraps `ts-node` so you can do just that. Run the `ts-babel-node` executable exactly the same way you'd run `ts-node` and require `ts-babel-node/register` instead of `ts-node/register`.

## Installation

### Command Line

To use `ts-babel-node` on the command line, install this package globally. Be sure to include whichever version of TypeScript you want to compile against.

```
$ npm install --global ts-babel-node [email protected]
$ ts-babel-node my-file.ts
```

### Library

To include `ts-babel-node` as a register function, install this package as a development dependency. Be sure to include whichever version of TypeScript you want to compile against.

```
$ npm install --save-dev ts-babel-node [email protected]
```

## Usage

### Command Line

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.

### 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 */);
// Or
require('ts-babel-node/register');
```

You can also use this with the `--require ` option on `node`.

```
$ node --require ts-babel-node/register my-file.ts
```

The second API only adds the babel-compilation step. This is useful if your code is run from `ts-node`, as is the case in the [gulp](#gulp) scenario.

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

require('ts-babel-node').registerBabel();
// Or
require('ts-babel-node/register-babel');
```

### Mocha

```
$ mocha --require ts-babel-node/register [...args]
```

### Tape

```
$ ts-babel-node node_modules/.bin/tape [...args]
```

### Gulp

In your `gulpfile.ts` (note, `.ts`, not `.js`):

```
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.
5 changes: 5 additions & 0 deletions bin/ts-babel-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#! /usr/bin/env node
'use strict';

require('..').registerBabel();
require('ts-node/dist/bin/ts-node');
85 changes: 85 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
'use strict';

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

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() {
// In case ts-node has already run...
tsLoader = require.extensions['.ts'];

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

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

sourceMapSupport.install({
handleUncaughtExceptions: false,
retrieveFile: function (filename) {
return outputs && outputs[filename] && outputs[filename].code;
},
retrieveSourceMap: function (filename) {
var map = outputs && outputs[filename] && outputs[filename].map;

if (!map) return null;

return {
url: null,
map: map,
};
},

// In case ts-node has already run...
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);
}

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;
},
};

loader(module, filename);
return content;
}
Loading

0 comments on commit b9af178

Please sign in to comment.