@tarojs/router 3.5.0-alpha.0 → 3.5.0-alpha.11

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/dist/index.cjs.js CHANGED
@@ -17,7 +17,31 @@ const addLeadingSlash = (url = '') => (url.charAt(0) === '/' ? url : '/' + url);
17
17
  const hasBasename = (path = '', prefix = '') => new RegExp('^' + prefix + '(\\/|\\?|#|$)', 'i').test(path) || path === prefix;
18
18
  const stripBasename = (path = '', prefix = '') => hasBasename(path, prefix) ? path.substr(prefix.length) : path;
19
19
  class RoutesAlias {
20
- conf = [];
20
+ constructor() {
21
+ this.conf = [];
22
+ this.getConfig = (url = '') => {
23
+ const customRoute = this.conf.filter((arr) => {
24
+ return arr.includes(url);
25
+ });
26
+ return customRoute[0];
27
+ };
28
+ this.getOrigin = (url = '') => {
29
+ var _a;
30
+ return ((_a = this.getConfig(url)) === null || _a === void 0 ? void 0 : _a[0]) || url;
31
+ };
32
+ this.getAlias = (url = '') => {
33
+ var _a;
34
+ return ((_a = this.getConfig(url)) === null || _a === void 0 ? void 0 : _a[1]) || url;
35
+ };
36
+ this.getAll = (url = '') => {
37
+ return this.conf
38
+ .filter((arr) => arr.includes(url))
39
+ .reduceRight((p, a) => {
40
+ p.unshift(a[1]);
41
+ return p;
42
+ }, []);
43
+ };
44
+ }
21
45
  set(customRoutes = {}) {
22
46
  for (let key in customRoutes) {
23
47
  const path = customRoutes[key];
@@ -25,36 +49,15 @@ class RoutesAlias {
25
49
  if (typeof path === 'string') {
26
50
  this.conf.push([key, addLeadingSlash(path)]);
27
51
  }
28
- else if (path?.length > 0) {
52
+ else if ((path === null || path === void 0 ? void 0 : path.length) > 0) {
29
53
  this.conf.push(...path.map(p => [key, addLeadingSlash(p)]));
30
54
  }
31
55
  }
32
56
  }
33
- getConfig = (url = '') => {
34
- const customRoute = this.conf.filter((arr) => {
35
- return arr.includes(url);
36
- });
37
- return customRoute[0];
38
- };
39
- getOrigin = (url = '') => {
40
- return this.getConfig(url)?.[0] || url;
41
- };
42
- getAlias = (url = '') => {
43
- return this.getConfig(url)?.[1] || url;
44
- };
45
- getAll = (url = '') => {
46
- return this.conf
47
- .filter((arr) => arr.includes(url))
48
- .reduceRight((p, a) => {
49
- p.unshift(a[1]);
50
- return p;
51
- }, []);
52
- };
53
57
  }
54
58
  const routesAlias = new RoutesAlias();
55
59
 
56
60
  class RouterConfig {
57
- static __config;
58
61
  static set config(e) {
59
62
  this.__config = e;
60
63
  }
@@ -79,7 +82,12 @@ class RouterConfig {
79
82
  exports.history = void 0;
80
83
  let basename = '/';
81
84
  class MpaHistory {
82
- action;
85
+ constructor() {
86
+ this.back = window.history.back;
87
+ this.forward = window.history.forward;
88
+ this.pushState = this.eventState('pushState');
89
+ this.replaceState = this.eventState('replaceState');
90
+ }
83
91
  get location() {
84
92
  return {
85
93
  pathname: window.location.pathname,
@@ -116,8 +124,6 @@ class MpaHistory {
116
124
  go(delta) {
117
125
  window.history.go(delta);
118
126
  }
119
- back = window.history.back;
120
- forward = window.history.forward;
121
127
  listen(listener) {
122
128
  function callback(e) {
123
129
  if (e.action === 'pushState') {
@@ -139,8 +145,6 @@ class MpaHistory {
139
145
  block(_blocker) {
140
146
  throw new Error('Method not implemented.');
141
147
  }
142
- pushState = this.eventState('pushState');
143
- replaceState = this.eventState('replaceState');
144
148
  eventState(action) {
145
149
  return (data, unused, url) => {
146
150
  const wrapper = window.history[action](data, unused, url);
@@ -174,9 +178,36 @@ function prependBasename(url = '') {
174
178
  return basename.replace(/\/$/, '') + '/' + url.replace(/^\//, '');
175
179
  }
176
180
 
181
+ /*! *****************************************************************************
182
+ Copyright (c) Microsoft Corporation.
183
+
184
+ Permission to use, copy, modify, and/or distribute this software for any
185
+ purpose with or without fee is hereby granted.
186
+
187
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
188
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
189
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
190
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
191
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
192
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
193
+ PERFORMANCE OF THIS SOFTWARE.
194
+ ***************************************************************************** */
195
+
196
+ function __awaiter(thisArg, _arguments, P, generator) {
197
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
198
+ return new (P || (P = Promise))(function (resolve, reject) {
199
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
200
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
201
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
202
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
203
+ });
204
+ }
205
+
177
206
  class Stacks {
178
- stacks = [];
179
- backDelta = 0;
207
+ constructor() {
208
+ this.stacks = [];
209
+ this.backDelta = 0;
210
+ }
180
211
  set delta(delta) {
181
212
  if (delta > 0) {
182
213
  this.backDelta = delta;
@@ -205,7 +236,7 @@ class Stacks {
205
236
  }
206
237
  getLastIndex(pathname, stateWith = 1) {
207
238
  const list = [...this.stacks].reverse();
208
- return list.findIndex((page, i) => i >= stateWith && page.path?.replace(/\?.*/g, '') === pathname);
239
+ return list.findIndex((page, i) => { var _a; return i >= stateWith && ((_a = page.path) === null || _a === void 0 ? void 0 : _a.replace(/\?.*/g, '')) === pathname; });
209
240
  }
210
241
  getDelta(pathname) {
211
242
  if (this.backDelta >= 1) {
@@ -232,9 +263,10 @@ class Stacks {
232
263
  const stacks = new Stacks();
233
264
 
234
265
  function processNavigateUrl(option) {
266
+ var _a;
235
267
  const pathPieces = history.parsePath(option.url);
236
268
  // 处理相对路径
237
- if (pathPieces.pathname?.includes('./')) {
269
+ if ((_a = pathPieces.pathname) === null || _a === void 0 ? void 0 : _a.includes('./')) {
238
270
  const parts = routesAlias.getOrigin(exports.history.location.pathname).split('/');
239
271
  parts.pop();
240
272
  pathPieces.pathname.split('/').forEach((item) => {
@@ -254,42 +286,44 @@ function processNavigateUrl(option) {
254
286
  pathPieces.search = '';
255
287
  return pathPieces;
256
288
  }
257
- async function navigate(option, method) {
258
- return new Promise((resolve, reject) => {
259
- const { success, complete, fail } = option;
260
- const unListen = exports.history.listen(() => {
261
- const res = { errMsg: `${method}:ok` };
262
- success?.(res);
263
- complete?.(res);
264
- resolve(res);
265
- unListen();
266
- });
267
- try {
268
- if ('url' in option) {
269
- const pathPieces = processNavigateUrl(option);
270
- const state = { timestamp: Date.now() };
271
- if (method === 'navigateTo') {
272
- exports.history.push(pathPieces, state);
273
- }
274
- else if (method === 'redirectTo' || method === 'switchTab') {
275
- exports.history.replace(pathPieces, state);
289
+ function navigate(option, method) {
290
+ return __awaiter(this, void 0, void 0, function* () {
291
+ return new Promise((resolve, reject) => {
292
+ const { success, complete, fail } = option;
293
+ const unListen = exports.history.listen(() => {
294
+ const res = { errMsg: `${method}:ok` };
295
+ success === null || success === void 0 ? void 0 : success(res);
296
+ complete === null || complete === void 0 ? void 0 : complete(res);
297
+ resolve(res);
298
+ unListen();
299
+ });
300
+ try {
301
+ if ('url' in option) {
302
+ const pathPieces = processNavigateUrl(option);
303
+ const state = { timestamp: Date.now() };
304
+ if (method === 'navigateTo') {
305
+ exports.history.push(pathPieces, state);
306
+ }
307
+ else if (method === 'redirectTo' || method === 'switchTab') {
308
+ exports.history.replace(pathPieces, state);
309
+ }
310
+ else if (method === 'reLaunch') {
311
+ stacks.delta = stacks.length;
312
+ exports.history.replace(pathPieces, state);
313
+ }
276
314
  }
277
- else if (method === 'reLaunch') {
278
- stacks.delta = stacks.length;
279
- exports.history.replace(pathPieces, state);
315
+ else if (method === 'navigateBack') {
316
+ stacks.delta = option.delta;
317
+ exports.history.go(-option.delta);
280
318
  }
281
319
  }
282
- else if (method === 'navigateBack') {
283
- stacks.delta = option.delta;
284
- exports.history.go(-option.delta);
320
+ catch (error) {
321
+ const res = { errMsg: `${method}:fail ${error.message || error}` };
322
+ fail === null || fail === void 0 ? void 0 : fail(res);
323
+ complete === null || complete === void 0 ? void 0 : complete(res);
324
+ reject(res);
285
325
  }
286
- }
287
- catch (error) {
288
- const res = { errMsg: `${method}:fail ${error.message || error}` };
289
- fail?.(res);
290
- complete?.(res);
291
- reject(res);
292
- }
326
+ });
293
327
  });
294
328
  }
295
329
  function navigateTo(option) {
@@ -315,7 +349,7 @@ function getCurrentPages() {
315
349
  console.warn('多页面路由模式不支持使用 getCurrentPages 方法!');
316
350
  }
317
351
  const pages = stacks.get();
318
- return pages.map(e => ({ ...e, route: e.path || '' }));
352
+ return pages.map(e => (Object.assign(Object.assign({}, e), { route: e.path || '' })));
319
353
  }
320
354
 
321
355
  /**
@@ -417,7 +451,7 @@ function initTabbar(config) {
417
451
  tabbar.conf.basename = routerConfig.basename;
418
452
  }
419
453
  const container = document.getElementById('container');
420
- container?.appendChild(tabbar);
454
+ container === null || container === void 0 ? void 0 : container.appendChild(tabbar);
421
455
  taro.initTabBarApis(config);
422
456
  }
423
457
 
@@ -427,13 +461,8 @@ function setDisplay(el, type = '') {
427
461
  }
428
462
  }
429
463
  class PageHandler {
430
- config;
431
- defaultAnimation = { duration: 300, delay: 50 };
432
- unloadTimer;
433
- hideTimer;
434
- lastHidePage;
435
- lastUnloadPage;
436
464
  constructor(config) {
465
+ this.defaultAnimation = { duration: 300, delay: 50 };
437
466
  this.config = config;
438
467
  this.mount();
439
468
  }
@@ -442,49 +471,57 @@ class PageHandler {
442
471
  get routerMode() { return this.router.mode || 'hash'; }
443
472
  get customRoutes() { return this.router.customRoutes || {}; }
444
473
  get routes() { return this.config.routes; }
445
- get tabBarList() { return this.config.tabBar?.list || []; }
474
+ get tabBarList() { var _a; return ((_a = this.config.tabBar) === null || _a === void 0 ? void 0 : _a.list) || []; }
446
475
  get PullDownRefresh() { return this.config.PullDownRefresh; }
447
- get animation() { return this.config?.animation ?? this.defaultAnimation; }
476
+ get animation() { var _a, _b; return (_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.animation) !== null && _b !== void 0 ? _b : this.defaultAnimation; }
448
477
  get animationDelay() {
478
+ var _a;
449
479
  return (typeof this.animation === 'object'
450
480
  ? this.animation.delay
451
481
  : this.animation
452
- ? this.defaultAnimation?.delay
482
+ ? (_a = this.defaultAnimation) === null || _a === void 0 ? void 0 : _a.delay
453
483
  : 0) || 0;
454
484
  }
455
485
  get animationDuration() {
486
+ var _a;
456
487
  return (typeof this.animation === 'object'
457
488
  ? this.animation.duration
458
489
  : this.animation
459
- ? this.defaultAnimation?.duration
490
+ ? (_a = this.defaultAnimation) === null || _a === void 0 ? void 0 : _a.duration
460
491
  : 0) || 0;
461
492
  }
462
493
  set pathname(p) { this.router.pathname = p; }
463
494
  get pathname() { return this.router.pathname; }
464
495
  get basename() { return this.router.basename || ''; }
496
+ get homePage() {
497
+ return this.config.entryPagePath || this.routes[0].path || this.basename;
498
+ }
465
499
  get pageConfig() {
500
+ const routePath = stripBasename(this.pathname, this.basename);
501
+ const homePage = this.homePage;
466
502
  return this.routes.find(r => {
467
- const routePath = stripBasename(this.pathname, this.basename);
503
+ var _a;
468
504
  const pagePath = addLeadingSlash(r.path);
469
- return pagePath === routePath || routesAlias.getConfig(pagePath)?.includes(routePath);
505
+ return [pagePath, homePage].includes(routePath) || ((_a = routesAlias.getConfig(pagePath)) === null || _a === void 0 ? void 0 : _a.includes(routePath));
470
506
  });
471
507
  }
472
508
  get isTabBar() {
509
+ var _a;
473
510
  const routePath = stripBasename(this.pathname, this.basename);
474
- const pagePath = Object.entries(this.customRoutes).find(([, target]) => {
511
+ const pagePath = ((_a = Object.entries(this.customRoutes).find(([, target]) => {
475
512
  if (typeof target === 'string') {
476
513
  return target === routePath;
477
514
  }
478
- else if (target?.length > 0) {
515
+ else if ((target === null || target === void 0 ? void 0 : target.length) > 0) {
479
516
  return target.includes(routePath);
480
517
  }
481
518
  return false;
482
- })?.[0] || routePath;
519
+ })) === null || _a === void 0 ? void 0 : _a[0]) || routePath;
483
520
  return !!pagePath && this.tabBarList.some(t => t.pagePath === pagePath);
484
521
  }
485
522
  isSamePage(page) {
486
523
  const routePath = stripBasename(this.pathname, this.basename);
487
- const pagePath = stripBasename(page?.path, this.basename);
524
+ const pagePath = stripBasename(page === null || page === void 0 ? void 0 : page.path, this.basename);
488
525
  return pagePath.startsWith(routePath + '?');
489
526
  }
490
527
  get search() {
@@ -506,11 +543,12 @@ class PageHandler {
506
543
  ? queryString__default["default"].parse(search, { decode: false })
507
544
  : {};
508
545
  query.stamp = stamp.toString();
509
- return { ...query, ...options };
546
+ return Object.assign(Object.assign({}, query), options);
510
547
  }
511
548
  mount() {
549
+ var _a;
512
550
  setHistoryMode(this.routerMode, this.router.basename);
513
- document.getElementById('app')?.remove();
551
+ (_a = document.getElementById('app')) === null || _a === void 0 ? void 0 : _a.remove();
514
552
  this.animation && loadAnimateStyle(this.animationDuration);
515
553
  const app = document.createElement('div');
516
554
  app.id = this.appId;
@@ -531,12 +569,14 @@ class PageHandler {
531
569
  }
532
570
  }
533
571
  onReady(page, onLoad = true) {
572
+ var _a, _b;
534
573
  const pageEl = this.getPageContainer(page);
535
- if (pageEl && !pageEl?.['__isReady']) {
574
+ if (pageEl && !(pageEl === null || pageEl === void 0 ? void 0 : pageEl['__isReady'])) {
536
575
  const el = pageEl.firstElementChild;
537
- el?.['componentOnReady']?.()?.then(() => {
576
+ (_b = (_a = el === null || el === void 0 ? void 0 : el['componentOnReady']) === null || _a === void 0 ? void 0 : _a.call(el)) === null || _b === void 0 ? void 0 : _b.then(() => {
538
577
  runtime.requestAnimationFrame(() => {
539
- page.onReady?.();
578
+ var _a;
579
+ (_a = page.onReady) === null || _a === void 0 ? void 0 : _a.call(page);
540
580
  pageEl['__isReady'] = true;
541
581
  });
542
582
  });
@@ -544,6 +584,7 @@ class PageHandler {
544
584
  }
545
585
  }
546
586
  load(page, pageConfig = {}, stacksIndex = 0) {
587
+ var _a, _b;
547
588
  if (!page)
548
589
  return;
549
590
  // NOTE: 页面栈推入太晚可能导致 getCurrentPages 无法获取到当前页面实例
@@ -554,21 +595,23 @@ class PageHandler {
554
595
  setDisplay(pageEl);
555
596
  this.isTabBar && pageEl.classList.add('taro_tabbar_page');
556
597
  this.addAnimation(pageEl, stacksIndex === 0);
557
- page.onShow?.();
598
+ (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
558
599
  this.bindPageEvents(page, pageEl, pageConfig);
559
600
  }
560
601
  else {
561
- page.onLoad?.(param, () => {
602
+ (_b = page.onLoad) === null || _b === void 0 ? void 0 : _b.call(page, param, () => {
603
+ var _a;
562
604
  pageEl = this.getPageContainer(page);
563
- this.isTabBar && pageEl?.classList.add('taro_tabbar_page');
605
+ this.isTabBar && (pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.add('taro_tabbar_page'));
564
606
  this.addAnimation(pageEl, stacksIndex === 0);
565
607
  this.onReady(page, true);
566
- page.onShow?.();
608
+ (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
567
609
  this.bindPageEvents(page, pageEl, pageConfig);
568
610
  });
569
611
  }
570
612
  }
571
613
  unload(page, delta = 1, top = false) {
614
+ var _a, _b, _c;
572
615
  if (!page)
573
616
  return;
574
617
  stacks.delta = --delta;
@@ -576,28 +619,30 @@ class PageHandler {
576
619
  if (this.animation && top) {
577
620
  if (this.unloadTimer) {
578
621
  clearTimeout(this.unloadTimer);
579
- this.lastUnloadPage?.onUnload?.();
622
+ (_b = (_a = this.lastUnloadPage) === null || _a === void 0 ? void 0 : _a.onUnload) === null || _b === void 0 ? void 0 : _b.call(_a);
580
623
  this.unloadTimer = null;
581
624
  }
582
625
  this.lastUnloadPage = page;
583
626
  const pageEl = this.getPageContainer(page);
584
- pageEl?.classList.remove('taro_page_stationed');
585
- pageEl?.classList.remove('taro_page_show');
627
+ pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.remove('taro_page_stationed');
628
+ pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.remove('taro_page_show');
586
629
  this.unloadTimer = setTimeout(() => {
630
+ var _a, _b;
587
631
  this.unloadTimer = null;
588
- this.lastUnloadPage?.onUnload?.();
632
+ (_b = (_a = this.lastUnloadPage) === null || _a === void 0 ? void 0 : _a.onUnload) === null || _b === void 0 ? void 0 : _b.call(_a);
589
633
  }, this.animationDuration);
590
634
  }
591
635
  else {
592
636
  const pageEl = this.getPageContainer(page);
593
- pageEl?.classList.remove('taro_page_stationed');
594
- pageEl?.classList.remove('taro_page_show');
595
- page?.onUnload?.();
637
+ pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.remove('taro_page_stationed');
638
+ pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.remove('taro_page_show');
639
+ (_c = page === null || page === void 0 ? void 0 : page.onUnload) === null || _c === void 0 ? void 0 : _c.call(page);
596
640
  }
597
641
  if (delta >= 1)
598
642
  this.unload(stacks.last, delta);
599
643
  }
600
644
  show(page, pageConfig = {}, stacksIndex = 0) {
645
+ var _a, _b;
601
646
  if (!page)
602
647
  return;
603
648
  const param = this.getQuery(stacks.length, '', page.options);
@@ -605,20 +650,22 @@ class PageHandler {
605
650
  if (pageEl) {
606
651
  setDisplay(pageEl);
607
652
  this.addAnimation(pageEl, stacksIndex === 0);
608
- page.onShow?.();
653
+ (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
609
654
  this.bindPageEvents(page, pageEl, pageConfig);
610
655
  }
611
656
  else {
612
- page.onLoad?.(param, () => {
657
+ (_b = page.onLoad) === null || _b === void 0 ? void 0 : _b.call(page, param, () => {
658
+ var _a;
613
659
  pageEl = this.getPageContainer(page);
614
660
  this.addAnimation(pageEl, stacksIndex === 0);
615
661
  this.onReady(page, false);
616
- page.onShow?.();
662
+ (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
617
663
  this.bindPageEvents(page, pageEl, pageConfig);
618
664
  });
619
665
  }
620
666
  }
621
667
  hide(page) {
668
+ var _a;
622
669
  if (!page)
623
670
  return;
624
671
  // NOTE: 修复多页并发问题,此处可能因为路由跳转过快,执行时页面可能还没有创建成功
@@ -634,7 +681,7 @@ class PageHandler {
634
681
  this.hideTimer = null;
635
682
  setDisplay(this.lastHidePage, 'none');
636
683
  }, this.animationDuration + this.animationDelay);
637
- page.onHide?.();
684
+ (_a = page.onHide) === null || _a === void 0 ? void 0 : _a.call(page);
638
685
  }
639
686
  else {
640
687
  setTimeout(() => this.hide(page), 0);
@@ -657,8 +704,9 @@ class PageHandler {
657
704
  }
658
705
  }
659
706
  getPageContainer(page) {
660
- const path = page ? page?.path : runtime.Current.page?.path;
661
- const id = path?.replace(/([^a-z0-9\u00a0-\uffff_-])/ig, '\\$1');
707
+ var _a;
708
+ const path = page ? page === null || page === void 0 ? void 0 : page.path : (_a = runtime.Current.page) === null || _a === void 0 ? void 0 : _a.path;
709
+ const id = path === null || path === void 0 ? void 0 : path.replace(/([^a-z0-9\u00a0-\uffff_-])/ig, '\\$1');
662
710
  if (page) {
663
711
  return document.querySelector(`.taro_page#${id}`);
664
712
  }
@@ -669,39 +717,45 @@ class PageHandler {
669
717
  return el || window;
670
718
  }
671
719
  bindPageEvents(page, pageEl, config = {}) {
720
+ var _a;
672
721
  if (!pageEl) {
673
722
  pageEl = this.getPageContainer();
674
723
  }
675
- const distance = config.onReachBottomDistance || this.config.window?.onReachBottomDistance || 50;
724
+ const distance = config.onReachBottomDistance || ((_a = this.config.window) === null || _a === void 0 ? void 0 : _a.onReachBottomDistance) || 50;
676
725
  bindPageScroll(page, pageEl, distance);
677
726
  bindPageResize(page);
678
727
  }
679
728
  }
680
729
 
681
- /* eslint-disable dot-notation */
682
730
  function createRouter(app, config, framework) {
731
+ var _a, _b;
683
732
  RouterConfig.config = config;
684
733
  const handler = new PageHandler(config);
685
734
  const runtimeHooks = runtime.container.get(runtime.SERVICE_IDENTIFIER.Hooks);
686
735
  routesAlias.set(handler.router.customRoutes);
687
736
  const basename = handler.router.basename;
688
- const routes = handler.routes.map(route => ({
689
- path: routesAlias.getAll(addLeadingSlash(route.path)),
690
- action: route.load
691
- }));
692
- const entryPagePath = config.entryPagePath || routes[0].path?.[0];
737
+ const routes = handler.routes.map(route => {
738
+ const routePath = addLeadingSlash(route.path);
739
+ const paths = routesAlias.getAll(routePath);
740
+ return {
741
+ path: paths.length < 1 ? routePath : paths,
742
+ action: route.load
743
+ };
744
+ });
693
745
  const router = new UniversalRouter__default["default"](routes, { baseUrl: basename || '' });
694
746
  const launchParam = handler.getQuery(stacks.length);
695
- app.onLaunch?.(launchParam);
696
- const render = async ({ location, action }) => {
747
+ (_a = app.onLaunch) === null || _a === void 0 ? void 0 : _a.call(app, launchParam);
748
+ const render = ({ location, action }) => __awaiter(this, void 0, void 0, function* () {
749
+ var _c, _d, _e, _f, _g;
697
750
  handler.pathname = decodeURI(location.pathname);
698
- let element;
751
+ let element, params;
699
752
  try {
700
- element = await router.resolve(handler.router.forcePath || handler.pathname);
753
+ const result = yield router.resolve(handler.router.forcePath || handler.pathname);
754
+ [element, , params] = yield Promise.all(result);
701
755
  }
702
756
  catch (error) {
703
757
  if (error.status === 404) {
704
- app.onPageNotFound?.({
758
+ (_c = app.onPageNotFound) === null || _c === void 0 ? void 0 : _c.call(app, {
705
759
  path: handler.pathname
706
760
  });
707
761
  }
@@ -712,14 +766,14 @@ function createRouter(app, config, framework) {
712
766
  if (!element)
713
767
  return;
714
768
  const pageConfig = handler.pageConfig;
715
- let enablePullDownRefresh = config?.window?.enablePullDownRefresh || false;
769
+ let enablePullDownRefresh = ((_d = config === null || config === void 0 ? void 0 : config.window) === null || _d === void 0 ? void 0 : _d.enablePullDownRefresh) || false;
716
770
  runtime.eventCenter.trigger('__taroRouterChange', {
717
771
  toLocation: {
718
772
  path: handler.pathname
719
773
  }
720
774
  });
721
775
  if (pageConfig) {
722
- document.title = pageConfig.navigationBarTitleText ?? document.title;
776
+ document.title = (_e = pageConfig.navigationBarTitleText) !== null && _e !== void 0 ? _e : document.title;
723
777
  if (typeof pageConfig.enablePullDownRefresh === 'boolean') {
724
778
  enablePullDownRefresh = pageConfig.enablePullDownRefresh;
725
779
  }
@@ -761,28 +815,27 @@ function createRouter(app, config, framework) {
761
815
  shouldLoad = true;
762
816
  }
763
817
  if (shouldLoad || stacks.length < 1) {
764
- const el = element.default ?? element;
765
- const loadConfig = { ...pageConfig };
818
+ const el = (_f = element.default) !== null && _f !== void 0 ? _f : element;
819
+ const loadConfig = Object.assign({}, pageConfig);
766
820
  const stacksIndex = stacks.length;
767
821
  delete loadConfig['path'];
768
822
  delete loadConfig['load'];
769
- const page = runtime.createPageConfig(enablePullDownRefresh ? runtimeHooks.createPullDownComponent?.(el, location.pathname, framework, handler.PullDownRefresh) : el, pathname + runtime.stringify(handler.getQuery(stacksIndex)), {}, loadConfig);
823
+ const page = runtime.createPageConfig(enablePullDownRefresh ? (_g = runtimeHooks.createPullDownComponent) === null || _g === void 0 ? void 0 : _g.call(runtimeHooks, el, location.pathname, framework, handler.PullDownRefresh) : el, pathname + runtime.stringify(handler.getQuery(stacksIndex)), {}, loadConfig);
824
+ if (params)
825
+ page.options = params;
770
826
  return handler.load(page, pageConfig, stacksIndex);
771
827
  }
772
- };
773
- if (exports.history.location.pathname === '/') {
774
- const stripped = stripBasename(exports.history.location.pathname, handler.basename);
775
- if (stripped === '/' || stripped === '') {
776
- exports.history.replace(prependBasename(entryPagePath + exports.history.location.search));
777
- }
828
+ });
829
+ const routePath = stripBasename(exports.history.location.pathname, handler.basename);
830
+ if (routePath === '/' || routePath === '') {
831
+ exports.history.replace(prependBasename(handler.homePage + exports.history.location.search));
778
832
  }
779
833
  render({ location: exports.history.location, action: history.Action.Push });
780
- app.onShow?.(launchParam);
834
+ (_b = app.onShow) === null || _b === void 0 ? void 0 : _b.call(app, launchParam);
781
835
  return exports.history.listen(render);
782
836
  }
783
837
 
784
838
  class MultiPageHandler {
785
- config;
786
839
  constructor(config) {
787
840
  this.config = config;
788
841
  this.mount();
@@ -791,23 +844,24 @@ class MultiPageHandler {
791
844
  get router() { return this.config.router; }
792
845
  get routerMode() { return this.router.mode || 'hash'; }
793
846
  get customRoutes() { return this.router.customRoutes || {}; }
794
- get tabBarList() { return this.config.tabBar?.list || []; }
847
+ get tabBarList() { var _a; return ((_a = this.config.tabBar) === null || _a === void 0 ? void 0 : _a.list) || []; }
795
848
  get PullDownRefresh() { return this.config.PullDownRefresh; }
796
849
  set pathname(p) { this.router.pathname = p; }
797
850
  get pathname() { return this.router.pathname; }
798
851
  get basename() { return this.router.basename || ''; }
799
852
  get pageConfig() { return this.config.route; }
800
853
  get isTabBar() {
854
+ var _a;
801
855
  const routePath = stripBasename(this.pathname, this.basename);
802
- const pagePath = Object.entries(this.customRoutes).find(([, target]) => {
856
+ const pagePath = ((_a = Object.entries(this.customRoutes).find(([, target]) => {
803
857
  if (typeof target === 'string') {
804
858
  return target === routePath;
805
859
  }
806
- else if (target?.length > 0) {
860
+ else if ((target === null || target === void 0 ? void 0 : target.length) > 0) {
807
861
  return target.includes(routePath);
808
862
  }
809
863
  return false;
810
- })?.[0] || routePath;
864
+ })) === null || _a === void 0 ? void 0 : _a[0]) || routePath;
811
865
  return !!pagePath && this.tabBarList.some(t => t.pagePath === pagePath);
812
866
  }
813
867
  get search() { return location.search.substr(1); }
@@ -816,11 +870,12 @@ class MultiPageHandler {
816
870
  const query = search
817
871
  ? queryString__default["default"].parse(search)
818
872
  : {};
819
- return { ...query, ...options };
873
+ return Object.assign(Object.assign({}, query), options);
820
874
  }
821
875
  mount() {
876
+ var _a;
822
877
  setHistoryMode(this.routerMode, this.router.basename);
823
- document.getElementById('app')?.remove();
878
+ (_a = document.getElementById('app')) === null || _a === void 0 ? void 0 : _a.remove();
824
879
  const app = document.createElement('div');
825
880
  app.id = this.appId;
826
881
  app.classList.add('taro_router');
@@ -840,27 +895,31 @@ class MultiPageHandler {
840
895
  }
841
896
  }
842
897
  onReady(page, onLoad = true) {
898
+ var _a;
843
899
  const pageEl = this.getPageContainer(page);
844
- if (pageEl && !pageEl?.['__isReady']) {
900
+ if (pageEl && !(pageEl === null || pageEl === void 0 ? void 0 : pageEl['__isReady'])) {
845
901
  const el = pageEl.firstElementChild;
846
- el?.['componentOnReady']?.();
902
+ (_a = el === null || el === void 0 ? void 0 : el['componentOnReady']) === null || _a === void 0 ? void 0 : _a.call(el);
847
903
  onLoad && (pageEl['__page'] = page);
848
904
  }
849
905
  }
850
906
  load(page, pageConfig = {}) {
907
+ var _a;
851
908
  if (!page)
852
909
  return;
853
- page.onLoad?.(this.getQuery('', page.options), () => {
910
+ (_a = page.onLoad) === null || _a === void 0 ? void 0 : _a.call(page, this.getQuery('', page.options), () => {
911
+ var _a;
854
912
  const pageEl = this.getPageContainer(page);
855
- this.isTabBar && pageEl?.classList.add('taro_tabbar_page');
913
+ this.isTabBar && (pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.add('taro_tabbar_page'));
856
914
  this.onReady(page, true);
857
- page.onShow?.();
915
+ (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
858
916
  this.bindPageEvents(page, pageEl, pageConfig);
859
917
  });
860
918
  }
861
919
  getPageContainer(page) {
862
- const path = page ? page?.path : runtime.Current.page?.path;
863
- const id = path?.replace(/([^a-z0-9\u00a0-\uffff_-])/ig, '\\$1');
920
+ var _a;
921
+ const path = page ? page === null || page === void 0 ? void 0 : page.path : (_a = runtime.Current.page) === null || _a === void 0 ? void 0 : _a.path;
922
+ const id = path === null || path === void 0 ? void 0 : path.replace(/([^a-z0-9\u00a0-\uffff_-])/ig, '\\$1');
864
923
  if (page) {
865
924
  return document.querySelector(`.taro_page#${id}`);
866
925
  }
@@ -871,16 +930,16 @@ class MultiPageHandler {
871
930
  return el || window;
872
931
  }
873
932
  bindPageEvents(page, pageEl, config = {}) {
933
+ var _a;
874
934
  if (!pageEl) {
875
935
  pageEl = this.getPageContainer();
876
936
  }
877
- const distance = config.onReachBottomDistance || this.config.window?.onReachBottomDistance || 50;
937
+ const distance = config.onReachBottomDistance || ((_a = this.config.window) === null || _a === void 0 ? void 0 : _a.onReachBottomDistance) || 50;
878
938
  bindPageScroll(page, pageEl, distance);
879
939
  bindPageResize(page);
880
940
  }
881
941
  }
882
942
 
883
- /* eslint-disable dot-notation */
884
943
  // TODO 支持多路由 (APP 生命周期仅触发一次)
885
944
  /** Note: 关于多页面应用
886
945
  * - 需要配置路由映射(根目录跳转、404 页面……)
@@ -889,42 +948,45 @@ class MultiPageHandler {
889
948
  * - TabBar 会多次加载
890
949
  * - 不支持路由动画
891
950
  */
892
- async function createMultiRouter(app, config, framework) {
893
- RouterConfig.config = config;
894
- const handler = new MultiPageHandler(config);
895
- const runtimeHooks = runtime.container.get(runtime.SERVICE_IDENTIFIER.Hooks);
896
- const launchParam = handler.getQuery();
897
- app.onLaunch?.(launchParam);
898
- const pathName = config.pageName;
899
- const pageConfig = handler.pageConfig;
900
- let element;
901
- try {
902
- element = await pageConfig.load?.();
903
- }
904
- catch (error) {
905
- throw new Error(error);
906
- }
907
- if (!element)
908
- return;
909
- let enablePullDownRefresh = config?.window?.enablePullDownRefresh || false;
910
- runtime.eventCenter.trigger('__taroRouterChange', {
911
- toLocation: {
912
- path: pathName
951
+ function createMultiRouter(app, config, framework) {
952
+ var _a, _b, _c, _d, _e, _f, _g;
953
+ return __awaiter(this, void 0, void 0, function* () {
954
+ RouterConfig.config = config;
955
+ const handler = new MultiPageHandler(config);
956
+ const runtimeHooks = runtime.container.get(runtime.SERVICE_IDENTIFIER.Hooks);
957
+ const launchParam = handler.getQuery();
958
+ (_a = app.onLaunch) === null || _a === void 0 ? void 0 : _a.call(app, launchParam);
959
+ const pathName = config.pageName;
960
+ const pageConfig = handler.pageConfig;
961
+ let element;
962
+ try {
963
+ element = yield ((_b = pageConfig.load) === null || _b === void 0 ? void 0 : _b.call(pageConfig));
964
+ }
965
+ catch (error) {
966
+ throw new Error(error);
967
+ }
968
+ if (!element)
969
+ return;
970
+ let enablePullDownRefresh = ((_c = config === null || config === void 0 ? void 0 : config.window) === null || _c === void 0 ? void 0 : _c.enablePullDownRefresh) || false;
971
+ runtime.eventCenter.trigger('__taroRouterChange', {
972
+ toLocation: {
973
+ path: pathName
974
+ }
975
+ });
976
+ if (pageConfig) {
977
+ document.title = (_d = pageConfig.navigationBarTitleText) !== null && _d !== void 0 ? _d : document.title;
978
+ if (typeof pageConfig.enablePullDownRefresh === 'boolean') {
979
+ enablePullDownRefresh = pageConfig.enablePullDownRefresh;
980
+ }
913
981
  }
982
+ const el = (_e = element.default) !== null && _e !== void 0 ? _e : element;
983
+ const loadConfig = Object.assign({}, pageConfig);
984
+ delete loadConfig['path'];
985
+ delete loadConfig['load'];
986
+ const page = runtime.createPageConfig(enablePullDownRefresh ? (_f = runtimeHooks.createPullDownComponent) === null || _f === void 0 ? void 0 : _f.call(runtimeHooks, el, location.pathname, framework, config.PullDownRefresh) : el, pathName + runtime.stringify(launchParam), {}, loadConfig);
987
+ handler.load(page, pageConfig);
988
+ (_g = app.onShow) === null || _g === void 0 ? void 0 : _g.call(app, launchParam);
914
989
  });
915
- if (pageConfig) {
916
- document.title = pageConfig.navigationBarTitleText ?? document.title;
917
- if (typeof pageConfig.enablePullDownRefresh === 'boolean') {
918
- enablePullDownRefresh = pageConfig.enablePullDownRefresh;
919
- }
920
- }
921
- const el = element.default ?? element;
922
- const loadConfig = { ...pageConfig };
923
- delete loadConfig['path'];
924
- delete loadConfig['load'];
925
- const page = runtime.createPageConfig(enablePullDownRefresh ? runtimeHooks.createPullDownComponent?.(el, location.pathname, framework, config.PullDownRefresh) : el, pathName + runtime.stringify(launchParam), {}, loadConfig);
926
- handler.load(page, pageConfig);
927
- app.onShow?.(launchParam);
928
990
  }
929
991
 
930
992
  exports.createMultiRouter = createMultiRouter;