File tree Expand file tree Collapse file tree 7 files changed +53
-2
lines changed Expand file tree Collapse file tree 7 files changed +53
-2
lines changed Original file line number Diff line number Diff line change
1
+ module . exports = function extractProps ( content ) {
2
+ const DETECT_PROP_DEFINITIONS = / ( p r o p s \. .* ?) ( } | | \. ) / g
3
+ const CHARS_TO_REMOVE = / ( \. | } | | p r o p s ) / g
4
+ const propDefinitions = content . match ( DETECT_PROP_DEFINITIONS )
5
+ if ( ! propDefinitions ) return '{}'
6
+ const props = propDefinitions . map ( ( match ) => {
7
+ const propName = match . trim ( ) . replace ( CHARS_TO_REMOVE , '' )
8
+ return `'${ propName } '`
9
+ } )
10
+ return `[ ${ props . join ( ', ' ) } ]`
11
+ }
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ const addTemplateMapping = require('./add-template-mapping')
6
6
const compileBabel = require ( './compilers/babel-compiler' )
7
7
const compileTypescript = require ( './compilers/typescript-compiler' )
8
8
const compileCoffeeScript = require ( './compilers/coffee-compiler' )
9
+ const extractPropsFromFunctionalTemplate = require ( './process-functional' )
9
10
10
11
const splitRE = / \r ? \n / g
11
12
@@ -32,7 +33,9 @@ function isFunctionalTemplate (parts) {
32
33
function changePartsIfFunctional ( parts ) {
33
34
if ( isFunctionalTemplate ( parts ) ) {
34
35
parts . lang = 'javascript'
35
- parts . script = { type : 'script' , content : 'export default { props: { props: Object } }' }
36
+ const functionalProps = extractPropsFromFunctionalTemplate ( parts . template . content )
37
+ parts . template . content = parts . template . content . replace ( 'props.' , '' )
38
+ parts . script = { type : 'script' , content : `export default { props: ${ functionalProps } }` }
36
39
}
37
40
}
38
41
Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ import FunctionalSFC from './resources/FunctionalSFC.vue'
3
3
4
4
test ( 'processes .vue file with functional template' , ( ) => {
5
5
const wrapper = shallow ( FunctionalSFC , {
6
- propsData : { props : { msg : 'Hello' } }
6
+ propsData : { msg : 'Hello' }
7
7
} )
8
8
expect ( wrapper . is ( 'div' ) ) . toBe ( true )
9
9
expect ( wrapper . text ( ) . trim ( ) ) . toBe ( 'Hello' )
Original file line number Diff line number Diff line change
1
+ import { mount } from 'vue-test-utils'
2
+ import FunctionalSFCParent from './resources/FunctionalSFCParent.vue'
3
+
4
+ test ( 'processes .vue file with functional template from parent' , ( ) => {
5
+ const wrapper = mount ( FunctionalSFCParent )
6
+ expect ( wrapper . text ( ) . trim ( ) ) . toBe ( 'TEST' )
7
+ } )
Original file line number Diff line number Diff line change
1
+ import extractProps from '../lib/process-functional'
2
+
3
+ describe ( 'when extracting props with props. prefix from functional template content' , ( ) => {
4
+ it ( 'extracts interpolated props ' , ( ) => {
5
+ const content = '<div> {{props.msg1 }} {{props.msg2}}</div>'
6
+
7
+ expect ( extractProps ( content ) ) . toBe ( "[ 'msg1', 'msg2' ]" )
8
+ } )
9
+
10
+ it ( 'extracts props used in v-for' , ( ) => {
11
+ const content = '<div v-for="bar in props.foo.bar"> {{ bar }}} </div>'
12
+
13
+ expect ( extractProps ( content ) ) . toBe ( "[ 'foo' ]" )
14
+ } )
15
+ } )
Original file line number Diff line number Diff line change 3
3
{{ props.msg }}
4
4
</div >
5
5
</template >
6
+
Original file line number Diff line number Diff line change
1
+ <template >
2
+ <FunctionalSFC msg =" TEST" />
3
+ </template >
4
+
5
+ <script >
6
+ import Vue from ' vue'
7
+ import FunctionalSFC from ' ./FunctionalSFC'
8
+
9
+ Vue .component (' FunctionalSFC' , FunctionalSFC)
10
+
11
+ export default {
12
+ components: FunctionalSFC
13
+ }
14
+ </script >
You can’t perform that action at this time.
0 commit comments