Skip to content

Commit d7cb67a

Browse files
authored
Merge pull request #5 from vuejs/docs/init
Add README files
2 parents c2f486b + fc10b38 commit d7cb67a

File tree

7 files changed

+550
-0
lines changed

7 files changed

+550
-0
lines changed

README.md

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# Babel Preset JSX
2+
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+
5+
## Installation
6+
7+
Install the preset with:
8+
9+
```bash
10+
npm install @vue/babel-preset-jsx @vue/babel-helper-vue-jsx-merge-props
11+
```
12+
13+
Then add the preset to `.babelrc`:
14+
15+
```json
16+
{
17+
"presets": ["@vue/babel-preset-jsx"]
18+
}
19+
```
20+
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:
132+
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.
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

0 commit comments

Comments
 (0)