Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ node_modules

# Build Outputs
build
dist
dist
.idea
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,39 @@ onMounted(() => {

</details>

<details>

<summary>
<strong>(点击展开) 功能三:过滤掉不需要根组件的页面</strong>
</summary>
<br />

如果遇到一些不需要根组件的页面,可以设置 `excludePages` 选项来过滤

> `excludePages` 选项支持采用 glob 模式进行编写

```ts
// vite.config.(js|ts)

import Uni from '@dcloudio/vite-plugin-uni'
import UniKuRoot from '@uni-ku/root'
import { defineConfig } from 'vite'

export default defineConfig({
plugins: [
UniKuRoot({
excludePages: [
'src/exclude.vue',
'src/exclude/**/*.vue'
],
}),
Uni()
]
})
```

</details>

### ✨ 例子

> [!TIP]
Expand Down
6 changes: 6 additions & 0 deletions examples/src/pages.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
"style": {
"navigationBarTitleText": "uni-app"
}
},
{
"path": "pages/excluded",
"style": {
"navigationBarTitleText": "uni-app"
}
}
],
"globalStyle": {
Expand Down
42 changes: 42 additions & 0 deletions examples/src/pages/excluded.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<script setup lang="ts">
import { useToast } from '@/composables/useToast'
import LayoutDefault from '@/layouts/default.vue'
import { onMounted, ref } from 'vue'

const { globalToastState, showToast, hideToast } = useToast()

function handleClick() {
return globalToastState.value ? hideToast() : showToast()
}

const uniKuRoot = ref()

onMounted(() => {
// eslint-disable-next-line no-console
console.log(uniKuRoot.value?.exposeRef)
})

const color = 'red'
</script>

<template root="uniKuRoot">
<LayoutDefault>
<!-- 这行代码仅小程序可见,完全支持 PageMeta -->

<view>
Hello KuRoot
</view>
<view class="text">
Hello v-bind css
</view>
<button @click="handleClick">
展示Toast
</button>
</LayoutDefault>
</template>

<style scoped>
.text {
color: v-bind(color)
}
</style>
1 change: 1 addition & 0 deletions examples/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default defineConfig({
UniKuRoot({
enabledVirtualHost: false,
rootFileName: 'KuRoot',
excludePages: 'src/pages/excluded.vue',
}),
Uni(),
],
Expand Down
12 changes: 10 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { MagicString } from '@vue/compiler-sfc'
import type { FSWatcher } from 'chokidar'
import type { Plugin } from 'vite'
import type { FilterPattern, Plugin } from 'vite'

import { resolve } from 'node:path'
import process from 'node:process'
Expand Down Expand Up @@ -28,6 +28,14 @@ interface UniKuRootOptions {
* @default 'App.ku'
*/
rootFileName?: string
/**
* 需要排除根组件的页面,支持 glob 匹配
* @example
* ```
* ['src/pages/some.vue', 'src/exclude/*.vue']
* ```
*/
excludePages?: FilterPattern
}
Comment thread
skiyee marked this conversation as resolved.

export default function UniKuRoot(options: UniKuRootOptions = {
Expand Down Expand Up @@ -71,7 +79,7 @@ export default function UniKuRoot(options: UniKuRootOptions = {

const pageId = hasPlatformPlugin ? normalizePlatformPath(id) : id

const filterPage = createFilter(pagesJson)
const filterPage = createFilter(pagesJson, options.excludePages)
if (filterPage(pageId)) {
ms = await transformPage(code, options.enabledGlobalRef)
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function formatPagePath(root: string, path: string) {
return normalizePath(`${join(root, path)}.vue`)
}

export function loadPagesJson(path: string, rootPath: string) {
export function loadPagesJson(path: string, rootPath: string): string[] {
const pagesJsonRaw = readFileSync(path, 'utf-8')

const { pages = [], subPackages = [] } = jsonParse(pagesJsonRaw)
Expand Down