Skip to content

Commit f7e4c83

Browse files
committed
docs: init README files
1 parent 2a31316 commit f7e4c83

File tree

7 files changed

+452
-0
lines changed

7 files changed

+452
-0
lines changed

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
## JSX
2+
3+
A monorepo containing all jsx-related packages for Vue.js.
4+
5+
### Babel Compatibility Notes
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)
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
19+
```
20+
21+
In your `.babelrc`:
22+
23+
```json
24+
{
25+
"presets": ["@vue/babel-preset-jsx"]
26+
}
27+
```
28+
29+
For more details please check each package's README:
30+
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
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
## @vue/babel-helper-vue-jsx-merge-props
2+
3+
A package used internally by vue jsx transformer to merge props spread. It is required to merge some prop trees like this:
4+
5+
```js
6+
import mergeProps from '@vue/babel-helper-vue-jsx-merge-props'
7+
8+
const MyComponent = {
9+
render(h) {
10+
// original: <button onClick={$event => console.log($event)} {...{ on: { click: $event => doSomething($event) } }} />
11+
return h(
12+
'button',
13+
mergeProps([
14+
{
15+
on: {
16+
click: $event => console.log($event),
17+
},
18+
},
19+
{
20+
on: {
21+
click: $event => doSomething($event),
22+
},
23+
},
24+
]),
25+
)
26+
},
27+
}
28+
```
29+
30+
This tool is used internally and there is no reason for you to ever use it.
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
## @vue/babel-plugin-transform-vue-jsx
2+
3+
> Babel plugin for Vue 2.0 JSX
4+
5+
### Babel Compatibility Notes
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)
8+
9+
### Requirements
10+
11+
- Assumes you are using Babel with a module bundler e.g. Webpack, because the spread merge helper is imported as a module to avoid duplication.
12+
13+
- This is mutually exclusive with `babel-plugin-transform-react-jsx`.
14+
15+
### Usage
16+
17+
```bash
18+
npm install @vue/babel-plugin-transform-vue-jsx --save-dev
19+
npm install @vue/babel-helper-vue-jsx-merge-props --save
20+
```
21+
22+
In your `.babelrc`:
23+
24+
```json
25+
{
26+
"plugins": ["transform-vue-jsx"]
27+
}
28+
```
29+
30+
However it is recommended to use the [configurable preset](../babel-preset-jsx/README.md) instead.
31+
32+
### Details
33+
34+
The plugin transpiles the following JSX:
35+
36+
```jsx
37+
<div id="foo">{this.text}</div>
38+
```
39+
40+
To the following JavaScript:
41+
42+
```js
43+
h(
44+
'div',
45+
{
46+
attrs: {
47+
id: 'foo',
48+
},
49+
},
50+
[this.text],
51+
)
52+
```
53+
54+
Note the `h` function, which is a shorthand for a Vue instance's `$createElement` method, must be in the scope where the JSX is. Since this method is passed to component render functions as the first argument, in most cases you'd do this:
55+
56+
```js
57+
Vue.component('jsx-example', {
58+
render(h) {
59+
// <-- h must be in scope
60+
return <div id="foo">bar</div>
61+
},
62+
})
63+
```
64+
65+
### Difference from React JSX
66+
67+
First, Vue 2.0's vnode format is different from React's. The second argument to the `createElement` call is a "data object" that accepts nested objects. Each nested object will be then processed by corresponding modules:
68+
69+
```js
70+
render (h) {
71+
return h('div', {
72+
// Component props
73+
props: {
74+
msg: 'hi'
75+
},
76+
// Normal HTML attributes
77+
attrs: {
78+
id: 'foo'
79+
},
80+
// DOM props
81+
domProps: {
82+
innerHTML: 'bar'
83+
},
84+
// Event handlers are nested under "on", though
85+
// modifiers such as in v-on:keyup.enter are not
86+
// supported. You'll have to manually check the
87+
// keyCode in the handler instead.
88+
on: {
89+
click: this.clickHandler
90+
},
91+
// For components only. Allows you to listen to
92+
// native events, rather than events emitted from
93+
// the component using vm.$emit.
94+
nativeOn: {
95+
click: this.nativeClickHandler
96+
},
97+
// Class is a special module, same API as `v-bind:class`
98+
class: {
99+
foo: true,
100+
bar: false
101+
},
102+
// Style is also same as `v-bind:style`
103+
style: {
104+
color: 'red',
105+
fontSize: '14px'
106+
},
107+
// Other special top-level properties
108+
key: 'key',
109+
ref: 'ref',
110+
// Assign the `ref` is used on elements/components with v-for
111+
refInFor: true,
112+
slot: 'slot'
113+
})
114+
}
115+
```
116+
117+
The equivalent of the above in Vue 2.0 JSX is:
118+
119+
```jsx
120+
render (h) {
121+
return (
122+
<div
123+
// Component props
124+
propsMsg="hi"
125+
// Normal attributes or component props.
126+
id="foo"
127+
// DOM properties are prefixed with `domProps`
128+
domPropsInnerHTML="bar"
129+
// event listeners are prefixed with `on` or `nativeOn`
130+
onClick={this.clickHandler}
131+
nativeOnClick={this.nativeClickHandler}
132+
// other special top-level properties
133+
class={{ foo: true, bar: false }}
134+
style={{ color: 'red', fontSize: '14px' }}
135+
key="key"
136+
ref="ref"
137+
// assign the `ref` is used on elements/components with v-for
138+
refInFor
139+
slot="slot">
140+
</div>
141+
)
142+
}
143+
```
144+
145+
### Component Tip
146+
147+
If a custom element starts with lowercase, it will be treated as a string id and used to lookup a registered component. If it starts with uppercase, it will be treated as an identifier, which allows you to do:
148+
149+
```js
150+
import Todo from './Todo.js'
151+
152+
export default {
153+
render(h) {
154+
return <Todo /> // no need to register Todo via components option
155+
},
156+
}
157+
```
158+
159+
### JSX Spread
160+
161+
JSX spread is supported, and this plugin will intelligently merge nested data properties. For example:
162+
163+
```jsx
164+
const data = {
165+
class: ['b', 'c'],
166+
}
167+
const vnode = <div class="a" {...data} />
168+
```
169+
170+
The merged data will be:
171+
172+
```js
173+
{ class: ['a', 'b', 'c'] }
174+
```
175+
176+
### Vue directives
177+
178+
Vue directives are usable the same way as in template with a few key differences:
179+
180+
1. You can use directives camelCased instead of kebab-cased (vMyDirective is treated as `v-my-directive`)
181+
2. You have to use underscore sign instead of dots for modifiers because of JSXIdentifier limitation.
182+
3. Only runtime directives work (only v-show and custom directives), compile-time directives are out of this project's scope.
183+
184+
A full example would be: `<MyComponent vMyDirective:argument_modifier1_modifier2={someExpression} />`

packages/babel-preset-jsx/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
## @vue/babel-preset-jsx
2+
3+
Configurable preset for Vue JSX plugins.
4+
5+
### Babel Compatibility Notes
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)
8+
9+
### Usage
10+
11+
Install the dependencies:
12+
13+
```sh
14+
# for yarn:
15+
yarn add @vue/babel-preset-jsx @vue/babel-helper-vue-jsx-merge-props
16+
# for npm:
17+
npm install @vue/babel-preset-jsx @vue/babel-helper-vue-jsx-merge-props --save
18+
```
19+
20+
In your `.babelrc`:
21+
22+
```json
23+
{
24+
"presets": ["@vue/babel-preset-jsx"]
25+
}
26+
```
27+
28+
You can toggle specific features, by default all features are enabled, e.g.:
29+
30+
```json
31+
{
32+
"presets": [
33+
[
34+
"@vue/babel-preset-jsx",
35+
{
36+
"vModel": false
37+
}
38+
]
39+
]
40+
}
41+
```
42+
43+
Options are:
44+
45+
- `functional` [@vue/babel-sugar-functional-vue](../babel-sugar-functional-vue/README.md) - Functional components syntactic sugar
46+
- `injectH` [@vue/babel-sugar-inject-h](../babel-sugar-inject-h/README.md) - Automatic `h` injection syntactic sugar
47+
- `vModel` [@vue/babel-sugar-v-model](../babel-sugar-v-model/README.md) - `vModel` syntactic sugar
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
## @vue/babel-sugar-functional-vue
2+
3+
Syntactic sugar for functional components.
4+
5+
### Babel Compatibility Notes
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)
8+
9+
### Usage
10+
11+
Install the dependencies:
12+
13+
```sh
14+
# for yarn:
15+
yarn add @vue/babel-sugar-functional-vue
16+
# for npm:
17+
npm install @vue/babel-sugar-functional-vue --save
18+
```
19+
20+
In your `.babelrc`:
21+
22+
```json
23+
{
24+
"plugins": ["@vue/babel-sugar-functional-vue"]
25+
}
26+
```
27+
28+
However it is recommended to use the [configurable preset](../babel-preset-jsx/README.md) instead.
29+
30+
### Details
31+
32+
This plugin transpiles arrow functions that return JSX into functional components but only if it's an uppercase variable declaration or default export:
33+
34+
```js
35+
// Original:
36+
export const A = ({ props, listeners }) => <div onClick={listeners.click}>{props.msg}</div>
37+
export const b = ({ props, listeners }) => <div onClick={listeners.click}>{props.msg}</div>
38+
export default ({ props, listeners }) => <div onClick={listeners.click}>{props.msg}</div>
39+
40+
// Result:
41+
export const A = {
42+
functional: true,
43+
render: (h, {
44+
props,
45+
listeners
46+
}) => <div onClick={listeners.click}>{props.msg}</div>
47+
}
48+
export const b = ({ props, listeners }) => <div onClick={listeners.click}>{props.msg}</div>
49+
export default {
50+
functional: true,
51+
render: (h, {
52+
props,
53+
listeners
54+
}) => <div onClick={listeners.click}>{props.msg}</div>
55+
}
56+
```

0 commit comments

Comments
 (0)