Skip to content

Commit d16081f

Browse files
committed
feat(core): refactor global types and enhance dollar vars support in templates
1 parent 7603e6c commit d16081f

File tree

12 files changed

+133
-77
lines changed

12 files changed

+133
-77
lines changed

inspect-extension/components/composition/basic01.mpx

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
<template>
2-
<view class="title"> title: {{ title }} </view>
3-
<view wx:class="list" bindtap="onClick">
4-
<view> msg: {{ msg }} </view>
5-
<view> obj: {{ obj.a + obj.b }} </view>
6-
<view> content: {{ content }} </view>
2+
<view>
3+
<view class="title"> title: {{ title }} </view>
4+
<view wx:class="list" bindtap="onClick">
5+
<view> msg: {{ msg }} </view>
6+
<view> obj: {{ obj.a + obj.b }} </view>
7+
<view> content: {{ content }} </view>
8+
</view>
9+
<view class="title">
10+
{{ $t('local.title') }}
11+
<view>
12+
{{ $tm('local.driver') }}
13+
</view>
14+
</view>
715
</view>
816
</template>
917

@@ -22,12 +30,12 @@ const content = computed(() => {
2230
return msg.value
2331
})
2432
const obj = {
25-
a: 1,
26-
b: 2
33+
a: 1,
34+
b: 2
2735
}
2836

2937
const onClick = (a?: number) => {
30-
console.log('click', a)
38+
console.log('click', a)
3139
}
3240

3341
defineExpose({
@@ -44,5 +52,4 @@ defineExpose({
4452
color green
4553
.list
4654
background-color red
47-
</style>
48-
55+
</style>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<template>
2+
<view>
3+
<view class="title">
4+
{{ $t('local.title') }}
5+
<view>
6+
{{ $tm('local.driver') }}
7+
</view>
8+
</view>
9+
</view>
10+
</template>
11+
12+
<script setup lang="ts">
13+
defineExpose({})
14+
</script>
15+
16+
<style lang="stylus">
17+
.title
18+
color green
19+
.list
20+
background-color red
21+
</style>

inspect-extension/components/options/basic01.mpx

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
<template>
2-
<view class="title">
3-
title: {{ title }}
4-
titleRef: {{ titleRef }}
5-
</view>
6-
<view wx:class="list" bindtap="onClick">
7-
<view> parentText: {{ parentText }} </view>
8-
<view> msg: {{ msg }} </view>
9-
<view> obj: {{ obj.a + obj.b }} </view>
10-
<view> content: {{ content }} </view>
2+
<view>
3+
<view class="title">
4+
title: {{ title }}
5+
titleRef: {{ titleRef }}
6+
</view>
7+
<view wx:class="list" bindtap="onClick">
8+
<view> parentText: {{ parentText }} </view>
9+
<view> msg: {{ msg }} </view>
10+
<view> obj: {{ obj.a + obj.b }} </view>
11+
<view> content: {{ content }} </view>
12+
</view>
13+
<view class="title">
14+
{{ $t('local.title') }}
15+
<view>
16+
{{ $tm('local.driver') }}
17+
</view>
18+
</view>
1119
</view>
1220
</template>
1321

1422
<script lang="ts">
1523
import { createComponent, ref } from '@mpxjs/core'
1624

1725
createComponent({
18-
properties:{
26+
properties: {
1927
parentText: {
2028
type: String,
2129
value: 'parent text',
@@ -47,7 +55,7 @@ createComponent({
4755
},
4856
methods: {
4957
onClick() {
50-
console.log('click', this.titleRef, this.content)
58+
console.log('click', this.titleRef, this.content, this.$forceUpdate())
5159
this.count++
5260
}
5361
},
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<template>
2+
<view>
3+
<view class="title">
4+
{{ $t('local.title') }}
5+
<view>
6+
{{ $tm('local.driver') }}
7+
</view>
8+
</view>
9+
</view>
10+
</template>
11+
12+
<script lang="ts">
13+
import { createComponent } from '@mpxjs/core'
14+
15+
createComponent({
16+
lifetimes: {
17+
ready() {
18+
console.log('ready')
19+
},
20+
},
21+
})
22+
</script>
23+
24+
<style lang="stylus">
25+
.title
26+
color green
27+
.list
28+
background-color red
29+
</style>
File renamed without changes.
File renamed without changes.
File renamed without changes.

packages/language-core/src/codegen/globalTypes/defineComponentTypes.ts

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
const globalTypes = `
1+
import { MpxCompilerOptions } from '../../types'
2+
3+
const globalTypes = () => `
24
// #region DefineComponent - global types
35
function DefineComponent<
46
D extends Data = {},
@@ -15,7 +17,7 @@ const globalTypes = `
1517
// #endregion
1618
1719
// #region DefinePage - global types
18-
export function DefinePage<
20+
function DefinePage<
1921
D extends Data = {},
2022
P extends Properties = {},
2123
C = {},
@@ -29,7 +31,7 @@ const globalTypes = `
2931
// #endregion
3032
`
3133

32-
const localTypes = `
34+
const localTypes = (lib: MpxCompilerOptions['lib']) => `
3335
// #region DefineComponent - local types
3436
type Data = object | (() => object)
3537
interface Properties {
@@ -122,19 +124,7 @@ type FullPropType<T> = {
122124
value?: T
123125
optionalTypes?: WechatMiniprogram.Component.ShortProperty[]
124126
}
125-
export type PropType<T> = {
126-
__type: T
127-
} & (T extends string
128-
? StringConstructor
129-
: T extends number
130-
? NumberConstructor
131-
: T extends boolean
132-
? BooleanConstructor
133-
: T extends any[]
134-
? ArrayConstructor
135-
: T extends object
136-
? ObjectConstructor
137-
: never)
127+
type PropType = import('${lib}').PropType
138128
type UnboxMixinField<T extends Mixin<{}, {}, {}, {}>, F> = F extends keyof T ? T[F] : {}
139129
type UnboxMixinsField<Mi extends Array<any>, F> = UnionToIntersection<
140130
RequiredPropertiesForUnion<UnboxMixinField<ArrayType<Mi>, F>>
@@ -152,7 +142,7 @@ interface Mixin<D, P, C, M> {
152142
methods?: M
153143
[index: string]: any
154144
}
155-
export type ComponentIns<
145+
type ComponentIns<
156146
D extends Data = {},
157147
P extends Properties = {},
158148
C = {},
@@ -164,18 +154,16 @@ export type ComponentIns<
164154
UnboxMixinsField<Mi, 'data'> &
165155
M &
166156
UnboxMixinsField<Mi, 'methods'> & {
167-
[K in keyof S]: S[K] extends import('@mpxjs/core').Ref<infer V> ? V : S[K]
157+
[K in keyof S]: S[K] extends import('${lib}').Ref<infer V> ? V : S[K]
168158
} & GetPropsType<P & UnboxMixinsField<Mi, 'properties'>> &
169159
GetComputedType<C & UnboxMixinsField<Mi, 'computed'>> &
170160
WxComponentIns<D, P, M> &
171161
MpxComponentIns &
172162
MpxComProps<O>
173163
type GetDataType<T> = T extends () => any ? ReturnType<T> : T
174164
type MpxComProps<O> = { $rawOptions: O }
175-
export interface MpxComponentIns {
176-
[k: string]: any
177-
}
178-
export type GetComputedType<T> = {
165+
type MpxComponentIns = import('${lib}').MpxComponentIns
166+
type GetComputedType<T> = {
179167
[K in keyof T]: T[K] extends { get: (...args: any[]) => infer R }
180168
? R
181169
: T[K] extends (...args: any[]) => infer R

packages/language-core/src/codegen/globalTypes/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,16 @@ export function generateGlobalTypes({
155155
function __VLS_asFunctionalSlot<S>(slot: S): S extends () => infer R ? (props: {}) => R : NonNullable<S>;
156156
function __VLS_tryAsConstant<const T>(t: T): T;
157157
158+
type I18nValues = { [k: string]: string } | Array<string>
159+
158160
type UnwrapRefs<T> = {
159161
[K in keyof T]: T[K] extends import('${lib}').Ref
160162
? import('${lib}').UnwrapRef<T[K]>
161163
: T[K]
162164
}
163-
${defineComponentTypesContents.globalTypes}
165+
${defineComponentTypesContents.globalTypes()}
164166
}
165-
` + defineComponentTypesContents.localTypes
167+
` + defineComponentTypesContents.localTypes(lib)
168+
166169
return text
167170
}

packages/language-core/src/codegen/script/template.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,13 @@ function* generateTemplateCtx(options: ScriptCodegenOptions): Generator<Code> {
3636
const { defineProps, defineExpose } = options.scriptSetupRanges || {}
3737
if (!defineProps) {
3838
yield `const __VLS_defineProps = {}${endOfLine}`
39-
} else {
40-
const name = defineProps?.name
41-
if (name) {
42-
yield `const __VLS_defineProps = ${name}${newLine}`
43-
}
4439
}
4540
if (!defineExpose) {
4641
yield `const __VLS_defineExposeUnrefs = {}${endOfLine}`
4742
} else {
4843
yield `const __VLS_defineExposeUnrefs = __VLS_defineExpose as any as UnwrapRefs<typeof __VLS_defineExpose>${newLine}`
4944
}
50-
yield `const __MPX_ctx = { ...__VLS_defineProps, ...__VLS_defineExposeUnrefs }${endOfLine}`
45+
yield `const __MPX_ctx = { ...__MPX_dollars, ...__VLS_defineProps, ...__VLS_defineExposeUnrefs }${endOfLine}`
5146
} else {
5247
yield `const __MPX_ctx = __VLS_defineComponent${endOfLine}`
5348
}

0 commit comments

Comments
 (0)