From 9151b16f332a0ab3f39b1197444ee1c513017dc7 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Wed, 31 Mar 2021 20:52:06 +0800 Subject: [PATCH] feat: add rpx2px --- packages/global.d.ts | 1 + packages/shims-uni-app.d.ts | 12 ++++ packages/uni-api/src/service/base/upx2px.ts | 3 +- packages/uni-h5-vue/dist/vue.runtime.esm.js | 17 ++++- packages/uni-h5-vue/lib/vue.runtime.esm.js | 17 ++++- packages/uni-h5/dist/uni-h5.esm.js | 71 ++++++++++++------- packages/uni-h5/src/framework/plugin/page.ts | 2 +- .../uni-h5/src/platform/getBaseSystemInfo.ts | 53 ++++++++++++++ packages/uni-h5/src/platform/index.ts | 1 + .../service/api/device/getSystemInfoSync.ts | 61 +++++++--------- packages/uni-mp-alipay/build.json | 8 +++ packages/uni-mp-alipay/dist/uni.api.esm.js | 6 +- packages/uni-mp-alipay/src/platform/index.ts | 3 + packages/uni-mp-baidu/build.json | 8 +++ packages/uni-mp-baidu/dist/uni.api.esm.js | 6 +- packages/uni-mp-baidu/src/platform/index.ts | 3 + packages/uni-mp-qq/build.json | 8 +++ packages/uni-mp-qq/dist/uni.api.esm.js | 6 +- packages/uni-mp-qq/src/platform/index.ts | 3 + packages/uni-mp-toutiao/build.json | 8 +++ packages/uni-mp-toutiao/dist/uni.api.esm.js | 6 +- packages/uni-mp-toutiao/src/platform/index.ts | 3 + packages/uni-mp-weixin/build.json | 8 +++ packages/uni-mp-weixin/dist/uni.api.esm.js | 6 +- packages/uni-mp-weixin/src/platform/index.ts | 3 + packages/uni-quickapp-webview/build.json | 8 +++ .../uni-quickapp-webview/dist/uni.api.esm.js | 6 +- .../src/platform/index.ts | 3 + .../src/configResolved/plugins/index.ts | 2 +- .../src/configResolved/plugins/pagesJson.ts | 11 ++- 30 files changed, 278 insertions(+), 75 deletions(-) create mode 100644 packages/uni-h5/src/platform/getBaseSystemInfo.ts create mode 100644 packages/uni-mp-alipay/src/platform/index.ts create mode 100644 packages/uni-mp-baidu/src/platform/index.ts create mode 100644 packages/uni-mp-qq/src/platform/index.ts create mode 100644 packages/uni-mp-toutiao/src/platform/index.ts create mode 100644 packages/uni-mp-weixin/src/platform/index.ts create mode 100644 packages/uni-quickapp-webview/src/platform/index.ts diff --git a/packages/global.d.ts b/packages/global.d.ts index cc0c3d72d..f683b1ebc 100644 --- a/packages/global.d.ts +++ b/packages/global.d.ts @@ -1,6 +1,7 @@ declare var tt: any declare var qa: any declare var swan: any +declare var qq: any declare var __PLATFORM__: UniApp.PLATFORM declare var __PLATFORM_PREFIX__: 'wx' | 'qq' | 'my' | 'swan' | 'tt' | 'qa' declare var __GLOBAL__: Record diff --git a/packages/shims-uni-app.d.ts b/packages/shims-uni-app.d.ts index e2738bf39..94e4ec5bf 100644 --- a/packages/shims-uni-app.d.ts +++ b/packages/shims-uni-app.d.ts @@ -1,3 +1,15 @@ +declare namespace Page { + interface PageInstance { + $page: { + id: number + path: string + route: string + fullPath: string + options: Record + meta: UniApp.PageRouteMeta + } + } +} declare namespace UniApp { type ClassObj = Record type StyleObj = Record diff --git a/packages/uni-api/src/service/base/upx2px.ts b/packages/uni-api/src/service/base/upx2px.ts index b7838ed57..d0574613a 100644 --- a/packages/uni-api/src/service/base/upx2px.ts +++ b/packages/uni-api/src/service/base/upx2px.ts @@ -1,3 +1,4 @@ +import { getBaseSystemInfo } from '@dcloudio/uni-platform' import { createSyncApi } from '../../helpers/api' import { Upx2pxProtocol } from '../../protocols/base/upx2px' @@ -8,7 +9,7 @@ let deviceWidth = 0 let deviceDPR = 0 function checkDeviceWidth() { - const { platform, pixelRatio, windowWidth } = __GLOBAL__.getSystemInfoSync() + const { platform, pixelRatio, windowWidth } = getBaseSystemInfo() deviceWidth = windowWidth as number deviceDPR = pixelRatio as number isIOS = platform === 'ios' diff --git a/packages/uni-h5-vue/dist/vue.runtime.esm.js b/packages/uni-h5-vue/dist/vue.runtime.esm.js index afc0bcdb5..535c3f9db 100644 --- a/packages/uni-h5-vue/dist/vue.runtime.esm.js +++ b/packages/uni-h5-vue/dist/vue.runtime.esm.js @@ -8084,7 +8084,7 @@ function patchStyle(el, prev, next) { else if (isString(next)) { if (prev !== next) { const current = style.display; - style.cssText = next; + style.cssText = normalizeRpx(next); // indicates that the `display` of the element is controlled by `v-show`, // so we always keep the current `display` value regardless of the `style` value, // thus handing over control to `v-show`. @@ -8112,6 +8112,7 @@ function setStyle(style, name, val) { val.forEach(v => setStyle(style, name, v)); } else { + val = normalizeRpx(val); if (name.startsWith('--')) { // custom property definition style.setProperty(name, val); @@ -8147,7 +8148,19 @@ function autoPrefix(style, rawName) { } } return rawName; -} +} +// fixed by xxxxxx +// upx,rpx +const rpxRE = /\b([+-]?\d+(\.\d+)?)[r|u]px\b/g; +const normalizeRpx = (val) => { + if (isString(val)) { + return val.replace(rpxRE, (a, b) => { + // @ts-ignore + return rpx2px(b) + 'px'; + }); + } + return val; +}; const xlinkNS = 'http://www.w3.org/1999/xlink'; function patchAttr(el, key, value, isSVG) { diff --git a/packages/uni-h5-vue/lib/vue.runtime.esm.js b/packages/uni-h5-vue/lib/vue.runtime.esm.js index e0f5a134a..5a44a8f06 100644 --- a/packages/uni-h5-vue/lib/vue.runtime.esm.js +++ b/packages/uni-h5-vue/lib/vue.runtime.esm.js @@ -8084,7 +8084,7 @@ function patchStyle(el, prev, next) { else if (isString(next)) { if (prev !== next) { const current = style.display; - style.cssText = next; + style.cssText = normalizeRpx(next); // indicates that the `display` of the element is controlled by `v-show`, // so we always keep the current `display` value regardless of the `style` value, // thus handing over control to `v-show`. @@ -8112,6 +8112,7 @@ function setStyle(style, name, val) { val.forEach(v => setStyle(style, name, v)); } else { + val = normalizeRpx(val); if (name.startsWith('--')) { // custom property definition style.setProperty(name, val); @@ -8147,7 +8148,19 @@ function autoPrefix(style, rawName) { } } return rawName; -} +} +// fixed by xxxxxx +// upx,rpx +const rpxRE = /\b([+-]?\d+(\.\d+)?)[r|u]px\b/g; +const normalizeRpx = (val) => { + if (isString(val)) { + return val.replace(rpxRE, (a, b) => { + // @ts-ignore + return rpx2px(b) + 'px'; + }); + } + return val; +}; const xlinkNS = 'http://www.w3.org/1999/xlink'; function patchAttr(el, key, value, isSVG) { diff --git a/packages/uni-h5/dist/uni-h5.esm.js b/packages/uni-h5/dist/uni-h5.esm.js index bc0355033..1579f9929 100644 --- a/packages/uni-h5/dist/uni-h5.esm.js +++ b/packages/uni-h5/dist/uni-h5.esm.js @@ -3339,6 +3339,33 @@ function getRealPath$1(filePath) { } return filePath; } +const ua = navigator.userAgent; +const isAndroid = /android/i.test(ua); +const isIOS$1 = /iphone|ipad|ipod/i.test(ua); +function getScreenFix() { + return /^Apple/.test(navigator.vendor) && typeof window.orientation === "number"; +} +function isLandscape(screenFix) { + return screenFix && Math.abs(window.orientation) === 90; +} +function getScreenWidth(screenFix, landscape) { + return screenFix ? Math[landscape ? "max" : "min"](screen.width, screen.height) : screen.width; +} +function getScreenHeight(screenFix, landscape) { + return screenFix ? Math[landscape ? "min" : "max"](screen.height, screen.width) : screen.height; +} +function getWindowWidth(screenWidth) { + return Math.min(window.innerWidth, document.documentElement.clientWidth, screenWidth) || screenWidth; +} +function getBaseSystemInfo() { + const screenFix = getScreenFix(); + const windowWidth = getWindowWidth(getScreenWidth(screenFix, isLandscape(screenFix))); + return { + platform: isIOS$1 ? "ios" : "other", + pixelRatio: window.devicePixelRatio, + windowWidth + }; +} var index_vue_vue_type_style_index_0_lang$d = "\nuni-image {\r\n width: 320px;\r\n height: 240px;\r\n display: inline-block;\r\n overflow: hidden;\r\n position: relative;\n}\nuni-image[hidden] {\r\n display: none;\n}\nuni-image > div {\r\n width: 100%;\r\n height: 100%;\n}\nuni-image > img {\r\n -webkit-touch-callout: none;\r\n -webkit-user-select: none;\r\n -moz-user-select: none;\r\n display: block;\r\n position: absolute;\r\n top: 0;\r\n left: 0;\r\n width: 100%;\r\n height: 100%;\r\n opacity: 0;\n}\nuni-image > .uni-image-will-change {\r\n will-change: transform;\n}\r\n"; const _sfc_main$j = { name: "Image", @@ -7736,14 +7763,14 @@ const Upx2pxProtocol = [ ]; const EPS = 1e-4; const BASE_DEVICE_WIDTH = 750; -let isIOS$1 = false; +let isIOS = false; let deviceWidth = 0; let deviceDPR = 0; function checkDeviceWidth() { - const {platform, pixelRatio: pixelRatio2, windowWidth} = __GLOBAL__.getSystemInfoSync(); + const {platform, pixelRatio: pixelRatio2, windowWidth} = getBaseSystemInfo(); deviceWidth = windowWidth; deviceDPR = pixelRatio2; - isIOS$1 = platform === "ios"; + isIOS = platform === "ios"; } const upx2px = /* @__PURE__ */ createSyncApi("upx2px", (number, newDeviceWidth) => { if (deviceWidth === 0) { @@ -7759,7 +7786,7 @@ const upx2px = /* @__PURE__ */ createSyncApi("upx2px", (number, newDeviceWidth) } result = Math.floor(result + EPS); if (result === 0) { - if (deviceDPR === 1 || !isIOS$1) { + if (deviceDPR === 1 || !isIOS) { result = 1; } else { result = 0.5; @@ -7984,24 +8011,20 @@ const canIUse = /* @__PURE__ */ createSyncApi("canIUse", (schema) => { const makePhoneCall = /* @__PURE__ */ createAsyncApi("makePhoneCall", (option) => { window.location.href = `tel:${option.phoneNumber}`; }, MakePhoneCallProtocol); -const ua = navigator.userAgent; -const isAndroid = /android/i.test(ua); -const isIOS = /iphone|ipad|ipod/i.test(ua); const getSystemInfoSync = /* @__PURE__ */ createSyncApi("getSystemInfoSync", () => { - var screen = window.screen; - var pixelRatio2 = window.devicePixelRatio; - const screenFix = /^Apple/.test(navigator.vendor) && typeof window.orientation === "number"; - const landscape = screenFix && Math.abs(window.orientation) === 90; - var screenWidth = screenFix ? Math[landscape ? "max" : "min"](screen.width, screen.height) : screen.width; - var screenHeight = screenFix ? Math[landscape ? "min" : "max"](screen.height, screen.width) : screen.height; - var windowWidth = Math.min(window.innerWidth, document.documentElement.clientWidth, screenWidth) || screenWidth; - var windowHeight = window.innerHeight; - var language = navigator.language; - var statusBarHeight = out.top; - var osname; - var osversion; - var model; - if (isIOS) { + const pixelRatio2 = window.devicePixelRatio; + const screenFix = getScreenFix(); + const landscape = isLandscape(screenFix); + const screenWidth = getScreenWidth(screenFix, landscape); + const screenHeight = getScreenHeight(screenFix, landscape); + const windowWidth = getWindowWidth(screenWidth); + let windowHeight = window.innerHeight; + const language = navigator.language; + const statusBarHeight = out.top; + let osname; + let osversion; + let model; + if (isIOS$1) { osname = "iOS"; const osversionFind = ua.match(/OS\s([\w_]+)\slike/); if (osversionFind) { @@ -8053,9 +8076,9 @@ const getSystemInfoSync = /* @__PURE__ */ createSyncApi("getSystemInfoSync", () osname = "Other"; osversion = "0"; } - var system = `${osname} ${osversion}`; - var platform = osname.toLocaleLowerCase(); - var safeArea = { + const system = `${osname} ${osversion}`; + const platform = osname.toLocaleLowerCase(); + const safeArea = { left: out.left, right: windowWidth - out.right, top: out.top, diff --git a/packages/uni-h5/src/framework/plugin/page.ts b/packages/uni-h5/src/framework/plugin/page.ts index 2c7d0dfac..76f9f4bb2 100644 --- a/packages/uni-h5/src/framework/plugin/page.ts +++ b/packages/uni-h5/src/framework/plugin/page.ts @@ -49,7 +49,7 @@ function initPublicPage(route: RouteLocationNormalizedLoaded) { } export function initPage(vm: ComponentPublicInstance) { - currentPages.push(vm as Page.PageInstance) + currentPages.push((vm as unknown) as Page.PageInstance) const route = vm.$route ;(vm as any).$page = initPublicPage(route) } diff --git a/packages/uni-h5/src/platform/getBaseSystemInfo.ts b/packages/uni-h5/src/platform/getBaseSystemInfo.ts new file mode 100644 index 000000000..2bc7ee4bb --- /dev/null +++ b/packages/uni-h5/src/platform/getBaseSystemInfo.ts @@ -0,0 +1,53 @@ +export const ua = navigator.userAgent + +export const isAndroid = /android/i.test(ua) + +export const isIOS = /iphone|ipad|ipod/i.test(ua) + +export function getScreenFix() { + return ( + /^Apple/.test(navigator.vendor) && typeof window.orientation === 'number' + ) +} + +export function isLandscape(screenFix: boolean) { + return screenFix && Math.abs(window.orientation as number) === 90 +} + +export function getScreenWidth(screenFix: boolean, landscape: boolean) { + return screenFix + ? Math[landscape ? 'max' : 'min'](screen.width, screen.height) + : screen.width +} + +export function getScreenHeight(screenFix: boolean, landscape: boolean) { + return screenFix + ? Math[landscape ? 'min' : 'max'](screen.height, screen.width) + : screen.height +} + +export function getWindowWidth(screenWidth: number) { + return ( + Math.min( + window.innerWidth, + document.documentElement.clientWidth, + screenWidth + ) || screenWidth + ) +} + +/** + * 简易版systemInfo,主要为upx2px服务 + * @returns + */ +export function getBaseSystemInfo() { + const screenFix = getScreenFix() + const windowWidth = getWindowWidth( + getScreenWidth(screenFix, isLandscape(screenFix)) + ) + return { + platform: isIOS ? 'ios' : 'other', + pixelRatio: window.devicePixelRatio, + windowWidth, + } +} diff --git a/packages/uni-h5/src/platform/index.ts b/packages/uni-h5/src/platform/index.ts index 6e1461991..ed451d79e 100644 --- a/packages/uni-h5/src/platform/index.ts +++ b/packages/uni-h5/src/platform/index.ts @@ -1 +1,2 @@ export * from './getRealPath' +export { getBaseSystemInfo } from './getBaseSystemInfo' diff --git a/packages/uni-h5/src/service/api/device/getSystemInfoSync.ts b/packages/uni-h5/src/service/api/device/getSystemInfoSync.ts index 530e5d19e..0c3c0899d 100644 --- a/packages/uni-h5/src/service/api/device/getSystemInfoSync.ts +++ b/packages/uni-h5/src/service/api/device/getSystemInfoSync.ts @@ -4,45 +4,36 @@ import { createSyncApi } from '@dcloudio/uni-api' import { getWindowOffset } from '@dcloudio/uni-core' -const ua = navigator.userAgent -/** - * 是否安卓设备 - */ -const isAndroid = /android/i.test(ua) -/** - * 是否iOS设备 - */ -const isIOS = /iphone|ipad|ipod/i.test(ua) +import { + ua, + isIOS, + isAndroid, + isLandscape, + getScreenFix, + getScreenWidth, + getWindowWidth, + getScreenHeight, +} from '../../../platform/getBaseSystemInfo' + /** * 获取系统信息-同步 */ export const getSystemInfoSync = createSyncApi( 'getSystemInfoSync', () => { - var screen = window.screen - var pixelRatio = window.devicePixelRatio + const pixelRatio = window.devicePixelRatio // 横屏时 iOS 获取的屏幕宽高颠倒,进行纠正 - const screenFix = - /^Apple/.test(navigator.vendor) && typeof window.orientation === 'number' - const landscape = screenFix && Math.abs(window.orientation as number) === 90 - var screenWidth = screenFix - ? Math[landscape ? 'max' : 'min'](screen.width, screen.height) - : screen.width - var screenHeight = screenFix - ? Math[landscape ? 'min' : 'max'](screen.height, screen.width) - : screen.height - var windowWidth = - Math.min( - window.innerWidth, - document.documentElement.clientWidth, - screenWidth - ) || screenWidth - var windowHeight = window.innerHeight - var language = navigator.language - var statusBarHeight = safeAreaInsets.top - var osname - var osversion - var model + const screenFix = getScreenFix() + const landscape = isLandscape(screenFix) + const screenWidth = getScreenWidth(screenFix, landscape) + const screenHeight = getScreenHeight(screenFix, landscape) + const windowWidth = getWindowWidth(screenWidth) + let windowHeight = window.innerHeight + const language = navigator.language + const statusBarHeight = safeAreaInsets.top + let osname + let osversion + let model if (isIOS) { osname = 'iOS' @@ -99,9 +90,9 @@ export const getSystemInfoSync = createSyncApi( osversion = '0' } - var system = `${osname} ${osversion}` - var platform = osname.toLocaleLowerCase() - var safeArea = { + const system = `${osname} ${osversion}` + const platform = osname.toLocaleLowerCase() + const safeArea = { left: safeAreaInsets.left, right: windowWidth - safeAreaInsets.right, top: safeAreaInsets.top, diff --git a/packages/uni-mp-alipay/build.json b/packages/uni-mp-alipay/build.json index 3c9cbe319..8a90d725f 100644 --- a/packages/uni-mp-alipay/build.json +++ b/packages/uni-mp-alipay/build.json @@ -3,6 +3,14 @@ "src/runtime/index.ts": "dist/uni.mp.esm.js", "src/api/index.ts": "dist/uni.api.esm.js" }, + "alias": { + "entries": [ + { + "find": "@dcloudio/uni-platform", + "replacement": "packages/uni-mp-alipay/src/platform/index.ts" + } + ] + }, "replacements": { "__GLOBAL__": "my", "__PLATFORM__": "\"mp-alipay\"", diff --git a/packages/uni-mp-alipay/dist/uni.api.esm.js b/packages/uni-mp-alipay/dist/uni.api.esm.js index b73dc2841..ab74fc079 100644 --- a/packages/uni-mp-alipay/dist/uni.api.esm.js +++ b/packages/uni-mp-alipay/dist/uni.api.esm.js @@ -301,6 +301,10 @@ function createApi(type, name, fn, protocol, options) { } } +function getBaseSystemInfo() { + return my.getSystemInfoSync() +} + const Upx2pxProtocol = [ { name: 'upx', @@ -315,7 +319,7 @@ let isIOS = false; let deviceWidth = 0; let deviceDPR = 0; function checkDeviceWidth() { - const { platform, pixelRatio, windowWidth } = my.getSystemInfoSync(); + const { platform, pixelRatio, windowWidth } = getBaseSystemInfo(); deviceWidth = windowWidth; deviceDPR = pixelRatio; isIOS = platform === 'ios'; diff --git a/packages/uni-mp-alipay/src/platform/index.ts b/packages/uni-mp-alipay/src/platform/index.ts new file mode 100644 index 000000000..90ad685ef --- /dev/null +++ b/packages/uni-mp-alipay/src/platform/index.ts @@ -0,0 +1,3 @@ +export function getBaseSystemInfo() { + return my.getSystemInfoSync() +} diff --git a/packages/uni-mp-baidu/build.json b/packages/uni-mp-baidu/build.json index 3929d760f..183b2ac9d 100644 --- a/packages/uni-mp-baidu/build.json +++ b/packages/uni-mp-baidu/build.json @@ -3,6 +3,14 @@ "src/runtime/index.ts": "dist/uni.mp.esm.js", "src/api/index.ts": "dist/uni.api.esm.js" }, + "alias": { + "entries": [ + { + "find": "@dcloudio/uni-platform", + "replacement": "packages/uni-mp-baidu/src/platform/index.ts" + } + ] + }, "replacements": { "__GLOBAL__": "swan", "__PLATFORM__": "\"mp-baidu\"", diff --git a/packages/uni-mp-baidu/dist/uni.api.esm.js b/packages/uni-mp-baidu/dist/uni.api.esm.js index a8e6aa909..669ceff70 100644 --- a/packages/uni-mp-baidu/dist/uni.api.esm.js +++ b/packages/uni-mp-baidu/dist/uni.api.esm.js @@ -301,6 +301,10 @@ function createApi(type, name, fn, protocol, options) { } } +function getBaseSystemInfo() { + return swan.getSystemInfoSync() +} + const Upx2pxProtocol = [ { name: 'upx', @@ -315,7 +319,7 @@ let isIOS = false; let deviceWidth = 0; let deviceDPR = 0; function checkDeviceWidth() { - const { platform, pixelRatio, windowWidth } = swan.getSystemInfoSync(); + const { platform, pixelRatio, windowWidth } = getBaseSystemInfo(); deviceWidth = windowWidth; deviceDPR = pixelRatio; isIOS = platform === 'ios'; diff --git a/packages/uni-mp-baidu/src/platform/index.ts b/packages/uni-mp-baidu/src/platform/index.ts new file mode 100644 index 000000000..2cc117b75 --- /dev/null +++ b/packages/uni-mp-baidu/src/platform/index.ts @@ -0,0 +1,3 @@ +export function getBaseSystemInfo() { + return swan.getSystemInfoSync() +} diff --git a/packages/uni-mp-qq/build.json b/packages/uni-mp-qq/build.json index 3d6a5a2f4..27ce14658 100644 --- a/packages/uni-mp-qq/build.json +++ b/packages/uni-mp-qq/build.json @@ -3,6 +3,14 @@ "src/runtime/index.ts": "dist/uni.mp.esm.js", "src/api/index.ts": "dist/uni.api.esm.js" }, + "alias": { + "entries": [ + { + "find": "@dcloudio/uni-platform", + "replacement": "packages/uni-mp-qq/src/platform/index.ts" + } + ] + }, "replacements": { "__GLOBAL__": "qq", "__PLATFORM__": "\"mp-qq\"", diff --git a/packages/uni-mp-qq/dist/uni.api.esm.js b/packages/uni-mp-qq/dist/uni.api.esm.js index fcec122db..1051f5eaf 100644 --- a/packages/uni-mp-qq/dist/uni.api.esm.js +++ b/packages/uni-mp-qq/dist/uni.api.esm.js @@ -301,6 +301,10 @@ function createApi(type, name, fn, protocol, options) { } } +function getBaseSystemInfo() { + return qq.getSystemInfoSync() +} + const Upx2pxProtocol = [ { name: 'upx', @@ -315,7 +319,7 @@ let isIOS = false; let deviceWidth = 0; let deviceDPR = 0; function checkDeviceWidth() { - const { platform, pixelRatio, windowWidth } = qq.getSystemInfoSync(); + const { platform, pixelRatio, windowWidth } = getBaseSystemInfo(); deviceWidth = windowWidth; deviceDPR = pixelRatio; isIOS = platform === 'ios'; diff --git a/packages/uni-mp-qq/src/platform/index.ts b/packages/uni-mp-qq/src/platform/index.ts new file mode 100644 index 000000000..17dbcd7d2 --- /dev/null +++ b/packages/uni-mp-qq/src/platform/index.ts @@ -0,0 +1,3 @@ +export function getBaseSystemInfo() { + return qq.getSystemInfoSync() +} diff --git a/packages/uni-mp-toutiao/build.json b/packages/uni-mp-toutiao/build.json index a15a632e6..d3239d1ae 100644 --- a/packages/uni-mp-toutiao/build.json +++ b/packages/uni-mp-toutiao/build.json @@ -3,6 +3,14 @@ "src/runtime/index.ts": "dist/uni.mp.esm.js", "src/api/index.ts": "dist/uni.api.esm.js" }, + "alias": { + "entries": [ + { + "find": "@dcloudio/uni-platform", + "replacement": "packages/uni-mp-toutiao/src/platform/index.ts" + } + ] + }, "replacements": { "__GLOBAL__": "tt", "__PLATFORM__": "\"mp-toutiao\"", diff --git a/packages/uni-mp-toutiao/dist/uni.api.esm.js b/packages/uni-mp-toutiao/dist/uni.api.esm.js index 3d0d8db35..d6f7b9ec6 100644 --- a/packages/uni-mp-toutiao/dist/uni.api.esm.js +++ b/packages/uni-mp-toutiao/dist/uni.api.esm.js @@ -301,6 +301,10 @@ function createApi(type, name, fn, protocol, options) { } } +function getBaseSystemInfo() { + return tt.getSystemInfoSync() +} + const Upx2pxProtocol = [ { name: 'upx', @@ -315,7 +319,7 @@ let isIOS = false; let deviceWidth = 0; let deviceDPR = 0; function checkDeviceWidth() { - const { platform, pixelRatio, windowWidth } = tt.getSystemInfoSync(); + const { platform, pixelRatio, windowWidth } = getBaseSystemInfo(); deviceWidth = windowWidth; deviceDPR = pixelRatio; isIOS = platform === 'ios'; diff --git a/packages/uni-mp-toutiao/src/platform/index.ts b/packages/uni-mp-toutiao/src/platform/index.ts new file mode 100644 index 000000000..53a55c0ac --- /dev/null +++ b/packages/uni-mp-toutiao/src/platform/index.ts @@ -0,0 +1,3 @@ +export function getBaseSystemInfo() { + return tt.getSystemInfoSync() +} diff --git a/packages/uni-mp-weixin/build.json b/packages/uni-mp-weixin/build.json index 8c2746c4a..1f2343cc3 100644 --- a/packages/uni-mp-weixin/build.json +++ b/packages/uni-mp-weixin/build.json @@ -3,6 +3,14 @@ "src/runtime/index.ts": "dist/uni.mp.esm.js", "src/api/index.ts": "dist/uni.api.esm.js" }, + "alias": { + "entries": [ + { + "find": "@dcloudio/uni-platform", + "replacement": "packages/uni-mp-weixin/src/platform/index.ts" + } + ] + }, "replacements": { "__GLOBAL__": "wx", "__PLATFORM__": "\"mp-weixin\"", diff --git a/packages/uni-mp-weixin/dist/uni.api.esm.js b/packages/uni-mp-weixin/dist/uni.api.esm.js index 046e5c39e..61188fe2a 100644 --- a/packages/uni-mp-weixin/dist/uni.api.esm.js +++ b/packages/uni-mp-weixin/dist/uni.api.esm.js @@ -301,6 +301,10 @@ function createApi(type, name, fn, protocol, options) { } } +function getBaseSystemInfo() { + return wx.getSystemInfoSync() +} + const Upx2pxProtocol = [ { name: 'upx', @@ -315,7 +319,7 @@ let isIOS = false; let deviceWidth = 0; let deviceDPR = 0; function checkDeviceWidth() { - const { platform, pixelRatio, windowWidth } = wx.getSystemInfoSync(); + const { platform, pixelRatio, windowWidth } = getBaseSystemInfo(); deviceWidth = windowWidth; deviceDPR = pixelRatio; isIOS = platform === 'ios'; diff --git a/packages/uni-mp-weixin/src/platform/index.ts b/packages/uni-mp-weixin/src/platform/index.ts new file mode 100644 index 000000000..80a7678ea --- /dev/null +++ b/packages/uni-mp-weixin/src/platform/index.ts @@ -0,0 +1,3 @@ +export function getBaseSystemInfo() { + return wx.getSystemInfoSync() +} diff --git a/packages/uni-quickapp-webview/build.json b/packages/uni-quickapp-webview/build.json index 8b93af1ba..d1115b5da 100644 --- a/packages/uni-quickapp-webview/build.json +++ b/packages/uni-quickapp-webview/build.json @@ -3,6 +3,14 @@ "src/runtime/index.ts": "dist/uni.mp.esm.js", "src/api/index.ts": "dist/uni.api.esm.js" }, + "alias": { + "entries": [ + { + "find": "@dcloudio/uni-platform", + "replacement": "packages/uni-quickapp-webview/src/platform/index.ts" + } + ] + }, "replacements": { "__GLOBAL__": "qa", "__PLATFORM__": "\"quickapp-webview\"", diff --git a/packages/uni-quickapp-webview/dist/uni.api.esm.js b/packages/uni-quickapp-webview/dist/uni.api.esm.js index 8c9d507f8..d4339b392 100644 --- a/packages/uni-quickapp-webview/dist/uni.api.esm.js +++ b/packages/uni-quickapp-webview/dist/uni.api.esm.js @@ -301,6 +301,10 @@ function createApi(type, name, fn, protocol, options) { } } +function getBaseSystemInfo() { + return qa.getSystemInfoSync() +} + const Upx2pxProtocol = [ { name: 'upx', @@ -315,7 +319,7 @@ let isIOS = false; let deviceWidth = 0; let deviceDPR = 0; function checkDeviceWidth() { - const { platform, pixelRatio, windowWidth } = qa.getSystemInfoSync(); + const { platform, pixelRatio, windowWidth } = getBaseSystemInfo(); deviceWidth = windowWidth; deviceDPR = pixelRatio; isIOS = platform === 'ios'; diff --git a/packages/uni-quickapp-webview/src/platform/index.ts b/packages/uni-quickapp-webview/src/platform/index.ts new file mode 100644 index 000000000..2d4565e6a --- /dev/null +++ b/packages/uni-quickapp-webview/src/platform/index.ts @@ -0,0 +1,3 @@ +export function getBaseSystemInfo() { + return qa.getSystemInfoSync() +} diff --git a/packages/vite-plugin-uni/src/configResolved/plugins/index.ts b/packages/vite-plugin-uni/src/configResolved/plugins/index.ts index 1c582e3f1..35873b5ed 100644 --- a/packages/vite-plugin-uni/src/configResolved/plugins/index.ts +++ b/packages/vite-plugin-uni/src/configResolved/plugins/index.ts @@ -51,11 +51,11 @@ const uniEasycomPluginOptions: Partial = { const uniInjectPluginOptions: Partial = { exclude: [...COMMON_EXCLUDE], - '__GLOBAL__.': '@dcloudio/uni-h5', 'uni.': '@dcloudio/uni-h5', getApp: ['@dcloudio/uni-h5', 'getApp'], getCurrentPages: ['@dcloudio/uni-h5', 'getCurrentPages'], UniServiceJSBridge: ['@dcloudio/uni-h5', 'UniServiceJSBridge'], + UniViewJSBridge: ['@dcloudio/uni-h5', 'UniViewJSBridge'], } export function resolvePlugins( diff --git a/packages/vite-plugin-uni/src/configResolved/plugins/pagesJson.ts b/packages/vite-plugin-uni/src/configResolved/plugins/pagesJson.ts index 02b84f100..a29aa7265 100644 --- a/packages/vite-plugin-uni/src/configResolved/plugins/pagesJson.ts +++ b/packages/vite-plugin-uni/src/configResolved/plugins/pagesJson.ts @@ -27,7 +27,8 @@ export function uniPagesJsonPlugin( if (id.endsWith(PAGES_JSON_JS)) { return { code: - (options.command === 'serve' ? registerGlobalCode : '') + + registerGlobalCode + + (options.command === 'serve' ? registerDevServerGlobalCode : '') + parsePagesJson(code, config, options), map: { mappings: '' }, } @@ -79,10 +80,14 @@ const hmrCode = `if(import.meta.hot){ }) }` -const registerGlobalCode = `import {uni,getCurrentPages,getApp,UniServiceJSBridge,UniViewJSBridge} from '@dcloudio/uni-h5' +const registerGlobalCode = `import {upx2px} from '@dcloudio/uni-h5' +window.rpx2px = upx2px +` + +const registerDevServerGlobalCode = `import {uni,getCurrentPages,getApp,UniServiceJSBridge,UniViewJSBridge} from '@dcloudio/uni-h5' window.getApp = getApp window.getCurrentPages = getCurrentPages -window.uni = window.__GLOBAL__ = uni +window.uni = uni window.UniViewJSBridge = UniViewJSBridge window.UniServiceJSBridge = UniServiceJSBridge ` -- GitLab