Skip to content

Commit 585b70c

Browse files
committed
feat: finish support functional component templates
1 parent 329eb19 commit 585b70c

File tree

4 files changed

+51
-47
lines changed

4 files changed

+51
-47
lines changed

lib/component-normalizer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module.exports = function normalizeComponent (
3030
if (compiledTemplate) {
3131
options.render = compiledTemplate.render
3232
options.staticRenderFns = compiledTemplate.staticRenderFns
33-
options.compiled = true
33+
options._compiled = true
3434
}
3535

3636
// functional template

lib/loader.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -89,11 +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 && parts.template.attrs.comments
93-
var functional = parts.template && parts.template.functional
94-
var bubleTemplateOptions = Object.create(options.buble || {})
95-
bubleTemplateOptions.transforms = Object.create(bubleTemplateOptions.transforms || {})
96-
bubleTemplateOptions.transforms.stripWithFunctional = functional
92+
var templateAttrs = parts.template && parts.template.attrs && parts.template.attrs
93+
var hasComment = templateAttrs && templateAttrs.comments
94+
var functionalTemplate = templateAttrs && templateAttrs.functional
95+
var bubleTemplateOptions = Object.assign({}, options.buble)
96+
bubleTemplateOptions.transforms = Object.assign({}, bubleTemplateOptions.transforms)
97+
bubleTemplateOptions.transforms.stripWithFunctional = functionalTemplate
9798

9899
var templateCompilerOptions = '?' + JSON.stringify({
99100
id: moduleId,
@@ -267,7 +268,7 @@ module.exports = function (content) {
267268

268269
// template functional
269270
output += '/* template functional */\n '
270-
output += 'var __vue_template_functional__ = ' + ((template && template.functional) ? 'true' : 'null') + '\n'
271+
output += 'var __vue_template_functional__ = ' + (functionalTemplate ? 'true' : 'false') + '\n'
271272

272273
// style
273274
output += '/* styles */\n'

lib/template-compiler/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ module.exports = function (html) {
5252
: 'module.exports={render:function(){},staticRenderFns:[]}'
5353
} else {
5454
var bubleOptions = options.buble
55-
var stripWithFunctional = bubleOptions.transforms.stripWithFunctional
5655
var stripWith = bubleOptions.transforms.stripWith !== false
56+
var stripWithFunctional = bubleOptions.transforms.stripWithFunctional
5757

5858
var staticRenderFns = compiled.staticRenderFns.map((fn) => toFunction(fn, stripWithFunctional))
5959

test/test.js

+42-39
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ var webpack = require('webpack')
77
var MemoryFS = require('memory-fs')
88
var expect = require('chai').expect
99
var hash = require('hash-sum')
10+
var Vue = require('vue')
1011
var SSR = require('vue-server-renderer')
11-
var Vue = require('vue/dist/vue.runtime.js')
1212
// var compiler = require('../lib/template-compiler')
1313
var normalizeNewline = require('normalize-newline')
1414
var ExtractTextPlugin = require('extract-text-webpack-plugin')
@@ -933,44 +933,47 @@ describe('vue-loader', function () {
933933
})
934934

935935
// Vue required tests for more complete test cases
936-
it('should allow functional template', done => {
937-
test({
938-
entry: './test/fixtures/functional-root.vue',
939-
vue: {
940-
preserveWhitespace: false
941-
}
942-
}, (window, module) => {
943-
expect(module.components.Functional.compiled).to.equal(true)
944-
expect(module.components.Functional.functional).to.equal(true)
945-
expect(module.components.Functional.staticRenderFns).to.exist
946-
expect(module.components.Functional.render).to.be.a('function')
947-
948-
const vnode = mockRender(module, {
949-
fn () {
950-
done()
936+
// this test case requires Vue >= 2.5
937+
if (Number(Vue.version.split('.')[1]) >= 5) {
938+
it('functional template', done => {
939+
test({
940+
entry: './test/fixtures/functional-root.vue',
941+
vue: {
942+
preserveWhitespace: false
951943
}
952-
}).children[0]
953-
954-
// Basic vnode
955-
expect(vnode.children[0].data.staticClass).to.equal('red')
956-
expect(vnode.children[0].children[0].text).to.equal('hello')
957-
// Default slot vnode
958-
expect(vnode.children[1].tag).to.equal('span')
959-
expect(vnode.children[1].children[0].text).to.equal('hello')
960-
// Named slot vnode
961-
expect(vnode.children[2].tag).to.equal('div')
962-
expect(vnode.children[2].children[0].text).to.equal('Second slot')
963-
// // Scoped slot vnode
964-
expect(vnode.children[3].text).to.equal('hello')
965-
// // Static content vnode
966-
expect(vnode.children[4].tag).to.equal('div')
967-
expect(vnode.children[4].children[0].text).to.equal('Some ')
968-
expect(vnode.children[4].children[1].tag).to.equal('span')
969-
expect(vnode.children[4].children[1].children[0].text).to.equal('text')
970-
// // v-if vnode
971-
expect(vnode.children[5].text).to.equal('')
972-
973-
vnode.children[6].data.on.click()
944+
}, (window, module) => {
945+
expect(module.components.Functional._compiled).to.equal(true)
946+
expect(module.components.Functional.functional).to.equal(true)
947+
expect(module.components.Functional.staticRenderFns).to.exist
948+
expect(module.components.Functional.render).to.be.a('function')
949+
950+
const vnode = mockRender(module, {
951+
fn () {
952+
done()
953+
}
954+
}).children[0]
955+
956+
// Basic vnode
957+
expect(vnode.children[0].data.staticClass).to.equal('red')
958+
expect(vnode.children[0].children[0].text).to.equal('hello')
959+
// Default slot vnode
960+
expect(vnode.children[1].tag).to.equal('span')
961+
expect(vnode.children[1].children[0].text).to.equal('hello')
962+
// Named slot vnode
963+
expect(vnode.children[2].tag).to.equal('div')
964+
expect(vnode.children[2].children[0].text).to.equal('Second slot')
965+
// // Scoped slot vnode
966+
expect(vnode.children[3].text).to.equal('hello')
967+
// // Static content vnode
968+
expect(vnode.children[4].tag).to.equal('div')
969+
expect(vnode.children[4].children[0].text).to.equal('Some ')
970+
expect(vnode.children[4].children[1].tag).to.equal('span')
971+
expect(vnode.children[4].children[1].children[0].text).to.equal('text')
972+
// // v-if vnode
973+
expect(vnode.children[5].text).to.equal('')
974+
975+
vnode.children[6].data.on.click()
976+
})
974977
})
975-
})
978+
}
976979
})

0 commit comments

Comments
 (0)