Skip to content

fix(runtime-vapor): add __vapor flag for dynamic component #13659

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: minor
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 36 additions & 28 deletions packages/runtime-vapor/__tests__/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ export interface InteropRenderContext {
html: () => string
}

export function makeInteropRender(): (comp: Component) => InteropRenderContext {
export function makeInteropRender(): {
define: (comp: Component) => InteropRenderContext
defineVapor: (comp: VaporComponent) => InteropRenderContext
} {
let host: HTMLElement
beforeEach(() => {
host = document.createElement('div')
Expand All @@ -102,36 +105,41 @@ export function makeInteropRender(): (comp: Component) => InteropRenderContext {
host.remove()
})

function define(comp: Component) {
let app: App
function render(
props: RawProps | undefined = undefined,
container: string | ParentNode = host,
) {
app?.unmount()
app = createApp(comp, props)
app.use(vaporInteropPlugin)
return mount(container)
}
const defineFactory =
(createAppFactory: typeof createApp | typeof createVaporApp) =>
(comp: Component | VaporComponent) => {
let app: App
function render(
props: RawProps | undefined = undefined,
container: string | ParentNode = host,
) {
app?.unmount()
app = createAppFactory(comp as any, props)
app.use(vaporInteropPlugin)
return mount(container)
}

function mount(container: string | ParentNode = host) {
app.mount(container)
return res()
}

function html() {
return host.innerHTML
}

const res = () => ({
host,
mount,
render,
html,
})

function mount(container: string | ParentNode = host) {
app.mount(container)
return res()
}

function html() {
return host.innerHTML
}

const res = () => ({
host,
mount,
render,
html,
})

return res()
return {
define: defineFactory(createApp),
defineVapor: defineFactory(createVaporApp),
}

return define
}
21 changes: 19 additions & 2 deletions packages/runtime-vapor/__tests__/vdomInterop.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ import {
applyTextModel,
child,
createComponent,
createDynamicComponent,
defineVaporComponent,
renderEffect,
setText,
template,
} from '../src'

const define = makeInteropRender()
const { define, defineVapor } = makeInteropRender()

describe('vdomInterop', () => {
describe('props', () => {
Expand Down Expand Up @@ -179,7 +180,23 @@ describe('vdomInterop', () => {

describe.todo('template ref', () => {})

describe.todo('dynamic component', () => {})
describe('dynamic component', () => {
it('should not render to Virtual DOM', () => {
const VaporChild = defineVaporComponent({
setup() {
return createDynamicComponent(
() => () => document.createTextNode('foo'),
)
},
})

const { host } = defineVapor(VaporChild).render()

expect(host.innerHTML).toMatchInlineSnapshot(
`"foo<!--dynamic-component-->"`,
)
})
})

describe('attribute fallthrough', () => {
it('should not fallthrough emit handlers to vdom child', () => {
Expand Down
5 changes: 4 additions & 1 deletion packages/runtime-vapor/src/apiCreateDynamicComponent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { resolveDynamicComponent } from '@vue/runtime-dom'
import { currentInstance, resolveDynamicComponent } from '@vue/runtime-dom'
import { DynamicFragment, type VaporFragment, insert } from './block'
import { createComponentWithFallback } from './component'
import { renderEffect } from './renderEffect'
Expand Down Expand Up @@ -31,6 +31,9 @@ export function createDynamicComponent(

renderEffect(() => {
const value = getter()
if (currentInstance && currentInstance.appContext.vapor) {
value.__vapor = true
}
frag.update(
() =>
createComponentWithFallback(
Expand Down