|
1 |
| -## JSX |
| 1 | +# Babel Preset JSX |
2 | 2 |
|
3 |
| -A monorepo containing all jsx-related packages for Vue.js. |
| 3 | +Configurable Babel preset to add Vue JSX support. See the [configuration options here](https://github.com/vuejs/jsx/tree/docs/init/packages/babel-preset-jsx). |
4 | 4 |
|
5 |
| -### Babel Compatibility Notes |
| 5 | +## Installation |
6 | 6 |
|
7 |
| -- This repo is only compatible with Babel 7.x, for 6.x please use [vuejs/babel-plugin-transform-vue-jsx](https://github.com/vuejs/babel-plugin-transform-vue-jsx) |
| 7 | +Install the preset with: |
8 | 8 |
|
9 |
| -### Details |
10 |
| - |
11 |
| -For normal usage it is recommended to use the default preset. |
12 |
| -To do that you need to first install the preset and the helper: |
13 |
| - |
14 |
| -```sh |
15 |
| -# for yarn: |
16 |
| -yarn add @vue/babel-preset-jsx @vue/babel-helper-vue-jsx-merge-props |
17 |
| -# for npm: |
18 |
| -npm install @vue/babel-preset-jsx @vue/babel-helper-vue-jsx-merge-props --save |
| 9 | +```bash |
| 10 | +npm install @vue/babel-preset-jsx @vue/babel-helper-vue-jsx-merge-props |
19 | 11 | ```
|
20 | 12 |
|
21 |
| -In your `.babelrc`: |
| 13 | +Then add the preset to `.babelrc`: |
22 | 14 |
|
23 | 15 | ```json
|
24 | 16 | {
|
25 | 17 | "presets": ["@vue/babel-preset-jsx"]
|
26 | 18 | }
|
27 | 19 | ```
|
28 | 20 |
|
29 |
| -For more details please check each package's README: |
| 21 | +## Syntax |
| 22 | + |
| 23 | +### Content |
| 24 | + |
| 25 | +```jsx |
| 26 | +render() { |
| 27 | + return <p>hello</p> |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +with dynamic content: |
| 32 | + |
| 33 | +```jsx |
| 34 | +render() { |
| 35 | + return <p>hello { this.message }</p> |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +when self-closing: |
| 40 | + |
| 41 | +```jsx |
| 42 | +render() { |
| 43 | + return <input /> |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +with a component: |
| 48 | + |
| 49 | +```jsx |
| 50 | +import MyComponent from './my-component' |
| 51 | + |
| 52 | +export default { |
| 53 | + render() { |
| 54 | + return <MyComponent>hello</MyComponent> |
| 55 | + } |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +### Attributes/Props |
| 60 | + |
| 61 | +```jsx |
| 62 | +render() { |
| 63 | + return <input type="email" /> |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +with a dynamic binding: |
| 68 | + |
| 69 | +```jsx |
| 70 | +render() { |
| 71 | + return <input |
| 72 | + type="email" |
| 73 | + placeholder={this.placeholderText} |
| 74 | + /> |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +with the spread operator: |
| 79 | + |
| 80 | +```jsx |
| 81 | +render() { |
| 82 | + const inputAttrs = { |
| 83 | + type: 'email', |
| 84 | + placeholder: 'Enter your email' |
| 85 | + } |
| 86 | + |
| 87 | + return <input {...inputAttrs} /> |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +### Directives |
| 92 | + |
| 93 | +```jsx |
| 94 | +<input vModel="newTodoText" /> |
| 95 | +``` |
| 96 | + |
| 97 | +with a modifier: |
| 98 | + |
| 99 | +```jsx |
| 100 | +<input vModel_trim="newTodoText" /> |
| 101 | +``` |
| 102 | + |
| 103 | +with an argument: |
| 104 | + |
| 105 | +```jsx |
| 106 | +<input vOn:click="newTodoText" /> |
| 107 | +``` |
| 108 | + |
| 109 | +with an argument and modifiers: |
| 110 | + |
| 111 | +```jsx |
| 112 | +<input vOn:click_stop_prevent="newTodoText" /> |
| 113 | +``` |
| 114 | + |
| 115 | +### Functional Components |
| 116 | + |
| 117 | +Transpiles arrow functions that return JSX into functional components, when they are either default exports: |
| 118 | + |
| 119 | +```jsx |
| 120 | +export default ({ props }) => <p>hello { props.message }</p> |
| 121 | +``` |
| 122 | + |
| 123 | +or PascalCase variable declarations: |
| 124 | + |
| 125 | +```jsx |
| 126 | +const HelloWorld = ({ props }) => <p>hello { props.message }</p> |
| 127 | +``` |
| 128 | + |
| 129 | +## Compatibility |
| 130 | + |
| 131 | +This repo is only compatible with: |
30 | 132 |
|
31 |
| -- [@vue/babel-helper-vue-jsx-merge-props](packages/babel-helper-vue-jsx-merge-props/README.md) - Runtime helper for props merging |
32 |
| -- [@vue/babel-plugin-transform-vue-jsx](packages/babel-plugin-transform-vue-jsx/README.md) - Main JSX transform plugin |
33 |
| -- [@vue/babel-preset-jsx](packages/babel-preset-jsx/README.md) - Configurable babel preset |
34 |
| -- [@vue/babel-sugar-functional-vue](packages/babel-sugar-functional-vue/README.md) - Functional components syntactic sugar |
35 |
| -- [@vue/babel-sugar-inject-h](packages/babel-sugar-inject-h/README.md) - Automatic `h` injection syntactic sugar |
36 |
| -- [@vue/babel-sugar-v-model](packages/babel-sugar-v-model/README.md) - `vModel` syntactic sugar |
| 133 | +- **Babel 7+**. For Babel 6 support, use [vuejs/babel-plugin-transform-vue-jsx](https://github.com/vuejs/babel-plugin-transform-vue-jsx) |
| 134 | +- **Vue 2+**. JSX is not supported for older versions. |
0 commit comments