mp-weixin-back 0.0.5 → 0.0.6

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 CHANGED
@@ -3,6 +3,7 @@
3
3
  const path = require('path');
4
4
  const fs = require('fs');
5
5
  const JSON5 = require('json5');
6
+ const chalk = require('chalk');
6
7
  const generate = require('@babel/generator');
7
8
  const compilerSfc = require('@vue/compiler-sfc');
8
9
  const astKit = require('ast-kit');
@@ -12,6 +13,7 @@ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'defau
12
13
  const path__default = /*#__PURE__*/_interopDefaultCompat(path);
13
14
  const fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
14
15
  const JSON5__default = /*#__PURE__*/_interopDefaultCompat(JSON5);
16
+ const chalk__default = /*#__PURE__*/_interopDefaultCompat(chalk);
15
17
  const generate__default = /*#__PURE__*/_interopDefaultCompat(generate);
16
18
 
17
19
  const virtualFileId = "mp-weixin-back-helper";
@@ -80,6 +82,7 @@ async function transformVueFile(code, id) {
80
82
  }
81
83
  if (!hasPageBack)
82
84
  return;
85
+ this.log.devLog(`\u9875\u9762${this.getPageById(id)}\u6CE8\u5165mp-weixin-back`);
83
86
  if (!pageBackConfig.preventDefault) {
84
87
  callbackCode += `uni.navigateBack({ delta: 1 });`;
85
88
  }
@@ -145,6 +148,19 @@ class pageContext {
145
148
  constructor(config) {
146
149
  __publicField(this, "config");
147
150
  __publicField(this, "pages", []);
151
+ __publicField(this, "log", {
152
+ info: (text) => {
153
+ console.log(chalk__default.white(text));
154
+ },
155
+ error: (text) => {
156
+ console.log(chalk__default.red(text));
157
+ },
158
+ devLog: (text) => {
159
+ if (this.config.mode === "development" && this.config.debug) {
160
+ console.log(chalk__default.green(text));
161
+ }
162
+ }
163
+ });
148
164
  this.config = config;
149
165
  }
150
166
  getPagesJsonPath() {
@@ -168,16 +184,19 @@ class pageContext {
168
184
  this.pages.push(...mainPages);
169
185
  }
170
186
  if (subpackages) {
171
- const root = subpackages.root;
172
- const subPages = subpackages.pages.reduce((acc, current) => {
173
- acc.push(`${root}/${current.path}`.replace("//", "/"));
174
- return acc;
175
- }, []);
176
- this.pages.push(...subPages);
187
+ for (let i = 0; i < subpackages.length; i++) {
188
+ const element = subpackages[i];
189
+ const root = element.root;
190
+ const subPages = element.pages.reduce((acc, current) => {
191
+ acc.push(`${root}/${current.path}`.replace("//", "/"));
192
+ return acc;
193
+ }, []);
194
+ this.pages.push(...subPages);
195
+ }
177
196
  }
178
197
  } catch (error) {
179
- console.error("\u8BFB\u53D6pages.json\u6587\u4EF6\u5931\u8D25");
180
- console.error(error);
198
+ this.log.error("\u8BFB\u53D6pages.json\u6587\u4EF6\u5931\u8D25");
199
+ this.log.devLog(String(error));
181
200
  }
182
201
  }
183
202
  // 获取指定id的page
@@ -195,7 +214,8 @@ function MpBackPlugin(userOptions = {}) {
195
214
  let context;
196
215
  const defaultOptions = {
197
216
  preventDefault: false,
198
- frequency: 1
217
+ frequency: 1,
218
+ debug: false
199
219
  };
200
220
  const options = { ...defaultOptions, ...userOptions };
201
221
  return {
package/dist/index.d.cts CHANGED
@@ -10,6 +10,10 @@ type Config = {
10
10
  * 阻止次数,默认是 `1`
11
11
  */
12
12
  frequency: number;
13
+ /**
14
+ * 调试模式
15
+ */
16
+ debug: boolean;
13
17
  /**
14
18
  * 页面回退时触发
15
19
  */
package/dist/index.d.mts CHANGED
@@ -10,6 +10,10 @@ type Config = {
10
10
  * 阻止次数,默认是 `1`
11
11
  */
12
12
  frequency: number;
13
+ /**
14
+ * 调试模式
15
+ */
16
+ debug: boolean;
13
17
  /**
14
18
  * 页面回退时触发
15
19
  */
package/dist/index.d.ts CHANGED
@@ -10,6 +10,10 @@ type Config = {
10
10
  * 阻止次数,默认是 `1`
11
11
  */
12
12
  frequency: number;
13
+ /**
14
+ * 调试模式
15
+ */
16
+ debug: boolean;
13
17
  /**
14
18
  * 页面回退时触发
15
19
  */
package/dist/index.mjs CHANGED
@@ -1,6 +1,7 @@
1
1
  import path from 'path';
2
2
  import fs from 'fs';
3
3
  import JSON5 from 'json5';
4
+ import chalk from 'chalk';
4
5
  import generate from '@babel/generator';
5
6
  import { parse } from '@vue/compiler-sfc';
6
7
  import { babelParse, walkAST } from 'ast-kit';
@@ -71,6 +72,7 @@ async function transformVueFile(code, id) {
71
72
  }
72
73
  if (!hasPageBack)
73
74
  return;
75
+ this.log.devLog(`\u9875\u9762${this.getPageById(id)}\u6CE8\u5165mp-weixin-back`);
74
76
  if (!pageBackConfig.preventDefault) {
75
77
  callbackCode += `uni.navigateBack({ delta: 1 });`;
76
78
  }
@@ -136,6 +138,19 @@ class pageContext {
136
138
  constructor(config) {
137
139
  __publicField(this, "config");
138
140
  __publicField(this, "pages", []);
141
+ __publicField(this, "log", {
142
+ info: (text) => {
143
+ console.log(chalk.white(text));
144
+ },
145
+ error: (text) => {
146
+ console.log(chalk.red(text));
147
+ },
148
+ devLog: (text) => {
149
+ if (this.config.mode === "development" && this.config.debug) {
150
+ console.log(chalk.green(text));
151
+ }
152
+ }
153
+ });
139
154
  this.config = config;
140
155
  }
141
156
  getPagesJsonPath() {
@@ -159,16 +174,19 @@ class pageContext {
159
174
  this.pages.push(...mainPages);
160
175
  }
161
176
  if (subpackages) {
162
- const root = subpackages.root;
163
- const subPages = subpackages.pages.reduce((acc, current) => {
164
- acc.push(`${root}/${current.path}`.replace("//", "/"));
165
- return acc;
166
- }, []);
167
- this.pages.push(...subPages);
177
+ for (let i = 0; i < subpackages.length; i++) {
178
+ const element = subpackages[i];
179
+ const root = element.root;
180
+ const subPages = element.pages.reduce((acc, current) => {
181
+ acc.push(`${root}/${current.path}`.replace("//", "/"));
182
+ return acc;
183
+ }, []);
184
+ this.pages.push(...subPages);
185
+ }
168
186
  }
169
187
  } catch (error) {
170
- console.error("\u8BFB\u53D6pages.json\u6587\u4EF6\u5931\u8D25");
171
- console.error(error);
188
+ this.log.error("\u8BFB\u53D6pages.json\u6587\u4EF6\u5931\u8D25");
189
+ this.log.devLog(String(error));
172
190
  }
173
191
  }
174
192
  // 获取指定id的page
@@ -186,7 +204,8 @@ function MpBackPlugin(userOptions = {}) {
186
204
  let context;
187
205
  const defaultOptions = {
188
206
  preventDefault: false,
189
- frequency: 1
207
+ frequency: 1,
208
+ debug: false
190
209
  };
191
210
  const options = { ...defaultOptions, ...userOptions };
192
211
  return {
package/package.json CHANGED
@@ -1,9 +1,11 @@
1
1
  {
2
2
  "name": "mp-weixin-back",
3
3
  "type": "module",
4
- "version": "0.0.5",
4
+ "version": "0.0.6",
5
5
  "description": "监听微信小程序的手势返回和页面默认导航栏的返回",
6
- "main": "./dist/index.ts",
6
+ "main": "dist/index.cjs",
7
+ "module": "dist/index.mjs",
8
+ "types": "./dist/index.d.ts",
7
9
  "scripts": {
8
10
  "build": "unbuild",
9
11
  "test": "vitest"
@@ -44,6 +46,7 @@
44
46
  "@vitejs/plugin-vue": "^5.2.0",
45
47
  "@vue/test-utils": "^2.4.6",
46
48
  "ast-kit": "^1.3.1",
49
+ "chalk": "^5.3.0",
47
50
  "happy-dom": "^15.11.6",
48
51
  "json5": "^2.2.3",
49
52
  "typescript": "^5.7.2",
package/src/context.ts CHANGED
@@ -1,12 +1,26 @@
1
1
  import path from 'path'
2
2
  import fs from 'fs'
3
3
  import JSON5 from 'json5'
4
+ import chalk from 'chalk'
4
5
  import { ContextConfig, PagesJson } from '../types'
5
6
  import { transformVueFile } from '../utils'
6
7
 
7
8
  export class pageContext {
8
9
  config: ContextConfig
9
10
  pages: string[] = []
11
+ log = {
12
+ info: (text: string) => {
13
+ console.log(chalk.white(text))
14
+ },
15
+ error: (text: string) => {
16
+ console.log(chalk.red(text))
17
+ },
18
+ devLog: (text: string) => {
19
+ if (this.config.mode === 'development' && this.config.debug) {
20
+ console.log(chalk.green(text))
21
+ }
22
+ },
23
+ }
10
24
 
11
25
  constructor(config: ContextConfig) {
12
26
  this.config = config
@@ -31,16 +45,19 @@ export class pageContext {
31
45
  this.pages.push(...mainPages)
32
46
  }
33
47
  if (subpackages) {
34
- const root = subpackages.root
35
- const subPages = subpackages.pages.reduce((acc: string[], current) => {
36
- acc.push(`${root}/${current.path}`.replace('//', '/'))
37
- return acc
38
- }, [])
39
- this.pages.push(...subPages)
48
+ for (let i = 0; i < subpackages.length; i++) {
49
+ const element = subpackages[i]
50
+ const root = element.root
51
+ const subPages = element.pages.reduce((acc: string[], current) => {
52
+ acc.push(`${root}/${current.path}`.replace('//', '/'))
53
+ return acc
54
+ }, [])
55
+ this.pages.push(...subPages)
56
+ }
40
57
  }
41
- } catch (error) {
42
- console.error('读取pages.json文件失败')
43
- console.error(error)
58
+ } catch (error: unknown) {
59
+ this.log.error('读取pages.json文件失败')
60
+ this.log.devLog(String(error))
44
61
  }
45
62
  }
46
63
  // 获取指定id的page
package/src/index.ts CHANGED
@@ -9,6 +9,7 @@ function MpBackPlugin(userOptions: UserOptions = {}): Plugin {
9
9
  const defaultOptions: Config = {
10
10
  preventDefault: false,
11
11
  frequency: 1,
12
+ debug: false,
12
13
  }
13
14
  const options = { ...defaultOptions, ...userOptions }
14
15
 
package/types/index.ts CHANGED
@@ -9,6 +9,10 @@ export type Config = {
9
9
  * 阻止次数,默认是 `1`
10
10
  */
11
11
  frequency: number
12
+ /**
13
+ * 调试模式
14
+ */
15
+ debug: boolean
12
16
  /**
13
17
  * 页面回退时触发
14
18
  */
@@ -31,5 +35,5 @@ type Pages = { path: string }[]
31
35
 
32
36
  export type PagesJson = {
33
37
  pages: Pages
34
- subpackages: { root: string; pages: Pages }
38
+ subpackages: { root: string; pages: Pages }[]
35
39
  }
package/utils/index.ts CHANGED
@@ -94,6 +94,8 @@ export async function transformVueFile(this: pageContext, code: string, id: stri
94
94
 
95
95
  if (!hasPageBack) return
96
96
 
97
+ this.log.devLog(`页面${this.getPageById(id)}注入mp-weixin-back`)
98
+
97
99
  // 不阻止默认行为就返回到上一层
98
100
  if (!pageBackConfig.preventDefault) {
99
101
  callbackCode += `uni.navigateBack({ delta: 1 });`
File without changes