mp-weixin-back 0.0.7 → 0.0.9
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 +285 -85
- package/dist/index.mjs +284 -85
- package/package.json +5 -4
- package/src/context.ts +6 -5
- package/test/data/index-default.vue +17 -0
- package/test/data/{index.vue → index-setup.vue} +1 -0
- package/test/generate.spec.ts +20 -3
- package/utils/index.ts +16 -155
- package/utils/walker.ts +392 -0
package/dist/index.mjs
CHANGED
|
@@ -2,133 +2,331 @@ import path from 'path';
|
|
|
2
2
|
import fs from 'fs';
|
|
3
3
|
import JSON5 from 'json5';
|
|
4
4
|
import { white, red, green } from 'kolorist';
|
|
5
|
-
import generate from '@babel/generator';
|
|
6
5
|
import { parse } from '@vue/compiler-sfc';
|
|
6
|
+
import generate from '@babel/generator';
|
|
7
7
|
import { babelParse, walkAST } from 'ast-kit';
|
|
8
|
+
import MagicString from 'magic-string';
|
|
8
9
|
|
|
9
10
|
const virtualFileId = "mp-weixin-back-helper";
|
|
10
11
|
|
|
12
|
+
const pageContainerComp = ' <page-container :show="__MP_BACK_SHOW_PAGE_CONTAINER__" :overlay="false" @beforeleave="onBeforeLeave" :z-index="1" :duration="false"></page-container>\n';
|
|
11
13
|
function isArrowFunction(func) {
|
|
12
14
|
if (typeof func !== "function")
|
|
13
15
|
return false;
|
|
14
16
|
return !func.hasOwnProperty("prototype") && func.toString().includes("=>");
|
|
15
17
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
if (!code.includes("<template")) {
|
|
28
|
-
return code;
|
|
29
|
-
}
|
|
30
|
-
const componentStr = '<page-container :show="__MP_BACK_SHOW_PAGE_CONTAINER__" :overlay="false" @beforeleave="onBeforeLeave" :z-index="1" :duration="false"></page-container>';
|
|
31
|
-
const sfc = await parseSFC(code);
|
|
32
|
-
const setupCode = sfc.scriptSetup?.loc.source;
|
|
33
|
-
const setupAst = babelParse(setupCode || "", sfc.scriptSetup?.lang);
|
|
34
|
-
let pageBackConfig = this.config;
|
|
35
|
-
let hasPageBack = false, hasImportRef = false, pageBackFnName = "onPageBack", callbackCode = ``;
|
|
18
|
+
function compositionWalk(context, code, sfc, id) {
|
|
19
|
+
const codeMs = new MagicString(code);
|
|
20
|
+
const setupAst = babelParse(sfc.scriptSetup.loc.source, sfc.scriptSetup.lang);
|
|
21
|
+
let pageInfo = {
|
|
22
|
+
hasPageBack: false,
|
|
23
|
+
pageBackFnName: "onPageBack",
|
|
24
|
+
hasImportRef: false,
|
|
25
|
+
backConfig: { ...context.config },
|
|
26
|
+
callbackCode: ""
|
|
27
|
+
};
|
|
36
28
|
if (setupAst) {
|
|
37
29
|
walkAST(setupAst, {
|
|
38
30
|
enter(node) {
|
|
39
|
-
if (node.type
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
31
|
+
if (node.type === "ImportDeclaration") {
|
|
32
|
+
if (node.source.value.includes(virtualFileId)) {
|
|
33
|
+
const importSpecifier = node.specifiers[0];
|
|
34
|
+
pageInfo.hasPageBack = true;
|
|
35
|
+
pageInfo.pageBackFnName = importSpecifier.local.name;
|
|
36
|
+
}
|
|
37
|
+
if (node.source.value === "vue") {
|
|
38
|
+
node.specifiers.some((specifier) => {
|
|
39
|
+
if (specifier.local.name === "ref") {
|
|
40
|
+
pageInfo.hasImportRef = true;
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
return false;
|
|
44
|
+
});
|
|
52
45
|
}
|
|
53
46
|
}
|
|
54
|
-
if (node.type
|
|
47
|
+
if (node.type === "ExpressionStatement" && node.expression.type === "CallExpression" && node.expression.callee.loc?.identifierName === pageInfo.pageBackFnName) {
|
|
55
48
|
const callback = node.expression.arguments[0];
|
|
56
49
|
const backArguments = node.expression.arguments[1];
|
|
57
|
-
if (backArguments
|
|
50
|
+
if (backArguments?.type === "ObjectExpression") {
|
|
58
51
|
const config = new Function(
|
|
59
52
|
// @ts-ignore
|
|
60
53
|
`return (${(generate.default ? generate.default : generate)(backArguments).code});`
|
|
61
54
|
)();
|
|
62
|
-
|
|
55
|
+
Object.assign(pageInfo.backConfig, config);
|
|
63
56
|
}
|
|
64
57
|
if (callback && (callback.type === "ArrowFunctionExpression" || callback.type === "FunctionExpression")) {
|
|
65
58
|
const body = callback.body;
|
|
66
59
|
if (body.type === "BlockStatement") {
|
|
67
|
-
body.body.
|
|
68
|
-
|
|
69
|
-
|
|
60
|
+
pageInfo.callbackCode += body.body.map(
|
|
61
|
+
// @ts-ignore
|
|
62
|
+
(statement) => (generate.default ? generate.default : generate)(statement).code
|
|
63
|
+
).join("");
|
|
70
64
|
}
|
|
71
65
|
}
|
|
72
66
|
}
|
|
73
67
|
}
|
|
74
68
|
});
|
|
75
69
|
}
|
|
76
|
-
if (!hasPageBack)
|
|
77
|
-
return;
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
70
|
+
if (!pageInfo.hasPageBack)
|
|
71
|
+
return code;
|
|
72
|
+
if (code.includes("<page-container")) {
|
|
73
|
+
context.log.debugLog(`${context.getPageById(id)}\u9875\u9762\u5DF2\u6709page-container\u7EC4\u4EF6\uFF0C\u6CE8\u5165\u5931\u8D25`);
|
|
74
|
+
return code;
|
|
75
|
+
}
|
|
76
|
+
if (!pageInfo.backConfig.preventDefault) {
|
|
77
|
+
pageInfo.callbackCode += "uni.navigateBack({ delta: 1 });";
|
|
81
78
|
}
|
|
79
|
+
const importRefFromVue = !pageInfo.hasImportRef ? `import { ref } from 'vue'` : "";
|
|
80
|
+
const stateFrequency = "let __MP_BACK_FREQUENCY__ = 1;";
|
|
81
|
+
const statePageContainerVar = "const __MP_BACK_SHOW_PAGE_CONTAINER__ = ref(true);";
|
|
82
82
|
const configBack = (() => {
|
|
83
|
-
|
|
83
|
+
const onPageBack = pageInfo.backConfig.onPageBack;
|
|
84
|
+
if (!onPageBack)
|
|
84
85
|
return "";
|
|
85
|
-
if (typeof
|
|
86
|
+
if (typeof onPageBack !== "function") {
|
|
86
87
|
throw new Error("`onPageBack` must be a function");
|
|
87
88
|
}
|
|
88
|
-
const params = JSON.stringify({
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
const hasFunction = pageBackConfig.onPageBack.toString().includes("function");
|
|
92
|
-
if (isArrowFunction(pageBackConfig.onPageBack) || hasFunction) {
|
|
93
|
-
return `(${pageBackConfig.onPageBack})(${params});`;
|
|
89
|
+
const params = JSON.stringify({ page: context.getPageById(id) });
|
|
90
|
+
if (isArrowFunction(onPageBack) || onPageBack.toString().includes("function")) {
|
|
91
|
+
return `(${onPageBack})(${params});`;
|
|
94
92
|
}
|
|
95
|
-
return `(function ${
|
|
93
|
+
return `(function ${onPageBack})()`;
|
|
96
94
|
})();
|
|
97
|
-
const
|
|
98
|
-
${!hasImportRef ? "import { ref } from 'vue'" : ""}
|
|
99
|
-
let __MP_BACK_FREQUENCY__ = 1
|
|
100
|
-
const __MP_BACK_SHOW_PAGE_CONTAINER__ = ref(true);
|
|
95
|
+
const stateBeforeLeave = `
|
|
101
96
|
const onBeforeLeave = () => {
|
|
102
|
-
|
|
103
|
-
if (__MP_BACK_FREQUENCY__ < ${pageBackConfig.frequency}) {
|
|
97
|
+
if (__MP_BACK_FREQUENCY__ < ${pageInfo.backConfig.frequency}) {
|
|
104
98
|
__MP_BACK_SHOW_PAGE_CONTAINER__.value = false
|
|
105
|
-
setTimeout(() =>
|
|
106
|
-
__MP_BACK_SHOW_PAGE_CONTAINER__.value = true
|
|
107
|
-
}, 0);
|
|
99
|
+
setTimeout(() => __MP_BACK_SHOW_PAGE_CONTAINER__.value = true, 0);
|
|
108
100
|
__MP_BACK_FREQUENCY__++
|
|
109
101
|
}
|
|
110
|
-
// \u8FD0\u884C\u914D\u7F6E\u7684\u533F\u540D\u51FD\u6570
|
|
111
102
|
${configBack}
|
|
112
|
-
${callbackCode}
|
|
103
|
+
${pageInfo.callbackCode}
|
|
113
104
|
};
|
|
114
105
|
`;
|
|
115
|
-
const
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
106
|
+
const { template, scriptSetup } = sfc;
|
|
107
|
+
const tempOffsets = {
|
|
108
|
+
start: template.loc.start.offset,
|
|
109
|
+
end: template.loc.end.offset,
|
|
110
|
+
content: template.content
|
|
111
|
+
};
|
|
112
|
+
const templateMagicString = new MagicString(tempOffsets.content);
|
|
113
|
+
templateMagicString.append(pageContainerComp);
|
|
114
|
+
codeMs.overwrite(tempOffsets.start, tempOffsets.end, templateMagicString.toString());
|
|
115
|
+
const scriptOffsets = {
|
|
116
|
+
start: scriptSetup.loc.start.offset,
|
|
117
|
+
end: scriptSetup.loc.end.offset,
|
|
118
|
+
content: scriptSetup.content || ""
|
|
119
|
+
};
|
|
120
|
+
const scriptMagicString = new MagicString(scriptOffsets.content);
|
|
121
|
+
scriptMagicString.prepend(
|
|
122
|
+
` ${importRefFromVue}
|
|
123
|
+
${stateFrequency}
|
|
124
|
+
${statePageContainerVar}
|
|
125
|
+
${stateBeforeLeave} `
|
|
126
|
+
);
|
|
127
|
+
codeMs.overwrite(scriptOffsets.start, scriptOffsets.end, scriptMagicString.toString());
|
|
128
|
+
return codeMs.toString();
|
|
129
|
+
}
|
|
130
|
+
function optionsWalk(context, code, sfc, id) {
|
|
131
|
+
const codeMs = new MagicString(code);
|
|
132
|
+
const ast = babelParse(sfc.script.loc.source, sfc.script.lang);
|
|
133
|
+
let pageInfo = {
|
|
134
|
+
hasPageBack: false,
|
|
135
|
+
pageBackFnName: "onPageBack",
|
|
136
|
+
backConfig: { ...context.config }
|
|
137
|
+
};
|
|
138
|
+
let exportDefaultNode = null;
|
|
139
|
+
let dataMethodNode = null;
|
|
140
|
+
let methodsNode = null;
|
|
141
|
+
let onPageBackNodeMethod = null;
|
|
142
|
+
let onPageBackNodeProperty = null;
|
|
143
|
+
if (ast) {
|
|
144
|
+
walkAST(ast, {
|
|
145
|
+
enter(node) {
|
|
146
|
+
if (node.type === "ExportDefaultDeclaration" && node.declaration.type === "ObjectExpression") {
|
|
147
|
+
exportDefaultNode = node.declaration;
|
|
148
|
+
const properties = node.declaration.properties;
|
|
149
|
+
for (let i = 0; i < properties.length; i++) {
|
|
150
|
+
const element = properties[i];
|
|
151
|
+
if (element.type === "ObjectMethod" && element.key.type === "Identifier" && element.key.name === "data" && element.body.type === "BlockStatement") {
|
|
152
|
+
dataMethodNode = element.body;
|
|
153
|
+
}
|
|
154
|
+
if (element.type === "ObjectProperty" && element.key.type === "Identifier" && element.key.name === "methods") {
|
|
155
|
+
methodsNode = element.value;
|
|
156
|
+
}
|
|
157
|
+
const blockStatementCondition = element.type === "ObjectMethod" && element.key.type === "Identifier" && element.key.name === pageInfo.pageBackFnName && element.body.type === "BlockStatement";
|
|
158
|
+
const functionExpressionCondition = element.type === "ObjectProperty" && element.key.type === "Identifier" && element.key.name === pageInfo.pageBackFnName && element.value.type === "FunctionExpression";
|
|
159
|
+
if (blockStatementCondition) {
|
|
160
|
+
pageInfo.hasPageBack = true;
|
|
161
|
+
onPageBackNodeMethod = element;
|
|
162
|
+
}
|
|
163
|
+
if (functionExpressionCondition) {
|
|
164
|
+
pageInfo.hasPageBack = true;
|
|
165
|
+
onPageBackNodeProperty = element;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
if (!pageInfo.hasPageBack)
|
|
173
|
+
return;
|
|
174
|
+
const newDataProperty = [
|
|
175
|
+
{
|
|
176
|
+
type: "ObjectProperty",
|
|
177
|
+
key: { type: "Identifier", name: "__MP_BACK_SHOW_PAGE_CONTAINER__" },
|
|
178
|
+
value: { type: "BooleanLiteral", value: true },
|
|
179
|
+
computed: false,
|
|
180
|
+
shorthand: false
|
|
181
|
+
},
|
|
182
|
+
{
|
|
183
|
+
type: "ObjectProperty",
|
|
184
|
+
key: { type: "Identifier", name: "__MP_BACK_FREQUENCY__" },
|
|
185
|
+
value: { type: "NumericLiteral", value: 1 },
|
|
186
|
+
computed: false,
|
|
187
|
+
shorthand: false
|
|
188
|
+
}
|
|
189
|
+
];
|
|
190
|
+
if (dataMethodNode) {
|
|
191
|
+
const returnStatement = dataMethodNode.body.find(
|
|
192
|
+
(node) => node.type === "ReturnStatement"
|
|
193
|
+
);
|
|
194
|
+
if (returnStatement && returnStatement.argument && returnStatement.argument.type === "ObjectExpression") {
|
|
195
|
+
returnStatement.argument.properties.push(...newDataProperty);
|
|
129
196
|
}
|
|
197
|
+
} else if (exportDefaultNode) {
|
|
198
|
+
const addData = {
|
|
199
|
+
type: "ObjectMethod",
|
|
200
|
+
key: { type: "Identifier", name: "data" },
|
|
201
|
+
kind: "method",
|
|
202
|
+
params: [],
|
|
203
|
+
async: false,
|
|
204
|
+
generator: false,
|
|
205
|
+
computed: false,
|
|
206
|
+
body: {
|
|
207
|
+
type: "BlockStatement",
|
|
208
|
+
directives: [],
|
|
209
|
+
body: [
|
|
210
|
+
{
|
|
211
|
+
type: "ReturnStatement",
|
|
212
|
+
argument: {
|
|
213
|
+
type: "ObjectExpression",
|
|
214
|
+
properties: newDataProperty
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
]
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
exportDefaultNode.properties.push(addData);
|
|
221
|
+
}
|
|
222
|
+
const configBack = (() => {
|
|
223
|
+
const onPageBack = pageInfo.backConfig.onPageBack;
|
|
224
|
+
if (!onPageBack)
|
|
225
|
+
return "";
|
|
226
|
+
if (typeof onPageBack !== "function") {
|
|
227
|
+
throw new Error("`onPageBack` must be a function");
|
|
228
|
+
}
|
|
229
|
+
const params = JSON.stringify({ page: context.getPageById(id) });
|
|
230
|
+
if (isArrowFunction(onPageBack) || onPageBack.toString().includes("function")) {
|
|
231
|
+
return `(${onPageBack})(${params});`;
|
|
232
|
+
}
|
|
233
|
+
return `(function ${onPageBack})()`;
|
|
234
|
+
})();
|
|
235
|
+
const stateBeforeLeave = `
|
|
236
|
+
function onBeforeLeave() {
|
|
237
|
+
if (this.__MP_BACK_FREQUENCY__ < ${pageInfo.backConfig.frequency}) {
|
|
238
|
+
this.__MP_BACK_SHOW_PAGE_CONTAINER__ = false
|
|
239
|
+
setTimeout(() => { this.__MP_BACK_SHOW_PAGE_CONTAINER__ = true }, 0);
|
|
240
|
+
this.__MP_BACK_FREQUENCY__++
|
|
241
|
+
}
|
|
242
|
+
${configBack}
|
|
243
|
+
${!pageInfo.backConfig.preventDefault ? "uni.navigateBack({ delta: 1 });" : ""}
|
|
244
|
+
};
|
|
245
|
+
`;
|
|
246
|
+
const stateBeforeLeaveAst = babelParse(stateBeforeLeave);
|
|
247
|
+
const stateBeforeLeaveNode = stateBeforeLeaveAst.body.find(
|
|
248
|
+
(node) => node.type === "FunctionDeclaration"
|
|
130
249
|
);
|
|
131
|
-
|
|
250
|
+
const newMethodsProperty = {
|
|
251
|
+
type: "ObjectMethod",
|
|
252
|
+
key: {
|
|
253
|
+
type: "Identifier",
|
|
254
|
+
name: "onBeforeLeave"
|
|
255
|
+
},
|
|
256
|
+
kind: "method",
|
|
257
|
+
generator: false,
|
|
258
|
+
async: false,
|
|
259
|
+
params: [],
|
|
260
|
+
computed: false,
|
|
261
|
+
body: {
|
|
262
|
+
type: "BlockStatement",
|
|
263
|
+
directives: [],
|
|
264
|
+
body: [
|
|
265
|
+
...onPageBackNodeMethod ? onPageBackNodeMethod.body.body : [],
|
|
266
|
+
...onPageBackNodeProperty ? onPageBackNodeProperty.value.body.body : [],
|
|
267
|
+
...stateBeforeLeaveNode.body.body
|
|
268
|
+
]
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
if (methodsNode) {
|
|
272
|
+
methodsNode.properties.push(newMethodsProperty);
|
|
273
|
+
const code2 = (generate.default ? generate.default : generate)(methodsNode);
|
|
274
|
+
console.log(code2);
|
|
275
|
+
} else if (exportDefaultNode) {
|
|
276
|
+
const addMethods = {
|
|
277
|
+
type: "ObjectProperty",
|
|
278
|
+
computed: false,
|
|
279
|
+
shorthand: false,
|
|
280
|
+
key: {
|
|
281
|
+
type: "Identifier",
|
|
282
|
+
name: "methods"
|
|
283
|
+
},
|
|
284
|
+
value: {
|
|
285
|
+
type: "ObjectExpression",
|
|
286
|
+
properties: [newMethodsProperty]
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
exportDefaultNode.properties.push(addMethods);
|
|
290
|
+
}
|
|
291
|
+
const { template, script } = sfc;
|
|
292
|
+
const tempOffsets = {
|
|
293
|
+
start: template.loc.start.offset,
|
|
294
|
+
end: template.loc.end.offset,
|
|
295
|
+
content: template.content
|
|
296
|
+
};
|
|
297
|
+
const templateMagicString = new MagicString(tempOffsets.content);
|
|
298
|
+
templateMagicString.append(pageContainerComp);
|
|
299
|
+
codeMs.overwrite(tempOffsets.start, tempOffsets.end, templateMagicString.toString());
|
|
300
|
+
const scriptOffsets = {
|
|
301
|
+
start: script.loc.start.offset,
|
|
302
|
+
end: script.loc.end.offset
|
|
303
|
+
};
|
|
304
|
+
const newScriptContent = (generate.default ? generate.default : generate)(ast).code;
|
|
305
|
+
codeMs.overwrite(scriptOffsets.start, scriptOffsets.end, newScriptContent);
|
|
306
|
+
return codeMs.toString();
|
|
307
|
+
}
|
|
308
|
+
const vueWalker = {
|
|
309
|
+
compositionWalk,
|
|
310
|
+
optionsWalk
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
async function transformVueFile(code, id) {
|
|
314
|
+
try {
|
|
315
|
+
const sfc = parse(code).descriptor;
|
|
316
|
+
const { template, script, scriptSetup } = sfc;
|
|
317
|
+
if (!template?.content) {
|
|
318
|
+
return code;
|
|
319
|
+
}
|
|
320
|
+
if (!script?.content && !scriptSetup?.content) {
|
|
321
|
+
return code;
|
|
322
|
+
}
|
|
323
|
+
const walker = scriptSetup ? "compositionWalk" : "optionsWalk";
|
|
324
|
+
return vueWalker[walker](this, code, sfc, id);
|
|
325
|
+
} catch (error) {
|
|
326
|
+
this.log.error("\u89E3\u6790vue\u6587\u4EF6\u5931\u8D25\uFF0C\u8BF7\u68C0\u67E5\u6587\u4EF6\u662F\u5426\u6B63\u786E");
|
|
327
|
+
this.log.debugLog(String(error));
|
|
328
|
+
return code;
|
|
329
|
+
}
|
|
132
330
|
}
|
|
133
331
|
|
|
134
332
|
var __defProp = Object.defineProperty;
|
|
@@ -139,18 +337,19 @@ var __publicField = (obj, key, value) => {
|
|
|
139
337
|
};
|
|
140
338
|
class pageContext {
|
|
141
339
|
constructor(config) {
|
|
340
|
+
__publicField(this, "logPreText", "[mp-weixin-back] : ");
|
|
142
341
|
__publicField(this, "config");
|
|
143
342
|
__publicField(this, "pages", []);
|
|
144
343
|
__publicField(this, "log", {
|
|
145
344
|
info: (text) => {
|
|
146
|
-
console.log(white(text));
|
|
345
|
+
console.log(white(this.logPreText + text));
|
|
147
346
|
},
|
|
148
347
|
error: (text) => {
|
|
149
|
-
console.log(red(text));
|
|
348
|
+
console.log(red(this.logPreText + text));
|
|
150
349
|
},
|
|
151
|
-
|
|
350
|
+
debugLog: (text) => {
|
|
152
351
|
if (this.config.mode === "development" && this.config.debug) {
|
|
153
|
-
console.log(green(text));
|
|
352
|
+
console.log(green(this.logPreText + text));
|
|
154
353
|
}
|
|
155
354
|
}
|
|
156
355
|
});
|
|
@@ -189,7 +388,7 @@ class pageContext {
|
|
|
189
388
|
}
|
|
190
389
|
} catch (error) {
|
|
191
390
|
this.log.error("\u8BFB\u53D6pages.json\u6587\u4EF6\u5931\u8D25");
|
|
192
|
-
this.log.
|
|
391
|
+
this.log.debugLog(String(error));
|
|
193
392
|
}
|
|
194
393
|
}
|
|
195
394
|
// 获取指定id的page
|
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.9",
|
|
5
5
|
"description": "监听微信小程序的手势返回和页面默认导航栏的返回",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
7
7
|
"module": "dist/index.mjs",
|
|
@@ -41,14 +41,13 @@
|
|
|
41
41
|
"@babel/generator": "^7.26.2",
|
|
42
42
|
"@babel/parser": "^7.26.2",
|
|
43
43
|
"@babel/traverse": "^7.25.9",
|
|
44
|
-
"@types/babel__generator": "^7.6.8",
|
|
45
|
-
"@types/node": "^22.9.3",
|
|
46
44
|
"@vitejs/plugin-vue": "^5.2.0",
|
|
47
45
|
"@vue/test-utils": "^2.4.6",
|
|
48
46
|
"ast-kit": "^1.3.1",
|
|
49
47
|
"happy-dom": "^15.11.6",
|
|
50
48
|
"json5": "^2.2.3",
|
|
51
49
|
"kolorist": "^1.8.0",
|
|
50
|
+
"magic-string": "^0.30.13",
|
|
52
51
|
"typescript": "^5.7.2",
|
|
53
52
|
"vitest": "^2.1.5"
|
|
54
53
|
},
|
|
@@ -60,7 +59,9 @@
|
|
|
60
59
|
"*": "prettier --write"
|
|
61
60
|
},
|
|
62
61
|
"devDependencies": {
|
|
62
|
+
"@types/babel__generator": "^7.6.8",
|
|
63
|
+
"@types/node": "^22.9.3",
|
|
63
64
|
"unbuild": "^2.0.0",
|
|
64
65
|
"vue": "^3.5.13"
|
|
65
66
|
}
|
|
66
|
-
}
|
|
67
|
+
}
|
package/src/context.ts
CHANGED
|
@@ -6,18 +6,19 @@ import { ContextConfig, PagesJson } from '../types'
|
|
|
6
6
|
import { transformVueFile } from '../utils'
|
|
7
7
|
|
|
8
8
|
export class pageContext {
|
|
9
|
+
private logPreText = '[mp-weixin-back] : '
|
|
9
10
|
config: ContextConfig
|
|
10
11
|
pages: string[] = []
|
|
11
12
|
log = {
|
|
12
13
|
info: (text: string) => {
|
|
13
|
-
console.log(white(text))
|
|
14
|
+
console.log(white(this.logPreText + text))
|
|
14
15
|
},
|
|
15
16
|
error: (text: string) => {
|
|
16
|
-
console.log(red(text))
|
|
17
|
+
console.log(red(this.logPreText + text))
|
|
17
18
|
},
|
|
18
|
-
|
|
19
|
+
debugLog: (text: string) => {
|
|
19
20
|
if (this.config.mode === 'development' && this.config.debug) {
|
|
20
|
-
console.log(green(text))
|
|
21
|
+
console.log(green(this.logPreText + text))
|
|
21
22
|
}
|
|
22
23
|
},
|
|
23
24
|
}
|
|
@@ -57,7 +58,7 @@ export class pageContext {
|
|
|
57
58
|
}
|
|
58
59
|
} catch (error: unknown) {
|
|
59
60
|
this.log.error('读取pages.json文件失败')
|
|
60
|
-
this.log.
|
|
61
|
+
this.log.debugLog(String(error))
|
|
61
62
|
}
|
|
62
63
|
}
|
|
63
64
|
// 获取指定id的page
|
package/test/generate.spec.ts
CHANGED
|
@@ -1,14 +1,31 @@
|
|
|
1
1
|
import { describe, expect, it } from 'vitest'
|
|
2
2
|
import { mount } from '@vue/test-utils'
|
|
3
3
|
// @ts-ignore
|
|
4
|
-
import
|
|
4
|
+
import IndexSetup from './data/index-setup.vue'
|
|
5
|
+
// @ts-ignore
|
|
6
|
+
import IndexDefault from './data/index-default.vue'
|
|
5
7
|
|
|
6
8
|
describe('generate page-container components', () => {
|
|
7
|
-
it('
|
|
8
|
-
const wrapper = mount(
|
|
9
|
+
it('setup compisitionAPI', async () => {
|
|
10
|
+
const wrapper = mount(IndexSetup)
|
|
9
11
|
await wrapper.vm.$nextTick()
|
|
10
12
|
|
|
11
13
|
const pageContainerRef = wrapper.find('page-container')
|
|
12
14
|
expect(pageContainerRef.exists()).toBe(true)
|
|
15
|
+
expect(wrapper.vm.__MP_BACK_SHOW_PAGE_CONTAINER__).toBe(true)
|
|
16
|
+
expect(wrapper.vm.onBeforeLeave()).toBe(true)
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it('default optionsAPI', async () => {
|
|
20
|
+
const wrapper = mount(IndexDefault)
|
|
21
|
+
await wrapper.vm.$nextTick()
|
|
22
|
+
const pageContainerRef = wrapper.find('page-container')
|
|
23
|
+
expect(pageContainerRef.exists()).toBe(true)
|
|
24
|
+
// @ts-ignore
|
|
25
|
+
expect(wrapper.vm.__MP_BACK_SHOW_PAGE_CONTAINER__).toBe(true)
|
|
26
|
+
// @ts-ignore
|
|
27
|
+
expect(wrapper.vm.__MP_BACK_FREQUENCY__).toBe(1)
|
|
28
|
+
// @ts-ignore
|
|
29
|
+
expect(wrapper.vm.onBeforeLeave()).toBe(true)
|
|
13
30
|
})
|
|
14
31
|
})
|