提交 e50aff50 编写于 作者: fxy060608's avatar fxy060608

feat(mp): support onShareAppMessage,onShareTimeline,onPageScroll in setup...

feat(mp): support onShareAppMessage,onShareTimeline,onPageScroll in setup script (#3097) (#3099) (question/136994)
上级 56305f54
...@@ -35,6 +35,10 @@ export function hasJsonFile(filename: string) { ...@@ -35,6 +35,10 @@ export function hasJsonFile(filename: string) {
) )
} }
export function getComponentJsonFilenames() {
return [...jsonComponentsCache.keys()]
}
export function findJsonFile(filename: string) { export function findJsonFile(filename: string) {
if (filename === 'app') { if (filename === 'app') {
return appJsonCache return appJsonCache
......
...@@ -122,6 +122,12 @@ class EventChannel { ...@@ -122,6 +122,12 @@ class EventChannel {
} }
} }
const MINI_PROGRAM_PAGE_RUNTIME_HOOKS = {
onPageScroll: 1,
onShareAppMessage: 1 << 1,
onShareTimeline: 1 << 2,
};
const eventChannels = {}; const eventChannels = {};
const eventChannelStack = []; const eventChannelStack = [];
function getEventChannel(id) { function getEventChannel(id) {
...@@ -236,7 +242,7 @@ function callHook(name, args) { ...@@ -236,7 +242,7 @@ function callHook(name, args) {
return hooks && invokeArrayFns(hooks, args); return hooks && invokeArrayFns(hooks, args);
} }
const PAGE_HOOKS = [ const PAGE_INIT_HOOKS = [
ON_LOAD, ON_LOAD,
ON_SHOW, ON_SHOW,
ON_HIDE, ON_HIDE,
...@@ -283,6 +289,17 @@ function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) { ...@@ -283,6 +289,17 @@ function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) {
} }
function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) { function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) {
findHooks(vueOptions).forEach((hook) => initHook(mpOptions, hook, excludes)); 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 = []; my.appLaunchHooks = [];
...@@ -878,8 +895,9 @@ function initCreatePage() { ...@@ -878,8 +895,9 @@ function initCreatePage() {
if (__VUE_OPTIONS_API__) { if (__VUE_OPTIONS_API__) {
pageOptions.data = initData(); pageOptions.data = initData();
} }
initHooks(pageOptions, PAGE_HOOKS); initHooks(pageOptions, PAGE_INIT_HOOKS);
initUnknownHooks(pageOptions, vueOptions); initUnknownHooks(pageOptions, vueOptions);
initRuntimeHooks(pageOptions, vueOptions.__runtimeHooks);
initWxsCallMethods(pageOptions, vueOptions.wxsCallMethods); initWxsCallMethods(pageOptions, vueOptions.wxsCallMethods);
return Page(pageOptions); return Page(pageOptions);
}; };
......
import { ComponentOptions } from 'vue' import { ComponentOptions } from 'vue'
import { import {
PAGE_HOOKS, PAGE_INIT_HOOKS,
initData, initData,
initHooks, initHooks,
initUnknownHooks, initUnknownHooks,
$destroyComponent, $destroyComponent,
initWxsCallMethods, initWxsCallMethods,
initRuntimeHooks,
} from '@dcloudio/uni-mp-core' } from '@dcloudio/uni-mp-core'
import { import {
...@@ -65,9 +66,9 @@ export function initCreatePage() { ...@@ -65,9 +66,9 @@ export function initCreatePage() {
if (__VUE_OPTIONS_API__) { if (__VUE_OPTIONS_API__) {
pageOptions.data = initData(vueOptions) pageOptions.data = initData(vueOptions)
} }
initHooks(pageOptions, PAGE_HOOKS) initHooks(pageOptions, PAGE_INIT_HOOKS)
initUnknownHooks(pageOptions, vueOptions) initUnknownHooks(pageOptions, vueOptions)
initRuntimeHooks(pageOptions, vueOptions.__runtimeHooks)
initWxsCallMethods( initWxsCallMethods(
pageOptions as WechatMiniprogram.Component.MethodOption, pageOptions as WechatMiniprogram.Component.MethodOption,
vueOptions.wxsCallMethods vueOptions.wxsCallMethods
......
...@@ -190,6 +190,12 @@ class EventChannel { ...@@ -190,6 +190,12 @@ class EventChannel {
} }
} }
const MINI_PROGRAM_PAGE_RUNTIME_HOOKS = {
onPageScroll: 1,
onShareAppMessage: 1 << 1,
onShareTimeline: 1 << 2,
};
const eventChannels = {}; const eventChannels = {};
const eventChannelStack = []; const eventChannelStack = [];
function getEventChannel(id) { function getEventChannel(id) {
...@@ -299,7 +305,7 @@ function callHook(name, args) { ...@@ -299,7 +305,7 @@ function callHook(name, args) {
return hooks && invokeArrayFns(hooks, args); return hooks && invokeArrayFns(hooks, args);
} }
const PAGE_HOOKS = [ const PAGE_INIT_HOOKS = [
ON_LOAD, ON_LOAD,
ON_SHOW, ON_SHOW,
ON_HIDE, ON_HIDE,
...@@ -346,6 +352,17 @@ function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) { ...@@ -346,6 +352,17 @@ function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) {
} }
function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) { function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) {
findHooks(vueOptions).forEach((hook) => initHook$1(mpOptions, hook, excludes)); 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 = []; swan.appLaunchHooks = [];
...@@ -891,8 +908,9 @@ function parsePage(vueOptions, parseOptions) { ...@@ -891,8 +908,9 @@ function parsePage(vueOptions, parseOptions) {
}; };
return this.$vm && this.$vm.$callHook(ON_LOAD, query); return this.$vm && this.$vm.$callHook(ON_LOAD, query);
}; };
initHooks(methods, PAGE_HOOKS); initHooks(methods, PAGE_INIT_HOOKS);
initUnknownHooks(methods, vueOptions); initUnknownHooks(methods, vueOptions);
initRuntimeHooks(methods, vueOptions.__runtimeHooks);
parse && parse(miniProgramPageOptions, { handleLink }); parse && parse(miniProgramPageOptions, { handleLink });
return miniProgramPageOptions; return miniProgramPageOptions;
} }
......
...@@ -24,9 +24,10 @@ export { ...@@ -24,9 +24,10 @@ export {
} from './runtime/componentOptions' } from './runtime/componentOptions'
export { initProps } from './runtime/componentProps' export { initProps } from './runtime/componentProps'
export { export {
PAGE_HOOKS,
initHooks, initHooks,
initUnknownHooks, initUnknownHooks,
initRuntimeHooks,
PAGE_INIT_HOOKS,
} from './runtime/componentHooks' } from './runtime/componentHooks'
export { initMocks, initComponentInstance } from './runtime/componentInstance' export { initMocks, initComponentInstance } from './runtime/componentInstance'
export { $createComponent, $destroyComponent } from './runtime/component' export { $createComponent, $destroyComponent } from './runtime/component'
......
import { import {
MINI_PROGRAM_PAGE_RUNTIME_HOOKS,
ON_ADD_TO_FAVORITES, ON_ADD_TO_FAVORITES,
ON_HIDE, ON_HIDE,
ON_LOAD, ON_LOAD,
...@@ -18,7 +19,7 @@ import { MiniProgramAppOptions } from '../index' ...@@ -18,7 +19,7 @@ import { MiniProgramAppOptions } from '../index'
import { CustomAppInstanceProperty } from './app' import { CustomAppInstanceProperty } from './app'
import { CustomComponentInstanceProperty } from './component' import { CustomComponentInstanceProperty } from './component'
export const PAGE_HOOKS = [ export const PAGE_INIT_HOOKS = [
ON_LOAD, ON_LOAD,
ON_SHOW, ON_SHOW,
ON_HIDE, ON_HIDE,
...@@ -95,3 +96,20 @@ export function initUnknownHooks( ...@@ -95,3 +96,20 @@ export function initUnknownHooks(
) { ) {
findHooks(vueOptions).forEach((hook) => initHook(mpOptions, hook, excludes)) 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, [])
}
})
}
...@@ -7,7 +7,12 @@ import { ...@@ -7,7 +7,12 @@ import {
parseComponent, parseComponent,
CustomComponentInstanceProperty, CustomComponentInstanceProperty,
} from './component' } from './component'
import { PAGE_HOOKS, initHooks, initUnknownHooks } from './componentHooks' import {
PAGE_INIT_HOOKS,
initHooks,
initUnknownHooks,
initRuntimeHooks,
} from './componentHooks'
import { initPageProps } from './componentProps' import { initPageProps } from './componentProps'
function parsePage( function parsePage(
...@@ -43,9 +48,9 @@ function parsePage( ...@@ -43,9 +48,9 @@ function parsePage(
return this.$vm && this.$vm.$callHook(ON_LOAD, query) return this.$vm && this.$vm.$callHook(ON_LOAD, query)
} }
initHooks(methods, PAGE_HOOKS) initHooks(methods, PAGE_INIT_HOOKS)
initUnknownHooks(methods, vueOptions) initUnknownHooks(methods, vueOptions)
initRuntimeHooks(methods, vueOptions.__runtimeHooks)
parse && parse(miniProgramPageOptions, { handleLink }) parse && parse(miniProgramPageOptions, { handleLink })
return miniProgramPageOptions return miniProgramPageOptions
......
...@@ -185,6 +185,12 @@ class EventChannel { ...@@ -185,6 +185,12 @@ class EventChannel {
} }
} }
const MINI_PROGRAM_PAGE_RUNTIME_HOOKS = {
onPageScroll: 1,
onShareAppMessage: 1 << 1,
onShareTimeline: 1 << 2,
};
const eventChannels = {}; const eventChannels = {};
const eventChannelStack = []; const eventChannelStack = [];
function getEventChannel(id) { function getEventChannel(id) {
...@@ -294,7 +300,7 @@ function callHook(name, args) { ...@@ -294,7 +300,7 @@ function callHook(name, args) {
return hooks && invokeArrayFns(hooks, args); return hooks && invokeArrayFns(hooks, args);
} }
const PAGE_HOOKS = [ const PAGE_INIT_HOOKS = [
ON_LOAD, ON_LOAD,
ON_SHOW, ON_SHOW,
ON_HIDE, ON_HIDE,
...@@ -341,6 +347,17 @@ function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) { ...@@ -341,6 +347,17 @@ function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) {
} }
function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) { function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) {
findHooks(vueOptions).forEach((hook) => initHook$1(mpOptions, hook, excludes)); 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 = []; ks.appLaunchHooks = [];
...@@ -875,8 +892,9 @@ function parsePage(vueOptions, parseOptions) { ...@@ -875,8 +892,9 @@ function parsePage(vueOptions, parseOptions) {
}; };
return this.$vm && this.$vm.$callHook(ON_LOAD, query); return this.$vm && this.$vm.$callHook(ON_LOAD, query);
}; };
initHooks(methods, PAGE_HOOKS); initHooks(methods, PAGE_INIT_HOOKS);
initUnknownHooks(methods, vueOptions); initUnknownHooks(methods, vueOptions);
initRuntimeHooks(methods, vueOptions.__runtimeHooks);
parse && parse(miniProgramPageOptions, { handleLink }); parse && parse(miniProgramPageOptions, { handleLink });
return miniProgramPageOptions; return miniProgramPageOptions;
} }
......
...@@ -185,6 +185,12 @@ class EventChannel { ...@@ -185,6 +185,12 @@ class EventChannel {
} }
} }
const MINI_PROGRAM_PAGE_RUNTIME_HOOKS = {
onPageScroll: 1,
onShareAppMessage: 1 << 1,
onShareTimeline: 1 << 2,
};
const eventChannels = {}; const eventChannels = {};
const eventChannelStack = []; const eventChannelStack = [];
function getEventChannel(id) { function getEventChannel(id) {
...@@ -290,7 +296,7 @@ function callHook(name, args) { ...@@ -290,7 +296,7 @@ function callHook(name, args) {
return hooks && invokeArrayFns(hooks, args); return hooks && invokeArrayFns(hooks, args);
} }
const PAGE_HOOKS = [ const PAGE_INIT_HOOKS = [
ON_LOAD, ON_LOAD,
ON_SHOW, ON_SHOW,
ON_HIDE, ON_HIDE,
...@@ -340,6 +346,17 @@ function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) { ...@@ -340,6 +346,17 @@ function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) {
} }
function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) { function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) {
findHooks(vueOptions).forEach((hook) => initHook$1(mpOptions, hook, excludes)); 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 = []; tt.appLaunchHooks = [];
...@@ -848,8 +865,9 @@ function parsePage(vueOptions, parseOptions) { ...@@ -848,8 +865,9 @@ function parsePage(vueOptions, parseOptions) {
}; };
return this.$vm && this.$vm.$callHook(ON_LOAD, query); return this.$vm && this.$vm.$callHook(ON_LOAD, query);
}; };
initHooks(methods, PAGE_HOOKS); initHooks(methods, PAGE_INIT_HOOKS);
initUnknownHooks(methods, vueOptions); initUnknownHooks(methods, vueOptions);
initRuntimeHooks(methods, vueOptions.__runtimeHooks);
parse && parse(miniProgramPageOptions, { handleLink }); parse && parse(miniProgramPageOptions, { handleLink });
return miniProgramPageOptions; return miniProgramPageOptions;
} }
......
...@@ -185,6 +185,12 @@ class EventChannel { ...@@ -185,6 +185,12 @@ class EventChannel {
} }
} }
const MINI_PROGRAM_PAGE_RUNTIME_HOOKS = {
onPageScroll: 1,
onShareAppMessage: 1 << 1,
onShareTimeline: 1 << 2,
};
const eventChannels = {}; const eventChannels = {};
const eventChannelStack = []; const eventChannelStack = [];
function getEventChannel(id) { function getEventChannel(id) {
...@@ -290,7 +296,7 @@ function callHook(name, args) { ...@@ -290,7 +296,7 @@ function callHook(name, args) {
return hooks && invokeArrayFns(hooks, args); return hooks && invokeArrayFns(hooks, args);
} }
const PAGE_HOOKS = [ const PAGE_INIT_HOOKS = [
ON_LOAD, ON_LOAD,
ON_SHOW, ON_SHOW,
ON_HIDE, ON_HIDE,
...@@ -337,6 +343,17 @@ function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) { ...@@ -337,6 +343,17 @@ function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) {
} }
function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) { function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) {
findHooks(vueOptions).forEach((hook) => initHook$1(mpOptions, hook, excludes)); 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 = []; qq.appLaunchHooks = [];
...@@ -837,8 +854,9 @@ function parsePage(vueOptions, parseOptions) { ...@@ -837,8 +854,9 @@ function parsePage(vueOptions, parseOptions) {
}; };
return this.$vm && this.$vm.$callHook(ON_LOAD, query); return this.$vm && this.$vm.$callHook(ON_LOAD, query);
}; };
initHooks(methods, PAGE_HOOKS); initHooks(methods, PAGE_INIT_HOOKS);
initUnknownHooks(methods, vueOptions); initUnknownHooks(methods, vueOptions);
initRuntimeHooks(methods, vueOptions.__runtimeHooks);
parse && parse(miniProgramPageOptions, { handleLink }); parse && parse(miniProgramPageOptions, { handleLink });
return miniProgramPageOptions; return miniProgramPageOptions;
} }
......
...@@ -185,6 +185,12 @@ class EventChannel { ...@@ -185,6 +185,12 @@ class EventChannel {
} }
} }
const MINI_PROGRAM_PAGE_RUNTIME_HOOKS = {
onPageScroll: 1,
onShareAppMessage: 1 << 1,
onShareTimeline: 1 << 2,
};
const eventChannels = {}; const eventChannels = {};
const eventChannelStack = []; const eventChannelStack = [];
function getEventChannel(id) { function getEventChannel(id) {
...@@ -290,7 +296,7 @@ function callHook(name, args) { ...@@ -290,7 +296,7 @@ function callHook(name, args) {
return hooks && invokeArrayFns(hooks, args); return hooks && invokeArrayFns(hooks, args);
} }
const PAGE_HOOKS = [ const PAGE_INIT_HOOKS = [
ON_LOAD, ON_LOAD,
ON_SHOW, ON_SHOW,
ON_HIDE, ON_HIDE,
...@@ -340,6 +346,17 @@ function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) { ...@@ -340,6 +346,17 @@ function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) {
} }
function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) { function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) {
findHooks(vueOptions).forEach((hook) => initHook$1(mpOptions, hook, excludes)); 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 = []; tt.appLaunchHooks = [];
...@@ -848,8 +865,9 @@ function parsePage(vueOptions, parseOptions) { ...@@ -848,8 +865,9 @@ function parsePage(vueOptions, parseOptions) {
}; };
return this.$vm && this.$vm.$callHook(ON_LOAD, query); return this.$vm && this.$vm.$callHook(ON_LOAD, query);
}; };
initHooks(methods, PAGE_HOOKS); initHooks(methods, PAGE_INIT_HOOKS);
initUnknownHooks(methods, vueOptions); initUnknownHooks(methods, vueOptions);
initRuntimeHooks(methods, vueOptions.__runtimeHooks);
parse && parse(miniProgramPageOptions, { handleLink }); parse && parse(miniProgramPageOptions, { handleLink });
return miniProgramPageOptions; return miniProgramPageOptions;
} }
......
...@@ -10,6 +10,7 @@ import { uniPagesJsonPlugin } from './plugins/pagesJson' ...@@ -10,6 +10,7 @@ import { uniPagesJsonPlugin } from './plugins/pagesJson'
import { uniEntryPlugin } from './plugins/entry' import { uniEntryPlugin } from './plugins/entry'
import { uniRenderjsPlugin } from './plugins/renderjs' import { uniRenderjsPlugin } from './plugins/renderjs'
import { uniRuntimeHooksPlugin } from './plugins/runtimeHooks'
import { uniSubpackagePlugin } from './plugins/subpackage' import { uniSubpackagePlugin } from './plugins/subpackage'
import { uniMiniProgramPluginPlugin } from './plugins/plugin' import { uniMiniProgramPluginPlugin } from './plugins/plugin'
...@@ -32,6 +33,7 @@ export default (options: UniMiniProgramPluginOptions) => { ...@@ -32,6 +33,7 @@ export default (options: UniMiniProgramPluginOptions) => {
uniEntryPlugin(options), uniEntryPlugin(options),
uniViteInjectPlugin(extend({}, options.vite.inject)), uniViteInjectPlugin(extend({}, options.vite.inject)),
uniRenderjsPlugin({ lang: options.template.filter?.lang }), uniRenderjsPlugin({ lang: options.template.filter?.lang }),
uniRuntimeHooksPlugin(),
uniMiniProgramPlugin(options), uniMiniProgramPlugin(options),
(options: { (options: {
vueOptions?: { script?: Partial<SFCScriptCompileOptions> } vueOptions?: { script?: Partial<SFCScriptCompileOptions> }
......
...@@ -8,6 +8,7 @@ import { ...@@ -8,6 +8,7 @@ import {
parseManifestJsonOnce, parseManifestJsonOnce,
findMiniProgramTemplateFiles, findMiniProgramTemplateFiles,
MiniProgramCompilerOptions, MiniProgramCompilerOptions,
getComponentJsonFilenames,
} from '@dcloudio/uni-cli-shared' } from '@dcloudio/uni-cli-shared'
import type { CompilerOptions } from '@dcloudio/uni-mp-compiler' import type { CompilerOptions } from '@dcloudio/uni-mp-compiler'
...@@ -22,6 +23,7 @@ import type { ...@@ -22,6 +23,7 @@ import type {
SFCTemplateCompileOptions, SFCTemplateCompileOptions,
SFCTemplateCompileResults, SFCTemplateCompileResults,
} from '@vue/compiler-sfc' } from '@vue/compiler-sfc'
import { hasOwn } from '@vue/shared'
export interface UniMiniProgramPluginOptions { export interface UniMiniProgramPluginOptions {
cdn?: number cdn?: number
...@@ -153,6 +155,16 @@ export function uniMiniProgramPlugin( ...@@ -153,6 +155,16 @@ export function uniMiniProgramPlugin(
source: templateFiles[filename], source: templateFiles[filename],
}) })
}) })
// 部分组件可能模板为空
getComponentJsonFilenames().forEach((jsonFilename) => {
if (!hasOwn(templateFiles, jsonFilename)) {
this.emitFile({
type: 'asset',
fileName: jsonFilename + template.extname,
source: `<block/>`,
})
}
})
if (!nvueCssEmitted) { if (!nvueCssEmitted) {
const nvueCssPaths = getNVueCssPaths(resolvedConfig) const nvueCssPaths = getNVueCssPaths(resolvedConfig)
if (nvueCssPaths && nvueCssPaths.length) { if (nvueCssPaths && nvueCssPaths.length) {
......
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<RuntimeHooks>(matches as RuntimeHooks[])
let flag = 0
for (const hook of hooks) {
flag |= MINI_PROGRAM_PAGE_RUNTIME_HOOKS[hook]
}
return source + `;_sfc_main.__runtimeHooks = ${flag};`
},
}
}
...@@ -57,6 +57,12 @@ const invokeArrayFns = (fns, arg) => { ...@@ -57,6 +57,12 @@ const invokeArrayFns = (fns, arg) => {
return ret; return ret;
}; };
const MINI_PROGRAM_PAGE_RUNTIME_HOOKS = {
onPageScroll: 1,
onShareAppMessage: 1 << 1,
onShareTimeline: 1 << 2,
};
const eventChannels = {}; const eventChannels = {};
const eventChannelStack = []; const eventChannelStack = [];
function getEventChannel(id) { function getEventChannel(id) {
...@@ -162,7 +168,7 @@ function callHook(name, args) { ...@@ -162,7 +168,7 @@ function callHook(name, args) {
return hooks && invokeArrayFns(hooks, args); return hooks && invokeArrayFns(hooks, args);
} }
const PAGE_HOOKS = [ const PAGE_INIT_HOOKS = [
ON_LOAD, ON_LOAD,
ON_SHOW, ON_SHOW,
ON_HIDE, ON_HIDE,
...@@ -209,6 +215,17 @@ function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) { ...@@ -209,6 +215,17 @@ function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) {
} }
function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) { function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) {
findHooks(vueOptions).forEach((hook) => initHook$1(mpOptions, hook, excludes)); 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 = []; wx.appLaunchHooks = [];
...@@ -709,8 +726,9 @@ function parsePage(vueOptions, parseOptions) { ...@@ -709,8 +726,9 @@ function parsePage(vueOptions, parseOptions) {
}; };
return this.$vm && this.$vm.$callHook(ON_LOAD, query); return this.$vm && this.$vm.$callHook(ON_LOAD, query);
}; };
initHooks(methods, PAGE_HOOKS); initHooks(methods, PAGE_INIT_HOOKS);
initUnknownHooks(methods, vueOptions); initUnknownHooks(methods, vueOptions);
initRuntimeHooks(methods, vueOptions.__runtimeHooks);
parse && parse(miniProgramPageOptions, { handleLink }); parse && parse(miniProgramPageOptions, { handleLink });
return miniProgramPageOptions; return miniProgramPageOptions;
} }
......
...@@ -185,6 +185,12 @@ class EventChannel { ...@@ -185,6 +185,12 @@ class EventChannel {
} }
} }
const MINI_PROGRAM_PAGE_RUNTIME_HOOKS = {
onPageScroll: 1,
onShareAppMessage: 1 << 1,
onShareTimeline: 1 << 2,
};
const eventChannels = {}; const eventChannels = {};
const eventChannelStack = []; const eventChannelStack = [];
function getEventChannel(id) { function getEventChannel(id) {
...@@ -290,7 +296,7 @@ function callHook(name, args) { ...@@ -290,7 +296,7 @@ function callHook(name, args) {
return hooks && invokeArrayFns(hooks, args); return hooks && invokeArrayFns(hooks, args);
} }
const PAGE_HOOKS = [ const PAGE_INIT_HOOKS = [
ON_LOAD, ON_LOAD,
ON_SHOW, ON_SHOW,
ON_HIDE, ON_HIDE,
...@@ -337,6 +343,17 @@ function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) { ...@@ -337,6 +343,17 @@ function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) {
} }
function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) { function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) {
findHooks(vueOptions).forEach((hook) => initHook$1(mpOptions, hook, excludes)); 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 = []; qa.appLaunchHooks = [];
...@@ -826,8 +843,9 @@ function parsePage(vueOptions, parseOptions) { ...@@ -826,8 +843,9 @@ function parsePage(vueOptions, parseOptions) {
}; };
return this.$vm && this.$vm.$callHook(ON_LOAD, query); return this.$vm && this.$vm.$callHook(ON_LOAD, query);
}; };
initHooks(methods, PAGE_HOOKS); initHooks(methods, PAGE_INIT_HOOKS);
initUnknownHooks(methods, vueOptions); initUnknownHooks(methods, vueOptions);
initRuntimeHooks(methods, vueOptions.__runtimeHooks);
parse && parse(miniProgramPageOptions, { handleLink }); parse && parse(miniProgramPageOptions, { handleLink });
return miniProgramPageOptions; return miniProgramPageOptions;
} }
......
...@@ -1201,7 +1201,12 @@ const UniLifecycleHooks = [ ...@@ -1201,7 +1201,12 @@ const UniLifecycleHooks = [
ON_NAVIGATION_BAR_SEARCH_INPUT_CHANGED, ON_NAVIGATION_BAR_SEARCH_INPUT_CHANGED,
ON_NAVIGATION_BAR_SEARCH_INPUT_CONFIRMED, ON_NAVIGATION_BAR_SEARCH_INPUT_CONFIRMED,
ON_NAVIGATION_BAR_SEARCH_INPUT_FOCUS_CHANGED, ON_NAVIGATION_BAR_SEARCH_INPUT_FOCUS_CHANGED,
]; ];
const MINI_PROGRAM_PAGE_RUNTIME_HOOKS = {
onPageScroll: 1,
onShareAppMessage: 1 << 1,
onShareTimeline: 1 << 2,
};
function getEnvLocale() { function getEnvLocale() {
const { env } = process; const { env } = process;
...@@ -1242,6 +1247,7 @@ exports.EventModifierFlags = EventModifierFlags; ...@@ -1242,6 +1247,7 @@ exports.EventModifierFlags = EventModifierFlags;
exports.I18N_JSON_DELIMITERS = I18N_JSON_DELIMITERS; exports.I18N_JSON_DELIMITERS = I18N_JSON_DELIMITERS;
exports.JSON_PROTOCOL = JSON_PROTOCOL; exports.JSON_PROTOCOL = JSON_PROTOCOL;
exports.LINEFEED = LINEFEED; exports.LINEFEED = LINEFEED;
exports.MINI_PROGRAM_PAGE_RUNTIME_HOOKS = MINI_PROGRAM_PAGE_RUNTIME_HOOKS;
exports.NAVBAR_HEIGHT = NAVBAR_HEIGHT; exports.NAVBAR_HEIGHT = NAVBAR_HEIGHT;
exports.NODE_TYPE_COMMENT = NODE_TYPE_COMMENT; exports.NODE_TYPE_COMMENT = NODE_TYPE_COMMENT;
exports.NODE_TYPE_ELEMENT = NODE_TYPE_ELEMENT; exports.NODE_TYPE_ELEMENT = NODE_TYPE_ELEMENT;
......
...@@ -258,6 +258,12 @@ export declare const JSON_PROTOCOL = "json://"; ...@@ -258,6 +258,12 @@ export declare const JSON_PROTOCOL = "json://";
export declare const LINEFEED = "\n"; 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; export declare const NAVBAR_HEIGHT = 44;
declare type NavigateToOptionEvents = Record<string, (...args: any[]) => void>; declare type NavigateToOptionEvents = Record<string, (...args: any[]) => void>;
......
...@@ -1197,7 +1197,12 @@ const UniLifecycleHooks = [ ...@@ -1197,7 +1197,12 @@ const UniLifecycleHooks = [
ON_NAVIGATION_BAR_SEARCH_INPUT_CHANGED, ON_NAVIGATION_BAR_SEARCH_INPUT_CHANGED,
ON_NAVIGATION_BAR_SEARCH_INPUT_CONFIRMED, ON_NAVIGATION_BAR_SEARCH_INPUT_CONFIRMED,
ON_NAVIGATION_BAR_SEARCH_INPUT_FOCUS_CHANGED, ON_NAVIGATION_BAR_SEARCH_INPUT_FOCUS_CHANGED,
]; ];
const MINI_PROGRAM_PAGE_RUNTIME_HOOKS = {
onPageScroll: 1,
onShareAppMessage: 1 << 1,
onShareTimeline: 1 << 2,
};
function getEnvLocale() { function getEnvLocale() {
const { env } = process; const { env } = process;
...@@ -1205,4 +1210,4 @@ function getEnvLocale() { ...@@ -1205,4 +1210,4 @@ function getEnvLocale() {
return (lang && lang.replace(/[.:].*/, '')) || 'en'; 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 };
...@@ -75,3 +75,9 @@ export const UniLifecycleHooks = [ ...@@ -75,3 +75,9 @@ export const UniLifecycleHooks = [
ON_NAVIGATION_BAR_SEARCH_INPUT_CONFIRMED, ON_NAVIGATION_BAR_SEARCH_INPUT_CONFIRMED,
ON_NAVIGATION_BAR_SEARCH_INPUT_FOCUS_CHANGED, ON_NAVIGATION_BAR_SEARCH_INPUT_FOCUS_CHANGED,
] as const ] as const
export const MINI_PROGRAM_PAGE_RUNTIME_HOOKS = {
onPageScroll: 1,
onShareAppMessage: 1 << 1,
onShareTimeline: 1 << 2,
} as const
'use strict'; 'use strict';
var version = "3.0.0-alpha-3030220211217012"; var version = "3.0.0-alpha-3030320211224001";
const STAT_VERSION = version; const STAT_VERSION = version;
const STAT_URL = 'https://tongji.dcloud.io/uni/stat'; const STAT_URL = 'https://tongji.dcloud.io/uni/stat';
......
var version = "3.0.0-alpha-3030220211217012"; var version = "3.0.0-alpha-3030320211224001";
const STAT_VERSION = version; const STAT_VERSION = version;
const STAT_URL = 'https://tongji.dcloud.io/uni/stat'; const STAT_URL = 'https://tongji.dcloud.io/uni/stat';
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册