From ce02c608ba503ea1133137b759db583ea79fad64 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Fri, 19 Feb 2021 21:27:23 +0800 Subject: [PATCH] feat(h5): build treeshaking for api --- packages/uni-api/src/helpers/api/index.ts | 39 +++++++++++++------ packages/uni-h5-vue/dist/vue.runtime.esm.js | 8 +--- packages/uni-h5-vue/src/index.ts | 7 ---- packages/uni-h5/dist/uni-h5.esm.js | 9 +++-- packages/uni-mp-alipay/dist/uni.api.esm.js | 7 ++-- packages/uni-mp-baidu/dist/uni.api.esm.js | 7 ++-- packages/uni-mp-qq/dist/uni.api.esm.js | 7 ++-- packages/uni-mp-toutiao/dist/uni.api.esm.js | 7 ++-- packages/uni-mp-weixin/dist/uni.api.esm.js | 7 ++-- .../uni-quickapp-webview/dist/uni.api.esm.js | 7 ++-- packages/vite-plugin-uni/src/load/main.ts | 5 ++- 11 files changed, 61 insertions(+), 49 deletions(-) diff --git a/packages/uni-api/src/helpers/api/index.ts b/packages/uni-api/src/helpers/api/index.ts index 8adf514c5..65e49c823 100644 --- a/packages/uni-api/src/helpers/api/index.ts +++ b/packages/uni-api/src/helpers/api/index.ts @@ -19,11 +19,8 @@ type API_TYPES = | typeof API_TYPE_SYNC | typeof API_TYPE_ASYNC -function validateProtocol( - _name: string, - _args: any[], - _protocol: ApiProtocols -) { +function validateProtocol(name: string, args: any[], protocol: ApiProtocols) { + console.log('validateProtocol', name, args, protocol) return true } @@ -71,10 +68,16 @@ function wrapperApi( export function createOnApi( name: string, fn: T, - options?: ApiOptions, - protocol?: ApiProtocols + protocol?: ApiProtocols, + options?: ApiOptions ) { - return createApi(API_TYPE_ON, name, fn, protocol, options) + return createApi( + API_TYPE_ON, + name, + fn, + __DEV__ ? protocol : undefined, + options + ) } export function createTaskApi( @@ -83,7 +86,13 @@ export function createTaskApi( protocol?: ApiProtocols, options?: ApiOptions ) { - return createApi(API_TYPE_TASK, name, fn, protocol, options) + return createApi( + API_TYPE_TASK, + name, + fn, + __DEV__ ? protocol : undefined, + options + ) } export function createSyncApi( @@ -92,7 +101,13 @@ export function createSyncApi( protocol?: ApiProtocols, options?: ApiOptions ) { - return createApi(API_TYPE_SYNC, name, fn, protocol, options) + return createApi( + API_TYPE_SYNC, + name, + fn, + __DEV__ ? protocol : undefined, + options + ) } export function createAsyncApi( @@ -101,7 +116,9 @@ export function createAsyncApi( protocol?: ApiProtocols, options?: ApiOptions ) { - return promisify(createApi(API_TYPE_ASYNC, name, fn, protocol, options)) + return promisify( + createApi(API_TYPE_ASYNC, name, fn, __DEV__ ? protocol : undefined, options) + ) } function createApi( diff --git a/packages/uni-h5-vue/dist/vue.runtime.esm.js b/packages/uni-h5-vue/dist/vue.runtime.esm.js index 23b1210aa..020674671 100644 --- a/packages/uni-h5-vue/dist/vue.runtime.esm.js +++ b/packages/uni-h5-vue/dist/vue.runtime.esm.js @@ -1,4 +1,3 @@ -import { plugin } from '@dcloudio/uni-h5'; import { isSymbol, extend, isMap, isObject, toRawType, def, isArray, isString, isFunction, isPromise, toHandlerKey, remove, EMPTY_OBJ, camelize, capitalize, EMPTY_ARR, normalizeClass, normalizeStyle, isOn, NOOP, isGloballyWhitelisted, toNumber, invokeArrayFns, looseIndexOf, isSet, looseEqual, hyphenate, isHTMLTag, isSVGTag, isIntegerKey, hasOwn, hasChanged, getGlobalThis, isReservedProp, NO, isModelListener, makeMap, isSpecialBooleanAttr } from '@vue/shared'; export { camelize, capitalize, toDisplayString, toHandlerKey } from '@vue/shared'; @@ -9122,9 +9121,4 @@ const onNavigationBarSearchInputClicked = /*#__PURE__*/ createHook$1("onNavigati const onNavigationBarSearchInputConfirmed = /*#__PURE__*/ createHook$1("onNavigationBarSearchInputConfirmed" /* ON_NAVIGATION_BAR_SEARCH_INPUT_CONFIRMED */); const onNavigationBarSearchInputFocusChanged = /*#__PURE__*/ createHook$1("onNavigationBarSearchInputFocusChanged" /* ON_NAVIGATION_BAR_SEARCH_INPUT_FOCUS_CHANGED */); -function createApp(rootComponent, rootProps = null) { - rootComponent && (rootComponent.mpType = 'app'); - return createVueApp(rootComponent, rootProps).use(plugin); -} - -export { BaseTransition, Comment, Fragment, KeepAlive, Static, Suspense, Teleport, Text, Transition, TransitionGroup, callWithAsyncErrorHandling, callWithErrorHandling, cloneVNode, compile$1 as compile, computed$1 as computed, createApp, createBlock, createCommentVNode, createHook$1 as createHook, createHydrationRenderer, createRenderer, createSSRApp, createSlots, createStaticVNode, createTextVNode, createVNode, createVueApp, customRef, defineAsyncComponent, defineComponent, defineEmit, defineProps, devtools, getCurrentInstance, getTransitionRawChildren, h, handleError, hydrate, initCustomFormatter, inject, injectHook, isInSSRComponentSetup, isProxy, isReactive, isReadonly, isRef, isVNode, markRaw, mergeProps, nextTick, onActivated, onAddToFavorites, onBackPress, onBeforeMount, onBeforeUnmount, onBeforeUpdate, onDeactivated, onError, onErrorCaptured, onHide, onLaunch, onLoad, onMounted, onNavigationBarButtonTap, onNavigationBarSearchInputChanged, onNavigationBarSearchInputClicked, onNavigationBarSearchInputConfirmed, onNavigationBarSearchInputFocusChanged, onPageNotFound, onPageScroll, onPullDownRefresh, onReachBottom, onReady, onRenderTracked, onRenderTriggered, onResize, onShareAppMessage, onShareTimeline, onShow, onTabItemTap, onThemeChange, onUnhandledRejection, onUnload, onUnmounted, onUpdated, openBlock, popScopeId, provide, proxyRefs, pushScopeId, queuePostFlushCb, reactive, readonly, ref, registerRuntimeCompiler, render, renderList, renderSlot, resolveComponent, resolveDirective, resolveDynamicComponent, resolveTransitionHooks, setBlockTracking, setDevtoolsHook, setTransitionHooks, shallowReactive, shallowReadonly, shallowRef, ssrContextKey, ssrUtils, toHandlers, toRaw, toRef, toRefs, transformVNodeArgs, triggerRef, unref, useContext, useCssModule, useCssVars, useSSRContext, useTransitionState, vModelCheckbox, vModelDynamic, vModelRadio, vModelSelect, vModelText, vShow, version, warn, watch, watchEffect, withCtx, withDirectives, withKeys, withModifiers, withScopeId }; +export { BaseTransition, Comment, Fragment, KeepAlive, Static, Suspense, Teleport, Text, Transition, TransitionGroup, callWithAsyncErrorHandling, callWithErrorHandling, cloneVNode, compile$1 as compile, computed$1 as computed, createBlock, createCommentVNode, createHook$1 as createHook, createHydrationRenderer, createRenderer, createSSRApp, createSlots, createStaticVNode, createTextVNode, createVNode, createVueApp, customRef, defineAsyncComponent, defineComponent, defineEmit, defineProps, devtools, getCurrentInstance, getTransitionRawChildren, h, handleError, hydrate, initCustomFormatter, inject, injectHook, isInSSRComponentSetup, isProxy, isReactive, isReadonly, isRef, isVNode, markRaw, mergeProps, nextTick, onActivated, onAddToFavorites, onBackPress, onBeforeMount, onBeforeUnmount, onBeforeUpdate, onDeactivated, onError, onErrorCaptured, onHide, onLaunch, onLoad, onMounted, onNavigationBarButtonTap, onNavigationBarSearchInputChanged, onNavigationBarSearchInputClicked, onNavigationBarSearchInputConfirmed, onNavigationBarSearchInputFocusChanged, onPageNotFound, onPageScroll, onPullDownRefresh, onReachBottom, onReady, onRenderTracked, onRenderTriggered, onResize, onShareAppMessage, onShareTimeline, onShow, onTabItemTap, onThemeChange, onUnhandledRejection, onUnload, onUnmounted, onUpdated, openBlock, popScopeId, provide, proxyRefs, pushScopeId, queuePostFlushCb, reactive, readonly, ref, registerRuntimeCompiler, render, renderList, renderSlot, resolveComponent, resolveDirective, resolveDynamicComponent, resolveTransitionHooks, setBlockTracking, setDevtoolsHook, setTransitionHooks, shallowReactive, shallowReadonly, shallowRef, ssrContextKey, ssrUtils, toHandlers, toRaw, toRef, toRefs, transformVNodeArgs, triggerRef, unref, useContext, useCssModule, useCssVars, useSSRContext, useTransitionState, vModelCheckbox, vModelDynamic, vModelRadio, vModelSelect, vModelText, vShow, version, warn, watch, watchEffect, withCtx, withDirectives, withKeys, withModifiers, withScopeId }; diff --git a/packages/uni-h5-vue/src/index.ts b/packages/uni-h5-vue/src/index.ts index ad60ca2f0..fdbeae970 100644 --- a/packages/uni-h5-vue/src/index.ts +++ b/packages/uni-h5-vue/src/index.ts @@ -1,10 +1,3 @@ -import { plugin } from '@dcloudio/uni-h5' -// @ts-ignore -import { createVueApp } from '../lib/vue.runtime.esm.js' -export function createApp(rootComponent: unknown, rootProps = null) { - rootComponent && ((rootComponent as any).mpType = 'app') - return createVueApp(rootComponent, rootProps).use(plugin) -} // @ts-ignore export * from '../lib/vue.runtime.esm.js' export * from '@dcloudio/uni-vue/src/apiLifecycle' diff --git a/packages/uni-h5/dist/uni-h5.esm.js b/packages/uni-h5/dist/uni-h5.esm.js index 153d6e929..cac8ea550 100644 --- a/packages/uni-h5/dist/uni-h5.esm.js +++ b/packages/uni-h5/dist/uni-h5.esm.js @@ -1407,7 +1407,8 @@ const API_TYPE_ON = 0; const API_TYPE_TASK = 1; const API_TYPE_SYNC = 2; const API_TYPE_ASYNC = 3; -function validateProtocol(_name, _args, _protocol) { +function validateProtocol(name, args, protocol) { + console.log("validateProtocol", name, args, protocol); return true; } function formatApiArgs(args, options) { @@ -1433,16 +1434,16 @@ function wrapperAsyncApi(name, fn, options) { } function wrapperApi(fn, name, protocol, options) { return function(...args) { - if (!(process.env.NODE_ENV !== "production" && protocol && !validateProtocol())) { + if (!(process.env.NODE_ENV !== "production" && protocol && !validateProtocol(name, args, protocol))) { return fn.apply(null, formatApiArgs(args)); } }; } function createSyncApi(name, fn, protocol, options) { - return createApi(API_TYPE_SYNC, name, fn, protocol, options); + return createApi(API_TYPE_SYNC, name, fn, process.env.NODE_ENV !== "production" ? protocol : void 0, options); } function createAsyncApi(name, fn, protocol, options) { - return promisify(createApi(API_TYPE_ASYNC, name, fn, protocol, options)); + return promisify(createApi(API_TYPE_ASYNC, name, fn, process.env.NODE_ENV !== "production" ? protocol : void 0, options)); } function createApi(type, name, fn, protocol, options) { switch (type) { diff --git a/packages/uni-mp-alipay/dist/uni.api.esm.js b/packages/uni-mp-alipay/dist/uni.api.esm.js index 3b2ba3eda..4b364c652 100644 --- a/packages/uni-mp-alipay/dist/uni.api.esm.js +++ b/packages/uni-mp-alipay/dist/uni.api.esm.js @@ -113,7 +113,8 @@ const API_TYPE_ON = 0; const API_TYPE_TASK = 1; const API_TYPE_SYNC = 2; const API_TYPE_ASYNC = 3; -function validateProtocol(_name, _args, _protocol) { +function validateProtocol(name, args, protocol) { + console.log('validateProtocol', name, args, protocol); return true; } function formatApiArgs(args, options) { @@ -139,13 +140,13 @@ function wrapperAsyncApi(name, fn, options) { } function wrapperApi(fn, name, protocol, options) { return function (...args) { - if (!((process.env.NODE_ENV !== 'production') && protocol && !validateProtocol())) { + if (!((process.env.NODE_ENV !== 'production') && protocol && !validateProtocol(name, args, protocol))) { return fn.apply(null, formatApiArgs(args)); } }; } function createSyncApi(name, fn, protocol, options) { - return createApi(API_TYPE_SYNC, name, fn, protocol, options); + return createApi(API_TYPE_SYNC, name, fn, (process.env.NODE_ENV !== 'production') ? protocol : undefined, options); } function createApi(type, name, fn, protocol, options) { switch (type) { diff --git a/packages/uni-mp-baidu/dist/uni.api.esm.js b/packages/uni-mp-baidu/dist/uni.api.esm.js index 70b9624e1..3b9ee02bf 100644 --- a/packages/uni-mp-baidu/dist/uni.api.esm.js +++ b/packages/uni-mp-baidu/dist/uni.api.esm.js @@ -113,7 +113,8 @@ const API_TYPE_ON = 0; const API_TYPE_TASK = 1; const API_TYPE_SYNC = 2; const API_TYPE_ASYNC = 3; -function validateProtocol(_name, _args, _protocol) { +function validateProtocol(name, args, protocol) { + console.log('validateProtocol', name, args, protocol); return true; } function formatApiArgs(args, options) { @@ -139,13 +140,13 @@ function wrapperAsyncApi(name, fn, options) { } function wrapperApi(fn, name, protocol, options) { return function (...args) { - if (!((process.env.NODE_ENV !== 'production') && protocol && !validateProtocol())) { + if (!((process.env.NODE_ENV !== 'production') && protocol && !validateProtocol(name, args, protocol))) { return fn.apply(null, formatApiArgs(args)); } }; } function createSyncApi(name, fn, protocol, options) { - return createApi(API_TYPE_SYNC, name, fn, protocol, options); + return createApi(API_TYPE_SYNC, name, fn, (process.env.NODE_ENV !== 'production') ? protocol : undefined, options); } function createApi(type, name, fn, protocol, options) { switch (type) { diff --git a/packages/uni-mp-qq/dist/uni.api.esm.js b/packages/uni-mp-qq/dist/uni.api.esm.js index 2d24b44a4..a93a18123 100644 --- a/packages/uni-mp-qq/dist/uni.api.esm.js +++ b/packages/uni-mp-qq/dist/uni.api.esm.js @@ -113,7 +113,8 @@ const API_TYPE_ON = 0; const API_TYPE_TASK = 1; const API_TYPE_SYNC = 2; const API_TYPE_ASYNC = 3; -function validateProtocol(_name, _args, _protocol) { +function validateProtocol(name, args, protocol) { + console.log('validateProtocol', name, args, protocol); return true; } function formatApiArgs(args, options) { @@ -139,13 +140,13 @@ function wrapperAsyncApi(name, fn, options) { } function wrapperApi(fn, name, protocol, options) { return function (...args) { - if (!((process.env.NODE_ENV !== 'production') && protocol && !validateProtocol())) { + if (!((process.env.NODE_ENV !== 'production') && protocol && !validateProtocol(name, args, protocol))) { return fn.apply(null, formatApiArgs(args)); } }; } function createSyncApi(name, fn, protocol, options) { - return createApi(API_TYPE_SYNC, name, fn, protocol, options); + return createApi(API_TYPE_SYNC, name, fn, (process.env.NODE_ENV !== 'production') ? protocol : undefined, options); } function createApi(type, name, fn, protocol, options) { switch (type) { diff --git a/packages/uni-mp-toutiao/dist/uni.api.esm.js b/packages/uni-mp-toutiao/dist/uni.api.esm.js index feb3b69b4..c76f183ad 100644 --- a/packages/uni-mp-toutiao/dist/uni.api.esm.js +++ b/packages/uni-mp-toutiao/dist/uni.api.esm.js @@ -113,7 +113,8 @@ const API_TYPE_ON = 0; const API_TYPE_TASK = 1; const API_TYPE_SYNC = 2; const API_TYPE_ASYNC = 3; -function validateProtocol(_name, _args, _protocol) { +function validateProtocol(name, args, protocol) { + console.log('validateProtocol', name, args, protocol); return true; } function formatApiArgs(args, options) { @@ -139,13 +140,13 @@ function wrapperAsyncApi(name, fn, options) { } function wrapperApi(fn, name, protocol, options) { return function (...args) { - if (!((process.env.NODE_ENV !== 'production') && protocol && !validateProtocol())) { + if (!((process.env.NODE_ENV !== 'production') && protocol && !validateProtocol(name, args, protocol))) { return fn.apply(null, formatApiArgs(args)); } }; } function createSyncApi(name, fn, protocol, options) { - return createApi(API_TYPE_SYNC, name, fn, protocol, options); + return createApi(API_TYPE_SYNC, name, fn, (process.env.NODE_ENV !== 'production') ? protocol : undefined, options); } function createApi(type, name, fn, protocol, options) { switch (type) { diff --git a/packages/uni-mp-weixin/dist/uni.api.esm.js b/packages/uni-mp-weixin/dist/uni.api.esm.js index 5233245a8..3e332fbc1 100644 --- a/packages/uni-mp-weixin/dist/uni.api.esm.js +++ b/packages/uni-mp-weixin/dist/uni.api.esm.js @@ -113,7 +113,8 @@ const API_TYPE_ON = 0; const API_TYPE_TASK = 1; const API_TYPE_SYNC = 2; const API_TYPE_ASYNC = 3; -function validateProtocol(_name, _args, _protocol) { +function validateProtocol(name, args, protocol) { + console.log('validateProtocol', name, args, protocol); return true; } function formatApiArgs(args, options) { @@ -139,13 +140,13 @@ function wrapperAsyncApi(name, fn, options) { } function wrapperApi(fn, name, protocol, options) { return function (...args) { - if (!((process.env.NODE_ENV !== 'production') && protocol && !validateProtocol())) { + if (!((process.env.NODE_ENV !== 'production') && protocol && !validateProtocol(name, args, protocol))) { return fn.apply(null, formatApiArgs(args)); } }; } function createSyncApi(name, fn, protocol, options) { - return createApi(API_TYPE_SYNC, name, fn, protocol, options); + return createApi(API_TYPE_SYNC, name, fn, (process.env.NODE_ENV !== 'production') ? protocol : undefined, options); } function createApi(type, name, fn, protocol, options) { switch (type) { diff --git a/packages/uni-quickapp-webview/dist/uni.api.esm.js b/packages/uni-quickapp-webview/dist/uni.api.esm.js index 4e0025454..747260dd6 100644 --- a/packages/uni-quickapp-webview/dist/uni.api.esm.js +++ b/packages/uni-quickapp-webview/dist/uni.api.esm.js @@ -113,7 +113,8 @@ const API_TYPE_ON = 0; const API_TYPE_TASK = 1; const API_TYPE_SYNC = 2; const API_TYPE_ASYNC = 3; -function validateProtocol(_name, _args, _protocol) { +function validateProtocol(name, args, protocol) { + console.log('validateProtocol', name, args, protocol); return true; } function formatApiArgs(args, options) { @@ -139,13 +140,13 @@ function wrapperAsyncApi(name, fn, options) { } function wrapperApi(fn, name, protocol, options) { return function (...args) { - if (!((process.env.NODE_ENV !== 'production') && protocol && !validateProtocol())) { + if (!((process.env.NODE_ENV !== 'production') && protocol && !validateProtocol(name, args, protocol))) { return fn.apply(null, formatApiArgs(args)); } }; } function createSyncApi(name, fn, protocol, options) { - return createApi(API_TYPE_SYNC, name, fn, protocol, options); + return createApi(API_TYPE_SYNC, name, fn, (process.env.NODE_ENV !== 'production') ? protocol : undefined, options); } function createApi(type, name, fn, protocol, options) { switch (type) { diff --git a/packages/vite-plugin-uni/src/load/main.ts b/packages/vite-plugin-uni/src/load/main.ts index 68ab2a833..8b2e91e92 100644 --- a/packages/vite-plugin-uni/src/load/main.ts +++ b/packages/vite-plugin-uni/src/load/main.ts @@ -19,8 +19,9 @@ export const main = { if (!pagesJsPath) { pagesJsPath = slash(path.resolve(options.inputDir, 'pages.json.js')) } - return `import '${pagesJsPath}';${fs + return `import { plugin } from '@dcloudio/uni-h5';import '${pagesJsPath}';function createApp(rootComponent,rootProps){rootComponent && (rootComponent.mpType = 'app');return createVueApp(rootComponent, rootProps).use(plugin)};${fs .readFileSync(filename, 'utf-8') - .toString()}` + .toString() + .replace('createApp', 'createVueApp')}` }, } -- GitLab