Require and transpile JSX on the fly
- Doesn't install any
require()hooks - Auto-detects React pragma (
React.createElement) and falls back tohpragma supported by Preact and others - Caches transpiled sources by default
- Bundles in object rest spread transform
$ npm install --save import-jsx
const importJsx = require('import-jsx');
const reactComponent = importJsx('./react');
const preactComponent = importJsx('./preact');
const customComponent = importJsx('./custom', {pragma: 'x'});react.js
const React = require('react');
module.exports = <div/>;preact.js
const {h} = require('preact');
module.exports = <div/>;custom.js
const x = (tagName, attrs, ...children) => {};
module.exports = <div/>;Type: string
Module id.
Type: string
Default: h
Override JSX pragma.
Type: string
Default: Fragment
Override pragma for JSX fragments.
Type: boolean
Default: true
Enable or disable caching of transpiled sources.
Factory method to create a version of importJsx() with pre-defined options.
Useful when you need a custom pragma, but don't want to pass it along with each importJsx() call.
Type: object
Options to pass to importJsx().
// Before
const importJsx = require('import-jsx');
importJsx('./a', {pragma: 'x'});
importJsx('./b', {pragma: 'x'});
// After
const importJsx = require('import-jsx').create({pragma: 'x'});
importJsx('./a');
importJsx('./b');MIT © Vadim Demedes