@tarojs/runtime 3.3.16 → 3.3.20

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.
@@ -1,5 +1,4 @@
1
1
  /// <reference types="react" />
2
- import { eventHandler } from '../dom/event';
3
2
  import type { PageConfig } from '@tarojs/taro';
4
3
  import type { Instance, PageInstance, PageProps } from './instance';
5
4
  export declare function injectPageInstance(inst: Instance<PageProps>, id: string): void;
@@ -13,24 +12,4 @@ export declare function getOnShowEventKey(path: string): string;
13
12
  export declare function getOnHideEventKey(path: string): string;
14
13
  export declare function createPageConfig(component: any, pageName?: string, data?: Record<string, unknown>, pageConfig?: PageConfig): PageInstance;
15
14
  export declare function createComponentConfig(component: React.ComponentClass, componentName?: string, data?: Record<string, unknown>): any;
16
- export declare function createRecursiveComponentConfig(componentName?: string): {
17
- properties: {
18
- i: {
19
- type: ObjectConstructor;
20
- value: {
21
- nn: string;
22
- };
23
- };
24
- l: {
25
- type: StringConstructor;
26
- value: string;
27
- };
28
- };
29
- options: {
30
- addGlobalClass: boolean;
31
- virtualHost: boolean;
32
- };
33
- methods: {
34
- eh: typeof eventHandler;
35
- };
36
- };
15
+ export declare function createRecursiveComponentConfig(componentName?: string): any;
@@ -21,13 +21,13 @@ interface VueInternal {
21
21
  export interface PageProps {
22
22
  tid?: string;
23
23
  }
24
- export interface ReactPageComponent<T = PageProps> extends ComponentClass<T>, Show, PageInstance {
24
+ export interface ReactPageComponent<T = PageProps> extends ComponentClass<T>, PageInstance {
25
25
  }
26
- export interface ReactPageInstance<T = PageProps> extends Component<T>, Show, PageInstance {
26
+ export interface ReactPageInstance<T = PageProps> extends Component<T>, PageInstance {
27
27
  componentDidShow?(): void;
28
28
  componentDidHide?(): void;
29
29
  }
30
- export interface ReactAppInstance<T = AppInstance> extends Component<T>, Show, AppInstance {
30
+ export interface ReactAppInstance<T = AppInstance> extends Component<T>, AppInstance {
31
31
  }
32
32
  export interface PageLifeCycle extends Show {
33
33
  onPullDownRefresh?(): void;
@@ -63,15 +63,17 @@ export interface PageInstance extends PageLifeCycle {
63
63
  options?: Record<string, unknown>;
64
64
  }
65
65
  interface Show {
66
- componentDidShow?(options?: unknown): void;
67
- componentDidHide?(options?: unknown): void;
68
- onShow?(options?: unknown): void;
69
- onHide?(options?: unknown): void;
66
+ componentDidShow?(): void;
67
+ componentDidHide?(): void;
68
+ onShow?(): void;
69
+ onHide?(): void;
70
70
  }
71
71
  export interface AppInstance extends Show {
72
- onLaunch?(options?: string): void;
72
+ onLaunch?(options?: Record<string, unknown>): void;
73
73
  mount?(component: React.ComponentClass | ComponentOptions<VueCtor> | Vue3Component, id: string, cb: (...args: any[]) => void): void;
74
74
  mount?(component: React.ComponentClass | ComponentOptions<VueCtor> | Vue3Component, id: string, cb: () => void): void;
75
+ componentDidShow?(options?: Record<string, unknown>): void;
76
+ onShow?(options?: Record<string, unknown>): void;
75
77
  unmount?(id: string): void;
76
78
  unmount?(id: string, cb: () => void): void;
77
79
  onPageNotFound?(res: any): void;
@@ -5,8 +5,9 @@ import type { AppInstance, ReactPageComponent, PageProps } from './instance';
5
5
  export declare let R: typeof React;
6
6
  export declare let PageContext: React.Context<string>;
7
7
  export declare function connectReactPage(R: typeof React, id: string): (component: ReactPageComponent) => React.ComponentClass<PageProps>;
8
- export declare function createReactApp(App: React.ComponentClass, react: typeof React, reactdom: any, config: AppConfig): AppInstance;
8
+ export declare function createReactApp(App: React.ComponentClass, react: typeof React, dom: any, config: AppConfig): AppInstance;
9
9
  export declare function createNativeComponentConfig(Component: any, react: typeof React, reactdom: any, componentConfig: any): {
10
+ options: any;
10
11
  properties: {
11
12
  props: {
12
13
  type: null;
@@ -19,7 +20,7 @@ export declare function createNativeComponentConfig(Component: any, react: typeo
19
20
  ready(): void;
20
21
  detached(): void;
21
22
  pageLifetimes: {
22
- show(): void;
23
+ show(options: any): void;
23
24
  hide(): void;
24
25
  };
25
26
  methods: {
@@ -1285,7 +1285,8 @@ function shortcutAttr(key) {
1285
1285
  default:
1286
1286
  return key;
1287
1287
  }
1288
- }
1288
+ }
1289
+ const customWrapperCache = new Map();
1289
1290
 
1290
1291
  let TaroEventTarget = class TaroEventTarget {
1291
1292
  constructor(// eslint-disable-next-line @typescript-eslint/indent
@@ -2141,7 +2142,7 @@ let TaroElement = class TaroElement extends TaroNode {
2141
2142
  qualifiedName = shortcutAttr(qualifiedName);
2142
2143
  const payload = {
2143
2144
  path: `${this._path}.${toCamelCase(qualifiedName)}`,
2144
- value
2145
+ value: isFunction$1(value) ? () => value : value
2145
2146
  };
2146
2147
  (_b = (_a = this.hooks).modifySetAttrPayload) === null || _b === void 0 ? void 0 : _b.call(_a, this, qualifiedName, payload);
2147
2148
  this.enqueueUpdate(payload);
@@ -3458,17 +3459,17 @@ let TaroRootElement = class TaroRootElement extends TaroElement {
3458
3459
  const dataPathArr = p.split('.');
3459
3460
  let hasCustomWrapper = false;
3460
3461
  for (let i = dataPathArr.length; i > 0; i--) {
3461
- const allPath = dataPathArr.slice(0, i).join('.');
3462
- const getData = get(ctx.__data__ || ctx.data, allPath);
3463
- if (getData && getData.nn && getData.nn === CUSTOM_WRAPPER) {
3462
+ const allPath = dataPathArr.slice(1, i).join('.').replace(/\bcn\b/g, 'childNodes');
3463
+ const getData = get(this, allPath);
3464
+ if (getData && getData.nodeName && getData.nodeName === CUSTOM_WRAPPER) {
3464
3465
  const customWrapperId = getData.uid;
3465
- const customWrapper = ctx.selectComponent(`#${customWrapperId}`);
3466
+ const customWrapper = customWrapperCache.get(customWrapperId);
3466
3467
  const splitedPath = dataPathArr.slice(i).join('.');
3467
3468
  if (customWrapper) {
3468
3469
  hasCustomWrapper = true;
3469
3470
  customWrapperMap.set(customWrapper, Object.assign(Object.assign({}, (customWrapperMap.get(customWrapper) || {})), { [`i.${splitedPath}`]: data[p] }));
3471
+ break;
3470
3472
  }
3471
- break;
3472
3473
  }
3473
3474
  }
3474
3475
  if (!hasCustomWrapper) {
@@ -3971,8 +3972,12 @@ class StyleTagParser {
3971
3972
  // console.log('res this.styles: ', this.styles)
3972
3973
  }
3973
3974
  parseSelector(src) {
3974
- // todo: 属性选择器里可以带空格:[a = "b"],这里的 split(' ') 需要作兼容
3975
- const list = src.trim().replace(/ *([>~+]) */g, ' $1').replace(/ +/g, ' ').split(' ');
3975
+ const list = src
3976
+ .trim()
3977
+ .replace(/ *([>~+]) */g, ' $1')
3978
+ .replace(/ +/g, ' ')
3979
+ .replace(/\[\s*([^[\]=\s]+)\s*=\s*([^[\]=\s]+)\s*\]/g, '[$1=$2]')
3980
+ .split(' ');
3976
3981
  const selectors = list.map(item => {
3977
3982
  const firstChar = item.charAt(0);
3978
3983
  const selector = {
@@ -5018,11 +5023,11 @@ if (process.env.TARO_ENV && process.env.TARO_ENV !== 'h5') {
5018
5023
  if (!(DATE in window$1)) {
5019
5024
  window$1.Date = Date;
5020
5025
  }
5021
- window$1.setTimeout = function (cb, delay) {
5022
- setTimeout(cb, delay);
5026
+ window$1.setTimeout = function (...args) {
5027
+ return setTimeout(...args);
5023
5028
  };
5024
- window$1.clearTimeout = function (seed) {
5025
- clearTimeout(seed);
5029
+ window$1.clearTimeout = function (...args) {
5030
+ return clearTimeout(...args);
5026
5031
  };
5027
5032
  }
5028
5033
 
@@ -5164,11 +5169,8 @@ function stringify(obj) {
5164
5169
  return path === '' ? path : '?' + path;
5165
5170
  }
5166
5171
  function getPath(id, options) {
5167
- let path = id;
5168
- if (!isBrowser) {
5169
- path = id + stringify(options);
5170
- }
5171
- return path;
5172
+ const idx = id.indexOf('?');
5173
+ return `${idx > -1 ? id.substring(0, idx) : id}${stringify(isBrowser ? { stamp: (options === null || options === void 0 ? void 0 : options.stamp) || '' } : options)}`;
5172
5174
  }
5173
5175
  function getOnReadyEventKey(path) {
5174
5176
  return path + '.' + 'onReady';
@@ -5189,7 +5191,7 @@ function createPageConfig(component, pageName, data, pageConfig) {
5189
5191
  let loadResolver;
5190
5192
  let hasLoaded;
5191
5193
  const config = {
5192
- onLoad(options, cb) {
5194
+ onLoad(options = {}, cb) {
5193
5195
  hasLoaded = new Promise(resolve => { loadResolver = resolve; });
5194
5196
  perf.start(PAGE_INIT);
5195
5197
  Current.page = this;
@@ -5197,6 +5199,9 @@ function createPageConfig(component, pageName, data, pageConfig) {
5197
5199
  options.$taroTimestamp = Date.now();
5198
5200
  // this.$taroPath 是页面唯一标识,不可变,因此页面参数 options 也不可变
5199
5201
  this.$taroPath = getPath(id, options);
5202
+ if (isBrowser) {
5203
+ config.path = this.$taroPath;
5204
+ }
5200
5205
  // this.$taroParams 作为暴露给开发者的页面参数对象,可以被随意修改
5201
5206
  if (this.$taroParams == null) {
5202
5207
  this.$taroParams = Object.assign({}, options);
@@ -5334,9 +5339,6 @@ function createPageConfig(component, pageName, data, pageConfig) {
5334
5339
  if (!isUndefined(data)) {
5335
5340
  config.data = data;
5336
5341
  }
5337
- if (isBrowser) {
5338
- config.path = id;
5339
- }
5340
5342
  return config;
5341
5343
  }
5342
5344
  function createComponentConfig(component, componentName, data) {
@@ -5380,8 +5382,26 @@ function createComponentConfig(component, componentName, data) {
5380
5382
  return config;
5381
5383
  }
5382
5384
  function createRecursiveComponentConfig(componentName) {
5383
- return {
5384
- properties: {
5385
+ const isCustomWrapper = componentName === 'custom-wrapper';
5386
+ const lifeCycles = isCustomWrapper
5387
+ ? {
5388
+ attached() {
5389
+ var _a;
5390
+ const componentId = (_a = this.data.i) === null || _a === void 0 ? void 0 : _a.uid;
5391
+ if (isString(componentId)) {
5392
+ customWrapperCache.set(componentId, this);
5393
+ }
5394
+ },
5395
+ detached() {
5396
+ var _a;
5397
+ const componentId = (_a = this.data.i) === null || _a === void 0 ? void 0 : _a.uid;
5398
+ if (isString(componentId)) {
5399
+ customWrapperCache.delete(componentId);
5400
+ }
5401
+ }
5402
+ }
5403
+ : EMPTY_OBJ;
5404
+ return Object.assign({ properties: {
5385
5405
  i: {
5386
5406
  type: Object,
5387
5407
  value: {
@@ -5392,15 +5412,12 @@ function createRecursiveComponentConfig(componentName) {
5392
5412
  type: String,
5393
5413
  value: ''
5394
5414
  }
5395
- },
5396
- options: {
5415
+ }, options: {
5397
5416
  addGlobalClass: true,
5398
- virtualHost: componentName !== 'custom-wrapper'
5399
- },
5400
- methods: {
5417
+ virtualHost: !isCustomWrapper
5418
+ }, methods: {
5401
5419
  eh: eventHandler
5402
- }
5403
- };
5420
+ } }, lifeCycles);
5404
5421
  }
5405
5422
 
5406
5423
  const hooks$3 = container.get(SERVICE_IDENTIFIER.Hooks);
@@ -5507,9 +5524,10 @@ function setReconciler() {
5507
5524
  }
5508
5525
  }
5509
5526
  const pageKeyId = incrementId();
5510
- function createReactApp(App, react, reactdom, config) {
5527
+ function createReactApp(App, react, dom, config) {
5528
+ var _a;
5511
5529
  R = react;
5512
- ReactDOM = reactdom;
5530
+ ReactDOM = dom;
5513
5531
  ensure(!!ReactDOM, '构建 React/Nerv 项目请把 process.env.FRAMEWORK 设置为 \'react\'/\'nerv\' ');
5514
5532
  const ref = R.createRef();
5515
5533
  const isReactComponent = isClassComponent(R, App);
@@ -5551,8 +5569,7 @@ function createReactApp(App, react, reactdom, config) {
5551
5569
  }
5552
5570
  let wrapper;
5553
5571
  if (!isBrowser) {
5554
- // eslint-disable-next-line react/no-render-return-value
5555
- wrapper = ReactDOM.render(R.createElement(AppWrapper), document$2.getElementById('app'));
5572
+ wrapper = (_a = ReactDOM.render) === null || _a === void 0 ? void 0 : _a.call(ReactDOM, R.createElement(AppWrapper), document$2.getElementById('app'));
5556
5573
  }
5557
5574
  const app = Object.create({
5558
5575
  render(cb) {
@@ -5576,11 +5593,11 @@ function createReactApp(App, react, reactdom, config) {
5576
5593
  enumerable: true,
5577
5594
  writable: true,
5578
5595
  value(options) {
5596
+ var _a;
5579
5597
  Current.router = Object.assign({ params: options === null || options === void 0 ? void 0 : options.query }, options);
5580
5598
  if (isBrowser) {
5581
5599
  // 由于 H5 路由初始化的时候会清除 app 下的 dom 元素,所以需要在路由初始化后执行 render
5582
- // eslint-disable-next-line react/no-render-return-value
5583
- wrapper = ReactDOM.render(R.createElement(AppWrapper), document$2.getElementById('app'));
5600
+ wrapper = (_a = ReactDOM.render) === null || _a === void 0 ? void 0 : _a.call(ReactDOM, R.createElement(AppWrapper), document$2.getElementById((config === null || config === void 0 ? void 0 : config.appId) || 'app'));
5584
5601
  }
5585
5602
  const app = ref.current;
5586
5603
  // For taroize
@@ -5619,16 +5636,16 @@ function createReactApp(App, react, reactdom, config) {
5619
5636
  app.componentDidShow(options);
5620
5637
  }
5621
5638
  // app useDidShow
5622
- triggerAppHook('onShow');
5639
+ triggerAppHook('onShow', options);
5623
5640
  }
5624
5641
  },
5625
5642
  onHide: {
5626
5643
  enumerable: true,
5627
5644
  writable: true,
5628
- value(options) {
5645
+ value() {
5629
5646
  const app = ref.current;
5630
5647
  if (app != null && isFunction$1(app.componentDidHide)) {
5631
- app.componentDidHide(options);
5648
+ app.componentDidHide();
5632
5649
  }
5633
5650
  // app useDidHide
5634
5651
  triggerAppHook('onHide');
@@ -5645,13 +5662,13 @@ function createReactApp(App, react, reactdom, config) {
5645
5662
  }
5646
5663
  }
5647
5664
  });
5648
- function triggerAppHook(lifecycle) {
5665
+ function triggerAppHook(lifecycle, ...option) {
5649
5666
  const instance = getPageInstance(HOOKS_APP_ID);
5650
5667
  if (instance) {
5651
5668
  const app = ref.current;
5652
5669
  const func = hooks$3.getLifecycle(instance, lifecycle);
5653
5670
  if (Array.isArray(func)) {
5654
- func.forEach(cb => cb.apply(app));
5671
+ func.forEach(cb => cb.apply(app, option));
5655
5672
  }
5656
5673
  }
5657
5674
  }
@@ -5731,6 +5748,7 @@ function createNativeComponentConfig(Component, react, reactdom, componentConfig
5731
5748
  ReactDOM = reactdom;
5732
5749
  setReconciler();
5733
5750
  const config = {
5751
+ options: componentConfig,
5734
5752
  properties: {
5735
5753
  props: {
5736
5754
  type: null,
@@ -5758,8 +5776,8 @@ function createNativeComponentConfig(Component, react, reactdom, componentConfig
5758
5776
  Current.app.unmount(this.compId);
5759
5777
  },
5760
5778
  pageLifetimes: {
5761
- show() {
5762
- safeExecute(this.compId, 'onShow');
5779
+ show(options) {
5780
+ safeExecute(this.compId, 'onShow', options);
5763
5781
  },
5764
5782
  hide() {
5765
5783
  safeExecute(this.compId, 'onHide');
@@ -5935,7 +5953,7 @@ function createVueApp(App, vue, config) {
5935
5953
  Current.router = Object.assign({ params: options === null || options === void 0 ? void 0 : options.query }, options);
5936
5954
  if (isBrowser) {
5937
5955
  // 由于 H5 路由初始化的时候会清除 app 下的 dom 元素,所以需要在路由初始化后再执行 render
5938
- wrapper.$mount(document$2.getElementById('app'));
5956
+ wrapper.$mount(document$2.getElementById((config === null || config === void 0 ? void 0 : config.appId) || 'app'));
5939
5957
  }
5940
5958
  appInstance = wrapper.$refs.app;
5941
5959
  if (appInstance != null && isFunction$1(appInstance.$options.onLaunch)) {
@@ -5976,10 +5994,6 @@ function createVue3Page(h, id) {
5976
5994
  },
5977
5995
  created() {
5978
5996
  injectPageInstance(this, id);
5979
- // vue3 组件 created 时机比小程序页面 onShow 慢,因此在 created 后再手动触发一次 onShow。
5980
- this.$nextTick(() => {
5981
- safeExecute(id, 'onShow');
5982
- });
5983
5997
  }
5984
5998
  };
5985
5999
  if (isArray$1(component.mixins)) {
@@ -6083,7 +6097,7 @@ function createVue3App(app, h, config) {
6083
6097
  var _a;
6084
6098
  Current.router = Object.assign({ params: options === null || options === void 0 ? void 0 : options.query }, options);
6085
6099
  if (isBrowser) {
6086
- appInstance = app.mount('#app');
6100
+ appInstance = app.mount(`#${config.appId || 'app'}`);
6087
6101
  }
6088
6102
  // 把 App Class 上挂载的额外属性同步到全局 app 对象中
6089
6103
  // eslint-disable-next-line dot-notation