Skip to content

Commit

Permalink
feat: 🚀 实现 App.ku 的模板引用
Browse files Browse the repository at this point in the history
closed #3
  • Loading branch information
skiyee committed Aug 22, 2024
1 parent 583efb5 commit a119a9b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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) {
Expand Down
14 changes: 11 additions & 3 deletions src/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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<uni-ku-root>`)
ms.appendRight(pageTempEnd, `</uni-ku-root>\n`)
ms.appendLeft(pageTempStart, `\n${pageMetaSource}\n<uni-ku-app-root ${pageRootRefSource}>`)
ms.appendRight(pageTempEnd, `</uni-ku-app-root>\n`)
}

return ms
Expand Down
4 changes: 2 additions & 2 deletions src/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)/,
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export async function parseSFC(code: string): Promise<SFCDescriptor> {
}
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.',
)
}
}
Expand Down

0 comments on commit a119a9b

Please sign in to comment.