File tree 5 files changed +115
-0
lines changed
5 files changed +115
-0
lines changed Original file line number Diff line number Diff line change @@ -18,6 +18,66 @@ return { props, bar }
18
18
}"
19
19
` ;
20
20
21
+ exports [` defineProps > custom element retains the props type & default value & production mode 1` ] = `
22
+ "import { defineComponent as _defineComponent } from 'vue'
23
+ interface Props {
24
+ foo ?: number ;
25
+ }
26
+
27
+ export default /*#__PURE__*/_defineComponent({
28
+ __name : ' app.ce' ,
29
+ props : {
30
+ foo: { default: 5.5 , type: Number }
31
+ },
32
+ setup (__props : any , { expose: __expose }) {
33
+ __expose();
34
+
35
+ const props = __props;
36
+
37
+ return { props }
38
+ }
39
+
40
+ })"
41
+ ` ;
42
+
43
+ exports [` defineProps > custom element retains the props type & production mode 1` ] = `
44
+ "import { defineComponent as _defineComponent } from 'vue'
45
+
46
+ export default /*#__PURE__*/_defineComponent({
47
+ __name : ' app.ce' ,
48
+ props : {
49
+ foo: {type: Number }
50
+ },
51
+ setup (__props : any , { expose: __expose }) {
52
+ __expose();
53
+
54
+ const props = __props
55
+
56
+ return { props }
57
+ }
58
+
59
+ })"
60
+ ` ;
61
+
62
+ exports [` defineProps > custom element retains the props type w/ production mode 1` ] = `
63
+ "import { defineComponent as _defineComponent } from 'vue'
64
+
65
+ export default /*#__PURE__*/_defineComponent({
66
+ __name : ' app.ce' ,
67
+ props : {
68
+ foo: {type: Number }
69
+ },
70
+ setup (__props : any , { expose: __expose }) {
71
+ __expose();
72
+
73
+ const props = __props
74
+
75
+ return { props }
76
+ }
77
+
78
+ })"
79
+ ` ;
80
+
21
81
exports [` defineProps > defineProps w/ runtime options 1` ] = `
22
82
"import { defineComponent as _defineComponent } from 'vue'
23
83
Original file line number Diff line number Diff line change @@ -710,4 +710,35 @@ const props = defineProps({ foo: String })
710
710
'da-sh' : BindingTypes . PROPS
711
711
} )
712
712
} )
713
+
714
+ // #8989
715
+ test ( 'custom element retains the props type & production mode' , ( ) => {
716
+ const { content } = compile (
717
+ `<script setup lang="ts">
718
+ const props = defineProps<{ foo: number}>()
719
+ </script>` ,
720
+ { isProd : true , customElement : filename => / \. c e \. v u e $ / . test ( filename ) } ,
721
+ { filename : 'app.ce.vue' }
722
+ )
723
+
724
+ expect ( content ) . toMatch ( `foo: {type: Number}` )
725
+ assertCode ( content )
726
+ } )
727
+
728
+ test ( 'custom element retains the props type & default value & production mode' , ( ) => {
729
+ const { content } = compile (
730
+ `<script setup lang="ts">
731
+ interface Props {
732
+ foo?: number;
733
+ }
734
+ const props = withDefaults(defineProps<Props>(), {
735
+ foo: 5.5,
736
+ });
737
+ </script>` ,
738
+ { isProd : true , customElement : filename => / \. c e \. v u e $ / . test ( filename ) } ,
739
+ { filename : 'app.ce.vue' }
740
+ )
741
+ expect ( content ) . toMatch ( `foo: { default: 5.5, type: Number }` )
742
+ assertCode ( content )
743
+ } )
713
744
} )
Original file line number Diff line number Diff line change @@ -130,6 +130,10 @@ export interface SFCScriptCompileOptions {
130
130
* using it, disable this and switch to the [Vue Macros implementation](https://vue-macros.sxzz.moe/features/reactivity-transform.html).
131
131
*/
132
132
reactivityTransform ?: boolean
133
+ /**
134
+ * Transform Vue SFCs into custom elements.
135
+ */
136
+ customElement ?: boolean | ( ( filename : string ) => boolean )
133
137
}
134
138
135
139
export interface ImportBinding {
Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ import { TypeScope } from './resolveType'
12
12
export class ScriptCompileContext {
13
13
isJS : boolean
14
14
isTS : boolean
15
+ isCE = false
15
16
16
17
scriptAst : Program | null
17
18
scriptSetupAst : Program | null
@@ -95,6 +96,14 @@ export class ScriptCompileContext {
95
96
scriptSetupLang === 'ts' ||
96
97
scriptSetupLang === 'tsx'
97
98
99
+ const customElement = options . customElement
100
+ const filename = this . descriptor . filename
101
+ if ( customElement ) {
102
+ this . isCE =
103
+ typeof customElement === 'boolean'
104
+ ? customElement
105
+ : customElement ( filename )
106
+ }
98
107
// resolve parser plugins
99
108
const plugins : ParserPlugin [ ] = resolveParserPlugins (
100
109
( scriptLang || scriptSetupLang ) ! ,
Original file line number Diff line number Diff line change @@ -276,6 +276,17 @@ function genRuntimePropFromType(
276
276
defaultString
277
277
] ) } }`
278
278
} else {
279
+ // #8989 for custom element, should keep the type
280
+ if ( ctx . isCE ) {
281
+ if ( defaultString ) {
282
+ return `${ finalKey } : ${ `{ ${ defaultString } , type: ${ toRuntimeTypeString (
283
+ type
284
+ ) } }`} `
285
+ } else {
286
+ return `${ finalKey } : {type: ${ toRuntimeTypeString ( type ) } }`
287
+ }
288
+ }
289
+
279
290
// production: checks are useless
280
291
return `${ finalKey } : ${ defaultString ? `{ ${ defaultString } }` : `{}` } `
281
292
}
You can’t perform that action at this time.
0 commit comments