From e50aff5015710ee394b48e2812fb6221c93ff6c5 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Mon, 27 Dec 2021 16:24:16 +0800 Subject: [PATCH] feat(mp): support onShareAppMessage,onShareTimeline,onPageScroll in setup script (#3097) (#3099) (question/136994) --- .../uni-cli-shared/src/json/mp/jsonFile.ts | 4 ++ packages/uni-mp-alipay/dist/uni.mp.esm.js | 22 ++++++++++- .../uni-mp-alipay/src/runtime/createPage.ts | 7 ++-- packages/uni-mp-baidu/dist/uni.mp.esm.js | 22 ++++++++++- packages/uni-mp-core/src/index.ts | 3 +- .../uni-mp-core/src/runtime/componentHooks.ts | 20 +++++++++- packages/uni-mp-core/src/runtime/page.ts | 11 ++++-- packages/uni-mp-kuaishou/dist/uni.mp.esm.js | 22 ++++++++++- packages/uni-mp-lark/dist/uni.mp.esm.js | 22 ++++++++++- packages/uni-mp-qq/dist/uni.mp.esm.js | 22 ++++++++++- packages/uni-mp-toutiao/dist/uni.mp.esm.js | 22 ++++++++++- packages/uni-mp-vite/src/index.ts | 2 + packages/uni-mp-vite/src/plugin/index.ts | 12 ++++++ .../uni-mp-vite/src/plugins/runtimeHooks.ts | 39 +++++++++++++++++++ packages/uni-mp-weixin/dist/uni.mp.esm.js | 22 ++++++++++- .../uni-quickapp-webview/dist/uni.mp.esm.js | 22 ++++++++++- packages/uni-shared/dist/uni-shared.cjs.js | 8 +++- packages/uni-shared/dist/uni-shared.d.ts | 6 +++ packages/uni-shared/dist/uni-shared.es.js | 9 ++++- packages/uni-shared/src/lifecycle.ts | 6 +++ packages/uni-stat/dist/uni-stat.cjs.js | 2 +- packages/uni-stat/dist/uni-stat.es.js | 2 +- 22 files changed, 278 insertions(+), 29 deletions(-) create mode 100644 packages/uni-mp-vite/src/plugins/runtimeHooks.ts diff --git a/packages/uni-cli-shared/src/json/mp/jsonFile.ts b/packages/uni-cli-shared/src/json/mp/jsonFile.ts index b115c3fa6..2e5a1842f 100644 --- a/packages/uni-cli-shared/src/json/mp/jsonFile.ts +++ b/packages/uni-cli-shared/src/json/mp/jsonFile.ts @@ -35,6 +35,10 @@ export function hasJsonFile(filename: string) { ) } +export function getComponentJsonFilenames() { + return [...jsonComponentsCache.keys()] +} + export function findJsonFile(filename: string) { if (filename === 'app') { return appJsonCache diff --git a/packages/uni-mp-alipay/dist/uni.mp.esm.js b/packages/uni-mp-alipay/dist/uni.mp.esm.js index 3cd9d61ba..436a55b41 100644 --- a/packages/uni-mp-alipay/dist/uni.mp.esm.js +++ b/packages/uni-mp-alipay/dist/uni.mp.esm.js @@ -122,6 +122,12 @@ class EventChannel { } } +const MINI_PROGRAM_PAGE_RUNTIME_HOOKS = { + onPageScroll: 1, + onShareAppMessage: 1 << 1, + onShareTimeline: 1 << 2, +}; + const eventChannels = {}; const eventChannelStack = []; function getEventChannel(id) { @@ -236,7 +242,7 @@ function callHook(name, args) { return hooks && invokeArrayFns(hooks, args); } -const PAGE_HOOKS = [ +const PAGE_INIT_HOOKS = [ ON_LOAD, ON_SHOW, ON_HIDE, @@ -283,6 +289,17 @@ function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) { } function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) { findHooks(vueOptions).forEach((hook) => initHook(mpOptions, hook, excludes)); +} +function initRuntimeHooks(mpOptions, runtimeHooks) { + if (!runtimeHooks) { + return; + } + const hooks = Object.keys(MINI_PROGRAM_PAGE_RUNTIME_HOOKS); + hooks.forEach((hook) => { + if (runtimeHooks & MINI_PROGRAM_PAGE_RUNTIME_HOOKS[hook]) { + initHook(mpOptions, hook, []); + } + }); } my.appLaunchHooks = []; @@ -878,8 +895,9 @@ function initCreatePage() { if (__VUE_OPTIONS_API__) { pageOptions.data = initData(); } - initHooks(pageOptions, PAGE_HOOKS); + initHooks(pageOptions, PAGE_INIT_HOOKS); initUnknownHooks(pageOptions, vueOptions); + initRuntimeHooks(pageOptions, vueOptions.__runtimeHooks); initWxsCallMethods(pageOptions, vueOptions.wxsCallMethods); return Page(pageOptions); }; diff --git a/packages/uni-mp-alipay/src/runtime/createPage.ts b/packages/uni-mp-alipay/src/runtime/createPage.ts index 6bac84e49..e6a360709 100644 --- a/packages/uni-mp-alipay/src/runtime/createPage.ts +++ b/packages/uni-mp-alipay/src/runtime/createPage.ts @@ -1,12 +1,13 @@ import { ComponentOptions } from 'vue' import { - PAGE_HOOKS, + PAGE_INIT_HOOKS, initData, initHooks, initUnknownHooks, $destroyComponent, initWxsCallMethods, + initRuntimeHooks, } from '@dcloudio/uni-mp-core' import { @@ -65,9 +66,9 @@ export function initCreatePage() { if (__VUE_OPTIONS_API__) { pageOptions.data = initData(vueOptions) } - initHooks(pageOptions, PAGE_HOOKS) + initHooks(pageOptions, PAGE_INIT_HOOKS) initUnknownHooks(pageOptions, vueOptions) - + initRuntimeHooks(pageOptions, vueOptions.__runtimeHooks) initWxsCallMethods( pageOptions as WechatMiniprogram.Component.MethodOption, vueOptions.wxsCallMethods diff --git a/packages/uni-mp-baidu/dist/uni.mp.esm.js b/packages/uni-mp-baidu/dist/uni.mp.esm.js index b41141be9..7576e376b 100644 --- a/packages/uni-mp-baidu/dist/uni.mp.esm.js +++ b/packages/uni-mp-baidu/dist/uni.mp.esm.js @@ -190,6 +190,12 @@ class EventChannel { } } +const MINI_PROGRAM_PAGE_RUNTIME_HOOKS = { + onPageScroll: 1, + onShareAppMessage: 1 << 1, + onShareTimeline: 1 << 2, +}; + const eventChannels = {}; const eventChannelStack = []; function getEventChannel(id) { @@ -299,7 +305,7 @@ function callHook(name, args) { return hooks && invokeArrayFns(hooks, args); } -const PAGE_HOOKS = [ +const PAGE_INIT_HOOKS = [ ON_LOAD, ON_SHOW, ON_HIDE, @@ -346,6 +352,17 @@ function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) { } function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) { findHooks(vueOptions).forEach((hook) => initHook$1(mpOptions, hook, excludes)); +} +function initRuntimeHooks(mpOptions, runtimeHooks) { + if (!runtimeHooks) { + return; + } + const hooks = Object.keys(MINI_PROGRAM_PAGE_RUNTIME_HOOKS); + hooks.forEach((hook) => { + if (runtimeHooks & MINI_PROGRAM_PAGE_RUNTIME_HOOKS[hook]) { + initHook$1(mpOptions, hook, []); + } + }); } swan.appLaunchHooks = []; @@ -891,8 +908,9 @@ function parsePage(vueOptions, parseOptions) { }; return this.$vm && this.$vm.$callHook(ON_LOAD, query); }; - initHooks(methods, PAGE_HOOKS); + initHooks(methods, PAGE_INIT_HOOKS); initUnknownHooks(methods, vueOptions); + initRuntimeHooks(methods, vueOptions.__runtimeHooks); parse && parse(miniProgramPageOptions, { handleLink }); return miniProgramPageOptions; } diff --git a/packages/uni-mp-core/src/index.ts b/packages/uni-mp-core/src/index.ts index f6cdff8d3..05a92ad09 100644 --- a/packages/uni-mp-core/src/index.ts +++ b/packages/uni-mp-core/src/index.ts @@ -24,9 +24,10 @@ export { } from './runtime/componentOptions' export { initProps } from './runtime/componentProps' export { - PAGE_HOOKS, initHooks, initUnknownHooks, + initRuntimeHooks, + PAGE_INIT_HOOKS, } from './runtime/componentHooks' export { initMocks, initComponentInstance } from './runtime/componentInstance' export { $createComponent, $destroyComponent } from './runtime/component' diff --git a/packages/uni-mp-core/src/runtime/componentHooks.ts b/packages/uni-mp-core/src/runtime/componentHooks.ts index b662ea89c..08144a733 100644 --- a/packages/uni-mp-core/src/runtime/componentHooks.ts +++ b/packages/uni-mp-core/src/runtime/componentHooks.ts @@ -1,4 +1,5 @@ import { + MINI_PROGRAM_PAGE_RUNTIME_HOOKS, ON_ADD_TO_FAVORITES, ON_HIDE, ON_LOAD, @@ -18,7 +19,7 @@ import { MiniProgramAppOptions } from '../index' import { CustomAppInstanceProperty } from './app' import { CustomComponentInstanceProperty } from './component' -export const PAGE_HOOKS = [ +export const PAGE_INIT_HOOKS = [ ON_LOAD, ON_SHOW, ON_HIDE, @@ -95,3 +96,20 @@ export function initUnknownHooks( ) { findHooks(vueOptions).forEach((hook) => initHook(mpOptions, hook, excludes)) } + +export function initRuntimeHooks( + mpOptions: MiniProgramAppOptions | WechatMiniprogram.Component.MethodOption, + runtimeHooks?: number +) { + if (!runtimeHooks) { + return + } + const hooks = Object.keys( + MINI_PROGRAM_PAGE_RUNTIME_HOOKS + ) as (keyof typeof MINI_PROGRAM_PAGE_RUNTIME_HOOKS)[] + hooks.forEach((hook) => { + if (runtimeHooks & MINI_PROGRAM_PAGE_RUNTIME_HOOKS[hook]) { + initHook(mpOptions, hook, []) + } + }) +} diff --git a/packages/uni-mp-core/src/runtime/page.ts b/packages/uni-mp-core/src/runtime/page.ts index 8af56e7a5..5aa10e968 100644 --- a/packages/uni-mp-core/src/runtime/page.ts +++ b/packages/uni-mp-core/src/runtime/page.ts @@ -7,7 +7,12 @@ import { parseComponent, CustomComponentInstanceProperty, } from './component' -import { PAGE_HOOKS, initHooks, initUnknownHooks } from './componentHooks' +import { + PAGE_INIT_HOOKS, + initHooks, + initUnknownHooks, + initRuntimeHooks, +} from './componentHooks' import { initPageProps } from './componentProps' function parsePage( @@ -43,9 +48,9 @@ function parsePage( return this.$vm && this.$vm.$callHook(ON_LOAD, query) } - initHooks(methods, PAGE_HOOKS) + initHooks(methods, PAGE_INIT_HOOKS) initUnknownHooks(methods, vueOptions) - + initRuntimeHooks(methods, vueOptions.__runtimeHooks) parse && parse(miniProgramPageOptions, { handleLink }) return miniProgramPageOptions diff --git a/packages/uni-mp-kuaishou/dist/uni.mp.esm.js b/packages/uni-mp-kuaishou/dist/uni.mp.esm.js index 5177333fa..02c77b2ab 100644 --- a/packages/uni-mp-kuaishou/dist/uni.mp.esm.js +++ b/packages/uni-mp-kuaishou/dist/uni.mp.esm.js @@ -185,6 +185,12 @@ class EventChannel { } } +const MINI_PROGRAM_PAGE_RUNTIME_HOOKS = { + onPageScroll: 1, + onShareAppMessage: 1 << 1, + onShareTimeline: 1 << 2, +}; + const eventChannels = {}; const eventChannelStack = []; function getEventChannel(id) { @@ -294,7 +300,7 @@ function callHook(name, args) { return hooks && invokeArrayFns(hooks, args); } -const PAGE_HOOKS = [ +const PAGE_INIT_HOOKS = [ ON_LOAD, ON_SHOW, ON_HIDE, @@ -341,6 +347,17 @@ function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) { } function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) { findHooks(vueOptions).forEach((hook) => initHook$1(mpOptions, hook, excludes)); +} +function initRuntimeHooks(mpOptions, runtimeHooks) { + if (!runtimeHooks) { + return; + } + const hooks = Object.keys(MINI_PROGRAM_PAGE_RUNTIME_HOOKS); + hooks.forEach((hook) => { + if (runtimeHooks & MINI_PROGRAM_PAGE_RUNTIME_HOOKS[hook]) { + initHook$1(mpOptions, hook, []); + } + }); } ks.appLaunchHooks = []; @@ -875,8 +892,9 @@ function parsePage(vueOptions, parseOptions) { }; return this.$vm && this.$vm.$callHook(ON_LOAD, query); }; - initHooks(methods, PAGE_HOOKS); + initHooks(methods, PAGE_INIT_HOOKS); initUnknownHooks(methods, vueOptions); + initRuntimeHooks(methods, vueOptions.__runtimeHooks); parse && parse(miniProgramPageOptions, { handleLink }); return miniProgramPageOptions; } diff --git a/packages/uni-mp-lark/dist/uni.mp.esm.js b/packages/uni-mp-lark/dist/uni.mp.esm.js index c97e9e905..bb9dd1b2c 100644 --- a/packages/uni-mp-lark/dist/uni.mp.esm.js +++ b/packages/uni-mp-lark/dist/uni.mp.esm.js @@ -185,6 +185,12 @@ class EventChannel { } } +const MINI_PROGRAM_PAGE_RUNTIME_HOOKS = { + onPageScroll: 1, + onShareAppMessage: 1 << 1, + onShareTimeline: 1 << 2, +}; + const eventChannels = {}; const eventChannelStack = []; function getEventChannel(id) { @@ -290,7 +296,7 @@ function callHook(name, args) { return hooks && invokeArrayFns(hooks, args); } -const PAGE_HOOKS = [ +const PAGE_INIT_HOOKS = [ ON_LOAD, ON_SHOW, ON_HIDE, @@ -340,6 +346,17 @@ function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) { } function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) { findHooks(vueOptions).forEach((hook) => initHook$1(mpOptions, hook, excludes)); +} +function initRuntimeHooks(mpOptions, runtimeHooks) { + if (!runtimeHooks) { + return; + } + const hooks = Object.keys(MINI_PROGRAM_PAGE_RUNTIME_HOOKS); + hooks.forEach((hook) => { + if (runtimeHooks & MINI_PROGRAM_PAGE_RUNTIME_HOOKS[hook]) { + initHook$1(mpOptions, hook, []); + } + }); } tt.appLaunchHooks = []; @@ -848,8 +865,9 @@ function parsePage(vueOptions, parseOptions) { }; return this.$vm && this.$vm.$callHook(ON_LOAD, query); }; - initHooks(methods, PAGE_HOOKS); + initHooks(methods, PAGE_INIT_HOOKS); initUnknownHooks(methods, vueOptions); + initRuntimeHooks(methods, vueOptions.__runtimeHooks); parse && parse(miniProgramPageOptions, { handleLink }); return miniProgramPageOptions; } diff --git a/packages/uni-mp-qq/dist/uni.mp.esm.js b/packages/uni-mp-qq/dist/uni.mp.esm.js index 16a1e2227..08cb7cf40 100644 --- a/packages/uni-mp-qq/dist/uni.mp.esm.js +++ b/packages/uni-mp-qq/dist/uni.mp.esm.js @@ -185,6 +185,12 @@ class EventChannel { } } +const MINI_PROGRAM_PAGE_RUNTIME_HOOKS = { + onPageScroll: 1, + onShareAppMessage: 1 << 1, + onShareTimeline: 1 << 2, +}; + const eventChannels = {}; const eventChannelStack = []; function getEventChannel(id) { @@ -290,7 +296,7 @@ function callHook(name, args) { return hooks && invokeArrayFns(hooks, args); } -const PAGE_HOOKS = [ +const PAGE_INIT_HOOKS = [ ON_LOAD, ON_SHOW, ON_HIDE, @@ -337,6 +343,17 @@ function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) { } function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) { findHooks(vueOptions).forEach((hook) => initHook$1(mpOptions, hook, excludes)); +} +function initRuntimeHooks(mpOptions, runtimeHooks) { + if (!runtimeHooks) { + return; + } + const hooks = Object.keys(MINI_PROGRAM_PAGE_RUNTIME_HOOKS); + hooks.forEach((hook) => { + if (runtimeHooks & MINI_PROGRAM_PAGE_RUNTIME_HOOKS[hook]) { + initHook$1(mpOptions, hook, []); + } + }); } qq.appLaunchHooks = []; @@ -837,8 +854,9 @@ function parsePage(vueOptions, parseOptions) { }; return this.$vm && this.$vm.$callHook(ON_LOAD, query); }; - initHooks(methods, PAGE_HOOKS); + initHooks(methods, PAGE_INIT_HOOKS); initUnknownHooks(methods, vueOptions); + initRuntimeHooks(methods, vueOptions.__runtimeHooks); parse && parse(miniProgramPageOptions, { handleLink }); return miniProgramPageOptions; } diff --git a/packages/uni-mp-toutiao/dist/uni.mp.esm.js b/packages/uni-mp-toutiao/dist/uni.mp.esm.js index ded399721..14d3d84e7 100644 --- a/packages/uni-mp-toutiao/dist/uni.mp.esm.js +++ b/packages/uni-mp-toutiao/dist/uni.mp.esm.js @@ -185,6 +185,12 @@ class EventChannel { } } +const MINI_PROGRAM_PAGE_RUNTIME_HOOKS = { + onPageScroll: 1, + onShareAppMessage: 1 << 1, + onShareTimeline: 1 << 2, +}; + const eventChannels = {}; const eventChannelStack = []; function getEventChannel(id) { @@ -290,7 +296,7 @@ function callHook(name, args) { return hooks && invokeArrayFns(hooks, args); } -const PAGE_HOOKS = [ +const PAGE_INIT_HOOKS = [ ON_LOAD, ON_SHOW, ON_HIDE, @@ -340,6 +346,17 @@ function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) { } function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) { findHooks(vueOptions).forEach((hook) => initHook$1(mpOptions, hook, excludes)); +} +function initRuntimeHooks(mpOptions, runtimeHooks) { + if (!runtimeHooks) { + return; + } + const hooks = Object.keys(MINI_PROGRAM_PAGE_RUNTIME_HOOKS); + hooks.forEach((hook) => { + if (runtimeHooks & MINI_PROGRAM_PAGE_RUNTIME_HOOKS[hook]) { + initHook$1(mpOptions, hook, []); + } + }); } tt.appLaunchHooks = []; @@ -848,8 +865,9 @@ function parsePage(vueOptions, parseOptions) { }; return this.$vm && this.$vm.$callHook(ON_LOAD, query); }; - initHooks(methods, PAGE_HOOKS); + initHooks(methods, PAGE_INIT_HOOKS); initUnknownHooks(methods, vueOptions); + initRuntimeHooks(methods, vueOptions.__runtimeHooks); parse && parse(miniProgramPageOptions, { handleLink }); return miniProgramPageOptions; } diff --git a/packages/uni-mp-vite/src/index.ts b/packages/uni-mp-vite/src/index.ts index 2e240b0cc..55768c0e7 100644 --- a/packages/uni-mp-vite/src/index.ts +++ b/packages/uni-mp-vite/src/index.ts @@ -10,6 +10,7 @@ import { uniPagesJsonPlugin } from './plugins/pagesJson' import { uniEntryPlugin } from './plugins/entry' import { uniRenderjsPlugin } from './plugins/renderjs' +import { uniRuntimeHooksPlugin } from './plugins/runtimeHooks' import { uniSubpackagePlugin } from './plugins/subpackage' import { uniMiniProgramPluginPlugin } from './plugins/plugin' @@ -32,6 +33,7 @@ export default (options: UniMiniProgramPluginOptions) => { uniEntryPlugin(options), uniViteInjectPlugin(extend({}, options.vite.inject)), uniRenderjsPlugin({ lang: options.template.filter?.lang }), + uniRuntimeHooksPlugin(), uniMiniProgramPlugin(options), (options: { vueOptions?: { script?: Partial } diff --git a/packages/uni-mp-vite/src/plugin/index.ts b/packages/uni-mp-vite/src/plugin/index.ts index 235f75d30..edc61aacb 100644 --- a/packages/uni-mp-vite/src/plugin/index.ts +++ b/packages/uni-mp-vite/src/plugin/index.ts @@ -8,6 +8,7 @@ import { parseManifestJsonOnce, findMiniProgramTemplateFiles, MiniProgramCompilerOptions, + getComponentJsonFilenames, } from '@dcloudio/uni-cli-shared' import type { CompilerOptions } from '@dcloudio/uni-mp-compiler' @@ -22,6 +23,7 @@ import type { SFCTemplateCompileOptions, SFCTemplateCompileResults, } from '@vue/compiler-sfc' +import { hasOwn } from '@vue/shared' export interface UniMiniProgramPluginOptions { cdn?: number @@ -153,6 +155,16 @@ export function uniMiniProgramPlugin( source: templateFiles[filename], }) }) + // 部分组件可能模板为空 + getComponentJsonFilenames().forEach((jsonFilename) => { + if (!hasOwn(templateFiles, jsonFilename)) { + this.emitFile({ + type: 'asset', + fileName: jsonFilename + template.extname, + source: ``, + }) + } + }) if (!nvueCssEmitted) { const nvueCssPaths = getNVueCssPaths(resolvedConfig) if (nvueCssPaths && nvueCssPaths.length) { diff --git a/packages/uni-mp-vite/src/plugins/runtimeHooks.ts b/packages/uni-mp-vite/src/plugins/runtimeHooks.ts new file mode 100644 index 000000000..8413e74b9 --- /dev/null +++ b/packages/uni-mp-vite/src/plugins/runtimeHooks.ts @@ -0,0 +1,39 @@ +import type { Plugin } from 'vite' +import { MINI_PROGRAM_PAGE_RUNTIME_HOOKS } from '@dcloudio/uni-shared' +import { isUniPageSfcFile } from '@dcloudio/uni-cli-shared' + +type RuntimeHooks = keyof typeof MINI_PROGRAM_PAGE_RUNTIME_HOOKS + +export function uniRuntimeHooksPlugin(): Plugin { + return { + name: 'vite:uni-mp-runtime-hooks', + enforce: 'post', + async transform(source, id) { + if (!isUniPageSfcFile(id)) { + return null + } + if (!source.includes('_sfc_main')) { + return null + } + + const matches = source.match( + new RegExp( + `(${Object.keys(MINI_PROGRAM_PAGE_RUNTIME_HOOKS).join('|')})`, + 'g' + ) + ) + if (!matches) { + return null + } + if (matches.includes('onShareTimeline')) { + matches.push('onShareAppMessage') + } + const hooks = new Set(matches as RuntimeHooks[]) + let flag = 0 + for (const hook of hooks) { + flag |= MINI_PROGRAM_PAGE_RUNTIME_HOOKS[hook] + } + return source + `;_sfc_main.__runtimeHooks = ${flag};` + }, + } +} diff --git a/packages/uni-mp-weixin/dist/uni.mp.esm.js b/packages/uni-mp-weixin/dist/uni.mp.esm.js index fdeb607d6..f3cf156b7 100644 --- a/packages/uni-mp-weixin/dist/uni.mp.esm.js +++ b/packages/uni-mp-weixin/dist/uni.mp.esm.js @@ -57,6 +57,12 @@ const invokeArrayFns = (fns, arg) => { return ret; }; +const MINI_PROGRAM_PAGE_RUNTIME_HOOKS = { + onPageScroll: 1, + onShareAppMessage: 1 << 1, + onShareTimeline: 1 << 2, +}; + const eventChannels = {}; const eventChannelStack = []; function getEventChannel(id) { @@ -162,7 +168,7 @@ function callHook(name, args) { return hooks && invokeArrayFns(hooks, args); } -const PAGE_HOOKS = [ +const PAGE_INIT_HOOKS = [ ON_LOAD, ON_SHOW, ON_HIDE, @@ -209,6 +215,17 @@ function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) { } function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) { findHooks(vueOptions).forEach((hook) => initHook$1(mpOptions, hook, excludes)); +} +function initRuntimeHooks(mpOptions, runtimeHooks) { + if (!runtimeHooks) { + return; + } + const hooks = Object.keys(MINI_PROGRAM_PAGE_RUNTIME_HOOKS); + hooks.forEach((hook) => { + if (runtimeHooks & MINI_PROGRAM_PAGE_RUNTIME_HOOKS[hook]) { + initHook$1(mpOptions, hook, []); + } + }); } wx.appLaunchHooks = []; @@ -709,8 +726,9 @@ function parsePage(vueOptions, parseOptions) { }; return this.$vm && this.$vm.$callHook(ON_LOAD, query); }; - initHooks(methods, PAGE_HOOKS); + initHooks(methods, PAGE_INIT_HOOKS); initUnknownHooks(methods, vueOptions); + initRuntimeHooks(methods, vueOptions.__runtimeHooks); parse && parse(miniProgramPageOptions, { handleLink }); return miniProgramPageOptions; } diff --git a/packages/uni-quickapp-webview/dist/uni.mp.esm.js b/packages/uni-quickapp-webview/dist/uni.mp.esm.js index 8df645dfb..6c05815eb 100644 --- a/packages/uni-quickapp-webview/dist/uni.mp.esm.js +++ b/packages/uni-quickapp-webview/dist/uni.mp.esm.js @@ -185,6 +185,12 @@ class EventChannel { } } +const MINI_PROGRAM_PAGE_RUNTIME_HOOKS = { + onPageScroll: 1, + onShareAppMessage: 1 << 1, + onShareTimeline: 1 << 2, +}; + const eventChannels = {}; const eventChannelStack = []; function getEventChannel(id) { @@ -290,7 +296,7 @@ function callHook(name, args) { return hooks && invokeArrayFns(hooks, args); } -const PAGE_HOOKS = [ +const PAGE_INIT_HOOKS = [ ON_LOAD, ON_SHOW, ON_HIDE, @@ -337,6 +343,17 @@ function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) { } function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) { findHooks(vueOptions).forEach((hook) => initHook$1(mpOptions, hook, excludes)); +} +function initRuntimeHooks(mpOptions, runtimeHooks) { + if (!runtimeHooks) { + return; + } + const hooks = Object.keys(MINI_PROGRAM_PAGE_RUNTIME_HOOKS); + hooks.forEach((hook) => { + if (runtimeHooks & MINI_PROGRAM_PAGE_RUNTIME_HOOKS[hook]) { + initHook$1(mpOptions, hook, []); + } + }); } qa.appLaunchHooks = []; @@ -826,8 +843,9 @@ function parsePage(vueOptions, parseOptions) { }; return this.$vm && this.$vm.$callHook(ON_LOAD, query); }; - initHooks(methods, PAGE_HOOKS); + initHooks(methods, PAGE_INIT_HOOKS); initUnknownHooks(methods, vueOptions); + initRuntimeHooks(methods, vueOptions.__runtimeHooks); parse && parse(miniProgramPageOptions, { handleLink }); return miniProgramPageOptions; } diff --git a/packages/uni-shared/dist/uni-shared.cjs.js b/packages/uni-shared/dist/uni-shared.cjs.js index e2637c45a..2af23c4a6 100644 --- a/packages/uni-shared/dist/uni-shared.cjs.js +++ b/packages/uni-shared/dist/uni-shared.cjs.js @@ -1201,7 +1201,12 @@ const UniLifecycleHooks = [ ON_NAVIGATION_BAR_SEARCH_INPUT_CHANGED, ON_NAVIGATION_BAR_SEARCH_INPUT_CONFIRMED, ON_NAVIGATION_BAR_SEARCH_INPUT_FOCUS_CHANGED, -]; +]; +const MINI_PROGRAM_PAGE_RUNTIME_HOOKS = { + onPageScroll: 1, + onShareAppMessage: 1 << 1, + onShareTimeline: 1 << 2, +}; function getEnvLocale() { const { env } = process; @@ -1242,6 +1247,7 @@ exports.EventModifierFlags = EventModifierFlags; exports.I18N_JSON_DELIMITERS = I18N_JSON_DELIMITERS; exports.JSON_PROTOCOL = JSON_PROTOCOL; exports.LINEFEED = LINEFEED; +exports.MINI_PROGRAM_PAGE_RUNTIME_HOOKS = MINI_PROGRAM_PAGE_RUNTIME_HOOKS; exports.NAVBAR_HEIGHT = NAVBAR_HEIGHT; exports.NODE_TYPE_COMMENT = NODE_TYPE_COMMENT; exports.NODE_TYPE_ELEMENT = NODE_TYPE_ELEMENT; diff --git a/packages/uni-shared/dist/uni-shared.d.ts b/packages/uni-shared/dist/uni-shared.d.ts index 60b459c5a..7279fc281 100644 --- a/packages/uni-shared/dist/uni-shared.d.ts +++ b/packages/uni-shared/dist/uni-shared.d.ts @@ -258,6 +258,12 @@ export declare const JSON_PROTOCOL = "json://"; export declare const LINEFEED = "\n"; +export declare const MINI_PROGRAM_PAGE_RUNTIME_HOOKS: { + readonly onPageScroll: 1; + readonly onShareAppMessage: number; + readonly onShareTimeline: number; +}; + export declare const NAVBAR_HEIGHT = 44; declare type NavigateToOptionEvents = Record void>; diff --git a/packages/uni-shared/dist/uni-shared.es.js b/packages/uni-shared/dist/uni-shared.es.js index d006ca45a..ff08bc3cb 100644 --- a/packages/uni-shared/dist/uni-shared.es.js +++ b/packages/uni-shared/dist/uni-shared.es.js @@ -1197,7 +1197,12 @@ const UniLifecycleHooks = [ ON_NAVIGATION_BAR_SEARCH_INPUT_CHANGED, ON_NAVIGATION_BAR_SEARCH_INPUT_CONFIRMED, ON_NAVIGATION_BAR_SEARCH_INPUT_FOCUS_CHANGED, -]; +]; +const MINI_PROGRAM_PAGE_RUNTIME_HOOKS = { + onPageScroll: 1, + onShareAppMessage: 1 << 1, + onShareTimeline: 1 << 2, +}; function getEnvLocale() { const { env } = process; @@ -1205,4 +1210,4 @@ function getEnvLocale() { return (lang && lang.replace(/[.:].*/, '')) || 'en'; } -export { ACTION_TYPE_ADD_EVENT, ACTION_TYPE_ADD_WXS_EVENT, ACTION_TYPE_CREATE, ACTION_TYPE_EVENT, ACTION_TYPE_INSERT, ACTION_TYPE_PAGE_CREATE, ACTION_TYPE_PAGE_CREATED, ACTION_TYPE_PAGE_SCROLL, ACTION_TYPE_REMOVE, ACTION_TYPE_REMOVE_ATTRIBUTE, ACTION_TYPE_REMOVE_EVENT, ACTION_TYPE_SET_ATTRIBUTE, ACTION_TYPE_SET_TEXT, ATTR_CHANGE_PREFIX, ATTR_CLASS, ATTR_INNER_HTML, ATTR_STYLE, ATTR_TEXT_CONTENT, ATTR_V_OWNER_ID, ATTR_V_RENDERJS, ATTR_V_SHOW, BACKGROUND_COLOR, BUILT_IN_TAGS, BUILT_IN_TAG_NAMES, COMPONENT_NAME_PREFIX, COMPONENT_PREFIX, COMPONENT_SELECTOR_PREFIX, DATA_RE, EventChannel, EventModifierFlags, I18N_JSON_DELIMITERS, JSON_PROTOCOL, LINEFEED, NAVBAR_HEIGHT, NODE_TYPE_COMMENT, NODE_TYPE_ELEMENT, NODE_TYPE_PAGE, NODE_TYPE_TEXT, NVueTextNode, ON_ADD_TO_FAVORITES, ON_APP_ENTER_BACKGROUND, ON_APP_ENTER_FOREGROUND, ON_BACK_PRESS, ON_ERROR, ON_HIDE, ON_KEYBOARD_HEIGHT_CHANGE, ON_LAUNCH, ON_LOAD, ON_NAVIGATION_BAR_BUTTON_TAP, ON_NAVIGATION_BAR_SEARCH_INPUT_CHANGED, ON_NAVIGATION_BAR_SEARCH_INPUT_CLICKED, ON_NAVIGATION_BAR_SEARCH_INPUT_CONFIRMED, ON_NAVIGATION_BAR_SEARCH_INPUT_FOCUS_CHANGED, ON_PAGE_NOT_FOUND, ON_PAGE_SCROLL, ON_PULL_DOWN_REFRESH, ON_REACH_BOTTOM, ON_REACH_BOTTOM_DISTANCE, ON_READY, ON_RESIZE, ON_SHARE_APP_MESSAGE, ON_SHARE_TIMELINE, ON_SHOW, ON_TAB_ITEM_TAP, ON_THEME_CHANGE, ON_UNHANDLE_REJECTION, ON_UNLOAD, ON_WEB_INVOKE_APP_SERVICE, ON_WXS_INVOKE_CALL_METHOD, PLUS_RE, PRIMARY_COLOR, RENDERJS_MODULES, RESPONSIVE_MIN_WIDTH, SCHEME_RE, SELECTED_COLOR, SLOT_DEFAULT_NAME, TABBAR_HEIGHT, TAGS, UNI_SSR, UNI_SSR_DATA, UNI_SSR_GLOBAL_DATA, UNI_SSR_STORE, UNI_SSR_TITLE, UNI_STORAGE_LOCALE, UniBaseNode, UniCommentNode, UniElement, UniEvent, UniInputElement, UniLifecycleHooks, UniNode, UniTextAreaElement, UniTextNode, WEB_INVOKE_APPSERVICE, WXS_MODULES, WXS_PROTOCOL, addFont, addLeadingSlash, cache, cacheStringFunction, callOptions, createIsCustomElement, createRpx2Unit, createUniEvent, debounce, decode, decodedQuery, defaultMiniProgramRpx2Unit, defaultRpx2Unit, dynamicSlotName, forcePatchProp, formatAppLog, formatDateTime, formatH5Log, formatLog, getCustomDataset, getEnvLocale, getLen, getValueByDataPath, initCustomDataset, invokeArrayFns, isAppNativeTag, isBuiltInComponent, isComponentInternalInstance, isComponentTag, isH5CustomElement, isH5NativeTag, isMiniProgramNativeTag, isRootHook, normalizeDataset, normalizeEventType, normalizeTarget, once, parseEventName, parseQuery, parseUrl, passive, plusReady, removeLeadingSlash, resolveComponentInstance, resolveOwnerEl, resolveOwnerVm, sanitise, scrollTo, stringifyQuery, updateElementStyle }; +export { ACTION_TYPE_ADD_EVENT, ACTION_TYPE_ADD_WXS_EVENT, ACTION_TYPE_CREATE, ACTION_TYPE_EVENT, ACTION_TYPE_INSERT, ACTION_TYPE_PAGE_CREATE, ACTION_TYPE_PAGE_CREATED, ACTION_TYPE_PAGE_SCROLL, ACTION_TYPE_REMOVE, ACTION_TYPE_REMOVE_ATTRIBUTE, ACTION_TYPE_REMOVE_EVENT, ACTION_TYPE_SET_ATTRIBUTE, ACTION_TYPE_SET_TEXT, ATTR_CHANGE_PREFIX, ATTR_CLASS, ATTR_INNER_HTML, ATTR_STYLE, ATTR_TEXT_CONTENT, ATTR_V_OWNER_ID, ATTR_V_RENDERJS, ATTR_V_SHOW, BACKGROUND_COLOR, BUILT_IN_TAGS, BUILT_IN_TAG_NAMES, COMPONENT_NAME_PREFIX, COMPONENT_PREFIX, COMPONENT_SELECTOR_PREFIX, DATA_RE, EventChannel, EventModifierFlags, I18N_JSON_DELIMITERS, JSON_PROTOCOL, LINEFEED, MINI_PROGRAM_PAGE_RUNTIME_HOOKS, NAVBAR_HEIGHT, NODE_TYPE_COMMENT, NODE_TYPE_ELEMENT, NODE_TYPE_PAGE, NODE_TYPE_TEXT, NVueTextNode, ON_ADD_TO_FAVORITES, ON_APP_ENTER_BACKGROUND, ON_APP_ENTER_FOREGROUND, ON_BACK_PRESS, ON_ERROR, ON_HIDE, ON_KEYBOARD_HEIGHT_CHANGE, ON_LAUNCH, ON_LOAD, ON_NAVIGATION_BAR_BUTTON_TAP, ON_NAVIGATION_BAR_SEARCH_INPUT_CHANGED, ON_NAVIGATION_BAR_SEARCH_INPUT_CLICKED, ON_NAVIGATION_BAR_SEARCH_INPUT_CONFIRMED, ON_NAVIGATION_BAR_SEARCH_INPUT_FOCUS_CHANGED, ON_PAGE_NOT_FOUND, ON_PAGE_SCROLL, ON_PULL_DOWN_REFRESH, ON_REACH_BOTTOM, ON_REACH_BOTTOM_DISTANCE, ON_READY, ON_RESIZE, ON_SHARE_APP_MESSAGE, ON_SHARE_TIMELINE, ON_SHOW, ON_TAB_ITEM_TAP, ON_THEME_CHANGE, ON_UNHANDLE_REJECTION, ON_UNLOAD, ON_WEB_INVOKE_APP_SERVICE, ON_WXS_INVOKE_CALL_METHOD, PLUS_RE, PRIMARY_COLOR, RENDERJS_MODULES, RESPONSIVE_MIN_WIDTH, SCHEME_RE, SELECTED_COLOR, SLOT_DEFAULT_NAME, TABBAR_HEIGHT, TAGS, UNI_SSR, UNI_SSR_DATA, UNI_SSR_GLOBAL_DATA, UNI_SSR_STORE, UNI_SSR_TITLE, UNI_STORAGE_LOCALE, UniBaseNode, UniCommentNode, UniElement, UniEvent, UniInputElement, UniLifecycleHooks, UniNode, UniTextAreaElement, UniTextNode, WEB_INVOKE_APPSERVICE, WXS_MODULES, WXS_PROTOCOL, addFont, addLeadingSlash, cache, cacheStringFunction, callOptions, createIsCustomElement, createRpx2Unit, createUniEvent, debounce, decode, decodedQuery, defaultMiniProgramRpx2Unit, defaultRpx2Unit, dynamicSlotName, forcePatchProp, formatAppLog, formatDateTime, formatH5Log, formatLog, getCustomDataset, getEnvLocale, getLen, getValueByDataPath, initCustomDataset, invokeArrayFns, isAppNativeTag, isBuiltInComponent, isComponentInternalInstance, isComponentTag, isH5CustomElement, isH5NativeTag, isMiniProgramNativeTag, isRootHook, normalizeDataset, normalizeEventType, normalizeTarget, once, parseEventName, parseQuery, parseUrl, passive, plusReady, removeLeadingSlash, resolveComponentInstance, resolveOwnerEl, resolveOwnerVm, sanitise, scrollTo, stringifyQuery, updateElementStyle }; diff --git a/packages/uni-shared/src/lifecycle.ts b/packages/uni-shared/src/lifecycle.ts index 496c70cb8..73b4c1cbc 100644 --- a/packages/uni-shared/src/lifecycle.ts +++ b/packages/uni-shared/src/lifecycle.ts @@ -75,3 +75,9 @@ export const UniLifecycleHooks = [ ON_NAVIGATION_BAR_SEARCH_INPUT_CONFIRMED, ON_NAVIGATION_BAR_SEARCH_INPUT_FOCUS_CHANGED, ] as const + +export const MINI_PROGRAM_PAGE_RUNTIME_HOOKS = { + onPageScroll: 1, + onShareAppMessage: 1 << 1, + onShareTimeline: 1 << 2, +} as const diff --git a/packages/uni-stat/dist/uni-stat.cjs.js b/packages/uni-stat/dist/uni-stat.cjs.js index 4af6d924c..76202c843 100644 --- a/packages/uni-stat/dist/uni-stat.cjs.js +++ b/packages/uni-stat/dist/uni-stat.cjs.js @@ -1,6 +1,6 @@ 'use strict'; -var version = "3.0.0-alpha-3030220211217012"; +var version = "3.0.0-alpha-3030320211224001"; const STAT_VERSION = version; const STAT_URL = 'https://tongji.dcloud.io/uni/stat'; diff --git a/packages/uni-stat/dist/uni-stat.es.js b/packages/uni-stat/dist/uni-stat.es.js index bbc0f31c5..d63b1dba6 100644 --- a/packages/uni-stat/dist/uni-stat.es.js +++ b/packages/uni-stat/dist/uni-stat.es.js @@ -1,4 +1,4 @@ -var version = "3.0.0-alpha-3030220211217012"; +var version = "3.0.0-alpha-3030320211224001"; const STAT_VERSION = version; const STAT_URL = 'https://tongji.dcloud.io/uni/stat'; -- GitLab