Converts JSX to HTML strings at compile time.
npm i unplugin-jsx-string
Vite
// vite.config.ts
import JsxString from 'unplugin-jsx-string/vite'
export default defineConfig({
plugins: [JsxString()],
})
Rollup
// rollup.config.js
import JsxString from 'unplugin-jsx-string/rollup'
export default {
plugins: [JsxString()],
}
esbuild
// esbuild.config.js
import { build } from 'esbuild'
build({
plugins: [require('unplugin-jsx-string/esbuild')()],
})
Webpack
// webpack.config.js
module.exports = {
/* ... */
plugins: [require('unplugin-jsx-string/webpack')()],
}
Vue CLI
// vue.config.js
module.exports = {
configureWebpack: {
plugins: [require('unplugin-jsx-string/webpack')()],
},
}
// tsconfig.json
{
"compilerOptions": {
// ...
"types": ["unplugin-jsx-string" /* ... */]
}
}
// basic usage
jsxToString(<div>Hello</div>)
// "<div>Hello</div>"
// class list
jsxToString(<div className={['bar', 'foo']} />)
// `<div class="bar foo"/>`
// styles
jsxToString(<div style={{ color: 'red', textAlign: 'center' }} />)
// `<div style="color:red;text-align:center"/>`
// events
jsxToString(<button onClick={() => 'clicked'}></button>)
// "<button onclick="'clicked'"></button>"
// children
jsxToString(
<div>
<p>foo</p>
<p>bar</p>
<br />
<div />
123
</div>
)
// "<div><p>foo</p><p>bar</p><br/><div/>123</div>"
<div>Hello World</div> x 89,429 ops/sec ±0.88% (95 runs sampled)
<div><img src={'foo'} /><div></div></div> x 62,522 ops/sec ±0.62% (93 runs sampled)
More samples are welcome.