Skip to content

Commit 7a96ee9

Browse files
committed
feat: attrs
1 parent 5f77a2a commit 7a96ee9

File tree

10 files changed

+244
-87
lines changed

10 files changed

+244
-87
lines changed

packages/compiler-vapor/src/generators/component.ts

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1+
import { isArray } from '@vue/shared'
12
import type { CodegenContext } from '../generate'
2-
import type { CreateComponentIRNode } from '../ir'
3-
import { type CodeFragment, NEWLINE, genCall } from './utils'
3+
import type { CreateComponentIRNode, IRProp } from '../ir'
4+
import {
5+
type CodeFragment,
6+
INDENT_END,
7+
INDENT_START,
8+
NEWLINE,
9+
genCall,
10+
genMulti,
11+
} from './utils'
12+
import { genExpression } from './expression'
13+
import { genPropKey } from './prop'
414

515
export function genCreateComponent(
616
oper: CreateComponentIRNode,
@@ -15,6 +25,40 @@ export function genCreateComponent(
1525
return [
1626
NEWLINE,
1727
`const n${oper.id} = `,
18-
...genCall(vaporHelper('createComponent'), tag),
28+
...genCall(vaporHelper('createComponent'), tag, genProps()),
1929
]
30+
31+
function genProps() {
32+
const props = oper.props
33+
.map(props => {
34+
if (isArray(props)) {
35+
if (!props.length) return undefined
36+
return genStaticProps(props)
37+
} else {
38+
return ['() => (', ...genExpression(props, context), ')']
39+
}
40+
})
41+
.filter(Boolean)
42+
if (props.length) {
43+
return genMulti(['[', ']', ', '], ...props)
44+
}
45+
}
46+
47+
function genStaticProps(props: IRProp[]) {
48+
return genMulti(
49+
[
50+
['{', INDENT_START, NEWLINE],
51+
[INDENT_END, NEWLINE, '}'],
52+
[', ', NEWLINE],
53+
],
54+
...props.map(prop => {
55+
return [
56+
...genPropKey(prop, context),
57+
': () => (',
58+
...genExpression(prop.values[0], context),
59+
')',
60+
]
61+
}),
62+
)
63+
}
2064
}

packages/compiler-vapor/src/generators/prop.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,14 @@ function genLiteralObjectProps(
7878
return genMulti(
7979
['{ ', ' }', ', '],
8080
...props.map(prop => [
81-
...genPropertyKey(prop, context),
81+
...genPropKey(prop, context),
8282
`: `,
8383
...genPropValue(prop.values, context),
8484
]),
8585
)
8686
}
8787

88-
function genPropertyKey(
88+
export function genPropKey(
8989
{ key: node, runtimeCamelize, modifier }: IRProp,
9090
context: CodegenContext,
9191
): CodeFragment[] {

packages/compiler-vapor/src/ir.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ export interface CreateComponentIRNode extends BaseIRNode {
178178
type: IRNodeTypes.CREATE_COMPONENT_NODE
179179
id: number
180180
tag: string
181-
props: IRProps
181+
props: IRProps[]
182182
// TODO slots
183183

184184
resolve: boolean

packages/compiler-vapor/src/transforms/transformElement.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,18 @@ export const transformElement: NodeTransform = (node, context) => {
5858

5959
function transformComponentElement(
6060
tag: string,
61-
propsResult: ReturnType<typeof buildProps>,
61+
propsResult: PropsResult,
6262
context: TransformContext,
6363
) {
6464
const { bindingMetadata } = context.options
6565
const resolve = !bindingMetadata[tag]
6666
context.dynamic.flags |= DynamicFlag.NON_TEMPLATE | DynamicFlag.INSERT
67+
6768
context.registerOperation({
6869
type: IRNodeTypes.CREATE_COMPONENT_NODE,
6970
id: context.reference(),
7071
tag,
71-
// TODO
72-
props: [],
72+
props: propsResult[0] ? propsResult[1] : [propsResult[1]],
7373
resolve,
7474
})
7575
}
@@ -118,12 +118,14 @@ function transformNativeElement(
118118
}
119119
}
120120

121+
export type PropsResult =
122+
| [dynamic: true, props: IRProps[], expressions: SimpleExpressionNode[]]
123+
| [dynamic: false, props: IRProp[]]
124+
121125
function buildProps(
122126
node: ElementNode,
123127
context: TransformContext<ElementNode>,
124-
):
125-
| [dynamic: true, props: IRProps[], expressions: SimpleExpressionNode[]]
126-
| [dynamic: false, props: IRProp[]] {
128+
): PropsResult {
127129
const props = node.props as (VaporDirectiveNode | AttributeNode)[]
128130
if (props.length === 0) return [false, []]
129131

packages/runtime-vapor/src/apiRender.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ function mountComponent(
8787
// hook: mounted
8888
queuePostRenderEffect(() => {
8989
invokeDirectiveHook(instance, 'mounted')
90+
// TODO
91+
for (const com of instance.comps) {
92+
com.isMounted = true
93+
}
9094
m && invokeArrayFns(m)
9195
})
9296
reset()

packages/runtime-vapor/src/component.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { DirectiveBinding } from './directives'
55
import {
66
type ComponentPropsOptions,
77
type NormalizedPropsOptions,
8+
type NormalizedRawProps,
89
type RawProps,
910
initProps,
1011
normalizePropsOptions,
@@ -48,6 +49,7 @@ export interface ComponentInternalInstance {
4849
comps: Set<ComponentInternalInstance>
4950
dirs: Map<Node, DirectiveBinding[]>
5051

52+
rawProps: NormalizedRawProps
5153
propsOptions: NormalizedPropsOptions
5254
emitsOptions: ObjectEmitsOptions | null
5355

@@ -156,6 +158,7 @@ export function createComponentInstance(
156158
dirs: new Map(),
157159

158160
// resolved props and emits options
161+
rawProps: null!, // set later
159162
propsOptions: normalizePropsOptions(component),
160163
emitsOptions: normalizeEmitsOptions(component),
161164

0 commit comments

Comments
 (0)