Skip to content

Commit a933215

Browse files
committed
feat: make vue templates optional
1 parent a47985f commit a933215

File tree

6 files changed

+77
-34
lines changed

6 files changed

+77
-34
lines changed

README.md

+57-16
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,13 @@ Now with `lit-html` you can use `.js` and `.ts` extensions:
5757
import { html } from 'lit-vue'
5858

5959
html`
60-
<div>
61-
<h1>hello</h1>
62-
<hr />
63-
<button @click="inc">{{ count }}</button>
64-
</div>
60+
<template>
61+
<div>
62+
<h1>hello</h1>
63+
<hr />
64+
<button @click="inc">{{ count }}</button>
65+
</div>
66+
</template>
6567
6668
<style scoped>
6769
h1 {
@@ -102,7 +104,9 @@ ESLint might complain about the the <code>html&#x60;&#x60;</code> expression not
102104

103105
```js
104106
export const template = html`
105-
<div>{{ count }}</div>
107+
<template>
108+
<div>{{ count }}</div>
109+
</template>
106110
`
107111
```
108112

@@ -112,7 +116,9 @@ You can just assign it to a variable and export it, though the exported variable
112116

113117
```js
114118
const template = html`
115-
<div>{{ count }}</div>
119+
<template>
120+
<div>{{ count }}</div>
121+
</template>
116122
`
117123

118124
export default {
@@ -165,24 +171,59 @@ module.exports = {
165171

166172
That's it, [all the goodies](https://vue-loader.vuejs.org/) of `.vue` SFC are available in your `.vue.js` and `.vue.ts` files now!
167173

168-
### Custom blocks
174+
### Optional `<template>` element
169175

170-
You can also use [custom blocks](https://vue-loader.vuejs.org/guide/custom-blocks.html) in the `html` tag:
176+
`<template>` inside `html` is optional:
171177

172178
```js
173179
html`
174-
<custom-block name="i18n"> { "en": {} } </custom-block>
180+
<h1>hello</h1>
181+
`
182+
183+
// or
184+
185+
html`
186+
<template>
187+
<h1>hello</h1>
188+
</template>
175189
`
176190
```
177191

178-
It will be converted to:
192+
When using templates without `<template>` tag, you have to use `<custom-block>` element to define custom blocks:
179193

180-
```vue
181-
<i18n>
182-
{
183-
"en": {}
194+
```js
195+
html`
196+
<h1>hello</h1>
197+
198+
<custom-block name="i18n"> {"en": {}} </custom-block>
199+
`
200+
201+
// or
202+
203+
html`
204+
<template>
205+
<h1>hello</h1>
206+
</template>
207+
208+
<i18n> {"en": {}} </i18n>
209+
`
210+
```
211+
212+
And in fact even the whole Vue template is optional in `html` tag, you can just use `<style>` and custom blocks with render function instead:
213+
214+
```js
215+
html`
216+
<style scoped lang="sass">
217+
h1
218+
color: red
219+
</style>
220+
`
221+
222+
export default {
223+
render(h) {
224+
return h('h1', null, ['hello'])
225+
}
184226
}
185-
</i18n>
186227
```
187228
188229
### Syntax higlighting

example/App.vue.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import { html } from 'lit-vue'
33
import Welcome from './Welcome.vue'
44

55
const template = html`
6-
<div>
7-
<Welcome name="Guest" />
8-
<hr />
9-
<button @click="inc">{{ count }}</button>
10-
</div>
6+
<template>
7+
<div>
8+
<Welcome name="Guest" />
9+
<hr />
10+
<button @click="inc">{{ count }}</button>
11+
</div>
12+
</template>
1113
1214
<style scoped>
1315
h1 {

example/Welcome.vue.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import Vue from 'vue'
22
import Component from 'vue-class-component'
33
import { html } from 'lit-vue'
44

5-
html`<h1>{{ message }}</h1>`
5+
html`
6+
<template>
7+
<h1>{{ message }}</h1>
8+
</template>
9+
`
610

711
@Component({
812
props: {

lib/compile.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ module.exports = async (content, filename = 'foo.js') => {
5959
const STYLE_RE = /<style[\s\S]*>[\s\S]*<\/style>/g
6060
const CUSTOM_BLOCK_RE = /<custom-block([\s\S]*)>([\s\S]*)<\/custom-block>/g
6161

62-
const template = html
62+
let template = html
6363
.replace(STYLE_RE, match => {
6464
styles.push(match)
6565
return ''
@@ -74,11 +74,15 @@ module.exports = async (content, filename = 'foo.js') => {
7474
customBlocks.push(`<${name}${stringifyAttrs(attrs)}>${content}</${name}>`)
7575
return ''
7676
})
77+
.trim()
78+
79+
const hasTemplateTag = /^<template.*>/.test(template)
80+
if (!hasTemplateTag && template) {
81+
template = `<template>${template}</template>`
82+
}
7783

7884
return `
79-
<template>
8085
${template}
81-
</template>
8286
8387
<script lang="${ext}">
8488
${generator.default(ast).code}

lib/loader.js

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module.exports = async function(content) {
33
const done = this.async()
44
try {
55
const result = await require('./compile')(content, this.resourcePath)
6-
console.log(this.resourcePath, result)
76
done(null, result)
87
} catch (error) {
98
done(error)

test/__snapshots__/compile.test.js.snap

+1-8
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,7 @@
22

33
exports[`simple 1`] = `
44
"
5-
<template>
6-
7-
<div></div>
8-
9-
10-
11-
12-
</template>
5+
<template><div></div></template>
136
147
<script lang=\\"ts\\">
158
const template: string = undefined;

0 commit comments

Comments
 (0)