Skip to content

Commit

Permalink
feat(core): adapt createApp
Browse files Browse the repository at this point in the history
  • Loading branch information
Luobinf committed Dec 4, 2023
1 parent d3f7410 commit 1a44d0d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 26 deletions.
6 changes: 4 additions & 2 deletions packages/core/src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Vue from './vue'
// import Vue from './vue'
import { error, diffAndCloneA, hasOwn, makeMap } from '@mpxjs/utils'
// import { APIs, InstanceAPIs } from './platform/export/api'
import { APIs } from './platform/export/api'
Expand Down Expand Up @@ -132,7 +132,9 @@ function factory () {
// Object.assign(Mpx.prototype, InstanceAPIs)
// 输出web时在mpx上挂载Vue对象
if (__mpx_mode__ === 'web') {
Mpx.__vue = Vue
// Mpx.__vue = Vue
Mpx.__vue = {}
// Mpx.__vue = app
}
return Mpx
}
Expand Down
50 changes: 28 additions & 22 deletions packages/webpack-plugin/lib/runtime/optionProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,40 @@ export function getWxsMixin (wxsModules) {

function createApp ({ componentsMap, Vue, pagesMap, firstPage, VueRouter, App, tabBarMap }) {
const option = {}

if (App.onAppInit) {
Object.assign(option, App.onAppInit() || {})
}

if (isBrowser && global.__mpxPinia) {
// 注入pinia
option.pinia = global.__mpxPinia
}

// const app = new Vue({
// ...option,
// render: (h) => h(App)
// })
const app = Vue.createApp({
...option,
render: (h) => h(App)
})

app.directive('animation', animation)

// 过滤器需要重写
// Vue.filter('transRpxStyle', transRpxStyle)

app.config.ignoredElements = ['page']

// 对于app中的组件需要全局注册
for (const componentName in componentsMap) {
if (hasOwn(componentsMap, componentName)) {
const component = componentsMap[componentName]
Vue.component(componentName, component)
app.component(componentName, component)
}
}

Vue.directive('animation', animation)

Vue.filter('transRpxStyle', transRpxStyle)

Vue.config.ignoredElements = ['page']

const routes = []

for (const pagePath in pagesMap) {
Expand Down Expand Up @@ -266,7 +286,7 @@ function createApp ({ componentsMap, Vue, pagesMap, firstPage, VueRouter, App, t
throw args[0]
}
}
Vue.config.errorHandler = (...args) => {
app.config.errorHandler = (...args) => {
return errorHandler(args, true)
}
window.addEventListener('error', (event) => {
Expand Down Expand Up @@ -306,20 +326,6 @@ function createApp ({ componentsMap, Vue, pagesMap, firstPage, VueRouter, App, t
global.__mpxRouter.__mpxHistoryLength = global.history.length
}
}

if (App.onAppInit) {
Object.assign(option, App.onAppInit() || {})
}

if (isBrowser && global.__mpxPinia) {
// 注入pinia
option.pinia = global.__mpxPinia
}

const app = new Vue({
...option,
render: (h) => h(App)
})
return {
app,
...option
Expand Down
5 changes: 3 additions & 2 deletions packages/webpack-plugin/lib/web/processMainScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ module.exports = function (script, {

let output = `\n import { processAppOption, getComponent } from ${stringifyRequest(loaderContext, optionProcessorPath)}
import '@mpxjs/webpack-plugin/lib/runtime/base.styl'
import Vue from 'vue'
// import Vue from 'vue'
import { createApp } from 'vue'
import VueRouter from 'vue-router'
import Mpx from '@mpxjs/core'
import App from ${stringifyRequest(loaderContext, addQuery(resource, { isApp: true }))}
Vue.use(VueRouter)
// Vue.use(VueRouter)
\n`

if (i18n) {
Expand Down

0 comments on commit 1a44d0d

Please sign in to comment.