An open API service indexing awesome lists of open source software.

https://github.com/unplugin/unplugin-jsx-string

Converts JSX to HTML strings at compile time.
https://github.com/unplugin/unplugin-jsx-string

convert html jsx react string unplugin

Last synced: 2 months ago
JSON representation

Converts JSX to HTML strings at compile time.

Awesome Lists containing this project

README

        

# unplugin-jsx-string [![npm](https://img.shields.io/npm/v/unplugin-jsx-string.svg)](https://npmjs.com/package/unplugin-jsx-string)

[![Unit Test](https://github.com/unplugin/unplugin-jsx-string/actions/workflows/unit-test.yml/badge.svg)](https://github.com/unplugin/unplugin-jsx-string/actions/workflows/unit-test.yml)

Converts JSX to HTML strings at compile time.

## Installation

```bash
npm i unplugin-jsx-string
```

Vite

```ts
// vite.config.ts
import JsxString from 'unplugin-jsx-string/vite'

export default defineConfig({
plugins: [JsxString()],
})
```


Rollup

```ts
// rollup.config.js
import JsxString from 'unplugin-jsx-string/rollup'

export default {
plugins: [JsxString()],
}
```


esbuild

```ts
// esbuild.config.js
import { build } from 'esbuild'

build({
plugins: [require('unplugin-jsx-string/esbuild')()],
})
```


Webpack

```ts
// webpack.config.js
module.exports = {
/* ... */
plugins: [require('unplugin-jsx-string/webpack')()],
}
```


Vue CLI

```ts
// vue.config.js
module.exports = {
configureWebpack: {
plugins: [require('unplugin-jsx-string/webpack')()],
},
}
```


#### TypeScript Support

```jsonc
// tsconfig.json
{
"compilerOptions": {
// ...
"types": ["unplugin-jsx-string" /* ... */],
},
}
```

## Usage

```tsx
// basic usage
jsxToString(

Hello
)
// "
Hello
"

// raw expression
const t = Date.now()
jsxToString(

Now: {jsxRaw(Math.trunc(t / 1000))}
)
// `
Now: ${Math.trunc(t / 1000)}
`

// class list
jsxToString(

)
// `
`

// styles
jsxToString(

)
// `
`

// events
jsxToString( 'clicked'}>)
// ""

// children
jsxToString(


foo


bar





123
,
)
// "

foo


bar



123
"
```

## Benchmark

```

Hello World
x 99,362 ops/sec ±0.55% (92 runs sampled)


x 66,281 ops/sec ±0.63% (95 runs sampled)
```


Tested on Apple M1 Max / 32GB

_More samples are welcome._

## Sponsors





## License

[MIT](./LICENSE) License © 2022-PRESENT [三咲智子](https://github.com/sxzz)