@mpxjs/core 2.8.42 → 2.9.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@mpxjs/core",
3
- "version": "2.8.42",
3
+ "version": "2.9.0-beta.1",
4
4
  "description": "mpx runtime core",
5
5
  "keywords": [
6
6
  "miniprogram",
7
7
  "mpx"
8
8
  ],
9
9
  "author": "donghongping",
10
- "license": "Apache",
10
+ "license": "Apache-2.0",
11
11
  "module": "src/index.js",
12
12
  "types": "@types/index.d.ts",
13
13
  "directories": {
@@ -19,7 +19,7 @@
19
19
  ],
20
20
  "main": "src/index.js",
21
21
  "dependencies": {
22
- "@mpxjs/utils": "^2.8.15",
22
+ "@mpxjs/utils": "^2.9.0-beta.1",
23
23
  "lodash": "^4.1.1",
24
24
  "miniprogram-api-typings": "^3.10.0"
25
25
  },
@@ -47,5 +47,5 @@
47
47
  "url": "https://github.com/didi/mpx/issues"
48
48
  },
49
49
  "sideEffects": false,
50
- "gitHead": "a0d7f0171b8e4535f1962bdb7415fec0d25abe93"
50
+ "gitHead": "5e4b44c9b8132e9192cfd119f63196ecc6c4486c"
51
51
  }
@@ -10,6 +10,7 @@ export const ONLOAD = '__onLoad__'
10
10
  export const ONSHOW = '__onShow__'
11
11
  export const ONHIDE = '__onHide__'
12
12
  export const ONRESIZE = '__onResize__'
13
+ export const SERVERPREFETCH = '__serverPrefetch__'
13
14
 
14
15
  export const INNER_LIFECYCLES = [
15
16
  BEFORECREATE,
@@ -19,6 +20,7 @@ export const INNER_LIFECYCLES = [
19
20
  BEFOREUPDATE,
20
21
  UPDATED,
21
22
  BEFOREUNMOUNT,
23
+ SERVERPREFETCH,
22
24
  UNMOUNTED,
23
25
  ONLOAD,
24
26
  ONSHOW,
@@ -352,7 +352,7 @@ function transformHOOKS (options) {
352
352
  const componentHooksMap = makeMap(convertRule.lifecycle.component)
353
353
  for (const key in options) {
354
354
  // 使用Component创建page实例,页面专属生命周期&自定义方法需写在methods内部
355
- if (typeof options[key] === 'function' && key !== 'dataFn' && key !== 'setup' && !componentHooksMap[key]) {
355
+ if (typeof options[key] === 'function' && key !== 'dataFn' && key !== 'setup' && key !== 'serverPrefetch' && !componentHooksMap[key]) {
356
356
  if (!options.methods) options.methods = {}
357
357
  options.methods[key] = options[key]
358
358
  delete options[key]
package/src/core/proxy.js CHANGED
@@ -36,6 +36,7 @@ import {
36
36
  BEFOREUPDATE,
37
37
  UPDATED,
38
38
  BEFOREUNMOUNT,
39
+ SERVERPREFETCH,
39
40
  UNMOUNTED,
40
41
  ONLOAD,
41
42
  ONSHOW,
@@ -641,6 +642,7 @@ export const onLoad = createHook(ONLOAD)
641
642
  export const onShow = createHook(ONSHOW)
642
643
  export const onHide = createHook(ONHIDE)
643
644
  export const onResize = createHook(ONRESIZE)
645
+ export const onServerPrefetch = createHook(SERVERPREFETCH)
644
646
  export const onPullDownRefresh = createHook('__onPullDownRefresh__')
645
647
  export const onReachBottom = createHook('__onReachBottom__')
646
648
  export const onShareAppMessage = createHook('__onShareAppMessage__')
package/src/index.js CHANGED
@@ -29,6 +29,7 @@ export {
29
29
  UPDATED,
30
30
  BEFOREUNMOUNT,
31
31
  UNMOUNTED,
32
+ SERVERPREFETCH,
32
33
  ONLOAD,
33
34
  ONSHOW,
34
35
  ONHIDE,
@@ -42,6 +43,7 @@ export {
42
43
  onUpdated,
43
44
  onBeforeUnmount,
44
45
  onUnmounted,
46
+ onServerPrefetch,
45
47
  onLoad,
46
48
  onShow,
47
49
  onHide,
@@ -63,7 +63,7 @@ export default function pageStatusMixin (mixinType) {
63
63
  },
64
64
  created () {
65
65
  // onLoad应该在用户声明周期CREATED后再执行,故此处使用原生created声明周期来触发onLoad
66
- const query = (global.__mpxRouter && global.__mpxRouter.currentRoute && global.__mpxRouter.currentRoute.query) || {}
66
+ const query = this.$root.$options?.router?.currentRoute?.query || {}
67
67
  this.__mpxProxy.callHook(ONLOAD, [query])
68
68
  }
69
69
  })
@@ -71,25 +71,27 @@ export default function pageStatusMixin (mixinType) {
71
71
 
72
72
  Object.assign(mixin, {
73
73
  [CREATED] () {
74
- const pageInstance = mixinType === 'page' ? this : getCurrentPageInstance()
75
- if (pageInstance) {
76
- this.$watch(() => pageInstance.mpxPageStatus, status => {
77
- if (!status) return
78
- if (status === 'show') this.__mpxProxy.callHook(ONSHOW)
79
- if (status === 'hide') this.__mpxProxy.callHook(ONHIDE)
80
- const pageLifetimes = this.__mpxProxy.options.pageLifetimes
81
- if (pageLifetimes) {
82
- if (/^resize/.test(status) && isFunction(pageLifetimes.resize)) {
83
- // resize
84
- pageLifetimes.resize.call(this, systemInfo)
85
- } else if (isFunction(pageLifetimes[status])) {
86
- // show & hide
87
- pageLifetimes[status].call(this)
74
+ if (isBrowser) {
75
+ const pageInstance = mixinType === 'page' ? this : getCurrentPageInstance()
76
+ if (pageInstance) {
77
+ this.$watch(() => pageInstance.mpxPageStatus, status => {
78
+ if (!status) return
79
+ if (status === 'show') this.__mpxProxy.callHook(ONSHOW)
80
+ if (status === 'hide') this.__mpxProxy.callHook(ONHIDE)
81
+ const pageLifetimes = this.__mpxProxy.options.pageLifetimes
82
+ if (pageLifetimes) {
83
+ if (/^resize/.test(status) && isFunction(pageLifetimes.resize)) {
84
+ // resize
85
+ pageLifetimes.resize.call(this, systemInfo)
86
+ } else if (isFunction(pageLifetimes[status])) {
87
+ // show & hide
88
+ pageLifetimes[status].call(this)
89
+ }
88
90
  }
89
- }
90
- }, {
91
- sync: true
92
- })
91
+ }, {
92
+ sync: true
93
+ })
94
+ }
93
95
  }
94
96
  }
95
97
  })
@@ -20,7 +20,7 @@ export default function proxyEventMixin () {
20
20
  setByPath(this, expr, value)
21
21
  },
22
22
  getOpenerEventChannel () {
23
- const router = global.__mpxRouter
23
+ const router = this.$root.$options && this.$root.$options.router
24
24
  const eventChannel = router && router.__mpxAction && router.__mpxAction.eventChannel
25
25
  return eventChannel
26
26
  }
@@ -1,7 +1,7 @@
1
1
  import transferOptions from '../core/transferOptions'
2
2
  import mergeOptions from '../core/mergeOptions'
3
3
  import builtInKeysMap from './patch/builtInKeysMap'
4
- import { makeMap, spreadProp } from '@mpxjs/utils'
4
+ import { makeMap, spreadProp, isBrowser } from '@mpxjs/utils'
5
5
  import * as webLifecycle from '../platform/patch/web/lifecycle'
6
6
  import Mpx from '../index'
7
7
 
@@ -35,7 +35,7 @@ export default function createApp (option, config = {}) {
35
35
  created () {
36
36
  Object.assign(this, Mpx.prototype)
37
37
  Object.assign(this, appData)
38
- const current = (global.__mpxRouter && global.__mpxRouter.currentRoute) || {}
38
+ const current = this.$root.$options?.router?.currentRoute || {}
39
39
  const options = {
40
40
  path: current.path && current.path.replace(/^\//, ''),
41
41
  query: current.query,
@@ -49,19 +49,24 @@ export default function createApp (option, config = {}) {
49
49
  hide: [],
50
50
  error: []
51
51
  }
52
- if (this.$options.onShow) {
53
- this.$options.onShow.call(this, options)
54
- global.__mpxAppCbs.show.push(this.$options.onShow.bind(this))
55
- }
56
- if (this.$options.onHide) {
57
- global.__mpxAppCbs.hide.push(this.$options.onHide.bind(this))
58
- }
59
- if (this.$options.onError) {
60
- global.__mpxAppCbs.error.push(this.$options.onError.bind(this))
52
+ if (isBrowser) {
53
+ if (this.$options.onShow) {
54
+ this.$options.onShow.call(this, options)
55
+ global.__mpxAppCbs.show.push(this.$options.onShow.bind(this))
56
+ }
57
+ if (this.$options.onHide) {
58
+ global.__mpxAppCbs.hide.push(this.$options.onHide.bind(this))
59
+ }
60
+ if (this.$options.onError) {
61
+ global.__mpxAppCbs.error.push(this.$options.onError.bind(this))
62
+ }
61
63
  }
62
64
  }
63
65
  })
64
66
  } else {
67
+ if (option.onAppInit) {
68
+ option.onAppInit()
69
+ }
65
70
  builtInMixins.push({
66
71
  onLaunch () {
67
72
  Object.assign(this, Mpx.prototype)
@@ -77,6 +82,10 @@ export default function createApp (option, config = {}) {
77
82
  global.__mpxOptionsMap = global.__mpxOptionsMap || {}
78
83
  global.__mpxOptionsMap[global.currentModuleId] = defaultOptions
79
84
  global.getApp = function () {
85
+ if (!isBrowser) {
86
+ console.error('[Mpx runtime error]: Dangerous API! global.getApp method is running in non browser environments')
87
+ return
88
+ }
80
89
  return appData
81
90
  }
82
91
  } else {
@@ -3,7 +3,7 @@ import mergeOptions from '../../../core/mergeOptions'
3
3
  import { diffAndCloneA, hasOwn } from '@mpxjs/utils'
4
4
  import { getCurrentInstance as getCurrentVueInstance } from '../../export/index'
5
5
  import MpxProxy, { setCurrentInstance, unsetCurrentInstance } from '../../../core/proxy'
6
- import { BEFORECREATE, BEFOREUPDATE, UPDATED, BEFOREUNMOUNT, UNMOUNTED } from '../../../core/innerLifecycle'
6
+ import { BEFORECREATE, BEFOREUPDATE, UPDATED, BEFOREUNMOUNT, UNMOUNTED, SERVERPREFETCH } from '../../../core/innerLifecycle'
7
7
 
8
8
  function filterOptions (options) {
9
9
  const newOptions = {}
@@ -79,6 +79,9 @@ export function getDefaultOptions (type, { rawOptions = {} }) {
79
79
  },
80
80
  destroyed () {
81
81
  if (this.__mpxProxy) this.__mpxProxy.callHook(UNMOUNTED)
82
+ },
83
+ serverPrefetch () {
84
+ if (this.__mpxProxy) return this.__mpxProxy.callHook(SERVERPREFETCH)
82
85
  }
83
86
  }]
84
87
  // 为了在builtMixin中可以使用某些rootMixin实现的特性(如数据响应等),此处builtInMixin在rootMixin之后执行,但是当builtInMixin使用存在对应内建生命周期的目标平台声明周期写法时,可能会出现用户生命周期比builtInMixin中的生命周期先执行的情况,为了避免这种情况发生,builtInMixin应该尽可能使用内建生命周期来编写
@@ -38,7 +38,9 @@ const APP_HOOKS = [
38
38
  'onError',
39
39
  'onPageNotFound',
40
40
  'onUnhandledRejection',
41
- 'onThemeChange'
41
+ 'onThemeChange',
42
+ 'onSSRAppCreated',
43
+ 'onAppInit'
42
44
  ]
43
45
 
44
46
  export const LIFECYCLE = {
@@ -14,7 +14,8 @@ const APP_HOOKS = [
14
14
  'onError',
15
15
  'onPageNotFound',
16
16
  'onUnhandledRejection',
17
- 'onThemeChange'
17
+ 'onThemeChange',
18
+ 'onAppInit'
18
19
  ]
19
20
 
20
21
  const PAGE_HOOKS = [