Skip to content

Commit 4eee630

Browse files
committedSep 4, 2024··
fix(build): improve built-in components treeshakability
1 parent 475977a commit 4eee630

File tree

3 files changed

+50
-37
lines changed

3 files changed

+50
-37
lines changed
 

‎packages/runtime-core/src/components/KeepAlive.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -362,13 +362,16 @@ const KeepAliveImpl: ComponentOptions = {
362362
},
363363
}
364364

365-
if (__COMPAT__) {
366-
KeepAliveImpl.__isBuildIn = true
365+
const decorate = (t: typeof KeepAliveImpl) => {
366+
t.__isBuiltIn = true
367+
return t
367368
}
368369

369370
// export the public type for h/tsx inference
370371
// also to avoid inline import() in generated d.ts files
371-
export const KeepAlive = KeepAliveImpl as any as {
372+
export const KeepAlive = (__COMPAT__
373+
? /*#__PURE__*/ decorate(KeepAliveImpl)
374+
: KeepAliveImpl) as any as {
372375
__isKeepAlive: true
373376
new (): {
374377
$props: VNodeProps & KeepAliveProps

‎packages/runtime-dom/src/components/Transition.ts

+27-19
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,6 @@ export interface ElementWithTransition extends HTMLElement {
4242
[vtcKey]?: Set<string>
4343
}
4444

45-
// DOM Transition is a higher-order-component based on the platform-agnostic
46-
// base Transition component, with DOM-specific logic.
47-
export const Transition: FunctionalComponent<TransitionProps> = (
48-
props,
49-
{ slots },
50-
) => h(BaseTransition, resolveTransitionProps(props), slots)
51-
52-
Transition.displayName = 'Transition'
53-
54-
if (__COMPAT__) {
55-
Transition.__isBuiltIn = true
56-
}
57-
5845
const DOMTransitionPropsValidators = {
5946
name: String,
6047
type: String,
@@ -74,12 +61,33 @@ const DOMTransitionPropsValidators = {
7461
leaveToClass: String,
7562
}
7663

77-
export const TransitionPropsValidators: any = (Transition.props =
78-
/*#__PURE__*/ extend(
79-
{},
80-
BaseTransitionPropsValidators as any,
81-
DOMTransitionPropsValidators,
82-
))
64+
export const TransitionPropsValidators: any = /*#__PURE__*/ extend(
65+
{},
66+
BaseTransitionPropsValidators as any,
67+
DOMTransitionPropsValidators,
68+
)
69+
70+
/**
71+
* Wrap logic that attaches extra properties to Transition in a function
72+
* so that it can be annotated as pure
73+
*/
74+
const decorate = (t: typeof Transition) => {
75+
t.displayName = 'Transition'
76+
t.props = TransitionPropsValidators
77+
if (__COMPAT__) {
78+
t.__isBuiltIn = true
79+
}
80+
return t
81+
}
82+
83+
/**
84+
* DOM Transition is a higher-order-component based on the platform-agnostic
85+
* base Transition component, with DOM-specific logic.
86+
*/
87+
export const Transition: FunctionalComponent<TransitionProps> =
88+
/*#__PURE__*/ decorate((props, { slots }) =>
89+
h(BaseTransition, resolveTransitionProps(props), slots),
90+
)
8391

8492
/**
8593
* #3227 Incoming hooks may be merged into arrays when wrapping Transition

‎packages/runtime-dom/src/components/TransitionGroup.ts

+17-15
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,27 @@ const positionMap = new WeakMap<VNode, DOMRect>()
3232
const newPositionMap = new WeakMap<VNode, DOMRect>()
3333
const moveCbKey = Symbol('_moveCb')
3434
const enterCbKey = Symbol('_enterCb')
35+
3536
export type TransitionGroupProps = Omit<TransitionProps, 'mode'> & {
3637
tag?: string
3738
moveClass?: string
3839
}
3940

40-
const TransitionGroupImpl: ComponentOptions = {
41+
/**
42+
* Wrap logic that modifies TransitionGroup properties in a function
43+
* so that it can be annotated as pure
44+
*/
45+
const decorate = (t: typeof TransitionGroupImpl) => {
46+
// TransitionGroup does not support "mode" so we need to remove it from the
47+
// props declarations, but direct delete operation is considered a side effect
48+
delete t.props.mode
49+
if (__COMPAT__) {
50+
t.__isBuiltIn = true
51+
}
52+
return t
53+
}
54+
55+
const TransitionGroupImpl: ComponentOptions = /*#__PURE__*/ decorate({
4156
name: 'TransitionGroup',
4257

4358
props: /*#__PURE__*/ extend({}, TransitionPropsValidators, {
@@ -152,20 +167,7 @@ const TransitionGroupImpl: ComponentOptions = {
152167
return createVNode(tag, null, children)
153168
}
154169
},
155-
}
156-
157-
if (__COMPAT__) {
158-
TransitionGroupImpl.__isBuiltIn = true
159-
}
160-
161-
/**
162-
* TransitionGroup does not support "mode" so we need to remove it from the
163-
* props declarations, but direct delete operation is considered a side effect
164-
* and will make the entire transition feature non-tree-shakeable, so we do it
165-
* in a function and mark the function's invocation as pure.
166-
*/
167-
const removeMode = (props: any) => delete props.mode
168-
/*#__PURE__*/ removeMode(TransitionGroupImpl.props)
170+
})
169171

170172
export const TransitionGroup = TransitionGroupImpl as unknown as {
171173
new (): {

0 commit comments

Comments
 (0)
Please sign in to comment.