From a119a9b97e0ddde492ea43c944f2f58201aa0c9a Mon Sep 17 00:00:00 2001
From: Skiyee <319619193@qq.com>
Date: Thu, 22 Aug 2024 22:37:27 +0800
Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=9A=80=20=E5=AE=9E=E7=8E=B0=20App?=
=?UTF-8?q?.ku=20=E7=9A=84=E6=A8=A1=E6=9D=BF=E5=BC=95=E7=94=A8?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
closed #3
---
src/index.ts | 8 ++++++--
src/page.ts | 14 +++++++++++---
src/root.ts | 4 ++--
src/utils.ts | 2 +-
4 files changed, 20 insertions(+), 8 deletions(-)
diff --git a/src/index.ts b/src/index.ts
index 3b7d52f..6d5f16b 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -11,7 +11,11 @@ import { loadPagesJson } from './utils'
import { rebuildKuApp, registerKuApp } from './root'
import { transformPage } from './page'
-export default function UniKuRoot(): Plugin {
+interface UniKuRootOptions {
+ enabledGlobalRef?: boolean
+}
+
+export default function UniKuRoot(options: UniKuRootOptions = {}): Plugin {
const rootPath = process.env.UNI_INPUT_DIR || (`${process.env.INIT_CWD}\\src`)
const appKuPath = resolve(rootPath, 'App.ku.vue')
const pagesPath = resolve(rootPath, 'pages.json')
@@ -41,7 +45,7 @@ export default function UniKuRoot(): Plugin {
const filterPage = createFilter(pagesJson)
if (filterPage(id)) {
- ms = await transformPage(code)
+ ms = await transformPage(code, options.enabledGlobalRef)
}
if (ms) {
diff --git a/src/page.ts b/src/page.ts
index e303c94..bca47d8 100644
--- a/src/page.ts
+++ b/src/page.ts
@@ -2,7 +2,7 @@ import { MagicString } from 'vue/compiler-sfc'
import { findNode, parseSFC } from './utils'
-export async function transformPage(code: string) {
+export async function transformPage(code: string, enabledGlobalRef = false) {
const sfc = await parseSFC(code)
const ms = new MagicString(code)
@@ -21,9 +21,17 @@ export async function transformPage(code: string) {
ms.remove(metaTempStart, metaTempEnd)
}
+ const pageTempAttrs = sfc.template?.attrs
+
+ let pageRootRefSource = enabledGlobalRef ? 'ref="uniKuRoot"' : ''
+
+ if (pageTempAttrs && pageTempAttrs.root) {
+ pageRootRefSource = `ref="${pageTempAttrs.root as string}"`
+ }
+
if (pageTempStart && pageTempEnd) {
- ms.appendLeft(pageTempStart, `\n${pageMetaSource}\n`)
- ms.appendRight(pageTempEnd, `\n`)
+ ms.appendLeft(pageTempStart, `\n${pageMetaSource}\n`)
+ ms.appendRight(pageTempEnd, `\n`)
}
return ms
diff --git a/src/root.ts b/src/root.ts
index 6ab70b4..89ddfed 100644
--- a/src/root.ts
+++ b/src/root.ts
@@ -5,9 +5,9 @@ import { MagicString } from 'vue/compiler-sfc'
export async function registerKuApp(code: string) {
const ms = new MagicString(code)
- const importCode = `import KuAppRoot from "./App.ku.vue";`
+ const importCode = `import UniKuAppRoot from "./App.ku.vue";`
- const vueUseComponentCode = `app.component("uni-ku-root", KuAppRoot);`
+ const vueUseComponentCode = `app.component("uni-ku-app-root", UniKuAppRoot);`
ms.prepend(`${importCode}\n`).replace(
/(createApp[\s\S]*?)(return\s\{\s*app)/,
diff --git a/src/utils.ts b/src/utils.ts
index b015f52..b68283c 100644
--- a/src/utils.ts
+++ b/src/utils.ts
@@ -14,7 +14,7 @@ export async function parseSFC(code: string): Promise {
}
catch {
throw new Error(
- '[vite-plugin-uni-root] Vue3\'s "@vue/compiler-sfc" is required.',
+ '[@uni-ku/root] Vue\'s version must be 3.2.13 or higher.',
)
}
}