@tarojs/router 3.5.0-theta.0 → 3.5.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/LICENSE +21 -0
- package/dist/events/scroll.js +2 -1
- package/dist/index.cjs.js +62 -15
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +61 -15
- package/dist/index.esm.js.map +1 -1
- package/dist/router/mpa.js +3 -1
- package/dist/router/multi-page.d.ts +1 -1
- package/dist/router/multi-page.js +1 -1
- package/dist/router/page.d.ts +1 -1
- package/dist/router/page.js +4 -4
- package/dist/router/spa.js +18 -8
- package/dist/{utils.d.ts → utils/index.d.ts} +2 -0
- package/dist/{utils.js → utils/index.js} +5 -1
- package/dist/utils/navigate.d.ts +5 -0
- package/dist/utils/navigate.js +44 -0
- package/package.json +12 -10
- package/types/router.d.ts +2 -2
package/dist/router/page.js
CHANGED
|
@@ -6,7 +6,7 @@ import { bindPageResize } from '../events/resize';
|
|
|
6
6
|
import { bindPageScroll } from '../events/scroll';
|
|
7
7
|
import { setHistoryMode } from '../history';
|
|
8
8
|
import { initTabbar } from '../tabbar';
|
|
9
|
-
import { addLeadingSlash, routesAlias, stripBasename } from '../utils';
|
|
9
|
+
import { addLeadingSlash, routesAlias, stripBasename, stripTrailing } from '../utils';
|
|
10
10
|
import stacks from './stack';
|
|
11
11
|
function setDisplay(el, type = '') {
|
|
12
12
|
if (el) {
|
|
@@ -20,10 +20,10 @@ export default class PageHandler {
|
|
|
20
20
|
this.mount();
|
|
21
21
|
}
|
|
22
22
|
get appId() { return 'app'; }
|
|
23
|
-
get router() { return this.config.router; }
|
|
23
|
+
get router() { return this.config.router || {}; }
|
|
24
24
|
get routerMode() { return this.router.mode || 'hash'; }
|
|
25
25
|
get customRoutes() { return this.router.customRoutes || {}; }
|
|
26
|
-
get routes() { return this.config.routes; }
|
|
26
|
+
get routes() { return this.config.routes || []; }
|
|
27
27
|
get tabBarList() { var _a; return ((_a = this.config.tabBar) === null || _a === void 0 ? void 0 : _a.list) || []; }
|
|
28
28
|
get PullDownRefresh() { return this.config.PullDownRefresh; }
|
|
29
29
|
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; }
|
|
@@ -70,7 +70,7 @@ export default class PageHandler {
|
|
|
70
70
|
}
|
|
71
71
|
return false;
|
|
72
72
|
})) === null || _a === void 0 ? void 0 : _a[0]) || routePath;
|
|
73
|
-
return !!pagePath && this.tabBarList.some(t => t.pagePath === pagePath);
|
|
73
|
+
return !!pagePath && this.tabBarList.some(t => stripTrailing(t.pagePath) === pagePath);
|
|
74
74
|
}
|
|
75
75
|
isSamePage(page) {
|
|
76
76
|
const routePath = stripBasename(this.pathname, this.basename);
|
package/dist/router/spa.js
CHANGED
|
@@ -13,6 +13,7 @@ import { Action as LocationAction } from 'history';
|
|
|
13
13
|
import UniversalRouter from 'universal-router';
|
|
14
14
|
import { history, prependBasename } from '../history';
|
|
15
15
|
import { addLeadingSlash, routesAlias, stripBasename } from '../utils';
|
|
16
|
+
import { setTitle } from '../utils/navigate';
|
|
16
17
|
import { RouterConfig } from '.';
|
|
17
18
|
import PageHandler from './page';
|
|
18
19
|
import stacks from './stack';
|
|
@@ -33,8 +34,9 @@ export function createRouter(app, config, framework) {
|
|
|
33
34
|
const router = new UniversalRouter(routes, { baseUrl: basename || '' });
|
|
34
35
|
const launchParam = handler.getQuery(stacks.length);
|
|
35
36
|
(_a = app.onLaunch) === null || _a === void 0 ? void 0 : _a.call(app, launchParam);
|
|
37
|
+
app.onError && window.addEventListener('error', e => { var _a; return (_a = app.onError) === null || _a === void 0 ? void 0 : _a.call(app, e.message); });
|
|
36
38
|
const render = ({ location, action }) => __awaiter(this, void 0, void 0, function* () {
|
|
37
|
-
var _c, _d, _e, _f;
|
|
39
|
+
var _c, _d, _e, _f, _g;
|
|
38
40
|
handler.pathname = decodeURI(location.pathname);
|
|
39
41
|
let element, params;
|
|
40
42
|
try {
|
|
@@ -47,6 +49,10 @@ export function createRouter(app, config, framework) {
|
|
|
47
49
|
path: handler.pathname
|
|
48
50
|
});
|
|
49
51
|
}
|
|
52
|
+
else if (/Loading hot update .* failed./.test(error.message)) {
|
|
53
|
+
// NOTE: webpack5 与 prebundle 搭配使用时,开发环境下初次启动时偶发错误,由于 HMR 加载 chunk hash 错误,导致热更新失败
|
|
54
|
+
window.location.reload();
|
|
55
|
+
}
|
|
50
56
|
else {
|
|
51
57
|
throw new Error(error);
|
|
52
58
|
}
|
|
@@ -62,6 +68,7 @@ export function createRouter(app, config, framework) {
|
|
|
62
68
|
});
|
|
63
69
|
if (pageConfig) {
|
|
64
70
|
document.title = (_e = pageConfig.navigationBarTitleText) !== null && _e !== void 0 ? _e : document.title;
|
|
71
|
+
setTitle((_f = pageConfig.navigationBarTitleText) !== null && _f !== void 0 ? _f : document.title);
|
|
65
72
|
if (typeof pageConfig.enablePullDownRefresh === 'boolean') {
|
|
66
73
|
enablePullDownRefresh = pageConfig.enablePullDownRefresh;
|
|
67
74
|
}
|
|
@@ -73,12 +80,15 @@ export function createRouter(app, config, framework) {
|
|
|
73
80
|
// NOTE: 浏览器事件退后多次时,该事件只会被触发一次
|
|
74
81
|
const prevIndex = stacks.getPrevIndex(pathname);
|
|
75
82
|
const delta = stacks.getDelta(pathname);
|
|
76
|
-
|
|
77
|
-
if (
|
|
78
|
-
handler.
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
83
|
+
// NOTE: Safari 内核浏览器在非应用页面返回上一页时,会触发额外的 POP 事件,此处需避免当前页面被错误卸载
|
|
84
|
+
if (currentPage !== stacks.getItem(prevIndex)) {
|
|
85
|
+
handler.unload(currentPage, delta, prevIndex > -1);
|
|
86
|
+
if (prevIndex > -1) {
|
|
87
|
+
handler.show(stacks.getItem(prevIndex), pageConfig, prevIndex);
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
shouldLoad = true;
|
|
91
|
+
}
|
|
82
92
|
}
|
|
83
93
|
}
|
|
84
94
|
else {
|
|
@@ -103,7 +113,7 @@ export function createRouter(app, config, framework) {
|
|
|
103
113
|
shouldLoad = true;
|
|
104
114
|
}
|
|
105
115
|
if (shouldLoad || stacks.length < 1) {
|
|
106
|
-
const el = (
|
|
116
|
+
const el = (_g = element.default) !== null && _g !== void 0 ? _g : element;
|
|
107
117
|
const loadConfig = Object.assign({}, pageConfig);
|
|
108
118
|
const stacksIndex = stacks.length;
|
|
109
119
|
delete loadConfig['path'];
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export declare const addLeadingSlash: (url?: string) => string;
|
|
2
2
|
export declare const hasBasename: (path?: string, prefix?: string) => boolean;
|
|
3
3
|
export declare const stripBasename: (path?: string, prefix?: string) => string;
|
|
4
|
+
export declare const stripTrailing: (str?: string) => string;
|
|
5
|
+
export declare const stripSuffix: (path?: string, suffix?: string) => string;
|
|
4
6
|
declare class RoutesAlias {
|
|
5
7
|
conf: Array<string[]>;
|
|
6
8
|
set(customRoutes?: Record<string, string | string[]>): void;
|
|
@@ -1,6 +1,10 @@
|
|
|
1
|
+
// export const removeLeadingSlash = (str = '') => str.replace(/^\.?\//, '')
|
|
2
|
+
// export const removeTrailingSearch = (str = '') => str.replace(/\?[\s\S]*$/, '')
|
|
1
3
|
export const addLeadingSlash = (url = '') => (url.charAt(0) === '/' ? url : '/' + url);
|
|
2
4
|
export const hasBasename = (path = '', prefix = '') => new RegExp('^' + prefix + '(\\/|\\?|#|$)', 'i').test(path) || path === prefix;
|
|
3
|
-
export const stripBasename = (path = '', prefix = '') => hasBasename(path, prefix) ? path.
|
|
5
|
+
export const stripBasename = (path = '', prefix = '') => hasBasename(path, prefix) ? path.substring(prefix.length) : path;
|
|
6
|
+
export const stripTrailing = (str = '') => str.replace(/[?#][\s\S]*$/, '');
|
|
7
|
+
export const stripSuffix = (path = '', suffix = '') => path.includes(suffix) ? path.substring(0, path.length - suffix.length) : path;
|
|
4
8
|
class RoutesAlias {
|
|
5
9
|
constructor() {
|
|
6
10
|
this.conf = [];
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import MobileDetect from 'mobile-detect';
|
|
11
|
+
let md;
|
|
12
|
+
let preTitle = document.title;
|
|
13
|
+
let isLoadDdEntry = false;
|
|
14
|
+
export function getMobileDetect() {
|
|
15
|
+
if (!md) {
|
|
16
|
+
md = new MobileDetect(navigator.userAgent);
|
|
17
|
+
}
|
|
18
|
+
return md;
|
|
19
|
+
}
|
|
20
|
+
export function setTitle(title) {
|
|
21
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
22
|
+
if (preTitle === title)
|
|
23
|
+
return title;
|
|
24
|
+
document.title = title;
|
|
25
|
+
preTitle = title;
|
|
26
|
+
if (process.env.SUPPORT_DINGTALK_NAVIGATE !== 'disabled' && isDingTalk()) {
|
|
27
|
+
if (!isLoadDdEntry) {
|
|
28
|
+
isLoadDdEntry = true;
|
|
29
|
+
require('dingtalk-jsapi/platform');
|
|
30
|
+
}
|
|
31
|
+
const setDingTitle = require('dingtalk-jsapi/api/biz/navigation/setTitle').default;
|
|
32
|
+
setDingTitle({ title });
|
|
33
|
+
}
|
|
34
|
+
return title;
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
export function isWeixin() {
|
|
38
|
+
const md = getMobileDetect();
|
|
39
|
+
return md.match(/MicroMessenger/ig);
|
|
40
|
+
}
|
|
41
|
+
export function isDingTalk() {
|
|
42
|
+
const md = getMobileDetect();
|
|
43
|
+
return md.match(/DingTalk/ig);
|
|
44
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tarojs/router",
|
|
3
|
-
"version": "3.5.
|
|
3
|
+
"version": "3.5.1",
|
|
4
4
|
"description": "Taro-router",
|
|
5
5
|
"browser": "dist/index.esm.js",
|
|
6
6
|
"main:h5": "dist/index.js",
|
|
@@ -12,12 +12,6 @@
|
|
|
12
12
|
"types",
|
|
13
13
|
"index.js"
|
|
14
14
|
],
|
|
15
|
-
"scripts": {
|
|
16
|
-
"build": "rimraf ./dist && tsc && rollup -c",
|
|
17
|
-
"dev": "tsc -w",
|
|
18
|
-
"test": "jest",
|
|
19
|
-
"test:dev": "jest --watch"
|
|
20
|
-
},
|
|
21
15
|
"repository": {
|
|
22
16
|
"type": "git",
|
|
23
17
|
"url": "git+https://github.com/NervJS/taro.git"
|
|
@@ -28,10 +22,18 @@
|
|
|
28
22
|
"author": "O2Team",
|
|
29
23
|
"license": "MIT",
|
|
30
24
|
"dependencies": {
|
|
31
|
-
"@tarojs/runtime": "
|
|
32
|
-
"@tarojs/taro": "
|
|
25
|
+
"@tarojs/runtime": "3.5.1",
|
|
26
|
+
"@tarojs/taro": "3.5.1",
|
|
27
|
+
"dingtalk-jsapi": "~2.15.2",
|
|
33
28
|
"history": "^5.1.0",
|
|
29
|
+
"mobile-detect": "^1.4.2",
|
|
34
30
|
"query-string": "^7.1.1",
|
|
35
31
|
"universal-router": "^8.3.0"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "rimraf ./dist && tsc && rollup -c",
|
|
35
|
+
"dev": "tsc -w",
|
|
36
|
+
"test": "jest",
|
|
37
|
+
"test:dev": "jest --watch"
|
|
36
38
|
}
|
|
37
|
-
}
|
|
39
|
+
}
|
package/types/router.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { AppConfig, PageConfig } from '@tarojs/taro'
|
|
2
|
-
import { IH5RouterConfig } from '@tarojs/taro/types/compile'
|
|
1
|
+
import type { AppConfig, PageConfig } from '@tarojs/taro'
|
|
2
|
+
import type { IH5RouterConfig } from '@tarojs/taro/types/compile'
|
|
3
3
|
|
|
4
4
|
export interface Route extends PageConfig {
|
|
5
5
|
path?: string
|