Skip to content

fix: process functional component #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions lib/extract-props.js

This file was deleted.

17 changes: 4 additions & 13 deletions lib/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const addTemplateMapping = require('./add-template-mapping')
const compileBabel = require('./compilers/babel-compiler')
const compileTypescript = require('./compilers/typescript-compiler')
const compileCoffeeScript = require('./compilers/coffee-compiler')
const extractPropsFromFunctionalTemplate = require('./extract-props')
const processStyle = require('./process-style')
const getVueJestConfig = require('./get-vue-jest-config')
const fs = require('fs')
Expand All @@ -30,23 +29,11 @@ function processScript (scriptPart, vueJestConfig) {
return compileBabel(scriptPart.content, undefined, undefined, vueJestConfig)
}

function changePartsIfFunctional (parts) {
const isFunctional = parts.template && parts.template.attrs && parts.template.attrs.functional
if (isFunctional) {
parts.lang = 'javascript'
const functionalProps = extractPropsFromFunctionalTemplate(parts.template.content)
parts.template.content = parts.template.content.replace(/props./g, '')
parts.script = { type: 'script', content: `export default { props: ${functionalProps} }` }
}
}

module.exports = function (src, filePath, jestConfig) {
const vueJestConfig = getVueJestConfig(jestConfig)

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

changePartsIfFunctional(parts)

if (parts.script && parts.script.src) {
parts.script.content = fs.readFileSync(join(filePath, '..', parts.script.src), 'utf8')
}
Expand Down Expand Up @@ -80,6 +67,10 @@ module.exports = function (src, filePath, jestConfig) {
output += '__vue__options__.render = ' + renderFunctions.render + '\n' +
'__vue__options__.staticRenderFns = ' + renderFunctions.staticRenderFns + '\n'

if (parts.template.attrs.functional) {
output += '__vue__options__.functional = true\n'
}

if (map) {
const beforeLines = output.split(splitRE).length
addTemplateMapping(script, parts, output, map, beforeLines)
Expand Down
20 changes: 13 additions & 7 deletions lib/template-compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,27 @@ function getTemplateContent (templatePart, config) {

module.exports = function compileTemplate (templatePart, config) {
var templateContent = getTemplateContent(templatePart, config)

var compiled = vueCompiler.compile(templateContent)
if (compiled.errors.length) {
compiled.errors.forEach(function (msg) {
console.error('\n' + chalk.red(msg) + '\n')
})
throwError('Vue template compilation failed')
} else {
return {
render: toFunction(compiled.render),
staticRenderFns: '[' + compiled.staticRenderFns.map(toFunction).join(',') + ']'
}
return compile(compiled, templatePart.attrs.functional)
}
}

function toFunction (code) {
return transpile('function render () {' + code + '}')
function compile (compiled, isFunctional) {
function toFunction (code) {
var renderArgs = isFunctional ? '_h, _vm' : ''
return transpile('function render (' + renderArgs + ') {' + code + '}', {
transforms: { stripWithFunctional: isFunctional }
})
}

return {
render: toFunction(compiled.render),
staticRenderFns: '[' + compiled.staticRenderFns.map(toFunction).join(',') + ']'
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"object-assign": "^4.1.1",
"source-map": "^0.5.6",
"tsconfig": "^7.0.0",
"vue-template-es2015-compiler": "^1.5.3"
"vue-template-es2015-compiler": "^1.6.0"
},
"jest": {
"moduleFileExtensions": [
Expand Down
9 changes: 8 additions & 1 deletion test/FunctionalSFC.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ let wrapper
const clickSpy = jest.fn()
beforeEach(() => {
wrapper = shallow(FunctionalSFC, {
propsData: { msg: { id: 1, title: 'foo' }, onClick: clickSpy }
context: {
props: { msg: { id: 1, title: 'foo' }, onClick: clickSpy }
}
})
})

Expand All @@ -18,4 +20,9 @@ describe('Processes .vue file with functional template', () => {
wrapper.trigger('click')
expect(clickSpy).toHaveBeenCalledWith(1)
})

it('is functional', () => {
// note: for new version of @vue/vue-utils we can use wrapper.isFunctionalComponent for this
expect(wrapper.vm._vnode.fnOptions.functional).toBe(true)
})
})
31 changes: 0 additions & 31 deletions test/extract-props.spec.js

This file was deleted.

2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4235,7 +4235,7 @@ vue-template-compiler@^2.4.2:
de-indent "^1.0.2"
he "^1.1.0"

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

Expand Down