diff --git a/packages/uni-app-vite/src/plugin/uni/index.ts b/packages/uni-app-vite/src/plugin/uni/index.ts index 27a3c3ec8baf65a2fe64953358178d0c70780588..afbfee8efb7f2b5a15fdbeb6589aa450cb6cd17c 100644 --- a/packages/uni-app-vite/src/plugin/uni/index.ts +++ b/packages/uni-app-vite/src/plugin/uni/index.ts @@ -1,6 +1,10 @@ import { isNativeTag, isCustomElement } from '@dcloudio/uni-shared' import { compileI18nJsonStr } from '@dcloudio/uni-i18n' -import { UniVitePlugin, initI18nOptions } from '@dcloudio/uni-cli-shared' +import { + UniVitePlugin, + initI18nOptions, + transformPageHead, +} from '@dcloudio/uni-cli-shared' export function uniOptions(): UniVitePlugin['uni'] { return { @@ -28,6 +32,7 @@ export function uniOptions(): UniVitePlugin['uni'] { compilerOptions: { isNativeTag, isCustomElement, + nodeTransforms: [transformPageHead], }, transformEvent: { tap: 'click', diff --git a/packages/uni-cli-shared/src/utils.ts b/packages/uni-cli-shared/src/utils.ts index d7db2c15aefb2b6cc088b5d58d877c1c55d182af..c066c6ab45b1b8f154a352777c5bea6f21cb7781 100644 --- a/packages/uni-cli-shared/src/utils.ts +++ b/packages/uni-cli-shared/src/utils.ts @@ -7,11 +7,23 @@ export { default as hash } from 'hash-sum' import { PAGE_EXTNAME, PAGE_EXTNAME_APP } from './constants' import { SFCTemplateCompileOptions } from '@vue/compiler-sfc' +import { NodeTypes, ElementNode, Node } from '@vue/compiler-core' export const isWindows = os.platform() === 'win32' export function normalizePath(id: string): string { return isWindows ? id.replace(/\\/g, '/') : id } +export function checkElementNodeTag( + node: Node | null | undefined, + tag: string +): node is ElementNode { + return ( + !!node && + node.type === NodeTypes.ELEMENT && + (node as ElementNode).tag === tag + ) +} + export const resolveMainPathOnce = once((inputDir: string) => { const mainTsPath = path.resolve(inputDir, 'main.ts') if (fs.existsSync(mainTsPath)) { diff --git a/packages/uni-cli-shared/src/vue/transforms/transformPageHead.ts b/packages/uni-cli-shared/src/vue/transforms/transformPageHead.ts index f18a4c19175f94f85a39cf5a2846eb31c062dd77..c5702e27b640797431f0c1e6a7b1d6e59c38a766 100644 --- a/packages/uni-cli-shared/src/vue/transforms/transformPageHead.ts +++ b/packages/uni-cli-shared/src/vue/transforms/transformPageHead.ts @@ -1,5 +1,9 @@ import { NodeTransform } from '@vue/compiler-core' +import { checkElementNodeTag } from '../../utils' -export const transformPageHead: NodeTransform = (_node, _context) => { - // 发现是page-meta下的page-head,直接remove该节点 +export const transformPageHead: NodeTransform = (node, context) => { + // 发现是page-meta下的page-meta-head,直接remove该节点 + checkElementNodeTag(node, 'page-meta-head') && + checkElementNodeTag(context.parent, 'page-meta') && + context.removeNode(node) } diff --git a/packages/uni-components/lib/page-meta-head/page-meta-head.vue b/packages/uni-components/lib/page-meta-head/page-meta-head.vue new file mode 100644 index 0000000000000000000000000000000000000000..197b7b32e5c93f22f93eb9b9a9df2d9479af89a9 --- /dev/null +++ b/packages/uni-components/lib/page-meta-head/page-meta-head.vue @@ -0,0 +1,9 @@ + + diff --git a/packages/uni-mp-vite/src/plugin/uni/index.ts b/packages/uni-mp-vite/src/plugin/uni/index.ts index 6a4710c36b3c0cc65308e3bd64edd3a5eadd72ca..26662655345b35c006a71eb24cf6e4878f743b84 100644 --- a/packages/uni-mp-vite/src/plugin/uni/index.ts +++ b/packages/uni-mp-vite/src/plugin/uni/index.ts @@ -4,6 +4,7 @@ import { CopyOptions, UniVitePlugin, MiniProgramCompilerOptions, + transformPageHead, } from '@dcloudio/uni-cli-shared' import { TemplateCompiler } from '@vue/compiler-sfc' import type { CompilerOptions } from '@dcloudio/uni-mp-compiler' @@ -26,6 +27,10 @@ export function uniOptions({ isNativeTag, isCustomElement, ...compilerOptions, + nodeTransforms: [ + transformPageHead, + ...(compilerOptions?.nodeTransforms || []), + ], } as any, } }