From 3928e3646c1baf8f53976f66e950d2e2a49c2d79 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Tue, 25 May 2021 16:44:09 +0800 Subject: [PATCH] feat: add resolveEasycom --- packages/shims-uni-app.d.ts | 1 + packages/uni-app/dist/uni-app.cjs.js | 5 ++++ packages/uni-app/dist/uni-app.d.ts | 2 ++ packages/uni-app/dist/uni-app.es.js | 8 +++++-- packages/uni-app/src/index.ts | 1 + packages/uni-app/src/utils.ts | 5 ++++ packages/uni-h5/dist/uni-h5.cjs.js | 2 +- packages/uni-h5/dist/uni-h5.es.js | 2 +- .../src/framework/components/layout/index.tsx | 3 ++- .../src/configResolved/plugins/easycom.ts | 24 ++++++++++++++----- 10 files changed, 42 insertions(+), 11 deletions(-) create mode 100644 packages/uni-app/src/utils.ts diff --git a/packages/shims-uni-app.d.ts b/packages/shims-uni-app.d.ts index d6efe7bc22..3684e90b9c 100644 --- a/packages/shims-uni-app.d.ts +++ b/packages/shims-uni-app.d.ts @@ -152,6 +152,7 @@ declare namespace UniApp { topWindow?: boolean leftWindow?: boolean rightWindow?: boolean + maxWidth?: string | number } interface PagesJsonPageOptions { diff --git a/packages/uni-app/dist/uni-app.cjs.js b/packages/uni-app/dist/uni-app.cjs.js index acad9b9587..a834ee3a7d 100644 --- a/packages/uni-app/dist/uni-app.cjs.js +++ b/packages/uni-app/dist/uni-app.cjs.js @@ -76,6 +76,10 @@ function getSsrGlobalData() { return sanitise(globalData); } +function resolveEasycom(component, easycom) { + return shared.isString(component) ? easycom : component; +} + // @ts-ignore // App and Page const ON_SHOW = 'onShow'; @@ -155,5 +159,6 @@ exports.onTabItemTap = onTabItemTap; exports.onThemeChange = onThemeChange; exports.onUnhandledRejection = onUnhandledRejection; exports.onUnload = onUnload; +exports.resolveEasycom = resolveEasycom; exports.shallowSsrRef = shallowSsrRef; exports.ssrRef = ssrRef; diff --git a/packages/uni-app/dist/uni-app.d.ts b/packages/uni-app/dist/uni-app.d.ts index 6002ef1fa6..a04a7bcf3f 100644 --- a/packages/uni-app/dist/uni-app.d.ts +++ b/packages/uni-app/dist/uni-app.d.ts @@ -50,6 +50,8 @@ export declare const onUnhandledRejection: (hook: () => any, target?: ComponentI export declare const onUnload: (hook: () => any, target?: ComponentInternalInstance | null) => any; +export declare function resolveEasycom(component: unknown, easycom: unknown): unknown; + export declare const shallowSsrRef: SSRRef; declare type SSRRef = (value: unknown, key?: string, shallow?: boolean) => ReturnType | ReturnType; diff --git a/packages/uni-app/dist/uni-app.es.js b/packages/uni-app/dist/uni-app.es.js index 237eff4cf4..00ab56a626 100644 --- a/packages/uni-app/dist/uni-app.es.js +++ b/packages/uni-app/dist/uni-app.es.js @@ -1,5 +1,5 @@ import { shallowRef, ref, getCurrentInstance, isInSSRComponentSetup, injectHook } from 'vue'; -import { hasOwn } from '@vue/shared'; +import { hasOwn, isString } from '@vue/shared'; const sanitise = (val) => (val && JSON.parse(JSON.stringify(val))) || val; const UNI_SSR = '__uniSSR'; @@ -41,6 +41,10 @@ function getSsrGlobalData() { return sanitise(globalData); } +function resolveEasycom(component, easycom) { + return isString(component) ? easycom : component; +} + // @ts-ignore // App and Page const ON_SHOW = 'onShow'; @@ -96,4 +100,4 @@ const onNavigationBarSearchInputClicked = /*#__PURE__*/ createHook(ON_NAVIGATION const onNavigationBarSearchInputConfirmed = /*#__PURE__*/ createHook(ON_NAVIGATION_BAR_SEARCH_INPUT_CONFIRMED); const onNavigationBarSearchInputFocusChanged = /*#__PURE__*/ createHook(ON_NAVIGATION_BAR_SEARCH_INPUT_FOCUS_CHANGED); -export { getSsrGlobalData, onAddToFavorites, onBackPress, onError, onHide, onLaunch, onNavigationBarButtonTap, onNavigationBarSearchInputChanged, onNavigationBarSearchInputClicked, onNavigationBarSearchInputConfirmed, onNavigationBarSearchInputFocusChanged, onPageNotFound, onPageScroll, onPullDownRefresh, onReachBottom, onReady, onResize, onShareAppMessage, onShareTimeline, onShow, onTabItemTap, onThemeChange, onUnhandledRejection, onUnload, shallowSsrRef, ssrRef }; +export { getSsrGlobalData, onAddToFavorites, onBackPress, onError, onHide, onLaunch, onNavigationBarButtonTap, onNavigationBarSearchInputChanged, onNavigationBarSearchInputClicked, onNavigationBarSearchInputConfirmed, onNavigationBarSearchInputFocusChanged, onPageNotFound, onPageScroll, onPullDownRefresh, onReachBottom, onReady, onResize, onShareAppMessage, onShareTimeline, onShow, onTabItemTap, onThemeChange, onUnhandledRejection, onUnload, resolveEasycom, shallowSsrRef, ssrRef }; diff --git a/packages/uni-app/src/index.ts b/packages/uni-app/src/index.ts index beea5d62a5..b10eef72b6 100644 --- a/packages/uni-app/src/index.ts +++ b/packages/uni-app/src/index.ts @@ -1,2 +1,3 @@ export * from './ssr' +export * from './utils' export * from './apiLifecycle' diff --git a/packages/uni-app/src/utils.ts b/packages/uni-app/src/utils.ts new file mode 100644 index 0000000000..85de0c234d --- /dev/null +++ b/packages/uni-app/src/utils.ts @@ -0,0 +1,5 @@ +import { isString } from '@vue/shared' + +export function resolveEasycom(component: unknown, easycom: unknown) { + return isString(component) ? easycom : component +} diff --git a/packages/uni-h5/dist/uni-h5.cjs.js b/packages/uni-h5/dist/uni-h5.cjs.js index 8dfe11d3d6..e3aca5a877 100644 --- a/packages/uni-h5/dist/uni-h5.cjs.js +++ b/packages/uni-h5/dist/uni-h5.cjs.js @@ -10617,7 +10617,7 @@ function initMediaQuery(minWidth, callback) { return mediaQueryList.matches; } function useMaxWidth(layoutState, rootRef) { - const route = vueRouter.useRoute(); + const route = usePageRoute(); function checkMaxWidth() { const windowWidth = document.body.clientWidth; const maxWidth = parseInt(String(route.meta.maxWidth || 1190)); diff --git a/packages/uni-h5/dist/uni-h5.es.js b/packages/uni-h5/dist/uni-h5.es.js index 94b60cc06c..425e2c4dfb 100644 --- a/packages/uni-h5/dist/uni-h5.es.js +++ b/packages/uni-h5/dist/uni-h5.es.js @@ -18101,7 +18101,7 @@ function initMediaQuery(minWidth, callback) { return mediaQueryList.matches; } function useMaxWidth(layoutState, rootRef) { - const route = useRoute(); + const route = usePageRoute(); function checkMaxWidth() { const windowWidth = document.body.clientWidth; const maxWidth = parseInt(String(route.meta.maxWidth || 1190)); diff --git a/packages/uni-h5/src/framework/components/layout/index.tsx b/packages/uni-h5/src/framework/components/layout/index.tsx index 16c84c63bc..fa8720bf65 100644 --- a/packages/uni-h5/src/framework/components/layout/index.tsx +++ b/packages/uni-h5/src/framework/components/layout/index.tsx @@ -29,6 +29,7 @@ import { checkMinWidth } from '../../../helpers/dom' import { hasOwn } from '@vue/shared' import TabBar from './tabBar' +import { usePageRoute } from '../../setup/provide' type KeepAliveRoute = ReturnType @@ -140,7 +141,7 @@ function useMaxWidth( layoutState: LayoutState, rootRef: Ref ) { - const route = useRoute() + const route = usePageRoute() function checkMaxWidth() { const windowWidth = document.body.clientWidth const maxWidth = parseInt(String(route.meta.maxWidth || 1190)) diff --git a/packages/vite-plugin-uni/src/configResolved/plugins/easycom.ts b/packages/vite-plugin-uni/src/configResolved/plugins/easycom.ts index f381f0134b..15bc4a32b5 100644 --- a/packages/vite-plugin-uni/src/configResolved/plugins/easycom.ts +++ b/packages/vite-plugin-uni/src/configResolved/plugins/easycom.ts @@ -50,7 +50,7 @@ const baseComponents = [ 'view', ] -const identifierRE = /^([a-zA-Z_$][a-zA-Z\\d_$]*)$/ +// const identifierRE = /^([a-zA-Z_$][a-zA-Z\\d_$]*)$/ export function uniEasycomPlugin( options: UniPluginFilterOptions, @@ -100,11 +100,10 @@ export function uniEasycomPlugin( } const source = matchEasycom(name) if (source) { - return ( - // 解决局部引入组件优先级(理论上让开发者使用script setup就可以解决局部引入) - (identifierRE.test(name) - ? `typeof ${name} !== 'undefined' ? ${name} : ` - : '') + + // 处理easycom组件优先级 + return genResolveEasycomCode( + importDeclarations, + str, addImportDeclaration( importDeclarations, `__easycom_${i++}`, @@ -124,6 +123,19 @@ export function uniEasycomPlugin( } } +const RESOLVE_EASYCOM_IMPORT_CODE = `import { resolveEasycom } from '@dcloudio/uni-app';` + +function genResolveEasycomCode( + importDeclarations: string[], + code: string, + name: string +) { + if (!importDeclarations.includes(RESOLVE_EASYCOM_IMPORT_CODE)) { + importDeclarations.push(RESOLVE_EASYCOM_IMPORT_CODE) + } + return `resolveEasycom(${code},${name})` +} + function resolveBuiltInCssImport(name: string) { const cssImports: string[] = [] if (baseComponents.includes(name)) { -- GitLab