Skip to content

Commit 88d4947

Browse files
committed
fix: process functional component
1 parent fc3164d commit 88d4947

7 files changed

+36
-78
lines changed

lib/extract-props.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

lib/process.js

Lines changed: 5 additions & 14 deletions
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
}
@@ -78,7 +65,11 @@ module.exports = function (src, filePath, jestConfig) {
7865
const renderFunctions = compileTemplate(parts.template, vueJestConfig)
7966

8067
output += '__vue__options__.render = ' + renderFunctions.render + '\n' +
81-
'__vue__options__.staticRenderFns = ' + renderFunctions.staticRenderFns + '\n'
68+
'__vue__options__.staticRenderFns = ' + renderFunctions.staticRenderFns + '\n';
69+
70+
if (parts.template.attrs.functional) {
71+
output += '__vue__options__.functional = true\n';
72+
}
8273

8374
if (map) {
8475
const beforeLines = output.split(splitRE).length

lib/template-compiler.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,28 @@ 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+
37+
function toFunction (code) {
38+
var renderArgs = isFunctional ? '_h, _vm' : ''
39+
return transpile('function render (' + renderArgs + ') {' + code + '}', {
40+
transforms: { stripWithFunctional: isFunctional }
41+
})
42+
}
43+
44+
return {
45+
render: toFunction(compiled.render),
46+
staticRenderFns: '[' + compiled.staticRenderFns.map(toFunction).join(',') + ']'
47+
}
4148
}

package.json

Lines changed: 1 addition & 1 deletion
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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import { shallow } from 'vue-test-utils'
22
import FunctionalSFC from './resources/FunctionalSFC.vue'
33

4-
let wrapper
5-
const clickSpy = jest.fn()
6-
beforeEach(() => {
7-
wrapper = shallow(FunctionalSFC, {
8-
propsData: { msg: { id: 1, title: 'foo' }, onClick: clickSpy }
4+
describe('Processes .vue file with functional template', () => {
5+
let wrapper
6+
const clickSpy = jest.fn()
7+
beforeEach(() => {
8+
wrapper = shallow(FunctionalSFC, {
9+
context: {
10+
props: { msg: { id: 1, title: 'foo' }, onClick: clickSpy }
11+
}
12+
})
913
})
10-
})
1114

12-
describe('Processes .vue file with functional template', () => {
1315
it('with nested props', () => {
1416
expect(wrapper.text().trim()).toBe('foo')
1517
})
@@ -18,4 +20,9 @@ describe('Processes .vue file with functional template', () => {
1820
wrapper.trigger('click')
1921
expect(clickSpy).toHaveBeenCalledWith(1)
2022
})
21-
})
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+
})
28+
})

test/extract-props.spec.js

Lines changed: 0 additions & 31 deletions
This file was deleted.

yarn.lock

Lines changed: 1 addition & 1 deletion
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)