-
Notifications
You must be signed in to change notification settings - Fork 267
feat: stubs mounting option #56
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
Changes from all commits
0db36c3
361019d
d4897f1
61b5e2b
11f47af
d082064
d515f3f
1a5024e
d14dc59
2355f55
1c65bf7
485b1ac
126d5dd
de4e38d
da5d3cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import { | |
defineComponent, | ||
VNodeNormalizedChildren, | ||
ComponentOptions, | ||
transformVNodeArgs, | ||
Plugin, | ||
Directive, | ||
Component, | ||
|
@@ -15,6 +16,7 @@ import { createWrapper } from './vue-wrapper' | |
import { createEmitMixin } from './emitMixin' | ||
import { createDataMixin } from './dataMixin' | ||
import { MOUNT_ELEMENT_ID } from './constants' | ||
import { stubComponents } from './stubs' | ||
|
||
type Slot = VNode | string | { render: Function } | ||
|
||
|
@@ -29,6 +31,7 @@ interface MountingOptions { | |
plugins?: Plugin[] | ||
mixins?: ComponentOptions[] | ||
mocks?: Record<string, any> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typing Vue components is very difficult, will need to investigate the Vue codebase to figure out how best to do this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed this too. Am interested to hear about what you find. |
||
stubs?: Record<any, any> | ||
provide?: Record<any, any> | ||
// TODO how to type `defineComponent`? Using `any` for now. | ||
components?: Record<string, Component | object> | ||
|
@@ -72,6 +75,7 @@ export function mount(originalComponent: any, options?: MountingOptions) { | |
|
||
// create the wrapper component | ||
const Parent = defineComponent({ | ||
name: 'VTU_COMPONENT', | ||
render() { | ||
return h(component, props, slots) | ||
} | ||
|
@@ -133,6 +137,13 @@ export function mount(originalComponent: any, options?: MountingOptions) { | |
const { emitMixin, events } = createEmitMixin() | ||
vm.mixin(emitMixin) | ||
|
||
// stubs | ||
if (options?.global?.stubs) { | ||
stubComponents(options.global.stubs) | ||
} else { | ||
transformVNodeArgs() | ||
} | ||
|
||
// mount the app! | ||
const app = vm.mount(el) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { transformVNodeArgs, h } from 'vue' | ||
|
||
import { pascalCase, kebabCase } from './utils' | ||
|
||
interface IStubOptions { | ||
name?: string | ||
} | ||
|
||
// TODO: figure out how to type this | ||
type VNodeArgs = any[] | ||
|
||
export const createStub = (options: IStubOptions) => { | ||
const tag = options.name ? `${options.name}-stub` : 'anonymous-stub' | ||
const render = () => h(tag) | ||
|
||
return { name: tag, render } | ||
} | ||
|
||
const resolveComponentStubByName = ( | ||
componentName: string, | ||
stubs: Record<any, any> | ||
) => { | ||
const componentPascalName = pascalCase(componentName) | ||
const componentKebabName = kebabCase(componentName) | ||
|
||
for (const [stubKey, value] of Object.entries(stubs)) { | ||
if ( | ||
stubKey === componentPascalName || | ||
stubKey === componentKebabName || | ||
stubKey === componentName | ||
) { | ||
return value | ||
} | ||
} | ||
} | ||
|
||
const isHTMLElement = (args: VNodeArgs) => | ||
Array.isArray(args) && typeof args[0] === 'string' | ||
|
||
const isCommentOrFragment = (args: VNodeArgs) => typeof args[0] === 'symbol' | ||
|
||
const isParent = (args: VNodeArgs) => | ||
typeof args[0] === 'object' && args[0]['name'] === 'VTU_COMPONENT' | ||
|
||
const isComponent = (args: VNodeArgs) => typeof args[0] === 'object' | ||
|
||
export function stubComponents(stubs: Record<any, any>) { | ||
transformVNodeArgs((args) => { | ||
// args[0] can either be: | ||
// 1. a HTML tag (div, span...) | ||
// 2. An object of component options, such as { name: 'foo', render: [Function], props: {...} } | ||
// Depending what it is, we do different things. | ||
if (isHTMLElement(args) || isCommentOrFragment(args) || isParent(args)) { | ||
return args | ||
} | ||
|
||
if (isComponent(args)) { | ||
const name = args[0]['name'] | ||
if (!name) { | ||
return args | ||
} | ||
|
||
const stub = resolveComponentStubByName(name, stubs) | ||
|
||
// we return a stub by matching Vue's `h` function | ||
// where the signature is h(Component, props) | ||
// case 1: default stub | ||
if (stub === true) { | ||
return [createStub({ name }), {}] | ||
} | ||
|
||
// case 2: custom implementation | ||
if (typeof stub === 'object') { | ||
return [stubs[name], {}] | ||
} | ||
} | ||
|
||
return args | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import camelCase from 'lodash/camelCase' | ||
import upperFirst from 'lodash/upperFirst' | ||
import kebabCase from 'lodash/kebabCase' | ||
import flow from 'lodash/flow' | ||
|
||
const pascalCase = flow(camelCase, upperFirst) | ||
|
||
export { kebabCase, pascalCase } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops :D