mp-weixin-back 0.0.8 → 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 +248 -56
- package/dist/index.mjs +248 -56
- package/package.json +2 -2
- 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 +15 -156
- package/utils/walker.ts +392 -0
package/dist/index.mjs
CHANGED
|
@@ -2,59 +2,49 @@ 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
8
|
import MagicString from 'magic-string';
|
|
9
9
|
|
|
10
10
|
const virtualFileId = "mp-weixin-back-helper";
|
|
11
11
|
|
|
12
|
+
const pageContainerComp = ' <page-container :show="__MP_BACK_SHOW_PAGE_CONTAINER__" :overlay="false" @beforeleave="onBeforeLeave" :z-index="1" :duration="false"></page-container>\n';
|
|
12
13
|
function isArrowFunction(func) {
|
|
13
14
|
if (typeof func !== "function")
|
|
14
15
|
return false;
|
|
15
16
|
return !func.hasOwnProperty("prototype") && func.toString().includes("=>");
|
|
16
17
|
}
|
|
17
|
-
|
|
18
|
-
try {
|
|
19
|
-
return parse(code).descriptor;
|
|
20
|
-
} catch (error) {
|
|
21
|
-
throw new Error(`\u89E3\u6790vue\u6587\u4EF6\u5931\u8D25\uFF0C\u8BF7\u68C0\u67E5\u6587\u4EF6\u662F\u5426\u6B63\u786E`);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
async function transformVueFile(code, id) {
|
|
25
|
-
const sfc = await parseSFC(code);
|
|
26
|
-
if (!sfc.template?.content) {
|
|
27
|
-
return code;
|
|
28
|
-
}
|
|
29
|
-
const componentStr = ' <page-container :show="__MP_BACK_SHOW_PAGE_CONTAINER__" :overlay="false" @beforeleave="onBeforeLeave" :z-index="1" :duration="false"></page-container>\n';
|
|
30
|
-
let pageBackConfig = { ...this.config };
|
|
31
|
-
let hasPageBack = false;
|
|
32
|
-
let hasImportRef = false;
|
|
33
|
-
let pageBackFnName = "onPageBack";
|
|
34
|
-
let callbackCode = ``;
|
|
18
|
+
function compositionWalk(context, code, sfc, id) {
|
|
35
19
|
const codeMs = new MagicString(code);
|
|
36
|
-
const
|
|
37
|
-
|
|
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
|
+
};
|
|
38
28
|
if (setupAst) {
|
|
39
29
|
walkAST(setupAst, {
|
|
40
30
|
enter(node) {
|
|
41
31
|
if (node.type === "ImportDeclaration") {
|
|
42
32
|
if (node.source.value.includes(virtualFileId)) {
|
|
43
33
|
const importSpecifier = node.specifiers[0];
|
|
44
|
-
hasPageBack = true;
|
|
45
|
-
pageBackFnName = importSpecifier.local.name;
|
|
34
|
+
pageInfo.hasPageBack = true;
|
|
35
|
+
pageInfo.pageBackFnName = importSpecifier.local.name;
|
|
46
36
|
}
|
|
47
37
|
if (node.source.value === "vue") {
|
|
48
38
|
node.specifiers.some((specifier) => {
|
|
49
39
|
if (specifier.local.name === "ref") {
|
|
50
|
-
hasImportRef = true;
|
|
40
|
+
pageInfo.hasImportRef = true;
|
|
51
41
|
return true;
|
|
52
42
|
}
|
|
53
43
|
return false;
|
|
54
44
|
});
|
|
55
45
|
}
|
|
56
46
|
}
|
|
57
|
-
if (node.type === "ExpressionStatement" && node.expression.type === "CallExpression" && node.expression.callee.loc?.identifierName === pageBackFnName) {
|
|
47
|
+
if (node.type === "ExpressionStatement" && node.expression.type === "CallExpression" && node.expression.callee.loc?.identifierName === pageInfo.pageBackFnName) {
|
|
58
48
|
const callback = node.expression.arguments[0];
|
|
59
49
|
const backArguments = node.expression.arguments[1];
|
|
60
50
|
if (backArguments?.type === "ObjectExpression") {
|
|
@@ -62,12 +52,12 @@ async function transformVueFile(code, id) {
|
|
|
62
52
|
// @ts-ignore
|
|
63
53
|
`return (${(generate.default ? generate.default : generate)(backArguments).code});`
|
|
64
54
|
)();
|
|
65
|
-
Object.assign(
|
|
55
|
+
Object.assign(pageInfo.backConfig, config);
|
|
66
56
|
}
|
|
67
57
|
if (callback && (callback.type === "ArrowFunctionExpression" || callback.type === "FunctionExpression")) {
|
|
68
58
|
const body = callback.body;
|
|
69
59
|
if (body.type === "BlockStatement") {
|
|
70
|
-
callbackCode += body.body.map(
|
|
60
|
+
pageInfo.callbackCode += body.body.map(
|
|
71
61
|
// @ts-ignore
|
|
72
62
|
(statement) => (generate.default ? generate.default : generate)(statement).code
|
|
73
63
|
).join("");
|
|
@@ -77,66 +67,267 @@ async function transformVueFile(code, id) {
|
|
|
77
67
|
}
|
|
78
68
|
});
|
|
79
69
|
}
|
|
80
|
-
if (!hasPageBack)
|
|
81
|
-
return;
|
|
82
|
-
this.log.devLog(`\u9875\u9762${this.getPageById(id)}\u6CE8\u5165mp-weixin-back`);
|
|
70
|
+
if (!pageInfo.hasPageBack)
|
|
71
|
+
return code;
|
|
83
72
|
if (code.includes("<page-container")) {
|
|
84
|
-
|
|
73
|
+
context.log.debugLog(`${context.getPageById(id)}\u9875\u9762\u5DF2\u6709page-container\u7EC4\u4EF6\uFF0C\u6CE8\u5165\u5931\u8D25`);
|
|
85
74
|
return code;
|
|
86
75
|
}
|
|
87
|
-
if (!
|
|
88
|
-
callbackCode +=
|
|
76
|
+
if (!pageInfo.backConfig.preventDefault) {
|
|
77
|
+
pageInfo.callbackCode += "uni.navigateBack({ delta: 1 });";
|
|
89
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);";
|
|
90
82
|
const configBack = (() => {
|
|
91
|
-
const onPageBack =
|
|
83
|
+
const onPageBack = pageInfo.backConfig.onPageBack;
|
|
92
84
|
if (!onPageBack)
|
|
93
85
|
return "";
|
|
94
86
|
if (typeof onPageBack !== "function") {
|
|
95
87
|
throw new Error("`onPageBack` must be a function");
|
|
96
88
|
}
|
|
97
|
-
const params = JSON.stringify({ page:
|
|
89
|
+
const params = JSON.stringify({ page: context.getPageById(id) });
|
|
98
90
|
if (isArrowFunction(onPageBack) || onPageBack.toString().includes("function")) {
|
|
99
91
|
return `(${onPageBack})(${params});`;
|
|
100
92
|
}
|
|
101
93
|
return `(function ${onPageBack})()`;
|
|
102
94
|
})();
|
|
103
|
-
const
|
|
104
|
-
${!hasImportRef ? "import { ref } from 'vue'" : ""}
|
|
105
|
-
let __MP_BACK_FREQUENCY__ = 1
|
|
106
|
-
const __MP_BACK_SHOW_PAGE_CONTAINER__ = ref(true);
|
|
95
|
+
const stateBeforeLeave = `
|
|
107
96
|
const onBeforeLeave = () => {
|
|
108
|
-
|
|
109
|
-
if (__MP_BACK_FREQUENCY__ < ${pageBackConfig.frequency}) {
|
|
97
|
+
if (__MP_BACK_FREQUENCY__ < ${pageInfo.backConfig.frequency}) {
|
|
110
98
|
__MP_BACK_SHOW_PAGE_CONTAINER__.value = false
|
|
111
99
|
setTimeout(() => __MP_BACK_SHOW_PAGE_CONTAINER__.value = true, 0);
|
|
112
100
|
__MP_BACK_FREQUENCY__++
|
|
113
101
|
}
|
|
114
102
|
${configBack}
|
|
115
|
-
${callbackCode}
|
|
103
|
+
${pageInfo.callbackCode}
|
|
116
104
|
};
|
|
117
105
|
`;
|
|
118
|
-
const { template,
|
|
106
|
+
const { template, scriptSetup } = sfc;
|
|
119
107
|
const tempOffsets = {
|
|
120
108
|
start: template.loc.start.offset,
|
|
121
109
|
end: template.loc.end.offset,
|
|
122
110
|
content: template.content
|
|
123
111
|
};
|
|
124
112
|
const templateMagicString = new MagicString(tempOffsets.content);
|
|
125
|
-
templateMagicString.append(
|
|
113
|
+
templateMagicString.append(pageContainerComp);
|
|
126
114
|
codeMs.overwrite(tempOffsets.start, tempOffsets.end, templateMagicString.toString());
|
|
127
|
-
const scriptSfc = script || scriptSetup;
|
|
128
|
-
if (!scriptSfc)
|
|
129
|
-
return;
|
|
130
115
|
const scriptOffsets = {
|
|
131
|
-
start:
|
|
132
|
-
end:
|
|
133
|
-
content:
|
|
116
|
+
start: scriptSetup.loc.start.offset,
|
|
117
|
+
end: scriptSetup.loc.end.offset,
|
|
118
|
+
content: scriptSetup.content || ""
|
|
134
119
|
};
|
|
135
120
|
const scriptMagicString = new MagicString(scriptOffsets.content);
|
|
136
|
-
scriptMagicString.prepend(
|
|
121
|
+
scriptMagicString.prepend(
|
|
122
|
+
` ${importRefFromVue}
|
|
123
|
+
${stateFrequency}
|
|
124
|
+
${statePageContainerVar}
|
|
125
|
+
${stateBeforeLeave} `
|
|
126
|
+
);
|
|
137
127
|
codeMs.overwrite(scriptOffsets.start, scriptOffsets.end, scriptMagicString.toString());
|
|
138
128
|
return codeMs.toString();
|
|
139
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);
|
|
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"
|
|
249
|
+
);
|
|
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
|
+
}
|
|
330
|
+
}
|
|
140
331
|
|
|
141
332
|
var __defProp = Object.defineProperty;
|
|
142
333
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
@@ -146,18 +337,19 @@ var __publicField = (obj, key, value) => {
|
|
|
146
337
|
};
|
|
147
338
|
class pageContext {
|
|
148
339
|
constructor(config) {
|
|
340
|
+
__publicField(this, "logPreText", "[mp-weixin-back] : ");
|
|
149
341
|
__publicField(this, "config");
|
|
150
342
|
__publicField(this, "pages", []);
|
|
151
343
|
__publicField(this, "log", {
|
|
152
344
|
info: (text) => {
|
|
153
|
-
console.log(white(text));
|
|
345
|
+
console.log(white(this.logPreText + text));
|
|
154
346
|
},
|
|
155
347
|
error: (text) => {
|
|
156
|
-
console.log(red(text));
|
|
348
|
+
console.log(red(this.logPreText + text));
|
|
157
349
|
},
|
|
158
|
-
|
|
350
|
+
debugLog: (text) => {
|
|
159
351
|
if (this.config.mode === "development" && this.config.debug) {
|
|
160
|
-
console.log(green(text));
|
|
352
|
+
console.log(green(this.logPreText + text));
|
|
161
353
|
}
|
|
162
354
|
}
|
|
163
355
|
});
|
|
@@ -196,7 +388,7 @@ class pageContext {
|
|
|
196
388
|
}
|
|
197
389
|
} catch (error) {
|
|
198
390
|
this.log.error("\u8BFB\u53D6pages.json\u6587\u4EF6\u5931\u8D25");
|
|
199
|
-
this.log.
|
|
391
|
+
this.log.debugLog(String(error));
|
|
200
392
|
}
|
|
201
393
|
}
|
|
202
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",
|
|
@@ -64,4 +64,4 @@
|
|
|
64
64
|
"unbuild": "^2.0.0",
|
|
65
65
|
"vue": "^3.5.13"
|
|
66
66
|
}
|
|
67
|
-
}
|
|
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
|
})
|