022f313f6af08c12df565a397cda9ca25f8894a030944598df89ce3266020a3bf397eb373ed0946be403351fc9e9107fbabbbfef7633e64c2edc4a153ccdf2 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. # Babel Plugin JSX for Vue 3
  2. [![npm package](https://img.shields.io/npm/v/@vue/babel-plugin-jsx.svg?style=flat-square)](https://www.npmjs.com/package/@vue/babel-plugin-jsx)
  3. [![issues-helper](https://img.shields.io/badge/Issues%20Manage%20By-issues--helper-blueviolet?style=flat-square)](https://github.com/actions-cool/issues-helper)
  4. To add Vue JSX support.
  5. English | [简体中文](/packages/babel-plugin-jsx/README-zh_CN.md)
  6. ## Installation
  7. Install the plugin with:
  8. ```bash
  9. npm install @vue/babel-plugin-jsx -D
  10. ```
  11. Then add the plugin to your babel config:
  12. ```json
  13. {
  14. "plugins": ["@vue/babel-plugin-jsx"]
  15. }
  16. ```
  17. ## Usage
  18. ### options
  19. #### transformOn
  20. Type: `boolean`
  21. Default: `false`
  22. transform `on: { click: xx }` to `onClick: xxx`
  23. #### optimize
  24. Type: `boolean`
  25. Default: `false`
  26. When enabled, this plugin generates optimized runtime code using [`PatchFlags`](https://vuejs.org/guide/extras/rendering-mechanism#patch-flags) and [`SlotFlags`](https://github.com/vuejs/core/blob/v3.5.13/packages/runtime-core/src/componentSlots.ts#L69-L77) to improve rendering performance. However, due to JSX's dynamic nature, the optimizations are not as comprehensive as those in Vue's official template compiler.
  27. Since the optimized code may skip certain re-renders to improve performance, we strongly recommend thorough testing of your application after enabling this option to ensure everything works as expected.
  28. #### isCustomElement
  29. Type: `(tag: string) => boolean`
  30. Default: `undefined`
  31. configuring custom elements
  32. #### mergeProps
  33. Type: `boolean`
  34. Default: `true`
  35. merge static and dynamic class / style attributes / onXXX handlers
  36. #### enableObjectSlots
  37. Type: `boolean`
  38. Default: `true`
  39. Whether to enable `object slots` (mentioned below the document) syntax". It might be useful in JSX, but it will add a lot of `_isSlot` condition expressions which increase your bundle size. And `v-slots` is still available even if `enableObjectSlots` is turned off.
  40. #### pragma
  41. Type: `string`
  42. Default: `createVNode`
  43. Replace the function used when compiling JSX expressions.
  44. #### resolveType
  45. Type: `boolean`
  46. Default: `false`
  47. (**Experimental**) Infer component metadata from types (e.g. `props`, `emits`, `name`). This is an experimental feature and may not work in all cases.
  48. ## Syntax
  49. ### Content
  50. functional component
  51. ```jsx
  52. const App = () => <div>Vue 3.0</div>;
  53. ```
  54. with render
  55. ```jsx
  56. const App = {
  57. render() {
  58. return <div>Vue 3.0</div>;
  59. },
  60. };
  61. ```
  62. ```jsx
  63. import { withModifiers, defineComponent } from 'vue';
  64. const App = defineComponent({
  65. setup() {
  66. const count = ref(0);
  67. const inc = () => {
  68. count.value++;
  69. };
  70. return () => (
  71. <div onClick={withModifiers(inc, ['self'])}>{count.value}</div>
  72. );
  73. },
  74. });
  75. ```
  76. Fragment
  77. ```jsx
  78. const App = () => (
  79. <>
  80. <span>I'm</span>
  81. <span>Fragment</span>
  82. </>
  83. );
  84. ```
  85. ### Attributes / Props
  86. ```jsx
  87. const App = () => <input type="email" />;
  88. ```
  89. with a dynamic binding:
  90. ```jsx
  91. const placeholderText = 'email';
  92. const App = () => <input type="email" placeholder={placeholderText} />;
  93. ```
  94. ### Directives
  95. #### v-show
  96. ```jsx
  97. const App = {
  98. data() {
  99. return { visible: true };
  100. },
  101. render() {
  102. return <input v-show={this.visible} />;
  103. },
  104. };
  105. ```
  106. #### v-model
  107. > Note: You should pass the second param as string for using `arg`.
  108. ```jsx
  109. <input v-model={val} />
  110. ```
  111. ```jsx
  112. <input v-model:argument={val} />
  113. ```
  114. ```jsx
  115. <input v-model={[val, ['modifier']]} />
  116. // Or
  117. <input v-model_modifier={val} />
  118. ```
  119. ```jsx
  120. <A v-model={[val, 'argument', ['modifier']]} />
  121. // Or
  122. <input v-model:argument_modifier={val} />
  123. ```
  124. Will compile to:
  125. ```js
  126. h(A, {
  127. argument: val,
  128. argumentModifiers: {
  129. modifier: true,
  130. },
  131. 'onUpdate:argument': ($event) => (val = $event),
  132. });
  133. ```
  134. #### v-models (Not recommended since v1.1.0)
  135. > Note: You should pass a Two-dimensional Arrays to v-models.
  136. ```jsx
  137. <A v-models={[[foo], [bar, 'bar']]} />
  138. ```
  139. ```jsx
  140. <A
  141. v-models={[
  142. [foo, 'foo'],
  143. [bar, 'bar'],
  144. ]}
  145. />
  146. ```
  147. ```jsx
  148. <A
  149. v-models={[
  150. [foo, ['modifier']],
  151. [bar, 'bar', ['modifier']],
  152. ]}
  153. />
  154. ```
  155. Will compile to:
  156. ```js
  157. h(A, {
  158. modelValue: foo,
  159. modelModifiers: {
  160. modifier: true,
  161. },
  162. 'onUpdate:modelValue': ($event) => (foo = $event),
  163. bar: bar,
  164. barModifiers: {
  165. modifier: true,
  166. },
  167. 'onUpdate:bar': ($event) => (bar = $event),
  168. });
  169. ```
  170. #### custom directive
  171. Recommended when using string arguments
  172. ```jsx
  173. const App = {
  174. directives: { custom: customDirective },
  175. setup() {
  176. return () => <a v-custom:arg={val} />;
  177. },
  178. };
  179. ```
  180. ```jsx
  181. const App = {
  182. directives: { custom: customDirective },
  183. setup() {
  184. return () => <a v-custom={[val, 'arg', ['a', 'b']]} />;
  185. },
  186. };
  187. ```
  188. ### Slot
  189. > Note: In `jsx`, _`v-slot`_ should be replaced with **`v-slots`**
  190. ```jsx
  191. const A = (props, { slots }) => (
  192. <>
  193. <h1>{slots.default ? slots.default() : 'foo'}</h1>
  194. <h2>{slots.bar?.()}</h2>
  195. </>
  196. );
  197. const App = {
  198. setup() {
  199. const slots = {
  200. bar: () => <span>B</span>,
  201. };
  202. return () => (
  203. <A v-slots={slots}>
  204. <div>A</div>
  205. </A>
  206. );
  207. },
  208. };
  209. // or
  210. const App = {
  211. setup() {
  212. const slots = {
  213. default: () => <div>A</div>,
  214. bar: () => <span>B</span>,
  215. };
  216. return () => <A v-slots={slots} />;
  217. },
  218. };
  219. // or you can use object slots when `enableObjectSlots` is not false.
  220. const App = {
  221. setup() {
  222. return () => (
  223. <>
  224. <A>
  225. {{
  226. default: () => <div>A</div>,
  227. bar: () => <span>B</span>,
  228. }}
  229. </A>
  230. <B>{() => 'foo'}</B>
  231. </>
  232. );
  233. },
  234. };
  235. ```
  236. ### In TypeScript
  237. `tsconfig.json`:
  238. ```json
  239. {
  240. "compilerOptions": {
  241. "jsx": "preserve"
  242. }
  243. }
  244. ```
  245. ## Who is using
  246. <table>
  247. <tbody>
  248. <tr>
  249. <td align="center">
  250. <a target="_blank" href="https://www.antdv.com/">
  251. <img
  252. width="32"
  253. src="https://github.com/vuejs/babel-plugin-jsx/assets/6481596/8d604d42-fe5f-4450-af87-97999537cd21"
  254. />
  255. <br>
  256. <strong>Ant Design Vue</strong>
  257. </a>
  258. </td>
  259. <td align="center">
  260. <a target="_blank" href="https://youzan.github.io/vant/#/zh-CN/">
  261. <img
  262. width="32"
  263. style="vertical-align: -0.32em; margin-right: 8px;"
  264. src="https://img.yzcdn.cn/vant/logo.png"
  265. />
  266. <br>
  267. <strong>Vant</strong>
  268. </a>
  269. </td>
  270. <td align="center">
  271. <a target="_blank" href="https://github.com/element-plus/element-plus">
  272. <img
  273. height="32"
  274. style="vertical-align: -0.32em; margin-right: 8px;"
  275. src="https://user-images.githubusercontent.com/10731096/91267529-259f3680-e7a6-11ea-9a60-3286f750de01.png"
  276. />
  277. <br>
  278. <strong>Element Plus</strong>
  279. </a>
  280. </td>
  281. <td align="center">
  282. <a target="_blank" href="https://github.com/leezng/vue-json-pretty">
  283. <img
  284. height="32"
  285. style="vertical-align: -0.32em; margin-right: 8px;"
  286. src="https://raw.githubusercontent.com/leezng/vue-json-pretty/master/static/logo.svg"
  287. />
  288. <br>
  289. <strong>Vue Json Pretty</strong>
  290. </a>
  291. </td>
  292. </tr>
  293. </tbody>
  294. </table>
  295. ## Compatibility
  296. This repo is only compatible with:
  297. - **Babel 7+**
  298. - **Vue 3+**