Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

import-jsx

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

import-jsx

Require and transpile JSX on the fly

  • 5.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
33K
decreased by-27.67%
Maintainers
1
Weekly downloads
 
Created
Source

import-jsx Build Status

Import and transpile JSX via loader hooks. It doesn't transpile anything besides JSX and caches transpiled sources by default.

Install

npm install import-jsx

Usage

Note: import-jsx only works with ES modules.

node --loader=import-jsx react-example.js

react-example.js

const HelloWorld = () => <h1>Hello world</h1>;

Examples

React

React is auto-detected by default and react will be auto-imported, if it's not already.

const HelloWorld = () => <h1>Hello world</h1>;

Preact

If an alternative library is used and exports createElement, like Preact, configure import-jsx to import it instead of React:

/** @jsxImportSource preact */

const HelloWorld = () => <h1>Hello world</h1>;

Any JSX pragma

For libraries not compatible with React's API, but which still support JSX, import it and configure import-jsx to use its pragma:

/** @jsxRuntime classic */
/** @jsx h */
import h from 'vhtml';

const HelloWorld = () => <h1>Hello world</h1>;

CLI

import-jsx can be used to transpile JSX inside CLI entrypoints defined in bin section of package.json and their imported files.

For example, given this package.json:

{
	"name": "my-amazing-cli",
	"bin": "cli.js"
}

Insert this hashbang at the beginning of cli.js:

#!/usr/bin/env NODE_NO_WARNINGS=1 node --loader=import-jsx

const HelloWorld = () => <h1>Hello world</h1>;

Disable cache

import-jsx caches transpiled sources by default, so that the same file is transpiled only once. If that's not a desired behavior, turn off caching by setting IMPORT_JSX_CACHE=0 or IMPORT_JSX_CACHE=false environment variable.

IMPORT_JSX_CACHE=0 node --loader=import-jsx my-code.js

Keywords

FAQs

Package last updated on 28 Feb 2023

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