@tarojs/router 3.5.0-alpha.1 → 3.5.0-alpha.4

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,4 +1,12 @@
1
- /* eslint-disable dot-notation */
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
+ };
2
10
  import { container, createPageConfig, Current, eventCenter, SERVICE_IDENTIFIER, stringify } from '@tarojs/runtime';
3
11
  import { Action as LocationAction } from 'history';
4
12
  import UniversalRouter from 'universal-router';
@@ -8,6 +16,7 @@ import stacks from './stack';
8
16
  import { addLeadingSlash, routesAlias, stripBasename } from '../utils';
9
17
  import { RouterConfig } from '.';
10
18
  export function createRouter(app, config, framework) {
19
+ var _a, _b, _c;
11
20
  RouterConfig.config = config;
12
21
  const handler = new PageHandler(config);
13
22
  const runtimeHooks = container.get(SERVICE_IDENTIFIER.Hooks);
@@ -17,19 +26,20 @@ export function createRouter(app, config, framework) {
17
26
  path: routesAlias.getAll(addLeadingSlash(route.path)),
18
27
  action: route.load
19
28
  }));
20
- const entryPagePath = config.entryPagePath || routes[0].path?.[0];
29
+ const entryPagePath = config.entryPagePath || ((_a = routes[0].path) === null || _a === void 0 ? void 0 : _a[0]) || '';
21
30
  const router = new UniversalRouter(routes, { baseUrl: basename || '' });
22
31
  const launchParam = handler.getQuery(stacks.length);
23
- app.onLaunch?.(launchParam);
24
- const render = async ({ location, action }) => {
32
+ (_b = app.onLaunch) === null || _b === void 0 ? void 0 : _b.call(app, launchParam);
33
+ const render = ({ location, action }) => __awaiter(this, void 0, void 0, function* () {
34
+ var _d, _e, _f, _g, _h;
25
35
  handler.pathname = decodeURI(location.pathname);
26
36
  let element;
27
37
  try {
28
- element = await router.resolve(handler.router.forcePath || handler.pathname);
38
+ element = yield router.resolve(handler.router.forcePath || handler.pathname);
29
39
  }
30
40
  catch (error) {
31
41
  if (error.status === 404) {
32
- app.onPageNotFound?.({
42
+ (_d = app.onPageNotFound) === null || _d === void 0 ? void 0 : _d.call(app, {
33
43
  path: handler.pathname
34
44
  });
35
45
  }
@@ -40,14 +50,14 @@ export function createRouter(app, config, framework) {
40
50
  if (!element)
41
51
  return;
42
52
  const pageConfig = handler.pageConfig;
43
- let enablePullDownRefresh = config?.window?.enablePullDownRefresh || false;
53
+ let enablePullDownRefresh = ((_e = config === null || config === void 0 ? void 0 : config.window) === null || _e === void 0 ? void 0 : _e.enablePullDownRefresh) || false;
44
54
  eventCenter.trigger('__taroRouterChange', {
45
55
  toLocation: {
46
56
  path: handler.pathname
47
57
  }
48
58
  });
49
59
  if (pageConfig) {
50
- document.title = pageConfig.navigationBarTitleText ?? document.title;
60
+ document.title = (_f = pageConfig.navigationBarTitleText) !== null && _f !== void 0 ? _f : document.title;
51
61
  if (typeof pageConfig.enablePullDownRefresh === 'boolean') {
52
62
  enablePullDownRefresh = pageConfig.enablePullDownRefresh;
53
63
  }
@@ -89,22 +99,20 @@ export function createRouter(app, config, framework) {
89
99
  shouldLoad = true;
90
100
  }
91
101
  if (shouldLoad || stacks.length < 1) {
92
- const el = element.default ?? element;
93
- const loadConfig = { ...pageConfig };
102
+ const el = (_g = element.default) !== null && _g !== void 0 ? _g : element;
103
+ const loadConfig = Object.assign({}, pageConfig);
94
104
  const stacksIndex = stacks.length;
95
105
  delete loadConfig['path'];
96
106
  delete loadConfig['load'];
97
- const page = createPageConfig(enablePullDownRefresh ? runtimeHooks.createPullDownComponent?.(el, location.pathname, framework, handler.PullDownRefresh) : el, pathname + stringify(handler.getQuery(stacksIndex)), {}, loadConfig);
107
+ const page = createPageConfig(enablePullDownRefresh ? (_h = runtimeHooks.createPullDownComponent) === null || _h === void 0 ? void 0 : _h.call(runtimeHooks, el, location.pathname, framework, handler.PullDownRefresh) : el, pathname + stringify(handler.getQuery(stacksIndex)), {}, loadConfig);
98
108
  return handler.load(page, pageConfig, stacksIndex);
99
109
  }
100
- };
101
- if (history.location.pathname === '/') {
102
- const stripped = stripBasename(history.location.pathname, handler.basename);
103
- if (stripped === '/' || stripped === '') {
104
- history.replace(prependBasename(entryPagePath + history.location.search));
105
- }
110
+ });
111
+ const stripped = stripBasename(history.location.pathname, handler.basename);
112
+ if (stripped === '/' || stripped === '') {
113
+ history.replace(prependBasename(entryPagePath + history.location.search));
106
114
  }
107
115
  render({ location: history.location, action: LocationAction.Push });
108
- app.onShow?.(launchParam);
116
+ (_c = app.onShow) === null || _c === void 0 ? void 0 : _c.call(app, launchParam);
109
117
  return history.listen(render);
110
118
  }
@@ -1,6 +1,8 @@
1
1
  class Stacks {
2
- stacks = [];
3
- backDelta = 0;
2
+ constructor() {
3
+ this.stacks = [];
4
+ this.backDelta = 0;
5
+ }
4
6
  set delta(delta) {
5
7
  if (delta > 0) {
6
8
  this.backDelta = delta;
@@ -29,7 +31,7 @@ class Stacks {
29
31
  }
30
32
  getLastIndex(pathname, stateWith = 1) {
31
33
  const list = [...this.stacks].reverse();
32
- return list.findIndex((page, i) => i >= stateWith && page.path?.replace(/\?.*/g, '') === pathname);
34
+ return list.findIndex((page, i) => { var _a; return i >= stateWith && ((_a = page.path) === null || _a === void 0 ? void 0 : _a.replace(/\?.*/g, '')) === pathname; });
33
35
  }
34
36
  getDelta(pathname) {
35
37
  if (this.backDelta >= 1) {
package/dist/tabbar.js CHANGED
@@ -24,6 +24,6 @@ export function initTabbar(config) {
24
24
  tabbar.conf.basename = routerConfig.basename;
25
25
  }
26
26
  const container = document.getElementById('container');
27
- container?.appendChild(tabbar);
27
+ container === null || container === void 0 ? void 0 : container.appendChild(tabbar);
28
28
  initTabBarApis(config);
29
29
  }
package/dist/utils.js CHANGED
@@ -2,7 +2,31 @@ export const addLeadingSlash = (url = '') => (url.charAt(0) === '/' ? url : '/'
2
2
  export const hasBasename = (path = '', prefix = '') => new RegExp('^' + prefix + '(\\/|\\?|#|$)', 'i').test(path) || path === prefix;
3
3
  export const stripBasename = (path = '', prefix = '') => hasBasename(path, prefix) ? path.substr(prefix.length) : path;
4
4
  class RoutesAlias {
5
- conf = [];
5
+ constructor() {
6
+ this.conf = [];
7
+ this.getConfig = (url = '') => {
8
+ const customRoute = this.conf.filter((arr) => {
9
+ return arr.includes(url);
10
+ });
11
+ return customRoute[0];
12
+ };
13
+ this.getOrigin = (url = '') => {
14
+ var _a;
15
+ return ((_a = this.getConfig(url)) === null || _a === void 0 ? void 0 : _a[0]) || url;
16
+ };
17
+ this.getAlias = (url = '') => {
18
+ var _a;
19
+ return ((_a = this.getConfig(url)) === null || _a === void 0 ? void 0 : _a[1]) || url;
20
+ };
21
+ this.getAll = (url = '') => {
22
+ return this.conf
23
+ .filter((arr) => arr.includes(url))
24
+ .reduceRight((p, a) => {
25
+ p.unshift(a[1]);
26
+ return p;
27
+ }, []);
28
+ };
29
+ }
6
30
  set(customRoutes = {}) {
7
31
  for (let key in customRoutes) {
8
32
  const path = customRoutes[key];
@@ -10,30 +34,10 @@ class RoutesAlias {
10
34
  if (typeof path === 'string') {
11
35
  this.conf.push([key, addLeadingSlash(path)]);
12
36
  }
13
- else if (path?.length > 0) {
37
+ else if ((path === null || path === void 0 ? void 0 : path.length) > 0) {
14
38
  this.conf.push(...path.map(p => [key, addLeadingSlash(p)]));
15
39
  }
16
40
  }
17
41
  }
18
- getConfig = (url = '') => {
19
- const customRoute = this.conf.filter((arr) => {
20
- return arr.includes(url);
21
- });
22
- return customRoute[0];
23
- };
24
- getOrigin = (url = '') => {
25
- return this.getConfig(url)?.[0] || url;
26
- };
27
- getAlias = (url = '') => {
28
- return this.getConfig(url)?.[1] || url;
29
- };
30
- getAll = (url = '') => {
31
- return this.conf
32
- .filter((arr) => arr.includes(url))
33
- .reduceRight((p, a) => {
34
- p.unshift(a[1]);
35
- return p;
36
- }, []);
37
- };
38
42
  }
39
43
  export const routesAlias = new RoutesAlias();
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@tarojs/router",
3
- "version": "3.5.0-alpha.1",
3
+ "version": "3.5.0-alpha.4",
4
4
  "description": "Taro-router",
5
- "main:h5": "dist/router.js",
6
- "main": "dist/index.cjs.js",
7
- "module": "dist/router.js",
5
+ "browser": "dist/index.esm.js",
6
+ "main:h5": "dist/index.js",
7
+ "main": "dist/index.js",
8
+ "module": "dist/index.cjs.js",
8
9
  "typings": "src/index.ts",
9
10
  "files": [
10
11
  "dist",
@@ -12,7 +13,7 @@
12
13
  "index.js"
13
14
  ],
14
15
  "scripts": {
15
- "build": "cross-env TARGET=umd rollup -c rollup.config.js && tsc",
16
+ "build": "rollup -c && tsc",
16
17
  "dev": "tsc -w",
17
18
  "test": "jest",
18
19
  "test:dev": "jest --watch"
@@ -27,11 +28,11 @@
27
28
  "author": "O2Team",
28
29
  "license": "MIT",
29
30
  "dependencies": {
30
- "@tarojs/runtime": "3.5.0-alpha.1",
31
- "@tarojs/taro": "3.5.0-alpha.1",
31
+ "@tarojs/runtime": "3.5.0-alpha.4",
32
+ "@tarojs/taro": "3.5.0-alpha.4",
32
33
  "history": "^5.1.0",
33
34
  "query-string": "^7.1.1",
34
35
  "universal-router": "^8.3.0"
35
36
  },
36
- "gitHead": "3bd136d26d77b847a1a18e028d5b0d048c89f288"
37
+ "gitHead": "b35cf347b6e511c93d509243867a4f67294d0c67"
37
38
  }
package/types/api.d.ts ADDED
@@ -0,0 +1,15 @@
1
+ declare module '@tarojs/router/types/api' {
2
+ interface Base {
3
+ success?: (...args: any[]) => void
4
+ fail?: (...args: any[]) => void
5
+ complete?: (...args: any[]) => void
6
+ }
7
+
8
+ interface Option extends Base {
9
+ url: string
10
+ }
11
+
12
+ interface NavigateBackOption extends Base {
13
+ delta: number
14
+ }
15
+ }
@@ -0,0 +1,8 @@
1
+ declare module '@tarojs/router/types/history' {
2
+ interface StateEvent extends Event {
3
+ action?: 'pushState' | 'replaceState' | 'popState'
4
+ state?: any
5
+ unused?: string
6
+ url?: string | URL | null
7
+ }
8
+ }
@@ -0,0 +1,32 @@
1
+ declare module '@tarojs/router/types/router' {
2
+ import { AppConfig, PageConfig } from '@tarojs/taro'
3
+ import { IH5RouterConfig } from '@tarojs/taro/types/compile'
4
+
5
+ interface Route extends PageConfig {
6
+ path?: string
7
+ load?: () => Promise<any>
8
+ }
9
+
10
+ interface Router {
11
+ mode: IH5RouterConfig['mode']
12
+ basename: string
13
+ customRoutes?: Record<string, string | string[]>
14
+ pathname: string
15
+ forcePath?: string
16
+ }
17
+
18
+ interface SpaRouterConfig extends AppConfig {
19
+ routes: Route[]
20
+ router: Router
21
+ // 下拉刷新组件
22
+ PullDownRefresh?: any
23
+ }
24
+
25
+ interface MpaRouterConfig extends AppConfig {
26
+ route: Route,
27
+ pageName: string
28
+ router: Router
29
+ // 下拉刷新组件
30
+ PullDownRefresh?: any
31
+ }
32
+ }
package/types/index.d.ts DELETED
@@ -1,288 +0,0 @@
1
- import { IH5RouterConfig } from '@tarojs/taro/types/compile'
2
- /**
3
- *
4
- * original code from: https://github.com/qiu8310/minapp/blob/master/packages/minapp-wx/typing/wx.d.ts
5
- * Lincenced under MIT license: https://github.com/qiu8310/minapp/issues/69
6
- * thanks for the great work by @qiu8310 👍👍👍
7
- *
8
- */
9
- declare namespace Router {
10
- namespace navigateTo {
11
- type Param = {
12
- /**
13
- * 需要跳转的应用内非 tabBar 的页面的路径 , 路径后可以带参数。参数与路径之间使用`?`分隔,参数键与参数值用`=`相连,不同参数用`&`分隔;如 'path?key=value&key2=value2'
14
- */
15
- url: string
16
- /**
17
- * 接口调用成功的回调函数
18
- */
19
- success?: ParamPropSuccess
20
- /**
21
- * 接口调用失败的回调函数
22
- */
23
- fail?: ParamPropFail
24
- /**
25
- * 接口调用结束的回调函数(调用成功、失败都会执行)
26
- */
27
- complete?: ParamPropComplete
28
- }
29
- /**
30
- * 接口调用成功的回调函数
31
- */
32
- type ParamPropSuccess = (res: any) => any
33
- /**
34
- * 接口调用失败的回调函数
35
- */
36
- type ParamPropFail = (err: any) => any
37
- /**
38
- * 接口调用结束的回调函数(调用成功、失败都会执行)
39
- */
40
- type ParamPropComplete = () => any
41
- }
42
- /**
43
- * 保留当前页面,跳转到应用内的某个页面,使用`Router.navigateBack`可以返回到原页面。
44
- *
45
- * **示例代码:**
46
- *
47
- * ```javascript
48
- * Router.navigateTo({
49
- * url: 'test?id=1'
50
- * })
51
- * ```
52
- *
53
- * **示例代码:**
54
- *
55
- * ```javascript
56
- * //test.js
57
- * Page({
58
- * onLoad: function(option){
59
- * console.log(option.query)
60
- * }
61
- * })
62
- * ```
63
- * @see https://developers.weixin.qq.com/miniprogram/dev/api/ui-navigate.html#wxnavigatetoobject
64
- */
65
- function navigateTo(OBJECT: navigateTo.Param): void
66
-
67
- namespace redirectTo {
68
- type Param = {
69
- /**
70
- * 需要跳转的应用内非 tabBar 的页面的路径,路径后可以带参数。参数与路径之间使用`?`分隔,参数键与参数值用`=`相连,不同参数用`&`分隔;如 'path?key=value&key2=value2'
71
- */
72
- url: string
73
- /**
74
- * 接口调用成功的回调函数
75
- */
76
- success?: ParamPropSuccess
77
- /**
78
- * 接口调用失败的回调函数
79
- */
80
- fail?: ParamPropFail
81
- /**
82
- * 接口调用结束的回调函数(调用成功、失败都会执行)
83
- */
84
- complete?: ParamPropComplete
85
- }
86
- /**
87
- * 接口调用成功的回调函数
88
- */
89
- type ParamPropSuccess = (res: any) => any
90
- /**
91
- * 接口调用失败的回调函数
92
- */
93
- type ParamPropFail = (err: any) => any
94
- /**
95
- * 接口调用结束的回调函数(调用成功、失败都会执行)
96
- */
97
- type ParamPropComplete = () => any
98
- }
99
- /**
100
- * 关闭当前页面,跳转到应用内的某个页面。
101
- *
102
- * **示例代码:**
103
- *
104
- * ```javascript
105
- * Router.redirectTo({
106
- * url: 'test?id=1'
107
- * })
108
- * ```
109
- * @see https://developers.weixin.qq.com/miniprogram/dev/api/ui-navigate.html#wxredirecttoobject
110
- */
111
- function redirectTo(OBJECT: redirectTo.Param): void
112
-
113
- namespace reLaunch {
114
- type Param = {
115
- /**
116
- * 需要跳转的应用内页面路径 , 路径后可以带参数。参数与路径之间使用`?`分隔,参数键与参数值用`=`相连,不同参数用`&`分隔;如 'path?key=value&key2=value2',如果跳转的页面路径是 tabBar 页面则不能带参数
117
- */
118
- url: string
119
- /**
120
- * 接口调用成功的回调函数
121
- */
122
- success?: ParamPropSuccess
123
- /**
124
- * 接口调用失败的回调函数
125
- */
126
- fail?: ParamPropFail
127
- /**
128
- * 接口调用结束的回调函数(调用成功、失败都会执行)
129
- */
130
- complete?: ParamPropComplete
131
- }
132
- /**
133
- * 接口调用成功的回调函数
134
- */
135
- type ParamPropSuccess = (res: any) => any
136
- /**
137
- * 接口调用失败的回调函数
138
- */
139
- type ParamPropFail = (err: any) => any
140
- /**
141
- * 接口调用结束的回调函数(调用成功、失败都会执行)
142
- */
143
- type ParamPropComplete = () => any
144
- }
145
- /**
146
- * @since 1.1.0
147
- *
148
- * 关闭所有页面,打开到应用内的某个页面。
149
- *
150
- * **示例代码:**
151
- *
152
- * ```javascript
153
- * Router.reLaunch({
154
- * url: 'test?id=1'
155
- * })
156
- * ```
157
- *
158
- * **示例代码:**
159
- *
160
- * ```javascript
161
- * //test.js
162
- * Page({
163
- * onLoad: function(option){
164
- * console.log(option.query)
165
- * }
166
- * })
167
- * ```
168
- * @see https://developers.weixin.qq.com/miniprogram/dev/api/ui-navigate.html#wxrelaunchobject
169
- */
170
- function reLaunch(OBJECT: reLaunch.Param): void
171
-
172
- namespace switchTab {
173
- type Param = {
174
- /**
175
- * 需要跳转的 tabBar 页面的路径(需在 app.json 的 [tabBar](https://developers.weixin.qq.com/miniprogram/dev/framework/config.html#tabbar) 字段定义的页面),路径后不能带参数
176
- */
177
- url: string
178
- /**
179
- * 接口调用成功的回调函数
180
- */
181
- success?: ParamPropSuccess
182
- /**
183
- * 接口调用失败的回调函数
184
- */
185
- fail?: ParamPropFail
186
- /**
187
- * 接口调用结束的回调函数(调用成功、失败都会执行)
188
- */
189
- complete?: ParamPropComplete
190
- }
191
- /**
192
- * 接口调用成功的回调函数
193
- */
194
- type ParamPropSuccess = (res: any) => any
195
- /**
196
- * 接口调用失败的回调函数
197
- */
198
- type ParamPropFail = (err: any) => any
199
- /**
200
- * 接口调用结束的回调函数(调用成功、失败都会执行)
201
- */
202
- type ParamPropComplete = () => any
203
- }
204
- /**
205
- * 跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面
206
- *
207
- * **示例代码:**
208
- *
209
- * ```json
210
- * {
211
- * "tabBar": {
212
- * "list": [{
213
- * "pagePath": "index",
214
- * "text": "首页"
215
- * },{
216
- * "pagePath": "other",
217
- * "text": "其他"
218
- * }]
219
- * }
220
- * }
221
- * ```
222
- *
223
- * **示例代码:**
224
- *
225
- * ```javascript
226
- * Router.switchTab({
227
- * url: '/index'
228
- * })
229
- * ```
230
- * @see https://developers.weixin.qq.com/miniprogram/dev/api/ui-navigate.html#wxswitchtabobject
231
- */
232
- function switchTab(OBJECT: switchTab.Param): void
233
-
234
- namespace navigateBack {
235
- type Param = {
236
- /**
237
- * 返回的页面数,如果 delta 大于现有页面数,则返回到首页。
238
- *
239
- * @default 1
240
- */
241
- delta?: number
242
- }
243
- }
244
- /**
245
- * 关闭当前页面,返回上一页面或多级页面。可通过 [`getCurrentPages()`](https://developers.weixin.qq.com/miniprogram/dev/framework/app-service/page.html#getCurrentPages()) 获取当前的页面栈,决定需要返回几层。
246
- *
247
- * **Tip:**
248
- *
249
- * 1. `tip`: Router.navigateTo 和 Router.redirectTo 不允许跳转到 tabbar 页面,只能用 Router.switchTab 跳转到 tabbar 页面
250
- *
251
- * **示例代码:**
252
- *
253
- * ```javascript
254
- * // 注意:调用 navigateTo 跳转时,调用该方法的页面会被加入堆栈,而 redirectTo 方法则不会。见下方示例代码
255
- *
256
- * // 此处是A页面
257
- * Router.navigateTo({
258
- * url: 'B?id=1'
259
- * })
260
- *
261
- * // 此处是B页面
262
- * Router.navigateTo({
263
- * url: 'C?id=1'
264
- * })
265
- *
266
- * // 在C页面内 navigateBack,将返回A页面
267
- * Router.navigateBack({
268
- * delta: 2
269
- * })
270
- * ```
271
- * @see https://developers.weixin.qq.com/miniprogram/dev/api/ui-navigate.html#wxnavigatebackobject
272
- */
273
- function navigateBack(OBJECT: navigateBack.Param): void
274
- }
275
-
276
- declare const createHistory: (props: {
277
- basename?: string;
278
- mode: IH5RouterConfig['mode'],
279
- firstPagePath: string;
280
- customRoutes: {
281
- [key: string]: string | string[]
282
- };
283
- }) => any;
284
-
285
- export {
286
- Router,
287
- createHistory
288
- }