Skip to content

Commit d30d820

Browse files
committed
chore: Merge branch 'minor' into edison/feat/hmr
2 parents 88ae742 + b07fa60 commit d30d820

File tree

19 files changed

+978
-339
lines changed

19 files changed

+978
-339
lines changed

packages/compiler-sfc/src/compileScript.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import {
3434
normalScriptDefaultVar,
3535
processNormalScript,
3636
} from './script/normalScript'
37-
import { CSS_VARS_HELPER, genCssVarsCode } from './style/cssVars'
37+
import { genCssVarsCode, getCssVarsHelper } from './style/cssVars'
3838
import {
3939
type SFCTemplateCompileOptions,
4040
compileTemplate,
@@ -825,7 +825,7 @@ export function compileScript(
825825
// no need to do this when targeting SSR
826826
!ssr
827827
) {
828-
ctx.helperImports.add(CSS_VARS_HELPER)
828+
ctx.helperImports.add(getCssVarsHelper(vapor))
829829
ctx.helperImports.add('unref')
830830
ctx.s.prependLeft(
831831
startOffset,
@@ -834,6 +834,7 @@ export function compileScript(
834834
ctx.bindingMetadata,
835835
scopeId,
836836
!!options.isProd,
837+
vapor,
837838
)}\n`,
838839
)
839840
}

packages/compiler-sfc/src/style/cssVars.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ import { getEscapedCssVarName } from '@vue/shared'
1414

1515
export const CSS_VARS_HELPER = `useCssVars`
1616

17+
export function getCssVarsHelper(vapor: boolean | undefined): string {
18+
return vapor ? `useVaporCssVars` : CSS_VARS_HELPER
19+
}
20+
1721
export function genCssVarsFromList(
1822
vars: string[],
1923
id: string,
@@ -168,6 +172,7 @@ export function genCssVarsCode(
168172
bindings: BindingMetadata,
169173
id: string,
170174
isProd: boolean,
175+
vapor?: boolean,
171176
) {
172177
const varsExp = genCssVarsFromList(vars, id, isProd)
173178
const exp = createSimpleExpression(varsExp, false)
@@ -188,7 +193,7 @@ export function genCssVarsCode(
188193
})
189194
.join('')
190195

191-
return `_${CSS_VARS_HELPER}(_ctx => (${transformedString}))`
196+
return `_${getCssVarsHelper(vapor)}(_ctx => (${transformedString}))`
192197
}
193198

194199
// <script setup> already gets the calls injected as part of the transform

packages/runtime-core/src/component.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,17 @@ export interface GenericComponentInstance {
461461
* @internal
462462
*/
463463
suspense: SuspenseBoundary | null
464+
/**
465+
* `updateTeleportCssVars`
466+
* For updating css vars on contained teleports
467+
* @internal
468+
*/
469+
ut?: (vars?: Record<string, string>) => void
470+
/**
471+
* dev only. For style v-bind hydration mismatch checks
472+
* @internal
473+
*/
474+
getCssVars?: () => Record<string, string>
464475

465476
// lifecycle
466477
/**
@@ -690,18 +701,6 @@ export interface ComponentInternalInstance extends GenericComponentInstance {
690701
* @internal
691702
*/
692703
n?: () => Promise<void>
693-
/**
694-
* `updateTeleportCssVars`
695-
* For updating css vars on contained teleports
696-
* @internal
697-
*/
698-
ut?: (vars?: Record<string, unknown>) => void
699-
700-
/**
701-
* dev only. For style v-bind hydration mismatch checks
702-
* @internal
703-
*/
704-
getCssVars?: () => Record<string, unknown>
705704

706705
/**
707706
* v2 compat only, for caching mutated $options

packages/runtime-core/src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,3 +669,8 @@ export {
669669
checkTransitionMode,
670670
leaveCbKey,
671671
} from './components/BaseTransition'
672+
673+
/**
674+
* @internal
675+
*/
676+
export type { GenericComponent } from './component'

packages/runtime-dom/src/helpers/useCssVars.ts

Lines changed: 65 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
Fragment,
3+
type GenericComponentInstance,
34
Static,
45
type VNode,
56
getCurrentInstance,
@@ -22,51 +23,25 @@ export function useCssVars(
2223
): void {
2324
if (!__BROWSER__ && !__TEST__) return
2425

25-
const instance = getCurrentInstance()
26-
/* v8 ignore start */
27-
if (!instance) {
28-
__DEV__ &&
29-
warn(`useCssVars is called without current active component instance.`)
30-
return
31-
}
32-
/* v8 ignore stop */
33-
34-
const updateTeleports = (instance.ut = (vars = getter(instance.proxy)) => {
35-
Array.from(
36-
document.querySelectorAll(`[data-v-owner="${instance.uid}"]`),
37-
).forEach(node => setVarsOnNode(node, vars))
38-
})
39-
40-
if (__DEV__) {
41-
instance.getCssVars = () => getter(instance.proxy)
42-
}
43-
44-
const setVars = () => {
45-
const vars = getter(instance.proxy)
26+
const instance = getCurrentInstance()! // to be check in baseUseCssVars
27+
const getVars = () => getter(instance.proxy)
28+
const setVars = (vars: Record<string, any>) => {
4629
if (instance.ce) {
4730
setVarsOnNode(instance.ce as any, vars)
4831
} else {
4932
setVarsOnVNode(instance.subTree, vars)
5033
}
51-
updateTeleports(vars)
5234
}
5335

54-
// handle cases where child component root is affected
55-
// and triggers reflow in onMounted
56-
onBeforeUpdate(() => {
57-
queuePostFlushCb(setVars)
58-
})
59-
60-
onMounted(() => {
61-
// run setVars synchronously here, but run as post-effect on changes
62-
watch(setVars, NOOP, { flush: 'post' })
63-
const ob = new MutationObserver(setVars)
64-
ob.observe(instance.subTree.el!.parentNode, { childList: true })
65-
onUnmounted(() => ob.disconnect())
66-
})
36+
baseUseCssVars(
37+
instance as GenericComponentInstance,
38+
() => instance.subTree.el!.parentNode!,
39+
getVars,
40+
setVars,
41+
)
6742
}
6843

69-
function setVarsOnVNode(vnode: VNode, vars: Record<string, unknown>) {
44+
function setVarsOnVNode(vnode: VNode, vars: Record<string, string>) {
7045
if (__FEATURE_SUSPENSE__ && vnode.shapeFlag & ShapeFlags.SUSPENSE) {
7146
const suspense = vnode.suspense!
7247
vnode = suspense.activeBranch!
@@ -96,7 +71,60 @@ function setVarsOnVNode(vnode: VNode, vars: Record<string, unknown>) {
9671
}
9772
}
9873

99-
function setVarsOnNode(el: Node, vars: Record<string, unknown>) {
74+
/**
75+
* @internal
76+
* shared between vdom and vapor
77+
*/
78+
export function baseUseCssVars(
79+
instance: GenericComponentInstance | null,
80+
getParentNode: () => Node,
81+
getVars: () => Record<string, any>,
82+
setVars: (vars: Record<string, any>) => void,
83+
): void {
84+
/* v8 ignore start */
85+
if (!instance) {
86+
__DEV__ &&
87+
warn(`useCssVars is called without current active component instance.`)
88+
return
89+
}
90+
/* v8 ignore stop */
91+
92+
if (__DEV__) {
93+
instance.getCssVars = getVars
94+
}
95+
96+
const updateTeleports = (instance.ut = (vars = getVars()) => {
97+
Array.from(
98+
document.querySelectorAll(`[data-v-owner="${instance.uid}"]`),
99+
).forEach(node => setVarsOnNode(node, vars))
100+
})
101+
102+
const applyCssCars = () => {
103+
const vars = getVars()
104+
setVars(vars)
105+
updateTeleports(vars)
106+
}
107+
108+
// handle cases where child component root is affected
109+
// and triggers reflow in onMounted
110+
onBeforeUpdate(() => {
111+
queuePostFlushCb(applyCssCars)
112+
})
113+
114+
onMounted(() => {
115+
// run setVars synchronously here, but run as post-effect on changes
116+
watch(applyCssCars, NOOP, { flush: 'post' })
117+
const ob = new MutationObserver(applyCssCars)
118+
ob.observe(getParentNode(), { childList: true })
119+
onUnmounted(() => ob.disconnect())
120+
})
121+
}
122+
123+
/**
124+
* @internal
125+
* shared between vdom and vapor
126+
*/
127+
export function setVarsOnNode(el: Node, vars: Record<string, string>): void {
100128
if (el.nodeType === 1) {
101129
const style = (el as HTMLElement).style
102130
let cssText = ''

packages/runtime-dom/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,10 @@ export { patchStyle } from './modules/style'
329329
* @internal
330330
*/
331331
export { shouldSetAsProp } from './patchProp'
332+
/**
333+
* @internal
334+
*/
335+
export { baseUseCssVars, setVarsOnNode } from './helpers/useCssVars'
332336
/**
333337
* @internal
334338
*/

packages/runtime-vapor/__tests__/apiDefineAsyncComponent.spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
defineVaporComponent,
1010
renderEffect,
1111
template,
12+
withVaporCtx,
1213
} from '@vue/runtime-vapor'
1314
import { setElementText } from '../src/dom/prop'
1415

@@ -785,12 +786,13 @@ describe('api: defineAsyncComponent', () => {
785786
const { html } = define({
786787
setup() {
787788
return createComponent(VaporKeepAlive, null, {
788-
default: () =>
789+
default: withVaporCtx(() =>
789790
createIf(
790791
() => toggle.value,
791792
() => createComponent(Foo),
792793
() => createComponent(Bar),
793794
),
795+
),
794796
})
795797
},
796798
}).render()
@@ -834,7 +836,7 @@ describe('api: defineAsyncComponent', () => {
834836
VaporKeepAlive,
835837
{ include: () => 'Foo' },
836838
{
837-
default: () => createComponent(Foo),
839+
default: withVaporCtx(() => createComponent(Foo)),
838840
},
839841
)
840842
},

0 commit comments

Comments
 (0)