Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
This repository was archived by the owner on Jan 13, 2022. It is now read-only.

A simple utility for pluggable JS syntax transforms using the esprima parser.

License

NotificationsYou must be signed in to change notification settings

facebookarchive/jstransform

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.


Usage

Advanced API

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.

jstransform.transform(visitors, code, options={})

visitorsArray of visitors. Seethe React JSX visitors as an example of what a visitor looks like.

codeString of code to be transformed.

optionsObject with options that will be passed through to esprima and transforms.

jstransform.Syntax

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.

Simple API

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.

simple.transform(code, options={})

codeString of code to be transformed.

optionsObject with options. Available options are:

optionvaluesdefault
reacttrue: enable React transforms (JSX, displayName)false
es6true: enable ES6 transformsfalse
es7true: enable ES7 transformsfalse
harmonytrue: shortcut to enable ES6 & ES7 transformsfalse
utilitytrue: enable utility transforms (trailing commas in objects, arrays)false
targetes3: generate ES3 compatible code
es5: generate ES5 compatible code
es5
stripTypestrue: strips out Flow type annotationsfalse
sourceMaptrue: generate and return a Source Mapfalse
sourceMapInlinetrue: append inline source map at the end of the transformed sourcefalse
sourceFilenamethe output filename for the source map"source.js"
es6moduletrue: parses the file as an ES6 modulefalse
nonStrictEs6moduletrue: 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

simple.transformFile(file, options={}, callback)

fileString of path to a file to transform. Will be passed directly tofs.readFile.

optionsSeetransform API.

callbackFunction to call with the result, whereresult is return value oftransform.

callback(err,result)

simple.transformFileSync(file, options={})

The same astransformFile but the file is read synchronously.

CLI

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

Examples

Advanced API

Using a pre-bundled or existing transform:

/** * 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);

Using multiple pre-bundled or existing transforms at once:

/** * 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);

Writing a simple custom transform:

/** * 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);

Simple API

Reading a file and applying tranforms

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);

Migration Guide

Simple API

If you are coming fromreact-tools and using Node, the APIs are very similar. There are a couple important differences.

  1. JSX will not be tranformed by default! You must specifyreact: true in the options.
  2. The return value oftransform 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.

CLI

These are virtually identical but again, there are some important difference.

  1. JSX will not be transformed by default! You must specify--react.
  2. We are using a different executable -jstransform - (jsx doesn't make sense here).
  3. 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.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp