Coder Social home page Coder Social logo

babel-plugin-transform-react-require's Introduction

babel-plugin-transform-react-require NPM version Build Status

Automatically require React (or other implementations) when using JSX. This babel plugin inserts CommonJS style require where it detects JSX and React isn't already required (or imported using ES2015 syntax).

If you're using JSX in your React (or similar) components, you often don't need the React module to anything else, and it will seem as if you have an unused require-statement. When using something like eslint you'd have to add a rule saying unused React should be ignored, but that's not always true.

With this module you'll no longer need to use this anti-pattern:

let React = require('react'); // free, seemingly unused

module.exports = function MyComponent () {
  return (
   <h1>Hello World!</h1>
  );
};

You can instead let it be an implementation detail and write:

module.exports = function MyComponent () {
  return (
   <h1>Hello World!</h1>
  );
};

Installation

$ npm install --save babel-plugin-transform-react-require

Usage

Via .babelrc (Recommended)

.babelrc

{
  "plugins": ["transform-react-require"]
}

Options

Not using React with JSX? You can define module name and module identifier as options. For instance, if you are using dom('div', {}) instead of React.createElement('div', {}), you might want to require dom, instead of React. In that case, you can override defaults by doing:

{
  "plugins": [
    ["transform-react-require", {
      "identifier": "dom",
      "moduleName": "my-dom-library"
    }]
  ]
}

This would cause the plugin to automatically require my-dom-library, when JSX syntax is detected, and store it in the identifier dom.

Via CLI

$ babel --plugins transform-react-require script.js

Via Node API

require('babel-core').transform('code', {
  plugins: ['transform-react-require']
});

Options

Same as through the .babelrc, you can override defaults:

require('babel-core').transform('code', {
  plugins: ['transform-react-require', {
    'identifier': 'dom',
    'moduleName': 'my-dom-library'
  }]
});

babel-plugin-transform-react-require's People

Contributors

mikaelbr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

babel-plugin-transform-react-require's Issues

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.