From c28e777b263bc990b935fba8aad64cf9033f2990 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Fri, 13 Aug 2021 19:58:45 +0800 Subject: [PATCH] fix: support multiple wxs --- .../src/framework/components/layout/index.tsx | 14 ++-- .../src/configResolved/plugins/preVue.ts | 68 ++++++++++--------- 2 files changed, 44 insertions(+), 38 deletions(-) diff --git a/packages/uni-h5/src/framework/components/layout/index.tsx b/packages/uni-h5/src/framework/components/layout/index.tsx index 603b776fe..b648308b5 100644 --- a/packages/uni-h5/src/framework/components/layout/index.tsx +++ b/packages/uni-h5/src/framework/components/layout/index.tsx @@ -24,7 +24,7 @@ import { defineSystemComponent } from '@dcloudio/uni-components' import { updateCssVar } from '@dcloudio/uni-core' import { useTabBar } from '../../setup/state' import { useKeepAliveRoute } from '../../setup/page' -import { RESPONSIVE_MIN_WIDTH } from '@dcloudio/uni-shared' +import { resolveOwnerEl, RESPONSIVE_MIN_WIDTH } from '@dcloudio/uni-shared' import { checkMinWidth } from '../../../helpers/dom' import { hasOwn } from '@vue/shared' @@ -388,8 +388,8 @@ function useTopWindow(layoutState: LayoutState): WindowComponentInfo { const { component, style } = __uniConfig.topWindow! const windowRef: Ref = ref(null) function updateWindow() { - const instalce = windowRef.value as ComponentPublicInstance - const el: HTMLElement = instalce.$el + const instance = windowRef.value as ComponentPublicInstance + const el = resolveOwnerEl(instance.$) as HTMLElement const height = el.getBoundingClientRect().height layoutState.topWindowHeight = height } @@ -408,8 +408,8 @@ function useLeftWindow(layoutState: LayoutState): WindowComponentInfo { const { component, style } = __uniConfig.leftWindow! const windowRef: Ref = ref(null) function updateWindow() { - const instalce = windowRef.value as ComponentPublicInstance - const el: HTMLElement = instalce.$el + const instance = windowRef.value as ComponentPublicInstance + const el = resolveOwnerEl(instance.$) as HTMLElement const width = el.getBoundingClientRect().width layoutState.leftWindowWidth = width } @@ -428,8 +428,8 @@ function useRightWindow(layoutState: LayoutState): WindowComponentInfo { const { component, style } = __uniConfig.rightWindow! const windowRef: Ref = ref(null) function updateWindow() { - const instalce = windowRef.value as ComponentPublicInstance - const el: HTMLElement = instalce.$el + const instance = windowRef.value as ComponentPublicInstance + const el = resolveOwnerEl(instance.$) as HTMLElement const width = el.getBoundingClientRect().width layoutState.rightWindowWidth = width } diff --git a/packages/vite-plugin-uni/src/configResolved/plugins/preVue.ts b/packages/vite-plugin-uni/src/configResolved/plugins/preVue.ts index 114abf173..6aec357c6 100644 --- a/packages/vite-plugin-uni/src/configResolved/plugins/preVue.ts +++ b/packages/vite-plugin-uni/src/configResolved/plugins/preVue.ts @@ -113,7 +113,7 @@ function normalizeBlockNode(code: string, blocks: ElementNode[]) { } export function normalizeWxsCode(ast: RootNode, code: string) { - const wxsNode = ast.children.find( + const wxsNodes = ast.children.filter( (node) => node.type === NodeTypes.ELEMENT && node.tag === 'script' && @@ -125,8 +125,8 @@ export function normalizeWxsCode(ast: RootNode, code: string) { WXS_ATTRS.includes(prop.value.content) ) ) - if (wxsNode) { - code = normalizeWxsNode(code, wxsNode as ElementNode) + if (wxsNodes.length) { + code = normalizeWxsNode(code, wxsNodes as ElementNode[]) } return code } @@ -134,35 +134,41 @@ export function normalizeWxsCode(ast: RootNode, code: string) { const SCRIPT_END_LEN = ''.length const SCRIPT_START_LEN = ' prop.name === 'lang') as AttributeNode - const moduleAttr = props.find( - (prop) => prop.name === 'module' - ) as AttributeNode - const startOffset = loc.start.offset - const endOffset = loc.end.offset - const lang = langAttr.value!.content - const langStartOffset = langAttr.loc.start.offset - magicString.overwrite(startOffset, startOffset + SCRIPT_START_LEN, '<' + lang) // ' - ) // or - - if (moduleAttr) { - const moduleStartOffset = moduleAttr.loc.start.offset + nodes.forEach(({ loc, props }) => { + const langAttr = props.find((prop) => prop.name === 'lang') as AttributeNode + const moduleAttr = props.find( + (prop) => prop.name === 'module' + ) as AttributeNode + const startOffset = loc.start.offset + const endOffset = loc.end.offset + const lang = langAttr.value!.content + const langStartOffset = langAttr.loc.start.offset magicString.overwrite( - moduleStartOffset, - moduleStartOffset + 'module'.length, - 'name' - ) // module="echarts" => name="echarts" - } + startOffset, + startOffset + SCRIPT_START_LEN, + '<' + lang + ) // ' + ) // or + + if (moduleAttr) { + const moduleStartOffset = moduleAttr.loc.start.offset + magicString.overwrite( + moduleStartOffset, + moduleStartOffset + 'module'.length, + 'name' + ) // module="echarts" => name="echarts" + } + }) return magicString.toString() } -- GitLab