Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: TypeStrong/ts-node
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.5.2
Choose a base ref
...
head repository: TypeStrong/ts-node
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v1.6.0
Choose a head ref
  • 2 commits
  • 4 files changed
  • 1 contributor

Commits on Oct 16, 2016

  1. Fix handling of ignore CLI input

    Also reverting the behaviour introduced to avoid compiling unknown TypeScript extensions. Added a documentation section on how `ts-node` and `node` works instead.
    blakeembrey committed Oct 16, 2016
    Copy the full SHA
    297c67f View commit details
  2. v1.6.0

    blakeembrey committed Oct 16, 2016
    Copy the full SHA
    5831ef4 View commit details
Showing with 22 additions and 19 deletions.
  1. +12 −5 README.md
  2. +1 −1 package.json
  3. +2 −2 src/_bin.ts
  4. +7 −11 src/index.ts
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ npm install -g typescript
* Interactive REPL
* Execute (and print) TypeScript through the CLI
* Uses source maps
* Loads from `tsconfig.json`
* Loads compiler options and `.d.ts` files from `tsconfig.json`

## Usage

@@ -64,20 +64,27 @@ ts-node node_modules/tape/bin/tape [...args]
gulp
```

### Loading `tsconfig.json`
## How It Works

**TypeScript Node** works by registering the TypeScript compiler for the `.ts`, `.tsx` and - when `allowJs` is enabled - `.js` extensions. When node.js has a file extension registered (the `require.extensions` object), it will use the extension internally with module resolution. By default, when an extension is unknown to node.js, it will fallback to handling the file as `.js` (JavaScript).

**P.S.** This means that if you don't register an extension, it'll be compiled as JavaScript. When `ts-node` is used with `allowJs`, JavaScript files are transpiled using the TypeScript compiler.

## Loading `tsconfig.json`

**Typescript Node** uses `tsconfig.json` automatically, use `-n` to skip loading `tsconfig.json`.

### Configuration Options
## Configuration Options

You can set options by passing them in before the script.

```sh
ts-node --compiler ntypescript --project src --ignoreWarnings 2304 hello-world.ts
```

* **--project, -P** Path to resolve `tsconfig.json` from (or `false`) (also `process.env.TS_NODE_PROJECT`)
* **--project, -P** Path to resolve `tsconfig.json` from (or `false` to disable) (also `process.env.TS_NODE_PROJECT`)
* **--compiler, -C** Use a custom, require-able TypeScript compiler compatible with `typescript@>=1.5.0-alpha` (also `process.env.TS_NODE_COMPILER`)
* **--ignore** Specify an array of regular expression strings for `ts-node` to skip compiling as TypeScript (defaults to `/node_modules/`, `false` to disable) (also `process.env.TS_NODE_IGNORE`)
* **--ignoreWarnings, -I** Set an array of TypeScript diagnostic codes to ignore (also `process.env.TS_NODE_IGNORE_WARNINGS`)
* **--disableWarnings, -D** Ignore all TypeScript errors (also `process.env.TS_NODE_DISABLE_WARNINGS`)
* **--compilerOptions, -O** Set compiler options using JSON (E.g. `--compilerOptions '{"target":"es6"}'`) (also `process.env.TS_NODE_COMPILER_OPTIONS`)
@@ -86,7 +93,7 @@ ts-node --compiler ntypescript --project src --ignoreWarnings 2304 hello-world.t
* **--no-cache** Skip hitting the compiled JavaScript cache (also `process.env.TS_NODE_CACHE`)
* **--cache-directory** Configure the TypeScript cache directory (also `process.env.TS_NODE_CACHE_DIRECTORY`)

### Programmatic Usage
## Programmatic Usage

```js
require('ts-node').register({ /* options */ })
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-node",
"version": "1.5.2",
"version": "1.6.0",
"preferGlobal": true,
"description": "TypeScript execution environment and REPL for node",
"main": "dist/index.js",
4 changes: 2 additions & 2 deletions src/_bin.ts
Original file line number Diff line number Diff line change
@@ -151,8 +151,8 @@ const service = register({
cacheDirectory: argv.cacheDirectory,
compiler: argv.compiler,
project: argv.project,
ignore: typeof argv.ignore === 'boolean' ? argv.ignore : arrify(argv.ignore),
ignoreWarnings: arrify(argv.ignoreWarnings),
ignore: argv.ignore,
ignoreWarnings: argv.ignoreWarnings,
disableWarnings: argv.disableWarnings,
compilerOptions: parse(argv.compilerOptions),
getFile: isEval ? getFileEval : getFile,
18 changes: 7 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ import extend = require('xtend')
import mkdirp = require('mkdirp')
import crypto = require('crypto')
import yn = require('yn')
import arrify = require('arrify')
import { BaseError } from 'make-error'
import * as TS from 'typescript'
import { loadSync } from 'tsconfig'
@@ -63,8 +64,8 @@ export interface Options {
cacheDirectory?: string
compiler?: string
project?: boolean | string
ignore?: boolean | string[]
ignoreWarnings?: Array<number | string>
ignore?: boolean | string | string[]
ignoreWarnings?: number | string | Array<number | string>
disableWarnings?: boolean | null
getFile?: (fileName: string) => string
fileExists?: (fileName: string) => boolean
@@ -109,7 +110,7 @@ const DEFAULTS = {
* Split a string array of values.
*/
export function split (value: string | undefined) {
return value ? value.split(/ *, */g) : []
return value ? value.split(/ *, */g) : undefined
}

/**
@@ -138,7 +139,7 @@ export interface Register {
*/
export function register (options: Options = {}): () => Register {
const compiler = options.compiler || 'typescript'
const ignoreWarnings = (options.ignoreWarnings || DEFAULTS.ignoreWarnings).map(Number)
const ignoreWarnings = arrify(options.ignoreWarnings || DEFAULTS.ignoreWarnings || []).map(Number)
const disableWarnings = !!(options.disableWarnings == null ? DEFAULTS.disableWarnings : options.disableWarnings)
const getFile = options.getFile || DEFAULTS.getFile
const fileExists = options.fileExists || DEFAULTS.fileExists
@@ -150,7 +151,7 @@ export function register (options: Options = {}): () => Register {
const originalJsHandler = require.extensions['.js']
let result: Register

const ignore = (
const ignore = arrify(
(
typeof options.ignore === 'boolean' ?
(options.ignore === false ? [] : undefined) :
@@ -376,13 +377,8 @@ export function register (options: Options = {}): () => Register {
*/
function shouldIgnore (filename: string, ignore: RegExp[], service: () => Register) {
const relname = slash(filename)
const extension = extname(filename)

if (!extension || service().extensions.indexOf(extension) > -1) {
return ignore.some(x => x.test(relname))
}

return false
return ignore.some(x => x.test(relname))
}

/**