# JSTransform [![Build Status](https://travis-ci.org/facebook/jstransform.svg?branch=master)](https://travis-ci.org/facebook/jstransform)
A simple utility for pluggable JS syntax transforms using the esprima parser.
* Makes it simple to write and plug-in syntax transformations
* Makes it simple to coalesce multiple syntax transformations in a single pass of the AST
* Gives complete control over the formatting of the output on a per-transformation basis
* Supports source map generation
* Comes pre-bundled with a small set of (optional) ES6 -> ES5 transforms
- - -
**Note:**
If you're looking for a library for writing new greenfield JS transformations, consider looking at [Babel](https://github.com/babel/babel) or [Recast](https://github.com/benjamn/recast) instead of jstransform. We are still supporting jstransform (and intend to for a little while), but longer term we would like to direct efforts toward other open source projects that do a far better job of supporting a multi-pass JS transformation pipeline. This is important when attempting to apply many transformations to a source file. jstransform does a single pass resulting in performance benefits, but the tradeoff is that many transformations are much harder to write.
- - -
## Usage
### Advanced API
```js
var jstranform = require('jstransform')
```
This is the API that jstransform has always supported. It gives very fine control over the transforms you use and what order they are run in. It also allows the use of custom transforms.
#### `jstransform.transform(visitors, code, options={})`
**`visitors`**
Array of visitors. See [the React JSX visitors](https://github.com/facebook/jstransform/blob/master/visitors/react-jsx-visitors.js) as an example of what a visitor looks like.
**`code`**
String of code to be transformed.
**`options`**
Object with options that will be passed through to esprima and transforms.
#### `jstransform.Syntax`
This is the `Syntax` object re-exported from `esprima`. This is available because visitors will need access to this in order to effectively write transforms. By re-exporting we avoid the problem of conflicting versions of esprima being used.
### Simple API
```js
var simple = require('jstransform/simple')
```
The simple API was added to mirror the new command line interface. It works similarly to how `react-tools` worked - there is no need to know exactly which transforms to run. Instead transforms are selected automatically based on the options.
#### `simple.transform(code, options={})`
**`code`**
String of code to be transformed.
**`options`**
Object with options. Available options are:
option | values | default
-------|--------|--------
`react` | `true`: enable React transforms (JSX, displayName) | `false`
`es6` | `true`: enable ES6 transforms | `false`
`es7` | `true`: enable ES7 transforms | `false`
`harmony` | `true`: shortcut to enable ES6 & ES7 transforms | `false`
`utility` | `true`: enable utility transforms (trailing commas in objects, arrays) | `false`
`target` | `es3`: generate ES3 compatible code `es5`: generate ES5 compatible code | `es5`
`stripTypes` | `true`: strips out Flow type annotations | `false`
`sourceMap` | `true`: generate and return a Source Map | `false`
`sourceMapInline` | `true`: append inline source map at the end of the transformed source | `false`
`sourceFilename` | the output filename for the source map | `"source.js"`
`es6module` | `true`: parses the file as an ES6 module | `false`
`nonStrictEs6module` | `true`: parses the file as an ES6 module, except disables implicit strict-mode (i.e. CommonJS modules et al are allowed) | `false`
*Returns:* An Object with the following:
**`code`**: the transformed code
**`sourceMap`**: the source map object or `null`
#### `simple.transformFile(file, options={}, callback)`
**`file`**
String of path to a file to transform. Will be passed directly to `fs.readFile`.
**`options`**
See `transform` API.
**`callback`**
Function to call with the result, where `result` is return value of `transform`.
```js
callback(err, result)
```
#### `simple.transformFileSync(file, options={})`
The same as `transformFile` but the file is read synchronously.
### CLI
JSTransform now ships with a CLI. It was taken from the `react-tools` CLI so should be very familiar.
```sh
% jstransform --help
Usage: jstransform [options]