|
2 | 2 |
|
3 | 3 | [](https://github.com/sxzz/unplugin-jsx-string/actions/workflows/unit-test.yml) |
4 | 4 |
|
5 | | -## Install |
| 5 | +## Installation |
6 | 6 |
|
7 | 7 | ```bash |
8 | 8 | npm i unplugin-jsx-string |
9 | 9 | ``` |
10 | 10 |
|
| 11 | +<details> |
| 12 | +<summary>Vite</summary><br> |
| 13 | + |
| 14 | +```ts |
| 15 | +// vite.config.ts |
| 16 | +import DefineOptions from 'unplugin-jsx-string/vite' |
| 17 | +import Vue from '@vitejs/plugin-vue' |
| 18 | + |
| 19 | +export default defineConfig({ |
| 20 | + plugins: [Vue(), DefineOptions()], |
| 21 | +}) |
| 22 | +``` |
| 23 | + |
| 24 | +<br></details> |
| 25 | + |
| 26 | +<details> |
| 27 | +<summary>Rollup</summary><br> |
| 28 | + |
| 29 | +```ts |
| 30 | +// rollup.config.js |
| 31 | +import DefineOptions from 'unplugin-jsx-string/rollup' |
| 32 | + |
| 33 | +export default { |
| 34 | + plugins: [DefineOptions()], // Must be before Vue plugin! |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +<br></details> |
| 39 | + |
| 40 | +<details> |
| 41 | +<summary>esbuild</summary><br> |
| 42 | + |
| 43 | +```ts |
| 44 | +// esbuild.config.js |
| 45 | +import { build } from 'esbuild' |
| 46 | + |
| 47 | +build({ |
| 48 | + plugins: [ |
| 49 | + require('unplugin-jsx-string/esbuild')(), // Must be before Vue plugin! |
| 50 | + ], |
| 51 | +}) |
| 52 | +``` |
| 53 | + |
| 54 | +<br></details> |
| 55 | + |
| 56 | +<details> |
| 57 | +<summary>Webpack</summary><br> |
| 58 | + |
| 59 | +```ts |
| 60 | +// webpack.config.js |
| 61 | +module.exports = { |
| 62 | + /* ... */ |
| 63 | + plugins: [require('unplugin-jsx-string/webpack')()], |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +<br></details> |
| 68 | + |
| 69 | +<details> |
| 70 | +<summary>Vue CLI</summary><br> |
| 71 | + |
| 72 | +```ts |
| 73 | +// vue.config.js |
| 74 | +module.exports = { |
| 75 | + configureWebpack: { |
| 76 | + plugins: [require('unplugin-jsx-string/webpack')()], |
| 77 | + }, |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +<br></details> |
| 82 | + |
| 83 | +## Usage |
| 84 | + |
| 85 | +```tsx |
| 86 | +// basic usage |
| 87 | +jsxToString(<div>Hello</div>) |
| 88 | +// "<div>Hello</div>" |
| 89 | + |
| 90 | +// class list |
| 91 | +jsxToString(<div className={['bar', 'foo']} />) |
| 92 | +// `<div class="bar foo"/>` |
| 93 | + |
| 94 | +// styles |
| 95 | +jsxToString(<div style={{ color: 'red', textAlign: 'center' }} />) |
| 96 | +// `<div style="color:red;text-align:center"/>` |
| 97 | + |
| 98 | +// events |
| 99 | +jsxToString(<button onClick={() => 'clicked'}></button>) |
| 100 | +// "<button onclick="'clicked'"></button>" |
| 101 | + |
| 102 | +// children |
| 103 | +jsxToString( |
| 104 | + <div> |
| 105 | + <p>foo</p> |
| 106 | + <p>bar</p> |
| 107 | + <br /> |
| 108 | + <div /> |
| 109 | + 123 |
| 110 | + </div> |
| 111 | +) |
| 112 | +// "<div><p>foo</p><p>bar</p><br/><div/>123</div>" |
| 113 | +``` |
| 114 | + |
11 | 115 | ## Sponsors |
12 | 116 |
|
13 | 117 | <p align="center"> |
|
0 commit comments