Skip to content

Commit

Permalink
Add transpileOnly flag, default to typeCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Apr 17, 2018
1 parent f2ede03 commit aa9052d
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 23 deletions.
18 changes: 7 additions & 11 deletions README.md
Expand Up @@ -11,8 +11,6 @@

```sh
npm install -g ts-node

# Install a TypeScript compiler (requires `typescript` by default).
npm install -g typescript
```

Expand All @@ -24,15 +22,13 @@ npm install -g typescript
* Source map support
* Loads compiler options from `tsconfig.json`

**Important:** The default mode of `ts-node` is transpile _without_ type checking. This can cause problems where type information is required to generate a valid JavaScript program. Two known examples of this are `const enum` and import elision of type files used in valid runtime positions. If you require these features, ensure you enable type checking.

## Usage

```sh
# Execute a script as you would normally with `node`.
# Execute a script as `node` + `tsc`.
ts-node script.ts

# Starts the TypeScript REPL.
# Starts a TypeScript REPL.
ts-node

# Execute code with TypeScript.
Expand All @@ -49,7 +45,7 @@ echo "console.log('Hello, world!')" | ts-node

### Programmatic

You can require `ts-node` and register the loader for future requires by using `require('ts-node').register({ /* options */ })`. You can also use the shortcuts `node -r ts-node/register` or `node -r ts-node/register/type-check` depending on your preferences.
You can require `ts-node` and register the loader for future requires by using `require('ts-node').register({ /* options */ })`. You can also use file shortcuts - `node -r ts-node/register` or `node -r ts-node/register/transpile-only` - depending on your preferences.

**Note:** If you need to use advanced node.js CLI arguments (e.g. `--inspect`), use them with `node -r ts-node/register` instead of the `ts-node` CLI.

Expand Down Expand Up @@ -95,7 +91,7 @@ Create a new node.js configuration, add `-r ts-node/register` to node args and m

## How It Works

**TypeScript Node** works by registering the TypeScript compiler for the `.ts`, `.tsx` and, with `allowJs` enabled, `.js` extensions. When node.js has a file extension registered (the `require.extensions` object), it will use the extension internally for module resolution. When an extension is unknown to node.js, it will handle the file as `.js` (JavaScript).
**TypeScript Node** works by registering the TypeScript compiler for `.tsx?` and `.jsx?` extension (when `allowJs == true`). When node.js has an extension registered (via `require.extensions`), it will use the extension internally for module resolution. When an extension is unknown to node.js, it handles the file as `.js` (JavaScript).

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

Expand Down Expand Up @@ -124,7 +120,7 @@ Supports `--print`, `--eval` and `--require` from [node.js CLI options](https://

_Environment variable denoted in parentheses._

* `--typeCheck` Enable type checking for TypeScript (`TS_NODE_TYPE_CHECK`)
* `--transpileOnly` Use TypeScript's faster `transpileModule` (`TS_NODE_TRANSPILE_ONLY`)
* `--cacheDirectory` Configure the output file cache directory (`TS_NODE_CACHE_DIRECTORY`)
* `-I, --ignore [pattern]` Override the path patterns to skip compilation (`TS_NODE_IGNORE`)
* `-P, --project [path]` Path to TypeScript JSON project file (`TS_NODE_PROJECT`)
Expand All @@ -143,9 +139,9 @@ _Environment variable denoted in parentheses._

## Watching and Restarting

**TypeScript Node** only compiles source code on the fly, watching files and code reloads are out of scope. If you want to restart the `ts-node` process on file changes, standard node.js tools exist already such as [nodemon](https://github.com/remy/nodemon), [onchange](https://github.com/Qard/onchange) and [node-dev](https://github.com/fgnass/node-dev).
**TypeScript Node** compiles source code via `require()`, watching files and code reloads are out of scope for the project. If you want to restart the `ts-node` process on file change, existing node.js tools such as [nodemon](https://github.com/remy/nodemon), [onchange](https://github.com/Qard/onchange) and [node-dev](https://github.com/fgnass/node-dev) work.

There is also [`ts-node-dev`](https://github.com/whitecolor/ts-node-dev), a modified version of [`node-dev`](https://github.com/fgnass/node-dev) that uses `ts-node` for compilation and doesn't restart the process on every change.
There's also [`ts-node-dev`](https://github.com/whitecolor/ts-node-dev), a modified version of [`node-dev`](https://github.com/fgnass/node-dev) using `ts-node` for compilation and won't restart the process on file change.

## License

Expand Down
3 changes: 3 additions & 0 deletions register/transpile-only.js
@@ -0,0 +1,3 @@
require('../').register({
transpileOnly: true
})
12 changes: 8 additions & 4 deletions src/bin.ts
Expand Up @@ -22,6 +22,7 @@ interface Argv {
version?: boolean
// Register options.
typeCheck?: boolean
transpileOnly?: boolean
cache?: boolean
cacheDirectory?: string
compiler?: string
Expand All @@ -37,14 +38,15 @@ interface Argv {
const argv = minimist<Argv>(process.argv.slice(2), {
stopEarly: true,
string: ['eval', 'print', 'compiler', 'project', 'ignoreDiagnostics', 'require', 'cacheDirectory', 'ignore'],
boolean: ['help', 'typeCheck', 'version', 'cache', 'skipProject', 'skipIgnore'],
boolean: ['help', 'transpileOnly', 'typeCheck', 'version', 'cache', 'skipProject', 'skipIgnore'],
alias: {
eval: ['e'],
print: ['p'],
require: ['r'],
help: ['h'],
version: ['v'],
typeCheck: ['type-check'],
transpileOnly: ['transpile-only'],
cacheDirectory: ['cache-directory'],
ignore: ['I'],
project: ['P'],
Expand All @@ -57,6 +59,7 @@ const argv = minimist<Argv>(process.argv.slice(2), {
default: {
cache: DEFAULTS.cache,
typeCheck: DEFAULTS.typeCheck,
transpileOnly: DEFAULTS.transpileOnly,
skipIgnore: DEFAULTS.skipIgnore,
skipProject: DEFAULTS.skipProject
}
Expand All @@ -75,7 +78,7 @@ Options:
-h, --help Print CLI usage
-v, --version Print module version information
--type-check Enable type checking through CLI
-T, --transpile-only Use TypeScript's faster \`transpileModule\`
--cache-directory Configure the output file cache directory
-I, --ignore [pattern] Override the path patterns to skip compilation
-P, --project [path] Path to TypeScript JSON project file
Expand All @@ -84,8 +87,8 @@ Options:
-O, --compilerOptions [opts] JSON object to merge with compiler options
--no-cache Disable the local TypeScript Node cache
--skip-project Skip project config resolution and loading
--skip-ignore Skip ignore checks
--skip-project Skip reading \`tsconfig.json\`
--skip-ignore Skip \`--ignore\` checks
`)

process.exit(0)
Expand All @@ -99,6 +102,7 @@ const isPrinted = argv.print !== undefined
// Register the TypeScript compiler instance.
const service = register({
typeCheck: argv.typeCheck,
transpileOnly: argv.transpileOnly,
cache: argv.cache,
cacheDirectory: argv.cacheDirectory,
ignore: argv.ignore,
Expand Down
12 changes: 6 additions & 6 deletions src/index.spec.ts
Expand Up @@ -107,7 +107,7 @@ describe('ts-node', function () {
})

it('should throw errors', function (done) {
exec(`${BIN_EXEC} --typeCheck -e "import * as m from './tests/module';console.log(m.example(123))"`, function (err) {
exec(`${BIN_EXEC} -e "import * as m from './tests/module';console.log(m.example(123))"`, function (err) {
if (err === null) {
return done('Command was expected to fail, but it succeeded.')
}
Expand All @@ -124,7 +124,7 @@ describe('ts-node', function () {

it('should be able to ignore diagnostic', function (done) {
exec(
`${BIN_EXEC} --type-check --ignoreDiagnostics 2345 -e "import * as m from './tests/module';console.log(m.example(123))"`,
`${BIN_EXEC} --ignoreDiagnostics 2345 -e "import * as m from './tests/module';console.log(m.example(123))"`,
function (err) {
if (err === null) {
return done('Command was expected to fail, but it succeeded.')
Expand All @@ -140,7 +140,7 @@ describe('ts-node', function () {
})

it('should work with source maps', function (done) {
exec(`${BIN_EXEC} --type-check tests/throw`, function (err) {
exec(`${BIN_EXEC} tests/throw`, function (err) {
if (err === null) {
return done('Command was expected to fail, but it succeeded.')
}
Expand All @@ -157,7 +157,7 @@ describe('ts-node', function () {
})

it.skip('eval should work with source maps', function (done) {
exec(`${BIN_EXEC} --type-check -p "import './tests/throw'"`, function (err) {
exec(`${BIN_EXEC} -p "import './tests/throw'"`, function (err) {
if (err === null) {
return done('Command was expected to fail, but it succeeded.')
}
Expand All @@ -172,8 +172,8 @@ describe('ts-node', function () {
})
})

it('should use transpile mode by default', function (done) {
exec(`${BIN_EXEC} -p "x"`, function (err) {
it('should support transpile only mode', function (done) {
exec(`${BIN_EXEC} --transpileOnly -p "x"`, function (err) {
if (err === null) {
return done('Command was expected to fail, but it succeeded.')
}
Expand Down
6 changes: 4 additions & 2 deletions src/index.ts
Expand Up @@ -52,6 +52,7 @@ export const VERSION = pkg.version
*/
export interface Options {
typeCheck?: boolean | null
transpileOnly?: boolean | null
cache?: boolean | null
cacheDirectory?: string
compiler?: string
Expand Down Expand Up @@ -96,7 +97,8 @@ export const DEFAULTS: Options = {
skipIgnore: yn(process.env['TS_NODE_SKIP_IGNORE']),
skipProject: yn(process.env['TS_NODE_SKIP_PROJECT']),
ignoreDiagnostics: split(process.env['TS_NODE_IGNORE_DIAGNOSTICS']),
typeCheck: yn(process.env['TS_NODE_TYPE_CHECK'])
typeCheck: yn(process.env['TS_NODE_TYPE_CHECK']),
transpileOnly: yn(process.env['TS_NODE_TRANSPILE_ONLY'])
}

/**
Expand Down Expand Up @@ -201,7 +203,7 @@ export function register (opts: Options = {}): Register {
// Require the TypeScript compiler and configuration.
const cwd = process.cwd()
const compiler = options.compiler || 'typescript'
const typeCheck = options.typeCheck || false
const typeCheck = options.typeCheck === true || options.transpileOnly !== true
const ts: typeof TS = require(compiler)
const transformers = options.transformers || undefined
const readFile = options.readFile || ts.sys.readFile
Expand Down

0 comments on commit aa9052d

Please sign in to comment.