Giter VIP home page Giter VIP logo

coffee-react-transform's Introduction

Coffeescript React JSX Transformer

Provides support for an equivalent of JSX syntax in Coffeescript (called CJSX) so you can write your Facebook React components with the full awesomeness of Coffeescript. Try it out.

Example

car-component.coffee

Car = React.createClass
  render: ->
    <Vehicle doors={4} locked={isLocked()} data-colour="red" on>
      <Parts.FrontSeat />
      <Parts.BackSeat />
      <p className="seat">Which seat can I take? {@props?.seat or 'none'}</p>
      {# also, this is a comment }
    </Vehicle>

transform

cjsx-transform car-component.coffee

output

Car = React.createClass
  render: ->
    React.createElement(Vehicle, {"doors": (4), "locked": (isLocked()), "data-colour": "red", "on": true},
      React.createElement(Parts.FrontSeat, null),
      React.createElement(Parts.BackSeat, null),
      React.createElement("p", {"className": "seat"}, "Which seat can I take? ", (@props?.seat or 'none'))
    )

Getting Started

coffee-react-transform simply handles preprocessing Coffeescript with JSX-style markup into valid Coffeescript. Instead of using it directly, you may want to make use of one of these more high-level tools:

CLI

cjsx-transform [input file]

Outputs Coffeescript code to stdout. Redirect it to a file or straight to the Coffeescript compiler, eg.

cjsx-transform examples/car.coffee | coffee -cs > car.js

API

transform = require 'coffee-react-transform'

transformed = transform('...some CJSX code...')

Installation

From npm:

npm install -g coffee-react-transform

Version compatibility

  • 3.x - React 0.13.x
  • 2.1.x - React 0.12.1
  • 2.x - React 0.12
  • 1.x - React 0.11.2
  • 0.x - React 0.11 and below

UMD bundle for the browser

If you want to use coffee-react-transform in the browser or under ExecJS or some other environment that doesn't support CommonJS modules, you can use this build provided by BrowserifyCDN, which will work as an AMD module or just a plain old script tag:

http://wzrd.in/standalone/coffee-react-transform

<script src="http://wzrd.in/standalone/coffee-react-transform"></script>
<script>
  coffeeReactTransform('-> <a />');
  // returns '-> React.createElement("a", null)'
</script>

Spread attributes

A semi-recent addition to JSX (and CJSX) is 'spread attributes' which allow merging an object of props into a component, eg:

extraProps = color: 'red', speed: 'fast'
<div color="blue" {... extraProps} />

which is transformed to:

extraProps = color: 'red', speed: 'fast'
React.createElement("div", React.__spread({"color": "blue"},  extraProps)

Tests

npm test or cake test or cake watch:test

Changelog

3.1.0

  • Fix literate CoffeeScript/CJSX with single apostrophes in Markdown (benjie)

3.0.1

  • Fixed some bugs relating to self-closing tags with spread attributes

3.0.0

  • Added CJSX single line comment syntax: {# comment goes here} (ConradIrwin)
  • All lower case tags now output component names as strings (eg. DOM or custom elements), and custom element names must contain a hyphen (AsaAyers)

2.4.1

  • Made spread attribute output not create unnecessary objects
  • Output legacy JSX pragma when legacy CJSX pragma used

2.2.0

  • Use React.__spread instead of Object.assign

Breaking Changes in 1.0

React 0.12 will introduce changes to the way component descriptors are constructed, where the return value of React.createClass is not a descriptor factory but simply the component class itself, and descriptors must be created manually using React.createElement or by wrapping the component class with React.createDescriptor. In preparation for this, coffee-react-transform now outputs calls to React.createElement to construct element descriptors from component classes for you, so you won't need to wrap your classes using React.createFactory. However, for this to work you will need to be using at least React 0.11.2, which adds React.createElement.

If you want the older style JSX output (which just desugars into function calls) then you need to use the 0.x branch, eg. 0.5.1.

coffee-react-transform's People

Contributors

alanhogan avatar asaayers avatar benjie avatar c58 avatar conradirwin avatar ghempton avatar jsdf avatar kyleamathews avatar riarheos avatar tgriesser avatar tomhicks-bsf avatar vonagam avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.