Skip to content

Commit 16218f1

Browse files
committed
new htm
1 parent 84cfabb commit 16218f1

12 files changed

+8903
-3002
lines changed

.prettierrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"semi": false,
3+
"singleQuote": true
4+
}

README.md

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,27 @@
22

33
[![NPM version](https://img.shields.io/npm/v/vue-html.svg?style=flat-square)](https://npmjs.com/package/vue-html) [![NPM downloads](https://img.shields.io/npm/dm/vue-html.svg?style=flat-square)](https://npmjs.com/package/vue-html) [![Build Status](https://img.shields.io/circleci/project/egoist/vue-html/master.svg?style=flat-square)](https://circleci.com/gh/egoist/vue-html)
44

5-
> Use tagged template string in Vue.js
5+
> Use tagged template string in Vue.js render function
6+
7+
## Why is this useful?
8+
9+
If you want to use Vue without a bundler / transpiler, this library will (reasonably) make your app smaller:
10+
11+
- Vue (runtime + template compiler): 32kB gzipped
12+
- Vue (runtime + vue-html): 23kB gzipped
13+
14+
__What's the downside?__ No handy sugars like `v-model` support.
615

716
## Install
817

918
```bash
1019
$ npm install --save vue-html
1120
```
1221

13-
CDN version: https://unpkg.com/vue-html/dist/
22+
CDN versions:
23+
24+
- `UMD`: https://unpkg.com/vue-html/dist/html.js (exposed as `window.HTML`)
25+
- `ESM`: https://unpkg.com/vue-html/dist/html.es.js
1426

1527
## Usage
1628

@@ -20,57 +32,48 @@ import HTML from 'vue-html'
2032

2133
Vue.use(HTML)
2234

35+
const Todos = {
36+
props: ['todos'],
37+
render(html) {
38+
return html`
39+
<ul>
40+
${this.todos.map((todo, index) => {
41+
return html`<li key=${index}>${todo}</li>`
42+
})}
43+
</ul>
44+
`
45+
}
46+
}
47+
2348
new Vue({
2449
el: '#app',
2550
data: {
26-
count: 0
51+
todos: [
52+
'Conquer the world',
53+
'Rewrite Peco'
54+
],
55+
todo: ''
2756
},
2857
methods: {
29-
handleClick() {
30-
this.count++
58+
add() {
59+
this.todos.push(this.todo)
60+
this.todo = ''
3161
}
3262
},
33-
render() {
34-
return this.$html`
35-
<button onClick=${this.handleClick}>${this.count}</button>
36-
`
37-
}
38-
})
39-
```
40-
41-
The syntax is exactly the same as Vue JSX despiting that we're using [tagged template string](https://github.com/substack/hyperx) here.
42-
43-
### Component
44-
45-
You still have to use the original `h` function:
46-
47-
```js
48-
new Vue({
49-
el: '#app',
50-
render(h) {
51-
return this.$html`
52-
${h(Counter, {props: {start: 0}})}
63+
render(html) {
64+
return html`
65+
<div>
66+
<input value=${this.todo} onInput=${e => this.todo = e.target.value} />
67+
<button type="button" onClick=${this.add}>Add</button>
68+
<hr />
69+
<${Todos} todos=${this.todos} />
70+
</div>
5371
`
5472
}
5573
})
56-
57-
const Counter = {}
5874
```
5975

60-
### Mixed with `h`
61-
62-
`this.$html` returns a vNode, it does the same thing that `h` function does, so you can use it with `h` function.
63-
64-
```js
65-
new Vue({
66-
el: '#app',
67-
render(h) {
68-
return h('div', null, [
69-
this.$html`<span>foo</span>`
70-
])
71-
}
72-
})
73-
```
76+
The usage is very similar to Vue JSX except for that the `html` function is powered by [HTM (Hyperscript Tagged Markup)](https://github.com/developit/htm).
7477

7578
## Contributing
7679

babel.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
presets: ['poi/babel', 'power-assert']
3+
}

circle.yml

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
1-
machine:
2-
node:
3-
version: 7
4-
environment:
5-
PATH: "${PATH}:${HOME}/${CIRCLE_PROJECT_REPONAME}/node_modules/.bin"
6-
7-
dependencies:
8-
override:
9-
- yarn
10-
cache_directories:
11-
- ~/.cache/yarn
12-
13-
test:
14-
override:
15-
- yarn test
1+
version: 2
2+
jobs:
3+
build:
4+
working_directory: ~/repo
5+
docker:
6+
- image: circleci/node:latest-browsers
7+
branches:
8+
ignore:
9+
- gh-pages # list of branches to ignore
10+
- /release\/.*/ # or ignore regexes
11+
steps:
12+
- checkout
13+
- restore_cache:
14+
key: dependency-cache-{{ checksum "yarn.lock" }}
15+
- run:
16+
name: install dependences
17+
command: yarn
18+
- save_cache:
19+
key: dependency-cache-{{ checksum "yarn.lock" }}
20+
paths:
21+
- ./node_modules
22+
- run:
23+
name: test
24+
command: yarn test

example.js

Lines changed: 0 additions & 35 deletions
This file was deleted.

example/index.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import Vue from 'vue'
2+
import HTML from 'vue-html' // eslint-disable-line import/no-unresolved
3+
4+
Vue.use(HTML)
5+
6+
const Todos = {
7+
props: ['todos'],
8+
render(html) {
9+
return html`
10+
<ul>
11+
${
12+
this.todos.map((todo, index) => {
13+
return html`
14+
<li key=${index}>${todo}</li>
15+
`
16+
})
17+
}
18+
</ul>
19+
`
20+
}
21+
}
22+
23+
new Vue({
24+
el: '#app',
25+
data: {
26+
todos: ['Conquer the world', 'Rewrite Peco'],
27+
todo: ''
28+
},
29+
methods: {
30+
add() {
31+
this.todos.push(this.todo)
32+
this.todo = ''
33+
}
34+
},
35+
render(html) {
36+
return html`
37+
<div>
38+
<input
39+
value=${this.todo}
40+
onInput=${
41+
e => {
42+
this.todo = e.target.value
43+
}
44+
}
45+
/>
46+
<button type="button" onClick=${this.add}>Add</button>
47+
<hr />
48+
<${Todos} todos=${this.todos} />
49+
</div>
50+
`
51+
}
52+
})

example/poi.config.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const path = require('path')
2+
3+
exports.entry = 'example/index'
4+
5+
exports.configureWebpack = {
6+
resolve: {
7+
alias: {
8+
'vue-html$': path.join(__dirname, '../src')
9+
}
10+
}
11+
}

package.json

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,54 @@
11
{
22
"name": "vue-html",
33
"version": "0.3.0",
4-
"description": "Use tagged template string in Vue.js",
4+
"description": "Use tagged template string in Vue.js render function",
55
"license": "MIT",
66
"repository": "egoist/vue-html",
77
"author": {
88
"name": "EGOIST",
99
"email": "0x142857@gmail.com",
10-
"url": "github.com/egoist"
10+
"url": "http://github.com/egoist"
1111
},
1212
"scripts": {
13-
"test": "jest && xo test/*.js src/*.js && npm run build",
14-
"build": "bili --format umd --format cjs --compress",
15-
"example": "vue build example.js"
13+
"test": "npm run lint && npm run test:unit",
14+
"test:unit": "poi puppet --test --plugin @poi/puppet --framework mocha",
15+
"lint": "xo",
16+
"build": "bili --format umd,cjs,es,es-min,umd-min --js buble --module-name HTML --inline --name html",
17+
"example": "poi -so --config example/poi.config.js"
1618
},
1719
"files": [
1820
"dist"
1921
],
2022
"xo": {
21-
"space": 2,
22-
"semicolon": false,
23-
"esnext": true,
23+
"extends": ["rem", "plugin:prettier/recommended"],
2424
"envs": [
25-
"jest",
26-
"browser"
27-
]
25+
"browser",
26+
"mocha"
27+
],
28+
"rules": {
29+
"unicorn/filename-case": "off",
30+
"no-new": "off"
31+
}
2832
},
29-
"main": "dist/vue-html.common.js",
33+
"main": "dist/html.js",
34+
"module": "dist/html.es.js",
3035
"keywords": [
3136
"vue",
32-
"hyperx"
37+
"htm",
38+
"html"
3339
],
3440
"devDependencies": {
35-
"babel-preset-es2015": "^6.22.0",
36-
"bili": "^0.14.0",
37-
"jest-cli": "^15.1.1",
38-
"vue": "latest",
39-
"xo": "^0.17.1"
40-
},
41-
"dependencies": {
42-
"hyperx": "^2.0.5"
43-
},
44-
"babel": {
45-
"env": {
46-
"test": {
47-
"presets": [
48-
"es2015"
49-
]
50-
}
51-
}
41+
"@poi/plugin-puppet": "^0.1.3",
42+
"babel-preset-power-assert": "^3.0.0",
43+
"bili": "^3.4.2",
44+
"eslint-config-prettier": "^3.3.0",
45+
"eslint-config-rem": "^4.0.0",
46+
"eslint-plugin-prettier": "^3.0.0",
47+
"htm": "^2.0.0",
48+
"poi": "^12.2.4",
49+
"power-assert": "^1.6.1",
50+
"prettier": "^1.15.3",
51+
"vue": "^2.5.21",
52+
"xo": "^0.23.0"
5253
}
5354
}

0 commit comments

Comments
 (0)