unplugin-jsx-string
Converts JSX to HTML strings at compile time.
Installation
npm i unplugin-jsx-string
Vite
import JsxString from 'unplugin-jsx-string/vite'
export default defineConfig({
plugins: [JsxString()],
})
Rollup
import JsxString from 'unplugin-jsx-string/rollup'
export default {
plugins: [JsxString()],
}
esbuild
import { build } from 'esbuild'
build({
plugins: [require('unplugin-jsx-string/esbuild')()],
})
Webpack
module.exports = {
plugins: [require('unplugin-jsx-string/webpack')()],
}
Vue CLI
module.exports = {
configureWebpack: {
plugins: [require('unplugin-jsx-string/webpack')()],
},
}
TypeScript Support
// tsconfig.json
{
"compilerOptions": {
// ...
"types": ["unplugin-jsx-string" /* ... */]
}
}
Usage
jsxToString(<div>Hello</div>)
jsxToString(<div className={['bar', 'foo']} />)
jsxToString(<div style={{ color: 'red', textAlign: 'center' }} />)
jsxToString(<button onClick={() => 'clicked'}></button>)
jsxToString(
<div>
<p>foo</p>
<p>bar</p>
<br />
<div />
123
</div>
)
Benchmark
<div>Hello World</div> x 73,893 ops/sec ±0.76% (91 runs sampled)
<div><img src={'foo'} /><div></div></div> x 52,859 ops/sec ±0.77% (93 runs sampled)
Tested on Apple M1 Max / 32GB
More samples are welcome.
License
MIT License © 2022 三咲智子