Skip to content

Commit

Permalink
support comment option in template (#897)
Browse files Browse the repository at this point in the history
* support comment option in template

* Update loader.js

* Update template-comment.vue
  • Loading branch information
Kingwl authored and yyx990803 committed Jul 29, 2017
1 parent 7253e32 commit e6abc19
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/loader.js
Expand Up @@ -89,10 +89,12 @@ module.exports = function (content) {
var output = ''
var parts = parse(content, fileName, this.sourceMap)
var hasScoped = parts.styles.some(function (s) { return s.scoped })
var hasComment = parts.template && parts.template.attrs.comments

This comment has been minimized.

Copy link
@ccforward

ccforward Aug 4, 2017

This line will report an error.
image

This comment has been minimized.

Copy link
@yyx990803

yyx990803 Aug 4, 2017

Member

Can you open a separate issue for this?


var templateCompilerOptions = '?' + JSON.stringify({
id: moduleId,
hasScoped: hasScoped,
hasComment: hasComment,
transformToRequire: options.transformToRequire,
preserveWhitespace: options.preserveWhitespace,
buble: options.buble,
Expand Down
3 changes: 2 additions & 1 deletion lib/template-compiler/index.js
Expand Up @@ -23,7 +23,8 @@ module.exports = function (html) {
var compilerOptions = {
preserveWhitespace: options.preserveWhitespace,
modules: defaultModules.concat(userModules || []),
scopeId: options.hasScoped ? options.id : null
scopeId: options.hasScoped ? options.id : null,
comments: options.hasComment
}

var compile = isServer && compiler.ssrCompile && vueOptions.optimizeSSR !== false
Expand Down
16 changes: 16 additions & 0 deletions test/fixtures/template-comment.vue
@@ -0,0 +1,16 @@
<template comments>
<div>
<h2 class="red">{{msg}}</h2><!-- comment here -->
</div>
</template>

<script>
export default {
comments: true,
data () {
return {
msg: 'Hello from Component A!'
}
}
}
</script>
25 changes: 25 additions & 0 deletions test/test.js
Expand Up @@ -98,11 +98,18 @@ function mockRender (options, data) {
children: children
}
}
function e (text = '') {
return {
text: text,
isComment: true
}
}
return options.render.call(Object.assign({
_v (val) {
return val
},
_self: {},
_e: e,
$createElement: h,
_m (index) {
return options.staticRenderFns[index].call(this)
Expand Down Expand Up @@ -907,4 +914,22 @@ describe('vue-loader', function () {
done()
})
})

it('template with comments', done => {
test({
entry: './test/fixtures/template-comment.vue'
}, (window, module, rawModule) => {
expect(module.comments).to.equal(true)
var vnode = mockRender(module, {
msg: 'hi'
})
expect(vnode.tag).to.equal('div')
expect(vnode.children.length).to.equal(2)
expect(vnode.children[0].data.staticClass).to.equal('red')
expect(vnode.children[0].children[0]).to.equal('hi')
expect(vnode.children[1].isComment).to.true
expect(vnode.children[1].text).to.equal(' comment here ')
done()
})
})
})

0 comments on commit e6abc19

Please sign in to comment.