GithubHelp home page GithubHelp logo

guilhermeccrz / jsx-requirejs-plugin Goto Github PK

View Code? Open in Web Editor NEW

This project forked from felipecrv/jsx-requirejs-plugin

0.0 2.0 0.0 2.89 MB

A RequireJS plugin for loading and compiling (with r.js) JavaScript files containing JSX.

License: MIT License

JavaScript 99.94% HTML 0.06%

jsx-requirejs-plugin's Introduction

jsx-requirejs-plugin

A RequireJS plugin for JavaScript files containing JSX. It's r.js friendly (i.e. all files containing JSX will be pre-compiled during the r.js build).

This is helpful when using React with RequireJS.

Install

Download the plugin jsx and the modified JSXTransformer.

Place this in the directory that is your baseUrl for your project, or set up a paths config for it for the module ID jsx. This plugin depends on the RequireJS text plugin to avoid reimplementation of loading logic, so it should be installed as well.

Usage

First, you need to configure RequireJS to use Facebook's JSXTransformer and React:

    require.config({
      // ...

      paths: {
        "react": "react-with-addons",
        "JSXTransformer": "JSXTransformer"
      }

      // ...
    });

Then, you can reference JSX files via the jsx! plugin syntax. For example, to load the Timer.jsx file that is in a components directory:

    require(['jsx!components/Timer'], function (Timer) {

    });

The Plugin is then going to load the JavaScript source file components/Timer.jsx, parse it with Facebook's JSXTransformer and execute the resulting JavaScript source.

Configuration options

To load files containing JSX code with a specific extension (components/Timer.jsx) add the following parameter to the RequireJS config object:

    require.config({
      // ...

      jsx: {
        fileExtension: '.jsx'
      }

      // ...
    });

The .jsx extension is used by default. r.js will get confused and fail to compile your JSX code if you use .js.

You can use the ES6 features supported by JSXTransformer by using the harmony option:

    jsx: {
      harmony: true
    }

You can pass the stripTypes flag along for Flow type annotations:

    jsx: {
      stripTypes: true
    }

Build

If you are using r.js to optimize your application for production, add text, jsx and JSXTransformer to the stubModules array field of the build.js.

    stubModules: ['jsx', 'text', 'JSXTransformer'],

    modules: [
      {
        name: "main"
      }
    ]

After successful optimization, all the jsx! files will be precompiled (converted from JSX to JS) and will be added/optimized into the build file.

HACK to fix an issue with the preprocessing of JSXTransformer

r.js strips out all occurrences of the 'use strict' string literal causing script errors in resulting files. A simple solution to this is replacing occurrences of 'use strict' by an expression like 'use ' + 'strict'.

You don't have to do it if you use the JSXTransformer.js provided here.

jsx-requirejs-plugin's People

Contributors

alirussell avatar aronwoost avatar byelims avatar christianalfoni avatar felipecrv avatar fizker avatar hardchor avatar iamnoah avatar pirelenito avatar pixeldrew avatar romanmatiasko avatar skyhaikuteam avatar stevoland avatar tsjoberg avatar victor-homyakov avatar wmira 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.