mp-weixin-back 0.0.12 → 0.0.14
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 +34 -10
- package/dist/index.mjs +34 -10
- package/package.json +7 -7
- package/utils/walker.ts +48 -12
package/dist/index.cjs
CHANGED
|
@@ -33,7 +33,8 @@ function compositionWalk(context, code, sfc, id) {
|
|
|
33
33
|
pageBackFnName: "onPageBack",
|
|
34
34
|
hasImportRef: false,
|
|
35
35
|
backConfig: { ...context.config },
|
|
36
|
-
|
|
36
|
+
onPageBackBodyAst: [],
|
|
37
|
+
onPageBackCallNodeToRemove: null,
|
|
37
38
|
activeFnName: "activeMpBack",
|
|
38
39
|
inActiveFnName: "inactiveMpBack"
|
|
39
40
|
};
|
|
@@ -71,6 +72,7 @@ function compositionWalk(context, code, sfc, id) {
|
|
|
71
72
|
}
|
|
72
73
|
}
|
|
73
74
|
if (node.type === "ExpressionStatement" && node.expression.type === "CallExpression" && node.expression.callee.loc?.identifierName === pageInfo.pageBackFnName) {
|
|
75
|
+
pageInfo.onPageBackCallNodeToRemove = node;
|
|
74
76
|
const callback = node.expression.arguments[0];
|
|
75
77
|
const backArguments = node.expression.arguments[1];
|
|
76
78
|
if (backArguments?.type === "ObjectExpression") {
|
|
@@ -81,14 +83,9 @@ function compositionWalk(context, code, sfc, id) {
|
|
|
81
83
|
Object.assign(pageInfo.backConfig, config);
|
|
82
84
|
}
|
|
83
85
|
if (callback && (callback.type === "ArrowFunctionExpression" || callback.type === "FunctionExpression")) {
|
|
84
|
-
|
|
85
|
-
if (body.type === "BlockStatement") {
|
|
86
|
-
pageInfo.callbackCode += body.body.map(
|
|
87
|
-
// @ts-ignore
|
|
88
|
-
(statement) => (generate__default.default ? generate__default.default : generate__default)(statement).code
|
|
89
|
-
).join("");
|
|
90
|
-
}
|
|
86
|
+
pageInfo.onPageBackBodyAst = callback.body.body;
|
|
91
87
|
}
|
|
88
|
+
return;
|
|
92
89
|
}
|
|
93
90
|
if (node.type === "ExpressionStatement" && node.expression.type === "CallExpression" && node.expression.callee.loc?.identifierName === pageInfo.activeFnName) {
|
|
94
91
|
activeFnCallsToModify.push({
|
|
@@ -117,12 +114,39 @@ function compositionWalk(context, code, sfc, id) {
|
|
|
117
114
|
}
|
|
118
115
|
if (!pageInfo.hasPageBack)
|
|
119
116
|
return code;
|
|
117
|
+
if (pageInfo.onPageBackCallNodeToRemove) {
|
|
118
|
+
const scriptSetupOffset = sfc.scriptSetup.loc.start.offset;
|
|
119
|
+
const nodeToRemove = pageInfo.onPageBackCallNodeToRemove;
|
|
120
|
+
const globalStart = scriptSetupOffset + nodeToRemove.start;
|
|
121
|
+
const globalEnd = scriptSetupOffset + nodeToRemove.end;
|
|
122
|
+
codeMs.remove(globalStart, globalEnd);
|
|
123
|
+
}
|
|
124
|
+
let callbackCode = "";
|
|
125
|
+
if (pageInfo.onPageBackBodyAst.length > 0) {
|
|
126
|
+
const tempAstRoot = {
|
|
127
|
+
type: "BlockStatement",
|
|
128
|
+
body: pageInfo.onPageBackBodyAst
|
|
129
|
+
};
|
|
130
|
+
astKit.walkAST(tempAstRoot, {
|
|
131
|
+
enter(node) {
|
|
132
|
+
if (node.type === "CallExpression" && node.callee.type === "Identifier") {
|
|
133
|
+
const createIdentifier = (name) => ({ type: "Identifier", name });
|
|
134
|
+
if (node.callee.name === pageInfo.activeFnName) {
|
|
135
|
+
node.arguments.unshift(createIdentifier("__MP_WEIXIN_ACTIVEBACK__"));
|
|
136
|
+
} else if (node.callee.name === pageInfo.inActiveFnName) {
|
|
137
|
+
node.arguments.unshift(createIdentifier("__MP_WEIXIN_INACTIVEBACK__"));
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
callbackCode = pageInfo.onPageBackBodyAst.map((statement) => (generate__default.default ? generate__default.default : generate__default)(statement).code).join("\n");
|
|
143
|
+
}
|
|
120
144
|
if (code.includes("<page-container")) {
|
|
121
145
|
context.log.debugLog(`${context.getPageById(id)}\u9875\u9762\u5DF2\u6709page-container\u7EC4\u4EF6\uFF0C\u6CE8\u5165\u5931\u8D25`);
|
|
122
146
|
return code;
|
|
123
147
|
}
|
|
124
148
|
if (!pageInfo.backConfig.preventDefault) {
|
|
125
|
-
|
|
149
|
+
callbackCode += "uni.navigateBack({ delta: 1 });";
|
|
126
150
|
}
|
|
127
151
|
const importUseMpWeixinBack = `import { useMpWeixinBack } from '${virtualFileId}'`;
|
|
128
152
|
const importRefFromVue = !pageInfo.hasImportRef ? `import { ref } from 'vue'` : "";
|
|
@@ -154,7 +178,7 @@ function compositionWalk(context, code, sfc, id) {
|
|
|
154
178
|
__MP_BACK_FREQUENCY__++
|
|
155
179
|
}
|
|
156
180
|
${configBack}
|
|
157
|
-
${
|
|
181
|
+
${callbackCode}
|
|
158
182
|
};
|
|
159
183
|
`;
|
|
160
184
|
const { template, scriptSetup } = sfc;
|
package/dist/index.mjs
CHANGED
|
@@ -23,7 +23,8 @@ function compositionWalk(context, code, sfc, id) {
|
|
|
23
23
|
pageBackFnName: "onPageBack",
|
|
24
24
|
hasImportRef: false,
|
|
25
25
|
backConfig: { ...context.config },
|
|
26
|
-
|
|
26
|
+
onPageBackBodyAst: [],
|
|
27
|
+
onPageBackCallNodeToRemove: null,
|
|
27
28
|
activeFnName: "activeMpBack",
|
|
28
29
|
inActiveFnName: "inactiveMpBack"
|
|
29
30
|
};
|
|
@@ -61,6 +62,7 @@ function compositionWalk(context, code, sfc, id) {
|
|
|
61
62
|
}
|
|
62
63
|
}
|
|
63
64
|
if (node.type === "ExpressionStatement" && node.expression.type === "CallExpression" && node.expression.callee.loc?.identifierName === pageInfo.pageBackFnName) {
|
|
65
|
+
pageInfo.onPageBackCallNodeToRemove = node;
|
|
64
66
|
const callback = node.expression.arguments[0];
|
|
65
67
|
const backArguments = node.expression.arguments[1];
|
|
66
68
|
if (backArguments?.type === "ObjectExpression") {
|
|
@@ -71,14 +73,9 @@ function compositionWalk(context, code, sfc, id) {
|
|
|
71
73
|
Object.assign(pageInfo.backConfig, config);
|
|
72
74
|
}
|
|
73
75
|
if (callback && (callback.type === "ArrowFunctionExpression" || callback.type === "FunctionExpression")) {
|
|
74
|
-
|
|
75
|
-
if (body.type === "BlockStatement") {
|
|
76
|
-
pageInfo.callbackCode += body.body.map(
|
|
77
|
-
// @ts-ignore
|
|
78
|
-
(statement) => (generate.default ? generate.default : generate)(statement).code
|
|
79
|
-
).join("");
|
|
80
|
-
}
|
|
76
|
+
pageInfo.onPageBackBodyAst = callback.body.body;
|
|
81
77
|
}
|
|
78
|
+
return;
|
|
82
79
|
}
|
|
83
80
|
if (node.type === "ExpressionStatement" && node.expression.type === "CallExpression" && node.expression.callee.loc?.identifierName === pageInfo.activeFnName) {
|
|
84
81
|
activeFnCallsToModify.push({
|
|
@@ -107,12 +104,39 @@ function compositionWalk(context, code, sfc, id) {
|
|
|
107
104
|
}
|
|
108
105
|
if (!pageInfo.hasPageBack)
|
|
109
106
|
return code;
|
|
107
|
+
if (pageInfo.onPageBackCallNodeToRemove) {
|
|
108
|
+
const scriptSetupOffset = sfc.scriptSetup.loc.start.offset;
|
|
109
|
+
const nodeToRemove = pageInfo.onPageBackCallNodeToRemove;
|
|
110
|
+
const globalStart = scriptSetupOffset + nodeToRemove.start;
|
|
111
|
+
const globalEnd = scriptSetupOffset + nodeToRemove.end;
|
|
112
|
+
codeMs.remove(globalStart, globalEnd);
|
|
113
|
+
}
|
|
114
|
+
let callbackCode = "";
|
|
115
|
+
if (pageInfo.onPageBackBodyAst.length > 0) {
|
|
116
|
+
const tempAstRoot = {
|
|
117
|
+
type: "BlockStatement",
|
|
118
|
+
body: pageInfo.onPageBackBodyAst
|
|
119
|
+
};
|
|
120
|
+
walkAST(tempAstRoot, {
|
|
121
|
+
enter(node) {
|
|
122
|
+
if (node.type === "CallExpression" && node.callee.type === "Identifier") {
|
|
123
|
+
const createIdentifier = (name) => ({ type: "Identifier", name });
|
|
124
|
+
if (node.callee.name === pageInfo.activeFnName) {
|
|
125
|
+
node.arguments.unshift(createIdentifier("__MP_WEIXIN_ACTIVEBACK__"));
|
|
126
|
+
} else if (node.callee.name === pageInfo.inActiveFnName) {
|
|
127
|
+
node.arguments.unshift(createIdentifier("__MP_WEIXIN_INACTIVEBACK__"));
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
callbackCode = pageInfo.onPageBackBodyAst.map((statement) => (generate.default ? generate.default : generate)(statement).code).join("\n");
|
|
133
|
+
}
|
|
110
134
|
if (code.includes("<page-container")) {
|
|
111
135
|
context.log.debugLog(`${context.getPageById(id)}\u9875\u9762\u5DF2\u6709page-container\u7EC4\u4EF6\uFF0C\u6CE8\u5165\u5931\u8D25`);
|
|
112
136
|
return code;
|
|
113
137
|
}
|
|
114
138
|
if (!pageInfo.backConfig.preventDefault) {
|
|
115
|
-
|
|
139
|
+
callbackCode += "uni.navigateBack({ delta: 1 });";
|
|
116
140
|
}
|
|
117
141
|
const importUseMpWeixinBack = `import { useMpWeixinBack } from '${virtualFileId}'`;
|
|
118
142
|
const importRefFromVue = !pageInfo.hasImportRef ? `import { ref } from 'vue'` : "";
|
|
@@ -144,7 +168,7 @@ function compositionWalk(context, code, sfc, id) {
|
|
|
144
168
|
__MP_BACK_FREQUENCY__++
|
|
145
169
|
}
|
|
146
170
|
${configBack}
|
|
147
|
-
${
|
|
171
|
+
${callbackCode}
|
|
148
172
|
};
|
|
149
173
|
`;
|
|
150
174
|
const { template, scriptSetup } = sfc;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mp-weixin-back",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.14",
|
|
5
5
|
"description": "监听微信小程序的手势返回和页面默认导航栏的返回",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
7
7
|
"module": "dist/index.mjs",
|
|
@@ -41,15 +41,10 @@
|
|
|
41
41
|
"@babel/generator": "^7.26.2",
|
|
42
42
|
"@babel/parser": "^7.26.2",
|
|
43
43
|
"@babel/traverse": "^7.25.9",
|
|
44
|
-
"@vitejs/plugin-vue": "^5.2.0",
|
|
45
|
-
"@vue/test-utils": "^2.4.6",
|
|
46
44
|
"ast-kit": "^1.3.1",
|
|
47
|
-
"happy-dom": "^15.11.6",
|
|
48
45
|
"json5": "^2.2.3",
|
|
49
46
|
"kolorist": "^1.8.0",
|
|
50
|
-
"magic-string": "^0.30.13"
|
|
51
|
-
"typescript": "^5.7.2",
|
|
52
|
-
"vitest": "^2.1.5"
|
|
47
|
+
"magic-string": "^0.30.13"
|
|
53
48
|
},
|
|
54
49
|
"peerDependencies": {
|
|
55
50
|
"@vue/compiler-sfc": "^2.7.0 || ^3.0.0",
|
|
@@ -61,7 +56,12 @@
|
|
|
61
56
|
"devDependencies": {
|
|
62
57
|
"@types/babel__generator": "^7.6.8",
|
|
63
58
|
"@types/node": "^22.9.3",
|
|
59
|
+
"@vitejs/plugin-vue": "^5.2.0",
|
|
60
|
+
"@vue/test-utils": "^2.4.6",
|
|
61
|
+
"happy-dom": "^15.11.6",
|
|
62
|
+
"typescript": "^5.7.2",
|
|
64
63
|
"unbuild": "^2.0.0",
|
|
64
|
+
"vitest": "^2.1.5",
|
|
65
65
|
"vue": "^3.5.13"
|
|
66
66
|
}
|
|
67
67
|
}
|
package/utils/walker.ts
CHANGED
|
@@ -4,7 +4,9 @@ import { babelParse, walkAST } from 'ast-kit'
|
|
|
4
4
|
import { pageContext } from '../src/context'
|
|
5
5
|
import { virtualFileId } from './constant'
|
|
6
6
|
import type {
|
|
7
|
+
WithStatement,
|
|
7
8
|
BlockStatement,
|
|
9
|
+
Statement,
|
|
8
10
|
FunctionExpression,
|
|
9
11
|
Node,
|
|
10
12
|
ObjectExpression,
|
|
@@ -29,7 +31,8 @@ function compositionWalk(context: pageContext, code: string, sfc: any, id: strin
|
|
|
29
31
|
pageBackFnName: 'onPageBack',
|
|
30
32
|
hasImportRef: false,
|
|
31
33
|
backConfig: { ...context.config },
|
|
32
|
-
|
|
34
|
+
onPageBackBodyAst: [] as Statement[],
|
|
35
|
+
onPageBackCallNodeToRemove: null as Node | null,
|
|
33
36
|
activeFnName: 'activeMpBack',
|
|
34
37
|
inActiveFnName: 'inactiveMpBack',
|
|
35
38
|
}
|
|
@@ -81,6 +84,8 @@ function compositionWalk(context: pageContext, code: string, sfc: any, id: strin
|
|
|
81
84
|
node.expression.type === 'CallExpression' &&
|
|
82
85
|
node.expression.callee.loc?.identifierName === pageInfo.pageBackFnName
|
|
83
86
|
) {
|
|
87
|
+
// 记录下整个 onPageBack(...) 语句节点,以便后续移除
|
|
88
|
+
pageInfo.onPageBackCallNodeToRemove = node
|
|
84
89
|
const callback = node.expression.arguments[0]
|
|
85
90
|
const backArguments = node.expression.arguments[1]
|
|
86
91
|
|
|
@@ -96,16 +101,11 @@ function compositionWalk(context: pageContext, code: string, sfc: any, id: strin
|
|
|
96
101
|
callback &&
|
|
97
102
|
(callback.type === 'ArrowFunctionExpression' || callback.type === 'FunctionExpression')
|
|
98
103
|
) {
|
|
99
|
-
|
|
100
|
-
if (body.type === 'BlockStatement') {
|
|
101
|
-
pageInfo.callbackCode += body.body
|
|
102
|
-
.map(
|
|
103
|
-
// @ts-ignore
|
|
104
|
-
(statement) => (generate.default ? generate.default : generate)(statement).code
|
|
105
|
-
)
|
|
106
|
-
.join('')
|
|
107
|
-
}
|
|
104
|
+
pageInfo.onPageBackBodyAst = callback.body.body
|
|
108
105
|
}
|
|
106
|
+
|
|
107
|
+
// 跳过此节点的子节点遍历,因为我们将手动处理其内部逻辑
|
|
108
|
+
return
|
|
109
109
|
}
|
|
110
110
|
|
|
111
111
|
if (
|
|
@@ -146,13 +146,49 @@ function compositionWalk(context: pageContext, code: string, sfc: any, id: strin
|
|
|
146
146
|
// 没有引入mp-weixin-back-helper
|
|
147
147
|
if (!pageInfo.hasPageBack) return code
|
|
148
148
|
|
|
149
|
+
if (pageInfo.onPageBackCallNodeToRemove) {
|
|
150
|
+
const scriptSetupOffset = sfc.scriptSetup!.loc.start.offset
|
|
151
|
+
const nodeToRemove = pageInfo.onPageBackCallNodeToRemove as any
|
|
152
|
+
const globalStart = scriptSetupOffset + nodeToRemove.start
|
|
153
|
+
const globalEnd = scriptSetupOffset + nodeToRemove.end
|
|
154
|
+
codeMs.remove(globalStart, globalEnd)
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
let callbackCode = ''
|
|
158
|
+
if (pageInfo.onPageBackBodyAst.length > 0) {
|
|
159
|
+
// 包装成一个临时的 AST 根节点以供遍历
|
|
160
|
+
const tempAstRoot = {
|
|
161
|
+
type: 'BlockStatement',
|
|
162
|
+
body: pageInfo.onPageBackBodyAst,
|
|
163
|
+
} as WithStatement
|
|
164
|
+
|
|
165
|
+
walkAST(tempAstRoot, {
|
|
166
|
+
enter(node: any) {
|
|
167
|
+
if (node.type === 'CallExpression' && node.callee.type === 'Identifier') {
|
|
168
|
+
const createIdentifier = (name: string) => ({ type: 'Identifier', name })
|
|
169
|
+
|
|
170
|
+
if (node.callee.name === pageInfo.activeFnName) {
|
|
171
|
+
node.arguments.unshift(createIdentifier('__MP_WEIXIN_ACTIVEBACK__'))
|
|
172
|
+
} else if (node.callee.name === pageInfo.inActiveFnName) {
|
|
173
|
+
node.arguments.unshift(createIdentifier('__MP_WEIXIN_INACTIVEBACK__'))
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
},
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
callbackCode = pageInfo.onPageBackBodyAst
|
|
180
|
+
// @ts-ignore
|
|
181
|
+
.map((statement) => (generate.default ? generate.default : generate)(statement).code)
|
|
182
|
+
.join('\n')
|
|
183
|
+
}
|
|
184
|
+
|
|
149
185
|
if (code.includes('<page-container')) {
|
|
150
186
|
context.log.debugLog(`${context.getPageById(id)}页面已有page-container组件,注入失败`)
|
|
151
187
|
return code
|
|
152
188
|
}
|
|
153
189
|
|
|
154
190
|
if (!pageInfo.backConfig.preventDefault) {
|
|
155
|
-
|
|
191
|
+
callbackCode += 'uni.navigateBack({ delta: 1 });'
|
|
156
192
|
}
|
|
157
193
|
|
|
158
194
|
const importUseMpWeixinBack = `import { useMpWeixinBack } from '${virtualFileId}'`
|
|
@@ -188,7 +224,7 @@ function compositionWalk(context: pageContext, code: string, sfc: any, id: strin
|
|
|
188
224
|
__MP_BACK_FREQUENCY__++
|
|
189
225
|
}
|
|
190
226
|
${configBack}
|
|
191
|
-
${
|
|
227
|
+
${callbackCode}
|
|
192
228
|
};
|
|
193
229
|
`
|
|
194
230
|
const { template, scriptSetup } = sfc
|