Skip to content

Commit e6abc19

Browse files
Kingwlyyx990803
authored andcommitted
support comment option in template (#897)
* support comment option in template * Update loader.js * Update template-comment.vue
1 parent 7253e32 commit e6abc19

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

lib/loader.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,12 @@ module.exports = function (content) {
8989
var output = ''
9090
var parts = parse(content, fileName, this.sourceMap)
9191
var hasScoped = parts.styles.some(function (s) { return s.scoped })
92+
var hasComment = parts.template && parts.template.attrs.comments
9293

9394
var templateCompilerOptions = '?' + JSON.stringify({
9495
id: moduleId,
9596
hasScoped: hasScoped,
97+
hasComment: hasComment,
9698
transformToRequire: options.transformToRequire,
9799
preserveWhitespace: options.preserveWhitespace,
98100
buble: options.buble,

lib/template-compiler/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ module.exports = function (html) {
2323
var compilerOptions = {
2424
preserveWhitespace: options.preserveWhitespace,
2525
modules: defaultModules.concat(userModules || []),
26-
scopeId: options.hasScoped ? options.id : null
26+
scopeId: options.hasScoped ? options.id : null,
27+
comments: options.hasComment
2728
}
2829

2930
var compile = isServer && compiler.ssrCompile && vueOptions.optimizeSSR !== false

test/fixtures/template-comment.vue

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<template comments>
2+
<div>
3+
<h2 class="red">{{msg}}</h2><!-- comment here -->
4+
</div>
5+
</template>
6+
7+
<script>
8+
export default {
9+
comments: true,
10+
data () {
11+
return {
12+
msg: 'Hello from Component A!'
13+
}
14+
}
15+
}
16+
</script>

test/test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,18 @@ function mockRender (options, data) {
9898
children: children
9999
}
100100
}
101+
function e (text = '') {
102+
return {
103+
text: text,
104+
isComment: true
105+
}
106+
}
101107
return options.render.call(Object.assign({
102108
_v (val) {
103109
return val
104110
},
105111
_self: {},
112+
_e: e,
106113
$createElement: h,
107114
_m (index) {
108115
return options.staticRenderFns[index].call(this)
@@ -907,4 +914,22 @@ describe('vue-loader', function () {
907914
done()
908915
})
909916
})
917+
918+
it('template with comments', done => {
919+
test({
920+
entry: './test/fixtures/template-comment.vue'
921+
}, (window, module, rawModule) => {
922+
expect(module.comments).to.equal(true)
923+
var vnode = mockRender(module, {
924+
msg: 'hi'
925+
})
926+
expect(vnode.tag).to.equal('div')
927+
expect(vnode.children.length).to.equal(2)
928+
expect(vnode.children[0].data.staticClass).to.equal('red')
929+
expect(vnode.children[0].children[0]).to.equal('hi')
930+
expect(vnode.children[1].isComment).to.true
931+
expect(vnode.children[1].text).to.equal(' comment here ')
932+
done()
933+
})
934+
})
910935
})

0 commit comments

Comments
 (0)