@xfe-repo/mini-router 0.0.3 → 0.0.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.
@@ -0,0 +1,34 @@
1
+ import Taro from '@tarojs/taro';
2
+ import { debounce } from '@xfe-repo/web-utils/tools';
3
+
4
+ type TabBarConfig = {
5
+ list: TabBarItem[];
6
+ };
7
+ type SubpackagesItem = {
8
+ root: string;
9
+ pages: string[];
10
+ };
11
+ type TabBarItem = {
12
+ iconPath: string;
13
+ selectedIconPath: string;
14
+ pagePath: string;
15
+ text: string;
16
+ };
17
+ type Config = {
18
+ pages?: string[];
19
+ tabBar?: TabBarConfig;
20
+ subpackages?: SubpackagesItem[];
21
+ };
22
+ declare const getAppConfig: () => Config;
23
+ declare const getAllPages: () => string[];
24
+ declare const getTabBarPages: () => string[];
25
+ declare const getHomePage: () => string;
26
+
27
+ declare const asyncNavigateTo: (path: string, replace?: boolean) => Promise<TaroGeneral.CallbackResult | undefined>;
28
+ declare const navigateTo: debounce<(path: string, replace?: boolean) => Promise<TaroGeneral.CallbackResult | undefined>>;
29
+ declare const navigateBack: (option?: Taro.navigateBack.Option & {
30
+ delay?: boolean;
31
+ }) => Promise<void>;
32
+ declare const formatPath: (path: string) => string;
33
+
34
+ export { asyncNavigateTo, formatPath, getAllPages, getAppConfig, getHomePage, getTabBarPages, navigateBack, navigateTo };
package/dist/index.mjs ADDED
@@ -0,0 +1,294 @@
1
+ // src/index.ts
2
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
3
+ try {
4
+ var info = gen[key](arg);
5
+ var value = info.value;
6
+ } catch (error) {
7
+ reject(error);
8
+ return;
9
+ }
10
+ if (info.done) {
11
+ resolve(value);
12
+ } else {
13
+ Promise.resolve(value).then(_next, _throw);
14
+ }
15
+ }
16
+ function _async_to_generator(fn) {
17
+ return function() {
18
+ var self = this, args = arguments;
19
+ return new Promise(function(resolve, reject) {
20
+ var gen = fn.apply(self, args);
21
+ function _next(value) {
22
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
23
+ }
24
+ function _throw(err) {
25
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
26
+ }
27
+ _next(undefined);
28
+ });
29
+ };
30
+ }
31
+ function _ts_generator(thisArg, body) {
32
+ var f, y, t, g, _ = {
33
+ label: 0,
34
+ sent: function() {
35
+ if (t[0] & 1) throw t[1];
36
+ return t[1];
37
+ },
38
+ trys: [],
39
+ ops: []
40
+ };
41
+ return g = {
42
+ next: verb(0),
43
+ "throw": verb(1),
44
+ "return": verb(2)
45
+ }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
46
+ return this;
47
+ }), g;
48
+ function verb(n) {
49
+ return function(v) {
50
+ return step([
51
+ n,
52
+ v
53
+ ]);
54
+ };
55
+ }
56
+ function step(op) {
57
+ if (f) throw new TypeError("Generator is already executing.");
58
+ while(_)try {
59
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
60
+ if (y = 0, t) op = [
61
+ op[0] & 2,
62
+ t.value
63
+ ];
64
+ switch(op[0]){
65
+ case 0:
66
+ case 1:
67
+ t = op;
68
+ break;
69
+ case 4:
70
+ _.label++;
71
+ return {
72
+ value: op[1],
73
+ done: false
74
+ };
75
+ case 5:
76
+ _.label++;
77
+ y = op[1];
78
+ op = [
79
+ 0
80
+ ];
81
+ continue;
82
+ case 7:
83
+ op = _.ops.pop();
84
+ _.trys.pop();
85
+ continue;
86
+ default:
87
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
88
+ _ = 0;
89
+ continue;
90
+ }
91
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
92
+ _.label = op[1];
93
+ break;
94
+ }
95
+ if (op[0] === 6 && _.label < t[1]) {
96
+ _.label = t[1];
97
+ t = op;
98
+ break;
99
+ }
100
+ if (t && _.label < t[2]) {
101
+ _.label = t[2];
102
+ _.ops.push(op);
103
+ break;
104
+ }
105
+ if (t[2]) _.ops.pop();
106
+ _.trys.pop();
107
+ continue;
108
+ }
109
+ op = body.call(thisArg, _);
110
+ } catch (e) {
111
+ op = [
112
+ 6,
113
+ e
114
+ ];
115
+ y = 0;
116
+ } finally{
117
+ f = t = 0;
118
+ }
119
+ if (op[0] & 5) throw op[1];
120
+ return {
121
+ value: op[0] ? op[1] : void 0,
122
+ done: true
123
+ };
124
+ }
125
+ }
126
+ import Taro2 from "@tarojs/taro";
127
+ import { delayHandle, debounce } from "@xfe-repo/web-utils/tools";
128
+ import { isApp } from "@xfe-repo/mini-utils/env";
129
+ import { appH5RouterChange } from "@xfe-repo/mini-utils/appJSBridge";
130
+ // src/routes.ts
131
+ import Taro from "@tarojs/taro";
132
+ var getAppConfig = function() {
133
+ var _Taro_getCurrentInstance_app;
134
+ var appConfig = ((_Taro_getCurrentInstance_app = Taro.getCurrentInstance().app) === null || _Taro_getCurrentInstance_app === void 0 ? void 0 : _Taro_getCurrentInstance_app.config) || {};
135
+ return appConfig;
136
+ };
137
+ var getAllPages = function() {
138
+ var _getAppConfig = getAppConfig(), _getAppConfig_pages = _getAppConfig.pages, pages = _getAppConfig_pages === void 0 ? [] : _getAppConfig_pages, _getAppConfig_subpackages = _getAppConfig.subpackages, subpackages = _getAppConfig_subpackages === void 0 ? [] : _getAppConfig_subpackages;
139
+ var allPages = pages;
140
+ subpackages.forEach(function(param) {
141
+ var root = param.root, subPages = param.pages;
142
+ allPages = allPages.concat(subPages.map(function(subPath) {
143
+ return "".concat(root, "/").concat(subPath);
144
+ }));
145
+ });
146
+ allPages = Array.from(new Set(allPages));
147
+ return allPages;
148
+ };
149
+ var getTabBarPages = function() {
150
+ var _getAppConfig_tabBar;
151
+ return ((_getAppConfig_tabBar = getAppConfig().tabBar) === null || _getAppConfig_tabBar === void 0 ? void 0 : _getAppConfig_tabBar.list.map(function(param) {
152
+ var pagePath = param.pagePath;
153
+ return formatPath(pagePath);
154
+ })) || [];
155
+ };
156
+ var getHomePage = function() {
157
+ return getTabBarPages()[0] || "/pages/home/index";
158
+ };
159
+ // src/index.ts
160
+ var asyncNavigateTo = function() {
161
+ var _ref = _async_to_generator(function(path) {
162
+ var replace, result, hasAppNativePage, targetPage, tabBarPages;
163
+ var _arguments = arguments;
164
+ return _ts_generator(this, function(_state) {
165
+ switch(_state.label){
166
+ case 0:
167
+ replace = _arguments.length > 1 && _arguments[1] !== void 0 ? _arguments[1] : false;
168
+ if (!isApp) return [
169
+ 3,
170
+ 2
171
+ ];
172
+ return [
173
+ 4,
174
+ appH5RouterChange({
175
+ path: path
176
+ })
177
+ ];
178
+ case 1:
179
+ result = _state.sent();
180
+ hasAppNativePage = Number(result) === 1;
181
+ if (hasAppNativePage) return [
182
+ 2
183
+ ];
184
+ _state.label = 2;
185
+ case 2:
186
+ targetPage = path.split("?")[0] || "";
187
+ tabBarPages = getTabBarPages().map(function(pagePath) {
188
+ return formatPath(pagePath);
189
+ });
190
+ if (tabBarPages.includes(targetPage)) {
191
+ if (replace) {
192
+ return [
193
+ 2,
194
+ Taro2.reLaunch({
195
+ url: path
196
+ })
197
+ ];
198
+ } else {
199
+ return [
200
+ 2,
201
+ Taro2.switchTab({
202
+ url: path
203
+ })
204
+ ];
205
+ }
206
+ }
207
+ if (replace) {
208
+ return [
209
+ 2,
210
+ Taro2.redirectTo({
211
+ url: path
212
+ })
213
+ ];
214
+ } else {
215
+ return [
216
+ 2,
217
+ Taro2.navigateTo({
218
+ url: path
219
+ })
220
+ ];
221
+ }
222
+ return [
223
+ 2
224
+ ];
225
+ }
226
+ });
227
+ });
228
+ return function asyncNavigateTo(path) {
229
+ return _ref.apply(this, arguments);
230
+ };
231
+ }();
232
+ var navigateTo = debounce(300, asyncNavigateTo, {
233
+ atBegin: true
234
+ });
235
+ var navigateBack = function() {
236
+ var _ref = _async_to_generator(function(option) {
237
+ var e, homeTabPagePath;
238
+ return _ts_generator(this, function(_state) {
239
+ switch(_state.label){
240
+ case 0:
241
+ _state.trys.push([
242
+ 0,
243
+ 2,
244
+ ,
245
+ 3
246
+ ]);
247
+ return [
248
+ 4,
249
+ Taro2.navigateBack(option)
250
+ ];
251
+ case 1:
252
+ _state.sent();
253
+ return [
254
+ 3,
255
+ 3
256
+ ];
257
+ case 2:
258
+ e = _state.sent();
259
+ homeTabPagePath = getHomePage();
260
+ if (homeTabPagePath) Taro2.switchTab({
261
+ url: homeTabPagePath
262
+ });
263
+ console.error("navigateBack error:", e);
264
+ return [
265
+ 3,
266
+ 3
267
+ ];
268
+ case 3:
269
+ if (!(option === null || option === void 0 ? void 0 : option.delay)) return [
270
+ 3,
271
+ 5
272
+ ];
273
+ return [
274
+ 4,
275
+ delayHandle(0.35)
276
+ ];
277
+ case 4:
278
+ _state.sent();
279
+ _state.label = 5;
280
+ case 5:
281
+ return [
282
+ 2
283
+ ];
284
+ }
285
+ });
286
+ });
287
+ return function navigateBack(option) {
288
+ return _ref.apply(this, arguments);
289
+ };
290
+ }();
291
+ var formatPath = function(path) {
292
+ return path.startsWith("/") ? path : "/".concat(path);
293
+ };
294
+ export { asyncNavigateTo, formatPath, getAllPages, getAppConfig, getHomePage, getTabBarPages, navigateBack, navigateTo };
package/package.json CHANGED
@@ -1,22 +1,34 @@
1
1
  {
2
2
  "name": "@xfe-repo/mini-router",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "sideEffects": false,
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "import": {
10
+ "types": "./dist/index.d.mts",
11
+ "default": "./dist/index.mjs"
12
+ },
13
+ "require": {
14
+ "types": "./dist/index.d.ts",
15
+ "default": "./dist/index.js"
16
+ }
17
+ }
18
+ },
7
19
  "files": [
8
20
  "dist"
9
21
  ],
10
22
  "dependencies": {
11
23
  "@tarojs/taro": "4.0.8",
12
24
  "query-string": "^7.1.3",
13
- "@xfe-repo/mini-utils": "0.0.6",
25
+ "@xfe-repo/mini-utils": "0.0.7",
14
26
  "@xfe-repo/web-utils": "1.3.5"
15
27
  },
16
28
  "devDependencies": {
17
29
  "@types/node": "^20.16.5",
18
- "@xfe-repo/eslint-config": "0.0.5",
19
- "@xfe-repo/typescript-config": "0.0.6"
30
+ "@xfe-repo/typescript-config": "0.0.6",
31
+ "@xfe-repo/eslint-config": "0.0.5"
20
32
  },
21
33
  "publishConfig": {
22
34
  "registry": "https://registry.npmjs.org/"