/ts-jsx-loader

Webpack loader for selectively transforming JSX based on special syntax. Most useful for embedding JSX in TypeScript.

Primary LanguageJavaScriptMIT LicenseMIT

ts-jsx-loader

Webpack loader for transforming JSX based on special syntax. Meant to be used with a TypeScript loader to allow using JSX with TypeScript.

Important Deprecation Note

Since native JSX support is coming to TypeScript 1.6, I consider this project to be complete. No new features will be added. However, bugs will still be fixed prior to the release of TypeScript 1.6.

Installation

npm install ts-jsx-loader

Usage

The loader chain should go from ts-jsx-loader into a TypeScript loader. Your configuration will look something like this:

module.exports = {
    entry: './app.ts',
    output: {
        filename: 'bundle.js'
    },
    resolve: {
        extensions: ['', '.webpack.js', '.web.js', '.js', '.ts']
    },
    module: {
        loaders: [
            { test: /\.ts$/, loader: 'ts-loader!ts-jsx-loader' }
        ]
    }
}

ts-jsx-loader defines a fake API on React called React.jsx(). You should reference the included react-jsx.d.ts or react-addons-jsx.d.ts definition file for IDE support. This API accepts either a string or nothing. You can then create JSX as a template string or within multiline comments.

///<reference path="path/to/react-jsx.d.ts"/>

import React = require('react');

var message = 'Hello world'

React.render(React.jsx(/*
    <div>
        <span>{message}</span>
    </div>
*/), document.body)

// or if you're using TypeScript 1.4 or above with template strings, with or without embedded expressions

React.render(React.jsx(`
    <div>
        <span>${message}</span>
    </div>
`), document.body)

React.render(React.jsx(`
    <div>
        <span>{message}</span>
    </div>
`), document.body)

// or if you simply want to use JSX without making it valid TypeScript

React.render(
    /*jsx*/
    <div>
        <span>{message}</span>
    </div>
    /*jsx*/
, document.body)

The loader will find occurrences of React.jsx() or /*jsx*/ and transform them into React.createElement() calls prior to being passed to the TypeScript loader.

Options

Specify options to the loader via query string:

    ...
    module: {
        loaders: [
            { test: /\.ts$/, loader: 'ts-loader!ts-jsx-loader?target=es3&identifier=react.jsx' }
        ]
    }
    ...

harmony (boolean) (default=true)

Allows the use of ES6 features within JSX.

  • es3
  • es5 (default)

identifier (string) (default='React.jsx')

Change the identifier to something other than React.jsx. For example, you could import React with a lower-case r like so:

import react = require('react')

react.jsx(`<div />`)

target (string)

Specify the output target. See this for more information.

  • es3
  • es5 (default)

License

The MIT License (MIT)

Copyright (c) 2015 James Brantly

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.