@xfe-repo/mini-router 0.0.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/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # `@xfe-repo/web-router`
2
+
3
+ web 路由库
@@ -0,0 +1,33 @@
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
+
26
+ declare const asyncNavigateTo: (path: string, replace?: boolean) => Promise<TaroGeneral.CallbackResult | undefined>;
27
+ declare const navigateTo: debounce<(path: string, replace?: boolean) => Promise<TaroGeneral.CallbackResult | undefined>>;
28
+ declare const navigateBack: (option?: Taro.navigateBack.Option & {
29
+ delay?: boolean;
30
+ }) => Promise<void>;
31
+ declare const formatPath: (path: string) => string;
32
+
33
+ export { asyncNavigateTo, formatPath, getAllPages, getAppConfig, getTabBarPages, navigateBack, navigateTo };
package/dist/index.js ADDED
@@ -0,0 +1,384 @@
1
+ "use strict";
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
+ var __create = Object.create;
127
+ var __defProp = Object.defineProperty;
128
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
129
+ var __getOwnPropNames = Object.getOwnPropertyNames;
130
+ var __getProtoOf = Object.getPrototypeOf;
131
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
132
+ var __export = function(target, all) {
133
+ for(var name in all)__defProp(target, name, {
134
+ get: all[name],
135
+ enumerable: true
136
+ });
137
+ };
138
+ var __copyProps = function(to, from, except, desc) {
139
+ if (from && typeof from === "object" || typeof from === "function") {
140
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
141
+ try {
142
+ var _loop = function() {
143
+ var key = _step.value;
144
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
145
+ get: function() {
146
+ return from[key];
147
+ },
148
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
149
+ });
150
+ };
151
+ for(var _iterator = __getOwnPropNames(from)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true)_loop();
152
+ } catch (err) {
153
+ _didIteratorError = true;
154
+ _iteratorError = err;
155
+ } finally{
156
+ try {
157
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
158
+ _iterator.return();
159
+ }
160
+ } finally{
161
+ if (_didIteratorError) {
162
+ throw _iteratorError;
163
+ }
164
+ }
165
+ }
166
+ }
167
+ return to;
168
+ };
169
+ var __toESM = function(mod, isNodeMode, target) {
170
+ return target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(// If the importer is in node compatibility mode or this is not an ESM
171
+ // file that has been converted to a CommonJS file using a Babel-
172
+ // compatible transform (i.e. "__esModule" has not been set), then set
173
+ // "default" to the CommonJS "module.exports" for node compatibility.
174
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
175
+ value: mod,
176
+ enumerable: true
177
+ }) : target, mod);
178
+ };
179
+ var __toCommonJS = function(mod) {
180
+ return __copyProps(__defProp({}, "__esModule", {
181
+ value: true
182
+ }), mod);
183
+ };
184
+ // src/index.ts
185
+ var src_exports = {};
186
+ __export(src_exports, {
187
+ asyncNavigateTo: function() {
188
+ return asyncNavigateTo;
189
+ },
190
+ formatPath: function() {
191
+ return formatPath;
192
+ },
193
+ getAllPages: function() {
194
+ return getAllPages;
195
+ },
196
+ getAppConfig: function() {
197
+ return getAppConfig;
198
+ },
199
+ getTabBarPages: function() {
200
+ return getTabBarPages;
201
+ },
202
+ navigateBack: function() {
203
+ return navigateBack;
204
+ },
205
+ navigateTo: function() {
206
+ return navigateTo;
207
+ }
208
+ });
209
+ module.exports = __toCommonJS(src_exports);
210
+ var import_taro2 = __toESM(require("@tarojs/taro"));
211
+ var import_tools = require("@xfe-repo/web-utils/tools");
212
+ var import_env = require("@xfe-repo/mini-utils/env");
213
+ var import_appJSBridge = require("@xfe-repo/mini-utils/appJSBridge");
214
+ // src/routes.ts
215
+ var import_taro = __toESM(require("@tarojs/taro"));
216
+ var getAppConfig = function() {
217
+ var _import_taro_default_getCurrentInstance_app;
218
+ var appConfig = ((_import_taro_default_getCurrentInstance_app = import_taro.default.getCurrentInstance().app) === null || _import_taro_default_getCurrentInstance_app === void 0 ? void 0 : _import_taro_default_getCurrentInstance_app.config) || {};
219
+ return appConfig;
220
+ };
221
+ var getAllPages = function() {
222
+ 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;
223
+ var allPages = pages;
224
+ subpackages.forEach(function(param) {
225
+ var root = param.root, subPages = param.pages;
226
+ allPages = allPages.concat(subPages.map(function(subPath) {
227
+ return "".concat(root, "/").concat(subPath);
228
+ }));
229
+ });
230
+ allPages = Array.from(new Set(allPages));
231
+ return allPages;
232
+ };
233
+ var getTabBarPages = function() {
234
+ var _getAppConfig_tabBar;
235
+ return ((_getAppConfig_tabBar = getAppConfig().tabBar) === null || _getAppConfig_tabBar === void 0 ? void 0 : _getAppConfig_tabBar.list.map(function(param) {
236
+ var pagePath = param.pagePath;
237
+ return pagePath;
238
+ })) || [];
239
+ };
240
+ // src/index.ts
241
+ var asyncNavigateTo = function() {
242
+ var _ref = _async_to_generator(function(path) {
243
+ var replace, result, hasAppNativePage, targetPage, tabBarPages;
244
+ var _arguments = arguments;
245
+ return _ts_generator(this, function(_state) {
246
+ switch(_state.label){
247
+ case 0:
248
+ replace = _arguments.length > 1 && _arguments[1] !== void 0 ? _arguments[1] : false;
249
+ if (!import_env.isApp) return [
250
+ 3,
251
+ 2
252
+ ];
253
+ return [
254
+ 4,
255
+ (0, import_appJSBridge.appH5RouterChange)({
256
+ path: path
257
+ })
258
+ ];
259
+ case 1:
260
+ result = _state.sent();
261
+ hasAppNativePage = Number(result) === 1;
262
+ if (hasAppNativePage) return [
263
+ 2
264
+ ];
265
+ _state.label = 2;
266
+ case 2:
267
+ targetPage = path.split("?")[0] || "";
268
+ tabBarPages = getTabBarPages().map(function(pagePath) {
269
+ return formatPath(pagePath);
270
+ });
271
+ if (tabBarPages.includes(targetPage)) {
272
+ if (replace) {
273
+ return [
274
+ 2,
275
+ import_taro2.default.reLaunch({
276
+ url: path
277
+ })
278
+ ];
279
+ } else {
280
+ return [
281
+ 2,
282
+ import_taro2.default.switchTab({
283
+ url: path
284
+ })
285
+ ];
286
+ }
287
+ }
288
+ if (replace) {
289
+ return [
290
+ 2,
291
+ import_taro2.default.redirectTo({
292
+ url: path
293
+ })
294
+ ];
295
+ } else {
296
+ return [
297
+ 2,
298
+ import_taro2.default.navigateTo({
299
+ url: path
300
+ })
301
+ ];
302
+ }
303
+ return [
304
+ 2
305
+ ];
306
+ }
307
+ });
308
+ });
309
+ return function asyncNavigateTo(path) {
310
+ return _ref.apply(this, arguments);
311
+ };
312
+ }();
313
+ var navigateTo = (0, import_tools.debounce)(300, asyncNavigateTo, {
314
+ atBegin: true
315
+ });
316
+ var navigateBack = function() {
317
+ var _ref = _async_to_generator(function(option) {
318
+ var e, homeTabPagePath;
319
+ return _ts_generator(this, function(_state) {
320
+ switch(_state.label){
321
+ case 0:
322
+ _state.trys.push([
323
+ 0,
324
+ 2,
325
+ ,
326
+ 3
327
+ ]);
328
+ return [
329
+ 4,
330
+ import_taro2.default.navigateBack(option)
331
+ ];
332
+ case 1:
333
+ _state.sent();
334
+ return [
335
+ 3,
336
+ 3
337
+ ];
338
+ case 2:
339
+ e = _state.sent();
340
+ homeTabPagePath = getTabBarPages()[0];
341
+ if (homeTabPagePath) import_taro2.default.switchTab({
342
+ url: homeTabPagePath
343
+ });
344
+ console.error("navigateBack error:", e);
345
+ return [
346
+ 3,
347
+ 3
348
+ ];
349
+ case 3:
350
+ if (!(option === null || option === void 0 ? void 0 : option.delay)) return [
351
+ 3,
352
+ 5
353
+ ];
354
+ return [
355
+ 4,
356
+ (0, import_tools.delayHandle)(0.35)
357
+ ];
358
+ case 4:
359
+ _state.sent();
360
+ _state.label = 5;
361
+ case 5:
362
+ return [
363
+ 2
364
+ ];
365
+ }
366
+ });
367
+ });
368
+ return function navigateBack(option) {
369
+ return _ref.apply(this, arguments);
370
+ };
371
+ }();
372
+ var formatPath = function(path) {
373
+ return path.startsWith("/") ? path : "/".concat(path);
374
+ };
375
+ // Annotate the CommonJS export names for ESM import in node:
376
+ 0 && (module.exports = {
377
+ asyncNavigateTo: asyncNavigateTo,
378
+ formatPath: formatPath,
379
+ getAllPages: getAllPages,
380
+ getAppConfig: getAppConfig,
381
+ getTabBarPages: getTabBarPages,
382
+ navigateBack: navigateBack,
383
+ navigateTo: navigateTo
384
+ });
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@xfe-repo/mini-router",
3
+ "version": "0.0.1",
4
+ "sideEffects": false,
5
+ "module": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "dependencies": {
11
+ "@tarojs/taro": "4.0.8",
12
+ "query-string": "^7.1.3",
13
+ "@xfe-repo/mini-utils": "0.0.1",
14
+ "@xfe-repo/web-utils": "1.2.1"
15
+ },
16
+ "devDependencies": {
17
+ "@types/node": "^20.16.5",
18
+ "@xfe-repo/eslint-config": "0.0.5",
19
+ "@xfe-repo/typescript-config": "0.0.6"
20
+ },
21
+ "scripts": {
22
+ "build": "tsup",
23
+ "dev": "tsup --watch",
24
+ "lint": "eslint \"src/**/*.ts*\"",
25
+ "clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
26
+ }
27
+ }