Skip to content

Commit 7a0e530

Browse files
committed
fix: process functional component
1 parent fc3164d commit 7a0e530

7 files changed

+27
-70
lines changed

lib/extract-props.js

-16
This file was deleted.

lib/process.js

+4-13
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const addTemplateMapping = require('./add-template-mapping')
55
const compileBabel = require('./compilers/babel-compiler')
66
const compileTypescript = require('./compilers/typescript-compiler')
77
const compileCoffeeScript = require('./compilers/coffee-compiler')
8-
const extractPropsFromFunctionalTemplate = require('./extract-props')
98
const processStyle = require('./process-style')
109
const getVueJestConfig = require('./get-vue-jest-config')
1110
const fs = require('fs')
@@ -30,23 +29,11 @@ function processScript (scriptPart, vueJestConfig) {
3029
return compileBabel(scriptPart.content, undefined, undefined, vueJestConfig)
3130
}
3231

33-
function changePartsIfFunctional (parts) {
34-
const isFunctional = parts.template && parts.template.attrs && parts.template.attrs.functional
35-
if (isFunctional) {
36-
parts.lang = 'javascript'
37-
const functionalProps = extractPropsFromFunctionalTemplate(parts.template.content)
38-
parts.template.content = parts.template.content.replace(/props./g, '')
39-
parts.script = { type: 'script', content: `export default { props: ${functionalProps} }` }
40-
}
41-
}
42-
4332
module.exports = function (src, filePath, jestConfig) {
4433
const vueJestConfig = getVueJestConfig(jestConfig)
4534

4635
var parts = vueCompiler.parseComponent(src, { pad: true })
4736

48-
changePartsIfFunctional(parts)
49-
5037
if (parts.script && parts.script.src) {
5138
parts.script.content = fs.readFileSync(join(filePath, '..', parts.script.src), 'utf8')
5239
}
@@ -80,6 +67,10 @@ module.exports = function (src, filePath, jestConfig) {
8067
output += '__vue__options__.render = ' + renderFunctions.render + '\n' +
8168
'__vue__options__.staticRenderFns = ' + renderFunctions.staticRenderFns + '\n'
8269

70+
if (parts.template.attrs.functional) {
71+
output += '__vue__options__.functional = true\n'
72+
}
73+
8374
if (map) {
8475
const beforeLines = output.split(splitRE).length
8576
addTemplateMapping(script, parts, output, map, beforeLines)

lib/template-compiler.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,27 @@ function getTemplateContent (templatePart, config) {
2121

2222
module.exports = function compileTemplate (templatePart, config) {
2323
var templateContent = getTemplateContent(templatePart, config)
24-
2524
var compiled = vueCompiler.compile(templateContent)
2625
if (compiled.errors.length) {
2726
compiled.errors.forEach(function (msg) {
2827
console.error('\n' + chalk.red(msg) + '\n')
2928
})
3029
throwError('Vue template compilation failed')
3130
} else {
32-
return {
33-
render: toFunction(compiled.render),
34-
staticRenderFns: '[' + compiled.staticRenderFns.map(toFunction).join(',') + ']'
35-
}
31+
return compile(compiled, templatePart.attrs.functional)
3632
}
3733
}
3834

39-
function toFunction (code) {
40-
return transpile('function render () {' + code + '}')
35+
function compile (compiled, isFunctional) {
36+
function toFunction (code) {
37+
var renderArgs = isFunctional ? '_h, _vm' : ''
38+
return transpile('function render (' + renderArgs + ') {' + code + '}', {
39+
transforms: { stripWithFunctional: isFunctional }
40+
})
41+
}
42+
43+
return {
44+
render: toFunction(compiled.render),
45+
staticRenderFns: '[' + compiled.staticRenderFns.map(toFunction).join(',') + ']'
46+
}
4147
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"object-assign": "^4.1.1",
6868
"source-map": "^0.5.6",
6969
"tsconfig": "^7.0.0",
70-
"vue-template-es2015-compiler": "^1.5.3"
70+
"vue-template-es2015-compiler": "^1.6.0"
7171
},
7272
"jest": {
7373
"moduleFileExtensions": [

test/FunctionalSFC.spec.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ let wrapper
55
const clickSpy = jest.fn()
66
beforeEach(() => {
77
wrapper = shallow(FunctionalSFC, {
8-
propsData: { msg: { id: 1, title: 'foo' }, onClick: clickSpy }
8+
context: {
9+
props: { msg: { id: 1, title: 'foo' }, onClick: clickSpy }
10+
}
911
})
1012
})
1113

@@ -18,4 +20,9 @@ describe('Processes .vue file with functional template', () => {
1820
wrapper.trigger('click')
1921
expect(clickSpy).toHaveBeenCalledWith(1)
2022
})
23+
24+
it('is functional', () => {
25+
// note: for new version of @vue/vue-utils we can use wrapper.isFunctionalComponent for this
26+
expect(wrapper.vm._vnode.fnOptions.functional).toBe(true)
27+
})
2128
})

test/extract-props.spec.js

-31
This file was deleted.

yarn.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -4235,7 +4235,7 @@ vue-template-compiler@^2.4.2:
42354235
de-indent "^1.0.2"
42364236
he "^1.1.0"
42374237

4238-
vue-template-es2015-compiler@^1.5.3:
4238+
vue-template-es2015-compiler@^1.6.0:
42394239
version "1.6.0"
42404240
resolved "https://registry.yarnpkg.com/vue-template-es2015-compiler/-/vue-template-es2015-compiler-1.6.0.tgz#dc42697133302ce3017524356a6c61b7b69b4a18"
42414241

0 commit comments

Comments
 (0)