Skip to content
Merged
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
70 changes: 42 additions & 28 deletions src/mount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ import {
ComponentOptionsWithArrayProps,
ComponentOptionsWithoutProps,
ExtractPropTypes,
Component
Component,
AppConfig,
VNodeProps
} from 'vue'

import { config } from './config'
import { GlobalMountOptions } from './types'
import { mergeGlobalProperties, isString } from './utils'
import { mergeGlobalProperties } from './utils'
import { processSlot } from './utils/compileSlots'
import { createWrapper, VueWrapper } from './vue-wrapper'
import { attachEmitListener } from './emitMixin'
Expand All @@ -28,7 +30,6 @@ import {
MOUNT_PARENT_NAME
} from './constants'
import { stubComponents } from './stubs'
import { parse } from '@vue/compiler-dom'

type Slot = VNode | string | { render: Function } | Function

Expand Down Expand Up @@ -89,17 +90,17 @@ export function mount(
// normalise the incoming component
const component =
typeof originalComponent === 'function'
? {
? defineComponent({
setup: (_, { attrs, slots }) => () =>
h(originalComponent, attrs, slots)
}
})
: { ...originalComponent }

const el = document.createElement('div')
el.id = MOUNT_ELEMENT_ID

if (options?.attachTo) {
let to: Element
let to: Element | null
if (typeof options.attachTo === 'string') {
to = document.querySelector(options.attachTo)
if (!to) {
Expand All @@ -117,29 +118,37 @@ export function mount(
// handle any slots passed via mounting options
const slots: VNodeNormalizedChildren =
options?.slots &&
Object.entries(options.slots).reduce((acc, [name, slot]) => {
// case of an SFC getting passed
if (typeof slot === 'object' && 'render' in slot) {
acc[name] = slot.render
return acc
}
Object.entries(options.slots).reduce(
(
acc: { [key: string]: Function },
[name, slot]: [string, Slot]
): { [key: string]: Function } => {
// case of an SFC getting passed
if (typeof slot === 'object' && 'render' in slot) {
acc[name] = slot.render
return acc
}

if (typeof slot === 'function') {
acc[name] = slot
return acc
}
if (typeof slot === 'function') {
acc[name] = slot
return acc
}

if (typeof slot === 'object') {
acc[name] = () => slot
return acc
}
if (typeof slot === 'object') {
acc[name] = () => slot
return acc
}

if (typeof slot === 'string') {
// slot is most probably a scoped slot string or a plain string
acc[name] = (props: VNodeProps) => h(processSlot(slot), props)
return acc
}

if (typeof slot === 'string') {
// slot is most probably a scoped slot string or a plain string
acc[name] = (props) => h(processSlot(slot), props)
return acc
}
}, {})
},
{}
)

// override component data with mounting options data
if (options?.data) {
Expand Down Expand Up @@ -184,8 +193,10 @@ export function mount(
if (global?.mocks) {
const mixin = {
beforeCreate() {
for (const [k, v] of Object.entries(global.mocks)) {
this[k] = v
for (const [k, v] of Object.entries(
global.mocks as { [key: string]: any }
)) {
;(this as any)[k] = v
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TS is so ugly sometimes (or maybe due to Vue's dynamic nature it cannot be avoided...)

}
}
}
Expand All @@ -195,7 +206,10 @@ export function mount(

// AppConfig
if (global.config) {
for (const [k, v] of Object.entries(global.config)) {
for (const [k, v] of Object.entries(global.config) as [
keyof Omit<AppConfig, 'isNativeTag'>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

any
][]) {
app.config[k] = v
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export type FindAllComponentsSelector = NameSelector | string

export type GlobalMountOptions = {
plugins?: Plugin[]
config?: AppConfig
config?: Omit<AppConfig, 'isNativeTag'> // isNativeTag is readonly, so we omit it
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this is how you do it, awesome, was trying to figure this out

mixins?: ComponentOptions[]
mocks?: Record<string, any>
provide?: Record<any, any>
Expand Down