Skip to content

Commit 3346d10

Browse files
committed
wip
1 parent a6af7d4 commit 3346d10

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

src/createApp.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import Vue from 'vue'
2+
import { VueConstructor } from 'vue/types/umd'
3+
import { currentVue } from './runtimeContext'
4+
import { warn } from './utils'
5+
6+
export interface App {
7+
config: VueConstructor['config']
8+
use: VueConstructor['use']
9+
mixin: VueConstructor['mixin']
10+
component: VueConstructor['component']
11+
directive: VueConstructor['directive']
12+
mount: Vue['$mount']
13+
unmount: Vue['$destroy']
14+
}
15+
16+
export function createApp(rootComponent: any, rootProps: any = undefined): App {
17+
const V = currentVue!
18+
19+
let mountedVM: Vue | undefined
20+
21+
return {
22+
config: V.config,
23+
use: V.use.bind(V),
24+
mixin: V.mixin.bind(V),
25+
component: V.component.bind(V),
26+
directive: V.directive.bind(V),
27+
mount: (el, hydrating) => {
28+
if (!mountedVM) {
29+
mountedVM = new V({
30+
propsData: rootProps,
31+
render: (h) => h(rootComponent),
32+
})
33+
mountedVM.$mount(el, hydrating)
34+
return mountedVM
35+
} else {
36+
if (__DEV__) {
37+
warn(
38+
`App has already been mounted.\n` +
39+
`If you want to remount the same app, move your app creation logic ` +
40+
`into a factory function and create fresh app instances for each ` +
41+
`mount - e.g. \`const createMyApp = () => createApp(App)\``
42+
)
43+
}
44+
return mountedVM
45+
}
46+
},
47+
unmount: () => {
48+
if (mountedVM) {
49+
mountedVM.$destroy()
50+
mountedVM = undefined
51+
} else if (__DEV__) {
52+
warn(`Cannot unmount an app that is not mounted.`)
53+
}
54+
},
55+
}
56+
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const VueCompositionAPI = {
1414
}
1515

1616
export default VueCompositionAPI
17+
export { createApp } from './createApp'
1718
export { nextTick } from './nextTick'
1819
export { createElement as h } from './createElement'
1920
export { getCurrentInstance } from './runtimeContext'

test/createApp.spec.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const { createApp, defineComponent } = require('../src')
2+
const { nextTick } = require('process')
3+
4+
describe('setup', () => {
5+
beforeEach(() => {
6+
warn = jest.spyOn(global.console, 'error').mockImplementation(() => null)
7+
})
8+
afterEach(() => {
9+
warn.mockRestore()
10+
})
11+
12+
it('should work', async () => {
13+
const app = createApp({
14+
setup() {
15+
return {
16+
a: ref(1),
17+
}
18+
},
19+
template: '<p>{{a}}</p>',
20+
})
21+
const vm = app.mount()
22+
23+
await nextTick()
24+
expect(vm.$el.textContent).toBe('1')
25+
})
26+
27+
it('should work with rootProps', async () => {
28+
const app = createApp(
29+
defineComponent({
30+
props: {
31+
msg: String,
32+
},
33+
template: '<p>{{msg}}</p>',
34+
}),
35+
{
36+
msg: 'foobar',
37+
}
38+
)
39+
const vm = app.mount()
40+
41+
await nextTick()
42+
expect(vm.$el.textContent).toBe('foobar')
43+
})
44+
})

0 commit comments

Comments
 (0)