New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

requirejs-react-jsx

Package Overview
Dependencies
Maintainers
2
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

requirejs-react-jsx

A RequireJS plugin for loading jsx in require.js and r.js

  • 0.16.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
161
decreased by-11.54%
Maintainers
2
Weekly downloads
 
Created
Source

requirejs-react-jsx

NPM version Dependency Status

A RequireJS plugin for compiling React JSX files. Will use react-tools when compiling using r.js, and will use JSXTransformer or Babel when running in the browser in development. This allows us to support multiple bundles in r.js and exclude the JSXTransformer from all of them since we're requiring it dynamically and not explicitly. This also means that we can get 1:1 Source Maps in both development and production.

Example

http://i.imgur.com/upv8B0g.png

Install

$ bower install requirejs-react-jsx --save

If you're not using bower to manage your dependencies (you should), you can just download the jsx.js file manually.

Since we're also using react-tools for the build step while running in a node process, and not in the browser, you will need to install that also:

$ npm install react-tools --save

Usage

Setup

app.js

define(function(require){

  var React = require('react');

  function App() {
    this.AppView = React.createClass({
      render: function () {
        return (
          <div>
            <p>Hello, React!</p>
          </div>
        );
      }
    });
  }

  App.prototype.init = function () {
    React.render(<this.AppView />, document.body);
  };

  return App;

});

main.js

require.config({
  paths: {
    "react": "bower_components/react/react-with-addons",
    "JSXTransformer": "bower_components/react/JSXTransformer",
    "jsx": "bower_components/requirejs-react-jsx/jsx",
    "text": "bower_components/requirejs-text/text"
  },

  shim : {
    "react": {
      "exports": "React"
    },
    "JSXTransformer": "JSXTransformer"
  },

  config: {
    jsx: {
      fileExtension: ".jsx",
      transformOptions: {
        harmony: true,
        stripTypes: false,
        inlineSourceMap: true
      },
      usePragma: false
    }
  }
});

require(['jsx!app'], function(App){

  var app = new App();
  app.init();

});

Can also be configured with Babel:

config: {
  jsx: {
    transformer: "babel",
    babelOptions: {
      sourceMaps: 'inline'
    }
  }
}

Building

Call with $ node bower_components/r.js/dist/r.js -o build.js

In your r.js build.js config:

// add `optimize=none` to skip script optimization (useful during debugging).

({
  appDir: "./",
  baseUrl: "./",
  dir: "./compiled",
  mainConfigFile: "./main.js",

  optimize: "uglify2",
  skipDirOptimize: true,
  generateSourceMaps: true,
  findNestedDependencies: true,
  preserveLicenseComments: false,

  onBuildWrite: function (moduleName, path, singleContents) {
    return singleContents.replace(/jsx!/g, '');
  },

  modules: [
    {
      name: "main",
      exclude: ['jsx']
    }
  ]
})

Istanbul Code Coverage

If you want code coverage with Istanbul you will have to do a little extra work. Istanbul only instruments code required by nodes require function by default. However, you can make Istanbul also instrument RequireJS loaded dependencies in a node environment by adding the --hook-run-in-context switch.

The --hook-run-in-context only makes Istanbul pick up normally loaded RequireJS files though, and not the ones transformed by requirejs-react-jsx. So requirejs-react-jsx has an added programmatic code instrumentation which is triggered based on the ISTANBUL environment variable being 'true'.

A full example of a coverage script in package.json could look like this:

{
  "scripts": {
    "test": "mocha",
    "coverage": "ISTANBUL=true istanbul cover --hook-run-in-context _mocha"
  }
}

License

MIT

Keywords

FAQs

Package last updated on 10 Dec 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc