@mpxjs/core 2.9.66 → 2.9.69-beta.0

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.
Files changed (52) hide show
  1. package/@types/global.d.ts +2 -0
  2. package/@types/index.d.ts +18 -2
  3. package/package.json +19 -7
  4. package/src/convertor/convertor.js +11 -32
  5. package/src/convertor/wxToAli.js +3 -3
  6. package/src/convertor/wxToReact.js +1 -1
  7. package/src/convertor/wxToSwan.js +3 -3
  8. package/src/convertor/wxToWeb.js +3 -3
  9. package/src/core/mergeOptions.js +1 -1
  10. package/src/core/proxy.js +86 -12
  11. package/src/dynamic/dynamicRenderMixin.js +2 -2
  12. package/src/index.js +3 -14
  13. package/src/observer/reactive.js +5 -4
  14. package/src/observer/ref.js +3 -2
  15. package/src/observer/scheduler.js +4 -0
  16. package/src/observer/watch.js +5 -4
  17. package/src/platform/builtInMixins/directiveHelperMixin.ios.js +4 -1
  18. package/src/platform/builtInMixins/styleHelperMixin.ios.js +28 -25
  19. package/src/platform/createApp.ios.js +110 -42
  20. package/src/platform/createApp.js +8 -8
  21. package/src/platform/env/event.js +108 -0
  22. package/src/platform/env/index.ios.js +51 -0
  23. package/src/platform/env/index.js +8 -0
  24. package/src/platform/env/index.web.js +48 -0
  25. package/src/{external → platform/env}/vuePlugin.js +1 -1
  26. package/src/platform/export/index.js +5 -0
  27. package/src/platform/export/index.web.js +3 -1
  28. package/src/platform/export/inject.js +68 -0
  29. package/src/platform/export/inject.web.js +1 -0
  30. package/src/platform/patch/builtInKeysMap.js +2 -0
  31. package/src/platform/patch/{ali/getDefaultOptions.js → getDefaultOptions.ali.js} +19 -6
  32. package/src/platform/patch/{react/getDefaultOptions.ios.js → getDefaultOptions.ios.js} +286 -200
  33. package/src/platform/patch/{wx/getDefaultOptions.js → getDefaultOptions.js} +20 -11
  34. package/src/platform/patch/{web/getDefaultOptions.js → getDefaultOptions.web.js} +9 -7
  35. package/src/platform/patch/index.js +4 -21
  36. package/src/platform/patch/{ali/lifecycle.js → lifecycle/index.ali.js} +2 -0
  37. package/src/platform/patch/lifecycle/index.js +1 -0
  38. package/src/platform/patch/{swan/lifecycle.js → lifecycle/index.swan.js} +2 -0
  39. package/src/platform/patch/{web/lifecycle.js → lifecycle/index.web.js} +4 -0
  40. package/src/platform/patch/{wx/lifecycle.js → lifecycle/index.wx.js} +2 -0
  41. package/src/runtime/createFactory.js +3 -0
  42. package/LICENSE +0 -433
  43. package/src/external/vue.js +0 -1
  44. package/src/external/vue.web.js +0 -6
  45. package/src/platform/builtInMixins/directiveHelperMixin.android.js +0 -2
  46. package/src/platform/builtInMixins/proxyEventMixin.android.js +0 -2
  47. package/src/platform/builtInMixins/refsMixin.android.js +0 -2
  48. package/src/platform/builtInMixins/styleHelperMixin.android.js +0 -2
  49. package/src/platform/createApp.android.js +0 -2
  50. package/src/platform/patch/react/getDefaultOptions.android.js +0 -1
  51. package/src/platform/patch/react/getDefaultOptions.js +0 -1
  52. package/src/platform/patch/swan/getDefaultOptions.js +0 -34
@@ -4,6 +4,8 @@ declare let __mpx_mode__: 'wx' | 'ali' | 'swan' | 'qq' | 'tt' | 'web' | 'dd' | '
4
4
  // declaration for mpx env
5
5
  declare let __mpx_env__: string
6
6
 
7
+ declare const Mixin: WechatMiniprogram.Behavior.Constructor
8
+
7
9
  // Wildcard module declarations for ?resolve case
8
10
  declare module '*?resolve' {
9
11
  const resourcePath: string
package/@types/index.d.ts CHANGED
@@ -137,6 +137,11 @@ interface ComponentOpt<D extends Data, P extends Properties, C, M extends Method
137
137
 
138
138
  initData?: Record<string, any>
139
139
 
140
+ provide?: Record<string, any> | (() => Record<string, any>)
141
+ inject?:
142
+ | { [key: string]: string | Symbol | { from?: string | Symbol; default?: any } }
143
+ | Array<string>
144
+
140
145
  [index: string]: any
141
146
  }
142
147
 
@@ -259,13 +264,15 @@ interface MpxConfig {
259
264
  ignoreWarning: boolean | string | RegExp | ((msg: string, location: string, e: Error) => boolean)
260
265
  ignoreProxyWhiteList: Array<string>
261
266
  observeClassInstance: boolean | Array<AnyConstructor>
262
- errorHandler: (e: Error, target: ComponentIns<{}, {}, {}, {}, []>, hookName: string) => any | null
267
+ errorHandler: (msg: String, location: String, e: Error) => any | null
268
+ warnHandler: (msg: String, location: String, e: Error) => any | null
263
269
  proxyEventHandler: (e: WechatMiniprogram.CustomEvent) => any | null
264
270
  setDataHandler: (data: object, target: ComponentIns<{}, {}, {}, {}, []>) => any | null
265
271
  forceFlushSync: boolean,
266
272
  webRouteConfig: object,
267
273
  webConfig: object,
268
- webviewConfig?: WebviewConfig
274
+ webviewConfig: WebviewConfig,
275
+ rnConfig: object,
269
276
  }
270
277
 
271
278
  type SupportedMode = 'wx' | 'ali' | 'qq' | 'swan' | 'tt' | 'web' | 'qa'
@@ -306,6 +313,8 @@ export interface Mpx {
306
313
 
307
314
  delete: typeof del
308
315
 
316
+ provide: typeof provide
317
+
309
318
  config: MpxConfig
310
319
 
311
320
  i18n: {
@@ -570,6 +579,13 @@ export function onScopeDispose (fn: () => void): void
570
579
  export function set<T extends object> (target: T, key: string | number, value: any): void
571
580
  export function del<T extends object> (target: T, key: keyof T): void
572
581
 
582
+ // provide & inject
583
+ export declare function provide<T>(key: InjectionKey<T> | string | number, value: T): void;
584
+ export declare function inject<T>(key: InjectionKey<T> | string): T | undefined;
585
+ export declare function inject<T>(key: InjectionKey<T> | string, defaultValue: T, treatDefaultAsFactory?: false): T;
586
+ export declare function inject<T>(key: InjectionKey<T> | string, defaultValue: T | (() => T), treatDefaultAsFactory: true): T;
587
+ export declare interface InjectionKey<T> extends Symbol {}
588
+
573
589
  // nextTick
574
590
  export function nextTick (fn: () => any): void
575
591
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mpxjs/core",
3
- "version": "2.9.66",
3
+ "version": "2.9.69-beta.0",
4
4
  "description": "mpx runtime core",
5
5
  "keywords": [
6
6
  "miniprogram",
@@ -19,21 +19,24 @@
19
19
  ],
20
20
  "main": "src/index.js",
21
21
  "dependencies": {
22
- "@mpxjs/utils": "^2.9.65",
22
+ "@mpxjs/utils": "^2.9.69",
23
23
  "lodash": "^4.1.1",
24
24
  "miniprogram-api-typings": "^3.10.0"
25
25
  },
26
26
  "peerDependencies": {
27
27
  "@ant-design/react-native": "^5.1.3",
28
+ "@d11/react-native-fast-image": "^8.6.12",
28
29
  "@mpxjs/api-proxy": "^2.9.0",
29
30
  "@mpxjs/store": "^2.9.0",
30
- "@react-navigation/native": "^6.1.17",
31
- "@react-navigation/native-stack": "^6.9.26",
31
+ "@react-navigation/native": "^7.0.3",
32
+ "@react-navigation/stack": "^7.0.4",
32
33
  "react": "*",
33
34
  "react-native": "*",
35
+ "promise": "^8.3.0",
34
36
  "react-native-gesture-handler": "^2.19.0",
35
37
  "react-native-linear-gradient": "^2.8.3",
36
- "react-native-safe-area-context": "^4.10.1",
38
+ "react-native-safe-area-context": "^4.14.0",
39
+ "react-native-screens": "^4.1.0",
37
40
  "react-native-webview": "^13.10.5",
38
41
  "vue": "^2.7.10",
39
42
  "vue-demi": "^0.14.6",
@@ -59,10 +62,13 @@
59
62
  "react-native": {
60
63
  "optional": true
61
64
  },
65
+ "promise": {
66
+ "optional": true
67
+ },
62
68
  "@react-navigation/native": {
63
69
  "optional": true
64
70
  },
65
- "@react-navigation/native-stack": {
71
+ "@react-navigation/stack": {
66
72
  "optional": true
67
73
  },
68
74
  "@ant-design/react-native": {
@@ -71,6 +77,9 @@
71
77
  "react-native-safe-area-context": {
72
78
  "optional": true
73
79
  },
80
+ "react-native-screens": {
81
+ "optional": true
82
+ },
74
83
  "react-native-webview": {
75
84
  "optional": true
76
85
  },
@@ -79,6 +88,9 @@
79
88
  },
80
89
  "react-native-linear-gradient": {
81
90
  "optional": true
91
+ },
92
+ "@d11/react-native-fast-image": {
93
+ "optional": true
82
94
  }
83
95
  },
84
96
  "publishConfig": {
@@ -97,5 +109,5 @@
97
109
  "url": "https://github.com/didi/mpx/issues"
98
110
  },
99
111
  "sideEffects": false,
100
- "gitHead": "ff9eb06a3be28538870823cebf813ed56f39bbd7"
112
+ "gitHead": "2d37697869b9bdda3efab92dda8c910b68fd05c0"
101
113
  }
@@ -1,9 +1,6 @@
1
- import * as wxLifecycle from '../platform/patch/wx/lifecycle'
2
- import * as aliLifecycle from '../platform/patch/ali/lifecycle'
3
- import * as webLifecycle from '../platform/patch/web/lifecycle'
4
- import * as swanLifecycle from '../platform/patch/swan/lifecycle'
1
+ import { LIFECYCLE, lifecycleProxyMap, pageMode } from '../platform/patch/lifecycle/index'
5
2
  import { mergeLifecycle } from './mergeLifecycle'
6
- import { error } from '@mpxjs/utils'
3
+ import { error, extend } from '@mpxjs/utils'
7
4
  import wxToAliRule from './wxToAli'
8
5
  import wxToWebRule from './wxToWeb'
9
6
  import wxToSwanRule from './wxToSwan'
@@ -13,24 +10,6 @@ import wxToDdRule from './wxToDd'
13
10
  import wxToJdRule from './wxToJd'
14
11
  import wxToReactRule from './wxToReact'
15
12
 
16
- // 根据当前环境获取的默认生命周期信息
17
- let lifecycleInfo
18
- let pageMode
19
-
20
- if (__mpx_mode__ === 'web') {
21
- lifecycleInfo = webLifecycle
22
- pageMode = ''
23
- } else if (__mpx_mode__ === 'ali') {
24
- lifecycleInfo = aliLifecycle
25
- pageMode = ''
26
- } else if (__mpx_mode__ === 'swan') {
27
- lifecycleInfo = swanLifecycle
28
- pageMode = 'blend'
29
- } else {
30
- lifecycleInfo = wxLifecycle
31
- pageMode = 'blend'
32
- }
33
-
34
13
  /**
35
14
  * 转换规则包含四点
36
15
  * lifecycle [object] 生命周期
@@ -40,25 +19,25 @@ if (__mpx_mode__ === 'web') {
40
19
  * convert [function] 自定义转换函数, 接收一个options
41
20
  */
42
21
  const defaultConvertRule = {
43
- lifecycle: mergeLifecycle(lifecycleInfo.LIFECYCLE),
44
- lifecycleProxyMap: lifecycleInfo.lifecycleProxyMap,
22
+ lifecycle: mergeLifecycle(LIFECYCLE),
23
+ lifecycleProxyMap: lifecycleProxyMap,
45
24
  pageMode,
46
25
  support: !!pageMode,
47
26
  convert: null
48
27
  }
49
28
 
50
29
  const rulesMap = {
51
- local: { ...defaultConvertRule },
30
+ local: extend({}, defaultConvertRule),
52
31
  default: defaultConvertRule,
53
32
  wxToWeb: wxToWebRule,
54
33
  wxToAli: wxToAliRule,
55
34
  wxToSwan: wxToSwanRule,
56
- wxToQq: { ...defaultConvertRule, ...wxToQqRule },
57
- wxToTt: { ...defaultConvertRule, ...wxToTtRule },
58
- wxToDd: { ...defaultConvertRule, ...wxToDdRule },
59
- wxToJd: { ...defaultConvertRule, ...wxToJdRule },
60
- wxToIos: { ...defaultConvertRule, ...wxToReactRule },
61
- wxToAndroid: { ...defaultConvertRule, ...wxToReactRule }
35
+ wxToQq: extend({}, defaultConvertRule, wxToQqRule),
36
+ wxToTt: extend({}, defaultConvertRule, wxToTtRule),
37
+ wxToDd: extend({}, defaultConvertRule, wxToDdRule),
38
+ wxToJd: extend({}, defaultConvertRule, wxToJdRule),
39
+ wxToIos: extend({}, defaultConvertRule, wxToReactRule),
40
+ wxToAndroid: extend({}, defaultConvertRule, wxToReactRule)
62
41
  }
63
42
 
64
43
  export function getConvertRule (convertMode) {
@@ -1,5 +1,5 @@
1
- import * as wxLifecycle from '../platform/patch/wx/lifecycle'
2
- import * as aliLifecycle from '../platform/patch/ali/lifecycle'
1
+ import * as wxLifecycle from '../platform/patch/lifecycle/index.wx'
2
+ import { LIFECYCLE } from '../platform/patch/lifecycle/index'
3
3
  import { mergeLifecycle } from './mergeLifecycle'
4
4
  import { error, hasOwn, isDev } from '@mpxjs/utils'
5
5
  import { implemented } from '../core/implement'
@@ -38,7 +38,7 @@ function notSupportTip (options) {
38
38
 
39
39
  export default {
40
40
  lifecycle: mergeLifecycle(wxLifecycle.LIFECYCLE),
41
- lifecycle2: mergeLifecycle(aliLifecycle.LIFECYCLE),
41
+ lifecycle2: mergeLifecycle(LIFECYCLE),
42
42
  pageMode: 'blend',
43
43
  support: false,
44
44
  lifecycleProxyMap: wxLifecycle.lifecycleProxyMap,
@@ -5,7 +5,7 @@ import {
5
5
  import { implemented } from '../core/implement'
6
6
 
7
7
  // 暂不支持的wx选项,后期需要各种花式支持
8
- const unsupported = ['relations', 'moved', 'definitionFilter', 'onShareAppMessage']
8
+ const unsupported = ['relations', 'moved', 'definitionFilter']
9
9
 
10
10
  function convertErrorDesc (key) {
11
11
  error(`Options.${key} is not supported in runtime conversion from wx to react native.`, global.currentResource)
@@ -1,8 +1,8 @@
1
1
  import { error, isDev } from '@mpxjs/utils'
2
2
  import { implemented } from '../core/implement'
3
3
  import { mergeLifecycle } from './mergeLifecycle'
4
- import * as wxLifecycle from '../platform/patch/wx/lifecycle'
5
- import * as swanLifecycle from '../platform/patch/swan/lifecycle'
4
+ import * as wxLifecycle from '../platform/patch/lifecycle/index.wx'
5
+ import { LIFECYCLE } from '../platform/patch/lifecycle/index'
6
6
 
7
7
  const BEHAVIORS_MAP = {
8
8
  'wx://form-field': 'swan://form-field',
@@ -30,7 +30,7 @@ function notSupportTip (options) {
30
30
 
31
31
  export default {
32
32
  lifecycle: mergeLifecycle(wxLifecycle.LIFECYCLE),
33
- lifecycle2: mergeLifecycle(swanLifecycle.LIFECYCLE),
33
+ lifecycle2: mergeLifecycle(LIFECYCLE),
34
34
  pageMode: 'blend',
35
35
  support: true,
36
36
  lifecycleProxyMap: wxLifecycle.lifecycleProxyMap,
@@ -1,5 +1,5 @@
1
- import * as wxLifecycle from '../platform/patch/wx/lifecycle'
2
- import * as webLifecycle from '../platform/patch/web/lifecycle'
1
+ import * as wxLifecycle from '../platform/patch/lifecycle/index.wx'
2
+ import { LIFECYCLE } from '../platform/patch/lifecycle/index'
3
3
  import { mergeLifecycle } from './mergeLifecycle'
4
4
  import {
5
5
  isObject,
@@ -32,7 +32,7 @@ function notSupportTip (options) {
32
32
 
33
33
  export default {
34
34
  lifecycle: mergeLifecycle(wxLifecycle.LIFECYCLE),
35
- lifecycle2: mergeLifecycle(webLifecycle.LIFECYCLE),
35
+ lifecycle2: mergeLifecycle(LIFECYCLE),
36
36
  pageMode: 'blend',
37
37
  support: true,
38
38
  lifecycleProxyMap: wxLifecycle.lifecycleProxyMap,
@@ -354,7 +354,7 @@ function transformHOOKS (options) {
354
354
  const componentHooksMap = makeMap(convertRule.lifecycle.component)
355
355
  for (const key in options) {
356
356
  // 使用Component创建page实例,页面专属生命周期&自定义方法需写在methods内部
357
- if (typeof options[key] === 'function' && key !== 'dataFn' && key !== 'setup' && !componentHooksMap[key]) {
357
+ if (typeof options[key] === 'function' && key !== 'dataFn' && key !== 'setup' && key !== 'provide' && !componentHooksMap[key]) {
358
358
  if (!options.methods) options.methods = {}
359
359
  options.methods[key] = options[key]
360
360
  delete options[key]
package/src/core/proxy.js CHANGED
@@ -8,10 +8,13 @@ import Mpx from '../index'
8
8
  import {
9
9
  noop,
10
10
  type,
11
+ isArray,
11
12
  isFunction,
12
13
  isObject,
13
14
  isEmptyObject,
14
15
  isPlainObject,
16
+ isWeb,
17
+ isReact,
15
18
  doGetByPath,
16
19
  getByPath,
17
20
  setByPath,
@@ -25,6 +28,7 @@ import {
25
28
  processUndefined,
26
29
  getFirstKey,
27
30
  callWithErrorHandling,
31
+ wrapMethodsWithErrorHandling,
28
32
  warn,
29
33
  error,
30
34
  getEnvObj
@@ -47,6 +51,7 @@ import {
47
51
  } from './innerLifecycle'
48
52
  import contextMap from '../dynamic/vnode/context'
49
53
  import { getAst } from '../dynamic/astCache'
54
+ import { inject, provide } from '../platform/export/inject'
50
55
 
51
56
  let uid = 0
52
57
 
@@ -112,7 +117,7 @@ export default class MpxProxy {
112
117
  this.ignoreProxyMap = makeMap(Mpx.config.ignoreProxyWhiteList)
113
118
  // 收集setup中动态注册的hooks,小程序与web环境都需要
114
119
  this.hooks = {}
115
- if (__mpx_mode__ !== 'web') {
120
+ if (!isWeb) {
116
121
  this.scope = effectScope(true)
117
122
  // props响应式数据代理
118
123
  this.props = {}
@@ -130,8 +135,12 @@ export default class MpxProxy {
130
135
  this.forceUpdateAll = false
131
136
  this.currentRenderTask = null
132
137
  this.propsUpdatedFlag = false
133
- // react专用,正确触发updated钩子
134
- this.pendingUpdatedFlag = false
138
+ if (isReact) {
139
+ // react专用,正确触发updated钩子
140
+ this.pendingUpdatedFlag = false
141
+ this.memoVersion = Symbol()
142
+ this.finalMemoVersion = Symbol()
143
+ }
135
144
  }
136
145
  this.initApi()
137
146
  }
@@ -156,22 +165,26 @@ export default class MpxProxy {
156
165
  // 缓存上下文,在 destoryed 阶段删除
157
166
  contextMap.set(this.uid, this.target)
158
167
  }
159
- if (__mpx_mode__ !== 'web') {
168
+ if (!isWeb) {
160
169
  // web中BEFORECREATE钩子通过vue的beforeCreate钩子单独驱动
161
170
  this.callHook(BEFORECREATE)
162
171
  setCurrentInstance(this)
172
+ // 在 props/data 初始化之前初始化 inject
173
+ this.initInject()
163
174
  this.initProps()
164
175
  this.initSetup()
165
176
  this.initData()
166
177
  this.initComputed()
167
178
  this.initWatch()
179
+ // 在 props/data 初始化之后初始化 provide
180
+ this.initProvide()
168
181
  unsetCurrentInstance()
169
182
  }
170
183
 
171
184
  this.state = CREATED
172
185
  this.callHook(CREATED)
173
186
 
174
- if (__mpx_mode__ !== 'web' && __mpx_mode__ !== 'ios' && __mpx_mode__ !== 'android') {
187
+ if (!isWeb && !isReact) {
175
188
  this.initRender()
176
189
  }
177
190
 
@@ -219,6 +232,16 @@ export default class MpxProxy {
219
232
  // 页面/组件销毁清除上下文的缓存
220
233
  contextMap.remove(this.uid)
221
234
  }
235
+ if (!isWeb && this.options.__type__ === 'page') {
236
+ // 小程序页面销毁时移除对应的 provide
237
+ if (isFunction(this.target.getPageId)) {
238
+ const pageId = this.target.getPageId()
239
+ const providesMap = global.__mpxProvidesMap
240
+ if (providesMap.__pages[pageId]) {
241
+ delete providesMap.__pages[pageId]
242
+ }
243
+ }
244
+ }
222
245
  this.callHook(BEFOREUNMOUNT)
223
246
  this.scope?.stop()
224
247
  if (this.update) this.update.active = false
@@ -254,7 +277,7 @@ export default class MpxProxy {
254
277
  }
255
278
  // 挂载$rawOptions
256
279
  this.target.$rawOptions = this.options
257
- if (__mpx_mode__ !== 'web') {
280
+ if (!isWeb) {
258
281
  // 挂载$watch
259
282
  this.target.$watch = this.watch.bind(this)
260
283
  // 强制执行render
@@ -264,20 +287,21 @@ export default class MpxProxy {
264
287
  }
265
288
 
266
289
  initProps () {
267
- if (__mpx_mode__ === 'ios' || __mpx_mode__ === 'android') {
290
+ if (isReact) {
268
291
  // react模式下props内部对象透传无需深clone,依赖对象深层的数据响应触发子组件更新
269
292
  this.props = this.target.__getProps()
293
+ reactive(this.processIgnoreReactive(this.props))
270
294
  } else {
271
295
  this.props = diffAndCloneA(this.target.__getProps(this.options)).clone
296
+ reactive(this.processIgnoreReactive(this.props))
272
297
  }
273
- reactive(this.processIgnoreReactive(this.props))
274
298
  proxy(this.target, this.props, undefined, false, this.createProxyConflictHandler('props'))
275
299
  }
276
300
 
277
301
  initSetup () {
278
302
  const setup = this.options.setup
279
303
  if (setup) {
280
- const setupResult = callWithErrorHandling(setup, this, 'setup function', [
304
+ let setupResult = callWithErrorHandling(setup, this, 'setup function', [
281
305
  this.props,
282
306
  {
283
307
  triggerEvent: this.target.triggerEvent ? this.target.triggerEvent.bind(this.target) : noop,
@@ -294,6 +318,7 @@ export default class MpxProxy {
294
318
  error(`Setup() should return a object, received: ${type(setupResult)}.`, this.options.mpxFileResource)
295
319
  return
296
320
  }
321
+ setupResult = wrapMethodsWithErrorHandling(setupResult, this)
297
322
  proxy(this.target, setupResult, undefined, false, this.createProxyConflictHandler('setup result'))
298
323
  this.collectLocalKeys(setupResult, (key, val) => !isFunction(val))
299
324
  }
@@ -350,6 +375,55 @@ export default class MpxProxy {
350
375
  }
351
376
  }
352
377
 
378
+ initProvide () {
379
+ const provideOpt = this.options.provide
380
+ if (provideOpt) {
381
+ const provided = isFunction(provideOpt)
382
+ ? callWithErrorHandling(provideOpt.bind(this.target), this, 'provide function')
383
+ : provideOpt
384
+ if (!isObject(provided)) {
385
+ return
386
+ }
387
+ Object.keys(provided).forEach(key => {
388
+ provide(key, provided[key])
389
+ })
390
+ }
391
+ }
392
+
393
+ initInject () {
394
+ const injectOpt = this.options.inject
395
+ if (injectOpt) {
396
+ this.resolveInject(injectOpt)
397
+ }
398
+ }
399
+
400
+ resolveInject (injectOpt) {
401
+ if (isArray(injectOpt)) {
402
+ const normalized = {}
403
+ for (let i = 0; i < injectOpt.length; i++) {
404
+ normalized[injectOpt[i]] = injectOpt[i]
405
+ }
406
+ injectOpt = normalized
407
+ }
408
+ const injectObj = {}
409
+ for (const key in injectOpt) {
410
+ const opt = injectOpt[key]
411
+ let injected
412
+ if (isObject(opt)) {
413
+ if ('default' in opt) {
414
+ injected = inject(opt.from || key, opt.default, true)
415
+ } else {
416
+ injected = inject(opt.from || key)
417
+ }
418
+ } else {
419
+ injected = inject(opt)
420
+ }
421
+ injectObj[key] = injected
422
+ }
423
+ proxy(this.target, injectObj, undefined, false, this.createProxyConflictHandler('inject'))
424
+ this.collectLocalKeys(injectObj)
425
+ }
426
+
353
427
  watch (source, cb, options) {
354
428
  const target = this.target
355
429
  const getter = isString(source)
@@ -494,7 +568,7 @@ export default class MpxProxy {
494
568
  }
495
569
  if (!processed) {
496
570
  // 如果当前数据和上次的miniRenderData完全无关,但存在于组件的视图数据中,则与组件视图数据进行diff
497
- if (this.target.data && hasOwn(this.target.data, firstKey)) {
571
+ if (hasOwn(this.target.data, firstKey)) {
498
572
  const localInitialData = getByPath(this.target.data, key)
499
573
  const { clone, diff, diffData } = diffAndCloneA(data, localInitialData)
500
574
  this.miniRenderData[key] = clone
@@ -687,12 +761,12 @@ export default class MpxProxy {
687
761
  }
688
762
  setByPath(this.target, key, data[key])
689
763
  })
690
- this.forceUpdateData = data
764
+ Object.assign(this.forceUpdateData, data)
691
765
  } else {
692
766
  this.forceUpdateAll = true
693
767
  }
694
768
 
695
- if (__mpx_mode__ === 'ios' || __mpx_mode__ === 'android') {
769
+ if (isReact) {
696
770
  // rn中不需要setdata
697
771
  this.forceUpdateData = {}
698
772
  this.forceUpdateAll = false
@@ -1,4 +1,4 @@
1
- import { hasOwn, isObject, error } from '@mpxjs/utils'
1
+ import { hasOwn, error } from '@mpxjs/utils'
2
2
  import genVnodeTree from './vnode/render'
3
3
  import contextMap from './vnode/context'
4
4
  import { CREATED } from '../core/innerLifecycle'
@@ -56,7 +56,7 @@ function dynamicRenderHelperMixin () {
56
56
  methods: {
57
57
  _g (astData, moduleId) {
58
58
  const location = this.__mpxProxy && this.__mpxProxy.options.mpxFileResource
59
- if (astData && isObject(astData) && hasOwn(astData, 'template')) {
59
+ if (hasOwn(astData, 'template')) {
60
60
  const vnodeTree = genVnodeTree(astData, [this], { moduleId, location })
61
61
  return vnodeTree
62
62
  } else {
package/src/index.js CHANGED
@@ -1,8 +1,7 @@
1
- import Vue from './external/vue'
1
+
2
2
  import { error, diffAndCloneA, hasOwn, makeMap } from '@mpxjs/utils'
3
3
  import { APIs, InstanceAPIs } from './platform/export/api'
4
-
5
- import { createI18n } from './platform/builtInMixins/i18nMixin'
4
+ import { init } from './platform/env/index'
6
5
 
7
6
  export * from './platform/export/index'
8
7
 
@@ -123,10 +122,6 @@ function factory () {
123
122
 
124
123
  Object.assign(Mpx, APIs)
125
124
  Object.assign(Mpx.prototype, InstanceAPIs)
126
- // 输出web时在mpx上挂载Vue对象
127
- if (__mpx_mode__ === 'web') {
128
- Mpx.__vue = Vue
129
- }
130
125
  return Mpx
131
126
  }
132
127
 
@@ -156,12 +151,6 @@ Mpx.config = {
156
151
  rnConfig: {}
157
152
  }
158
153
 
159
- global.__mpx = Mpx
160
-
161
- if (__mpx_mode__ !== 'web') {
162
- if (global.i18n) {
163
- Mpx.i18n = createI18n(global.i18n)
164
- }
165
- }
154
+ init(Mpx)
166
155
 
167
156
  export default Mpx
@@ -34,6 +34,7 @@ export class Observer {
34
34
 
35
35
  constructor (value, shallow) {
36
36
  this.value = value
37
+ this.shallow = shallow
37
38
  def(value, ObKey, this)
38
39
  if (Array.isArray(value)) {
39
40
  const augment = hasProto && arrayProtoAugment
@@ -120,7 +121,7 @@ export function defineReactive (obj, key, val, shallow) {
120
121
  const getter = property && property.get
121
122
  const setter = property && property.set
122
123
 
123
- let childOb = !shallow && observe(val)
124
+ let childOb = shallow ? getObserver(val) : observe(val)
124
125
  Object.defineProperty(obj, key, {
125
126
  enumerable: true,
126
127
  configurable: true,
@@ -149,7 +150,7 @@ export function defineReactive (obj, key, val, shallow) {
149
150
  } else {
150
151
  val = newVal
151
152
  }
152
- childOb = !shallow && observe(newVal)
153
+ childOb = shallow ? getObserver(newVal) : observe(newVal)
153
154
  dep.notify()
154
155
  }
155
156
  })
@@ -175,7 +176,7 @@ export function set (target, key, val) {
175
176
  target[key] = val
176
177
  return val
177
178
  }
178
- defineReactive(ob.value, key, val)
179
+ defineReactive(ob.value, key, val, ob.shallow)
179
180
  ob.dep.notify()
180
181
  return val
181
182
  }
@@ -225,7 +226,7 @@ export function shallowReactive (value) {
225
226
  }
226
227
 
227
228
  export function isReactive (value) {
228
- return value && hasOwn(value, ObKey) && value[ObKey] instanceof Observer
229
+ return hasOwn(value, ObKey) && value[ObKey] instanceof Observer
229
230
  }
230
231
 
231
232
  export function getObserver (value) {
@@ -3,12 +3,13 @@ import { RefKey } from '../helper/const'
3
3
  import {
4
4
  warn,
5
5
  isPlainObject,
6
- hasOwn
6
+ hasOwn,
7
+ extend
7
8
  } from '@mpxjs/utils'
8
9
 
9
10
  export class RefImpl {
10
11
  constructor (options) {
11
- Object.defineProperty(this, 'value', { enumerable: true, ...options })
12
+ Object.defineProperty(this, 'value', extend({ enumerable: true }, options))
12
13
  }
13
14
  }
14
15
 
@@ -83,6 +83,10 @@ export function queueJob (job) {
83
83
  }
84
84
  }
85
85
 
86
+ export function hasPendingJob (job) {
87
+ return queue.length && queue.includes(job, isFlushing && job.allowRecurse ? flushIndex + 1 : flushIndex)
88
+ }
89
+
86
90
  function queueFlush () {
87
91
  if (!isFlushing && !isFlushPending) {
88
92
  isFlushPending = true
@@ -12,7 +12,8 @@ import {
12
12
  isArray,
13
13
  remove,
14
14
  callWithErrorHandling,
15
- hasChanged
15
+ hasChanged,
16
+ extend
16
17
  } from '@mpxjs/utils'
17
18
 
18
19
  export function watchEffect (effect, options) {
@@ -20,11 +21,11 @@ export function watchEffect (effect, options) {
20
21
  }
21
22
 
22
23
  export function watchPostEffect (effect, options) {
23
- return watch(effect, null, { ...options, flush: 'post' })
24
+ return watch(effect, null, extend({}, options, { flush: 'post' }))
24
25
  }
25
26
 
26
27
  export function watchSyncEffect (effect, options) {
27
- return watch(effect, null, { ...options, flush: 'sync' })
28
+ return watch(effect, null, extend({}, options, { flush: 'sync' }))
28
29
  }
29
30
 
30
31
  const warnInvalidSource = (s) => {
@@ -34,7 +35,7 @@ const warnInvalidSource = (s) => {
34
35
  const shouldTrigger = (value, oldValue) => hasChanged(value, oldValue) || isObject(value)
35
36
 
36
37
  const processWatchOptionsCompat = (options) => {
37
- const newOptions = { ...options }
38
+ const newOptions = extend({}, options)
38
39
  if (options.sync) {
39
40
  newOptions.flush = 'sync'
40
41
  }
@@ -1,7 +1,10 @@
1
1
  export default function directiveHelperMixin () {
2
2
  return {
3
3
  methods: {
4
- __getWxKey (item, key) {
4
+ __getWxKey (item, key, index) {
5
+ if (key === 'index') {
6
+ return index
7
+ }
5
8
  return key === '*this' ? item : item[key]
6
9
  }
7
10
  }