Skip to content

Commit 84ffd55

Browse files
agualiseddyerburgh
authored andcommitted
feat(functional components): process functional single file components (#17)
1 parent d11ff1e commit 84ffd55

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

lib/process.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,26 @@ function processScript (scriptPart) {
2121
return compileBabel(scriptPart.content)
2222
}
2323

24+
function isFunctionalTemplate (parts) {
25+
try {
26+
if (parts.template.attrs.functional === true) return true
27+
} catch (error) {
28+
return false
29+
}
30+
}
31+
32+
function changePartsIfFunctional (parts) {
33+
if (isFunctionalTemplate(parts)) {
34+
parts.lang = 'javascript'
35+
parts.script = { type: 'script', content: 'export default { props: { props: Object } }' }
36+
}
37+
}
38+
2439
module.exports = function (src, path) {
2540
var parts = vueCompiler.parseComponent(src, { pad: true })
2641

42+
changePartsIfFunctional(parts)
43+
2744
const result = processScript(parts.script)
2845
const script = result.code
2946
const inputMap = result.sourceMap

test/FunctionalSFC.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { shallow } from 'vue-test-utils'
2+
import FunctionalSFC from './resources/FunctionalSFC.vue'
3+
4+
test('processes .vue file with functional template', () => {
5+
const wrapper = shallow(FunctionalSFC, {
6+
propsData: { props: { msg: 'Hello' }}
7+
})
8+
expect(wrapper.is('div')).toBe(true)
9+
expect(wrapper.text().trim()).toBe('Hello')
10+
})

test/resources/FunctionalSFC.vue

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<template functional>
2+
<div class="hello">
3+
{{ props.msg }}
4+
</div>
5+
</template>

0 commit comments

Comments
 (0)