- Notifications
You must be signed in to change notification settings - Fork66
A simple utility for pluggable JS syntax transforms using the esprima parser.
License
facebookarchive/jstransform
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This project is not actively maintained. Proceed at your own risk!
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 atBabel orRecast 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.
varjstranform=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.
visitors
Array of visitors. Seethe React JSX visitors 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.
This is theSyntax
object re-exported fromesprima
. 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.
varsimple=require('jstransform/simple')
The simple API was added to mirror the new command line interface. It works similarly to howreact-tools
worked - there is no need to know exactly which transforms to run. Instead transforms are selected automatically based on the 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 codees5 : 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 ornull
file
String of path to a file to transform. Will be passed directly tofs.readFile
.
options
Seetransform
API.
callback
Function to call with the result, whereresult
is return value oftransform
.
callback(err,result)
The same astransformFile
but the file is read synchronously.
JSTransform now ships with a CLI. It was taken from thereact-tools
CLI so should be very familiar.
% jstransform --help Usage: jstransform [options]<source directory><output directory> [<module ID> [<module ID> ...]] Options:-h, --help output usage information -V, --version output the version number-c, --config [file] JSON configuration file (no file or - means STDIN)-w, --watch Continually rebuild-x, --extension<js| coffee| ...> File extension to assume when resolving module identifiers --relativize Rewrite all module identifiers to be relative --follow-requires Scan modulesfor required dependencies --use-provides-module Respect @providesModules pragmain files --cache-dir<directory> Alternate directory to usefor disk cache --no-cache-dir Disable the disk cache --source-charset<utf8| win1252| ...> Charset ofsource (default: utf8) --output-charset<utf8| win1252| ...> Charset of output (default: utf8) --react Turns on the React JSX and React displayName transforms --es6 Turns on available ES6 transforms --es7 Turns on available ES7 transforms --harmony Shorthand toenable all ES6 and ES7 transforms --utility Turns on available utility transforms --target [version] Specify your target version of ECMAScript. Valid values are"es3" and"es5". The default is"es5"."es3" will avoid uses of defineProperty and will quote reserved words. WARNING:"es5" is not properly supported, even with the use of es5shim, es5sham. If you need to support IE8, use"es3". --strip-types Strips outtype annotations. --es6module Parses the file as a valid ES6 module. (Note that this means implicit strict mode) --non-strict-es6module Parses the file as an ES6 module, except disables implicit strict-mode. (This is usefulif you're porting non-ES6 modules to ES6, but haven't yet verified that they are strict-mode safe yet) --source-map-inline Embed inline sourcemapin transformedsource --source-filename Filename to use when generating the inline sourcemap. Will default to filename when processing files
/** * Reads a source file that may (or may not) contain ES6 classes, transforms it * to ES5 compatible code using the pre-bundled ES6 class visitors, and prints * out the result. */vares6ClassVisitors=require('jstransform/visitors/es6-class-visitors').visitorList;varfs=require('fs');varjstransform=require('jstransform');varoriginalFileContents=fs.readFileSync('path/to/original/file.js','utf-8');vartransformedFileData=jstransform.transform(es6ClassVisitors,originalFileContents);console.log(transformedFileData.code);
/** * Reads a source file that may (or may not) contain ES6 classes *or* arrow * functions, transforms them to ES5 compatible code using the pre-bundled ES6 * visitors, and prints out the result. */vares6ArrowFuncVisitors=require('jstransform/visitors/es6-arrow-function-visitors').visitorList;vares6ClassVisitors=require('jstransform/visitors/es6-class-visitors').visitorList;varjstransform=require('jstransform');// Normally you'd read this from the filesystem, but I'll just use a string here// to simplify the example.varoriginalFileContents="var a = (param1) => param1; class FooClass {}";vartransformedFileData=jstransform.transform(es6ClassVisitors.concat(es6ArrowFuncVisitors),originalFileContents);// var a = function(param1) {return param1;}; function FooClass(){"use strict";}console.log(transformedFileData.code);
/** * Creates a custom transformation visitor that prefixes all calls to the * `eval()` function with a call to `alert()` saying how much of a clown you are * for using eval. */varjstransform=require('jstransform');varutils=require('jstransform/src/utils');varSyntax=jstransform.Syntax;functionvisitEvalCallExpressions(traverse,node,path,state){// Appends an alert() call to the output buffer *before* the visited node// (in this case the eval call) is appended to the output bufferutils.append('alert("...eval?...really?...");',state);// Now we copy the eval expression to the output buffer from the original// sourceutils.catchup(node.range[1],state);}visitEvalCallExpressions.test=function(node,path,state){returnnode.type===Syntax.CallExpression&&node.callee.type===Syntax.Identifier&&node.callee.name==='eval';};// Normally you'd read this from the filesystem, but I'll just use a string here// to simplify the example.varoriginalFileContents="eval('foo');";vartransformedFileData=jstransform.transform([visitEvalCallExpressions],// Multiple visitors may be applied at once, so an// array is always expected for the first argumentoriginalFileContents);// alert("...eval?...really?...");eval('foo');console.log(transformedFileData.code);
varsimple=require('jstransform/simple');varfs=require('fs');varoriginalCode=fs.readFileSync('path/to/file.js');// Apply all available ES6 transformsvartransformed=simple.transform(originalCode,{es6:true});console.log(transformed.code);// Apply ES6 and ES7, generating ES3 compatible code (for IE8)transformed=simple.transform(originalCode,{harmony:true,target:'es3'});console.log(transformed.code);
If you are coming fromreact-tools
and using Node, the APIs are very similar. There are a couple important differences.
- JSX will not be tranformed by default! You must specify
react: true
in the options. - The return value of
transform
is not the same.react-tools.transform
only returned the resulting code.simple.transform
always returns and object with acode
property. However, if you were usingreact-tools.transformWithDetails
,simple.transform
is essentially the same.
These are virtually identical but again, there are some important difference.
- JSX will not be transformed by default! You must specify
--react
. - We are using a different executable -
jstransform
- (jsx
doesn't make sense here). - There are a numbr of new options available. Namely
--utility
,--es6
and--es7
(--harmony
is still available and will enable both).
About
A simple utility for pluggable JS syntax transforms using the esprima parser.