mp-weixin-back 0.0.16 → 0.0.18

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.
@@ -1,64 +0,0 @@
1
- import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest'
2
- import { mount } from '@vue/test-utils'
3
- import IndexSetup from './data/index-setup.vue'
4
- import IndexUtils from './data/index-utils.vue'
5
- import IndexDefault from './data/index-default.vue'
6
-
7
- describe('generate page-container components', () => {
8
- let logSpy: ReturnType<typeof vi.spyOn>
9
-
10
- beforeEach(() => {
11
- logSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
12
- })
13
-
14
- afterEach(() => {
15
- logSpy.mockRestore()
16
- })
17
-
18
- describe('setup compositionAPI', () => {
19
- it('default case', async () => {
20
- const wrapper = mount(IndexSetup)
21
- await wrapper.vm.$nextTick()
22
-
23
- const pageContainerRef = wrapper.find('page-container')
24
- expect(pageContainerRef.exists()).toBe(true)
25
- expect(wrapper.vm.__MP_BACK_SHOW_PAGE_CONTAINER__).toBe(true)
26
- expect(typeof wrapper.vm.onBeforeLeave).toBe('function')
27
- })
28
-
29
- it('utils case', async () => {
30
- const wrapper = mount(IndexUtils)
31
- await wrapper.vm.$nextTick()
32
-
33
- const pageContainerRef = wrapper.find('page-container')
34
- expect(pageContainerRef.exists()).toBe(true)
35
- expect(wrapper.vm.__MP_BACK_SHOW_PAGE_CONTAINER__).toBe(false)
36
- expect(typeof wrapper.vm.onBeforeLeave).toBe('function')
37
-
38
- await wrapper.find('#button2').trigger('click')
39
- await new Promise(resolve => setTimeout(resolve, 0))
40
-
41
- expect(logSpy).toHaveBeenCalledWith('执行了activeMpBack')
42
-
43
- expect(wrapper.vm.__MP_BACK_SHOW_PAGE_CONTAINER__).toBe(true)
44
-
45
- await wrapper.find('#button').trigger('click')
46
- await new Promise(resolve => setTimeout(resolve, 0))
47
-
48
- expect(wrapper.vm.__MP_BACK_SHOW_PAGE_CONTAINER__).toBe(false)
49
- })
50
- })
51
-
52
- describe('optionsAPI', () => {
53
- it('default case', async () => {
54
- const wrapper = mount(IndexDefault)
55
- await wrapper.vm.$nextTick()
56
-
57
- const pageContainerRef = wrapper.find('page-container')
58
- expect(pageContainerRef.exists()).toBe(true)
59
- expect(wrapper.vm.__MP_BACK_SHOW_PAGE_CONTAINER__).toBe(true)
60
- expect(wrapper.vm.__MP_BACK_FREQUENCY__).toBe(1)
61
- expect(wrapper.vm.onBeforeLeave()).toBe(true)
62
- })
63
- })
64
- })
package/tsconfig.json DELETED
@@ -1,15 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "es2018",
4
- "lib": ["esnext"],
5
- "types": ["node"],
6
- "module": "esnext",
7
- "moduleResolution": "node",
8
- "resolveJsonModule": true,
9
- "strict": true,
10
- "strictNullChecks": true,
11
- "esModuleInterop": true,
12
- "skipDefaultLibCheck": true,
13
- "skipLibCheck": true
14
- }
15
- }
package/types/index.ts DELETED
@@ -1,80 +0,0 @@
1
- /**
2
- * Parameters passed to the onPageBack callback.
3
- */
4
- export type BackParams = {
5
- /**
6
- * The path of the current page that triggered the back event.
7
- * @example 'pages/index/index'
8
- */
9
- page: string
10
- }
11
-
12
- /**
13
- * Per-page options for `onPageBack()`.
14
- */
15
- export type OnPageBackOptions = {
16
- /**
17
- * Whether to block the default back navigation for this page.
18
- * When `true`, the user stays on the current page and only the callback fires.
19
- * @default false
20
- * @example
21
- * onPageBack(() => showDialog(), { preventDefault: true })
22
- */
23
- preventDefault: boolean
24
-
25
- /**
26
- * How many times to intercept the back event before allowing through.
27
- * Set to a large number (e.g. `9999`) for persistent interception.
28
- * @default 1
29
- * @example
30
- * // Block 3 times, then allow back
31
- * onPageBack(() => {}, { frequency: 3 })
32
- */
33
- frequency: number
34
-
35
- /**
36
- * Whether to start listening immediately when the page mounts.
37
- * Set to `false` to start disabled and enable manually via `activeMpBack()`.
38
- * @default true
39
- * @example
40
- * // Start disabled, enable after user action
41
- * onPageBack(() => {}, { initialValue: false })
42
- * activeMpBack()
43
- */
44
- initialValue: boolean
45
- }
46
-
47
- /**
48
- * Global plugin options passed to `mpBackPlugin()` in `vite.config.ts`.
49
- */
50
- export type Config = OnPageBackOptions & {
51
- /**
52
- * Enable debug logging in development mode.
53
- * @default false
54
- */
55
- debug: boolean
56
-
57
- /**
58
- * Global callback fired every time a back event is detected on any page.
59
- * Page-level callbacks registered via `onPageBack()` run in addition to this.
60
- * @example
61
- * mpBackPlugin({
62
- * onPageBack: ({ page }) => console.log('back on:', page)
63
- * })
64
- */
65
- onPageBack?: (params: BackParams) => void
66
- }
67
-
68
- export type UserOptions = Partial<Config>
69
-
70
- export type ContextConfig = Config & {
71
- mode: string
72
- root: string
73
- }
74
-
75
- type Pages = { path: string }[]
76
-
77
- export type PagesJson = {
78
- pages: Pages
79
- subpackages: { root: string; pages: Pages }[]
80
- }
package/utils/constant.ts DELETED
@@ -1,2 +0,0 @@
1
- export const virtualFileId = 'mp-weixin-back-helper'
2
-
package/utils/index.ts DELETED
@@ -1,62 +0,0 @@
1
- import { createRequire } from 'module'
2
- import { pageContext } from '../src/context'
3
- import { vueWalker } from './walker'
4
-
5
- let compilerPromise: Promise<typeof import('@vue/compiler-sfc')> | null = null
6
-
7
- async function resolveCompiler(root: string): Promise<typeof import('@vue/compiler-sfc')> {
8
- // 避免重复解析(防止并发调用时的竞态条件)
9
- if (compilerPromise) {
10
- return compilerPromise
11
- }
12
-
13
- compilerPromise = (async () => {
14
- // 尝试加载用户项目中的 @vue/compiler-sfc
15
- try {
16
- const _require = createRequire(import.meta.url)
17
- // 尝试从用户根目录解析
18
- const compilerPath = _require.resolve('@vue/compiler-sfc', { paths: [root] })
19
- return _require(compilerPath) as typeof import('@vue/compiler-sfc')
20
- } catch (firstError) {
21
- try {
22
- // 降级尝试直接 import
23
- return await import('@vue/compiler-sfc')
24
- } catch (secondError) {
25
- throw new Error(
26
- `[mp-weixin-back] Cannot resolve @vue/compiler-sfc.\n` +
27
- `This plugin requires @vue/compiler-sfc to be installed in your project.\n` +
28
- `Fix: pnpm add -D @vue/compiler-sfc\n` +
29
- `Docs: https://github.com/DBAAZzz/mp-weixin-back#%EF%B8%8F-vite-配置\n`
30
- )
31
- }
32
- }
33
- })()
34
-
35
- return compilerPromise
36
- }
37
-
38
- export async function transformVueFile(this: pageContext, code: string, id: string) {
39
- try {
40
- const sfcCompiler = await resolveCompiler(this.config.root)
41
- const sfc = sfcCompiler.parse(code).descriptor
42
- const { template, script, scriptSetup } = sfc
43
- if (!template?.content) {
44
- return code
45
- }
46
-
47
- if (!script?.content && !scriptSetup?.content) {
48
- return code
49
- }
50
-
51
- // 判断页面是否为组合式写法
52
- const walker = scriptSetup ? 'compositionWalk' : 'optionsWalk'
53
- return vueWalker[walker](this, code, sfc, id)
54
- } catch (error) {
55
- this.log.error(
56
- `Failed to transform ${id}. Please check the file is a valid Vue SFC.\n` +
57
- ` Docs: https://github.com/DBAAZzz/mp-weixin-back#-快速开始`
58
- )
59
- this.log.error(String(error))
60
- return code
61
- }
62
- }