impronunciable / babel-plugin-preact-require

[TODO] Babel plugin that adds React import declaration if file contains JSX tags

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

babel-plugin-preact-require

Babel plugin that adds Preact's h declaration if file contains JSX tags.

This plugin is only about stateless components that doesn't extends Component. If you want to use any other Preact functions then you should import their by yourself.

This code was forked from babel-plugin-react-require so all credit should go to @vslinko

Example

Your component.js that contains this code:

export default function Component() {
  return (
    <div />
  )
}

will be transpiled into something like this:

import { h } from 'preact'

export default function Component() {
  /* this part will be transpiled by babel itself as usual */
  return (
    h('div')
  )
}

Usage

  • Install babel-plugin-preact-require.
npm install babel-plugin-preact-require --save-dev
  • Add preact-require into .babelrc. This plugin should be defined before transform-es2015-modules-commonjs plugin because it's using ES2015 modules syntax to import preact's h into scope.
{
  "plugins": [
    "preact-require"
  ]
}

About

[TODO] Babel plugin that adds React import declaration if file contains JSX tags

License:MIT License


Languages

Language:JavaScript 100.0%