From 0779f670c4cc3d6337be57a969f01ac43f4f80b2 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Sat, 22 May 2021 20:06:18 +0800 Subject: [PATCH] fix(easycom): local registration --- packages/uni-h5/dist/uni-h5.es.js | 300 ++++-------------- .../src/configResolved/plugins/easycom.ts | 12 +- 2 files changed, 65 insertions(+), 247 deletions(-) diff --git a/packages/uni-h5/dist/uni-h5.es.js b/packages/uni-h5/dist/uni-h5.es.js index 85aabc443..7b447e82a 100644 --- a/packages/uni-h5/dist/uni-h5.es.js +++ b/packages/uni-h5/dist/uni-h5.es.js @@ -300,27 +300,27 @@ function initLongPress() { window.addEventListener("touchend", clearLongPressTimer, passiveOptions$2); window.addEventListener("touchcancel", clearLongPressTimer, passiveOptions$2); } -var attrs$1 = ["top", "left", "right", "bottom"]; -var inited$2; -var elementComputedStyle$1 = {}; -var support$1; -function getSupport$1() { +var attrs = ["top", "left", "right", "bottom"]; +var inited$1; +var elementComputedStyle = {}; +var support; +function getSupport() { if (!("CSS" in window) || typeof CSS.supports != "function") { - support$1 = ""; + support = ""; } else if (CSS.supports("top: env(safe-area-inset-top)")) { - support$1 = "env"; + support = "env"; } else if (CSS.supports("top: constant(safe-area-inset-top)")) { - support$1 = "constant"; + support = "constant"; } else { - support$1 = ""; + support = ""; } - return support$1; + return support; } -function init$1() { - support$1 = typeof support$1 === "string" ? support$1 : getSupport$1(); - if (!support$1) { - attrs$1.forEach(function(attr2) { - elementComputedStyle$1[attr2] = 0; +function init() { + support = typeof support === "string" ? support : getSupport(); + if (!support) { + attrs.forEach(function(attr2) { + elementComputedStyle[attr2] = 0; }); return; } @@ -364,7 +364,7 @@ function init$1() { height: "200px", boxSizing: "border-box", overflow: "hidden", - paddingBottom: support$1 + "(safe-area-inset-" + attr2 + ")" + paddingBottom: support + "(safe-area-inset-" + attr2 + ")" }; setStyle(a1, aStyle); setStyle(a2, aStyle); @@ -395,13 +395,13 @@ function init$1() { a1.scrollTop = a2.scrollTop = MAX; a1LastScrollTop = a1.scrollTop; a2LastScrollTop = a2.scrollTop; - attrChange$1(attr2); + attrChange(attr2); } a1.addEventListener("scroll", onScroll, passiveEvents); a2.addEventListener("scroll", onScroll, passiveEvents); }); var computedStyle = getComputedStyle(a1); - Object.defineProperty(elementComputedStyle$1, attr2, { + Object.defineProperty(elementComputedStyle, attr2, { configurable: true, get: function() { return parseFloat(computedStyle.paddingBottom); @@ -419,73 +419,73 @@ function init$1() { overflow: "hidden", visibility: "hidden" }); - attrs$1.forEach(function(key) { + attrs.forEach(function(key) { addChild(parentDiv, key); }); document.body.appendChild(parentDiv); parentReady(); - inited$2 = true; + inited$1 = true; } -function getAttr$1(attr2) { - if (!inited$2) { - init$1(); +function getAttr(attr2) { + if (!inited$1) { + init(); } - return elementComputedStyle$1[attr2]; + return elementComputedStyle[attr2]; } -var changeAttrs$1 = []; -function attrChange$1(attr2) { - if (!changeAttrs$1.length) { +var changeAttrs = []; +function attrChange(attr2) { + if (!changeAttrs.length) { setTimeout(function() { var style = {}; - changeAttrs$1.forEach(function(attr3) { - style[attr3] = elementComputedStyle$1[attr3]; + changeAttrs.forEach(function(attr3) { + style[attr3] = elementComputedStyle[attr3]; }); - changeAttrs$1.length = 0; - callbacks$4.forEach(function(callback) { + changeAttrs.length = 0; + callbacks$3.forEach(function(callback) { callback(style); }); }, 0); } - changeAttrs$1.push(attr2); + changeAttrs.push(attr2); } -var callbacks$4 = []; -function onChange$1(callback) { - if (!getSupport$1()) { +var callbacks$3 = []; +function onChange(callback) { + if (!getSupport()) { return; } - if (!inited$2) { - init$1(); + if (!inited$1) { + init(); } if (typeof callback === "function") { - callbacks$4.push(callback); + callbacks$3.push(callback); } } -function offChange$1(callback) { - var index2 = callbacks$4.indexOf(callback); +function offChange(callback) { + var index2 = callbacks$3.indexOf(callback); if (index2 >= 0) { - callbacks$4.splice(index2, 1); + callbacks$3.splice(index2, 1); } } -var safeAreaInsets$1 = { +var safeAreaInsets = { get support() { - return (typeof support$1 === "string" ? support$1 : getSupport$1()).length != 0; + return (typeof support === "string" ? support : getSupport()).length != 0; }, get top() { - return getAttr$1("top"); + return getAttr("top"); }, get left() { - return getAttr$1("left"); + return getAttr("left"); }, get right() { - return getAttr$1("right"); + return getAttr("right"); }, get bottom() { - return getAttr$1("bottom"); + return getAttr("bottom"); }, - onChange: onChange$1, - offChange: offChange$1 + onChange, + offChange }; -var out$1 = safeAreaInsets$1; +var out = safeAreaInsets; const onEventPrevent = /* @__PURE__ */ withModifiers(() => { }, ["prevent"]); const onEventStop = /* @__PURE__ */ withModifiers(() => { @@ -497,10 +497,10 @@ function getWindowOffset() { const left = parseInt(style.getPropertyValue("--window-left")); const right = parseInt(style.getPropertyValue("--window-right")); return { - top: top ? top + out$1.top : 0, - bottom: bottom ? bottom + out$1.bottom : 0, - left: left ? left + out$1.left : 0, - right: right ? right + out$1.right : 0 + top: top ? top + out.top : 0, + bottom: bottom ? bottom + out.bottom : 0, + left: left ? left + out.left : 0, + right: right ? right + out.right : 0 }; } function updateCssVar(cssVars) { @@ -679,15 +679,15 @@ function getRealRoute(fromRoute, toRoute) { fromRouteArray.splice(fromRouteArray.length - i - 1, i + 1); return "/" + fromRouteArray.concat(toRouteArray).join("/"); } -const callbacks$3 = {}; +const callbacks$2 = {}; function createCallbacks(namespace) { - let scopedCallbacks = callbacks$3[namespace]; + let scopedCallbacks = callbacks$2[namespace]; if (!scopedCallbacks) { scopedCallbacks = { id: 1, callbacks: Object.create(null) }; - callbacks$3[namespace] = scopedCallbacks; + callbacks$2[namespace] = scopedCallbacks; } return { get(id2) { @@ -1135,192 +1135,6 @@ function initApp$1(app) { globalProperties.$applyOptions = applyOptions; } } -var attrs = ["top", "left", "right", "bottom"]; -var inited$1; -var elementComputedStyle = {}; -var support; -function getSupport() { - if (!("CSS" in window) || typeof CSS.supports != "function") { - support = ""; - } else if (CSS.supports("top: env(safe-area-inset-top)")) { - support = "env"; - } else if (CSS.supports("top: constant(safe-area-inset-top)")) { - support = "constant"; - } else { - support = ""; - } - return support; -} -function init() { - support = typeof support === "string" ? support : getSupport(); - if (!support) { - attrs.forEach(function(attr2) { - elementComputedStyle[attr2] = 0; - }); - return; - } - function setStyle(el, style) { - var elStyle = el.style; - Object.keys(style).forEach(function(key) { - var val = style[key]; - elStyle[key] = val; - }); - } - var cbs = []; - function parentReady(callback) { - if (callback) { - cbs.push(callback); - } else { - cbs.forEach(function(cb) { - cb(); - }); - } - } - var passiveEvents = false; - try { - var opts = Object.defineProperty({}, "passive", { - get: function() { - passiveEvents = {passive: true}; - } - }); - window.addEventListener("test", null, opts); - } catch (e2) { - } - function addChild(parent, attr2) { - var a1 = document.createElement("div"); - var a2 = document.createElement("div"); - var a1Children = document.createElement("div"); - var a2Children = document.createElement("div"); - var W = 100; - var MAX = 1e4; - var aStyle = { - position: "absolute", - width: W + "px", - height: "200px", - boxSizing: "border-box", - overflow: "hidden", - paddingBottom: support + "(safe-area-inset-" + attr2 + ")" - }; - setStyle(a1, aStyle); - setStyle(a2, aStyle); - setStyle(a1Children, { - transition: "0s", - animation: "none", - width: "400px", - height: "400px" - }); - setStyle(a2Children, { - transition: "0s", - animation: "none", - width: "250%", - height: "250%" - }); - a1.appendChild(a1Children); - a2.appendChild(a2Children); - parent.appendChild(a1); - parent.appendChild(a2); - parentReady(function() { - a1.scrollTop = a2.scrollTop = MAX; - var a1LastScrollTop = a1.scrollTop; - var a2LastScrollTop = a2.scrollTop; - function onScroll() { - if (this.scrollTop === (this === a1 ? a1LastScrollTop : a2LastScrollTop)) { - return; - } - a1.scrollTop = a2.scrollTop = MAX; - a1LastScrollTop = a1.scrollTop; - a2LastScrollTop = a2.scrollTop; - attrChange(attr2); - } - a1.addEventListener("scroll", onScroll, passiveEvents); - a2.addEventListener("scroll", onScroll, passiveEvents); - }); - var computedStyle = getComputedStyle(a1); - Object.defineProperty(elementComputedStyle, attr2, { - configurable: true, - get: function() { - return parseFloat(computedStyle.paddingBottom); - } - }); - } - var parentDiv = document.createElement("div"); - setStyle(parentDiv, { - position: "absolute", - left: "0", - top: "0", - width: "0", - height: "0", - zIndex: "-1", - overflow: "hidden", - visibility: "hidden" - }); - attrs.forEach(function(key) { - addChild(parentDiv, key); - }); - document.body.appendChild(parentDiv); - parentReady(); - inited$1 = true; -} -function getAttr(attr2) { - if (!inited$1) { - init(); - } - return elementComputedStyle[attr2]; -} -var changeAttrs = []; -function attrChange(attr2) { - if (!changeAttrs.length) { - setTimeout(function() { - var style = {}; - changeAttrs.forEach(function(attr3) { - style[attr3] = elementComputedStyle[attr3]; - }); - changeAttrs.length = 0; - callbacks$2.forEach(function(callback) { - callback(style); - }); - }, 0); - } - changeAttrs.push(attr2); -} -var callbacks$2 = []; -function onChange(callback) { - if (!getSupport()) { - return; - } - if (!inited$1) { - init(); - } - if (typeof callback === "function") { - callbacks$2.push(callback); - } -} -function offChange(callback) { - var index2 = callbacks$2.indexOf(callback); - if (index2 >= 0) { - callbacks$2.splice(index2, 1); - } -} -var safeAreaInsets = { - get support() { - return (typeof support === "string" ? support : getSupport()).length != 0; - }, - get top() { - return getAttr("top"); - }, - get left() { - return getAttr("left"); - }, - get right() { - return getAttr("right"); - }, - get bottom() { - return getAttr("bottom"); - }, - onChange, - offChange -}; -var out = safeAreaInsets; const pageMetaKey = PolySymbol(process.env.NODE_ENV !== "production" ? "UniPageMeta" : "upm"); function usePageMeta() { return inject(pageMetaKey); diff --git a/packages/vite-plugin-uni/src/configResolved/plugins/easycom.ts b/packages/vite-plugin-uni/src/configResolved/plugins/easycom.ts index f9099d9b9..418462e2f 100644 --- a/packages/vite-plugin-uni/src/configResolved/plugins/easycom.ts +++ b/packages/vite-plugin-uni/src/configResolved/plugins/easycom.ts @@ -80,10 +80,14 @@ export function uniEasycomPlugin(options: UniPluginFilterOptions): Plugin { } const source = matchEasycom(name) if (source) { - return addImportDeclaration( - importDeclarations, - `__easycom_${i++}`, - source + return ( + // 解决局部引入组件优先级(理论上让开发者使用script setup就可以解决局部引入) + `typeof ${name} !== 'undefined' ? ${name} : ` + + addImportDeclaration( + importDeclarations, + `__easycom_${i++}`, + source + ) ) } } -- GitLab