Skip to content

Commit

Permalink
feat: support layout dir globs
Browse files Browse the repository at this point in the history
closes #119
  • Loading branch information
JohnCampionJr committed Dec 5, 2023
1 parent 31d3474 commit 1cead57
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ All .vue files in this folder are imported async into the generated code.

Can also be an array of layout dirs

Can use `**` to support scenarios like `module1/layouts` and `modules2/layouts` with a setting of `src/**/layouts`

Any files named `__*__.vue` will be excluded, and you can specify any additional exclusions with the `exclude` option

**Default:** `'src/layouts'`
Expand Down
8 changes: 8 additions & 0 deletions examples/spa/src/module1/layouts/module1layout.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<template>
<main class="px-4 py-10 text-center text-gray-700 dark:text-gray-200">
<div class="w-1/4 m-auto text-center text-gray-300 bg-teal-800">
Module 1 Layout
</div>
<router-view />
</main>
</template>
8 changes: 8 additions & 0 deletions examples/spa/src/module2/layouts/module2layout.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<template>
<main class="px-4 py-10 text-center text-gray-700 dark:text-gray-200">
<div class="w-1/4 m-auto text-center text-gray-300 bg-teal-800">
Module 2 Layout
</div>
<router-view />
</main>
</template>
10 changes: 10 additions & 0 deletions examples/spa/src/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@
no layout
</router-link>
</p>
<p>
<router-link to="/module1">
module 1 layout
</router-link>
</p>
<p>
<router-link to="/module2">
module 2 layout
</router-link>
</p>
</div>
</template>

Expand Down
10 changes: 10 additions & 0 deletions examples/spa/src/pages/module1.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<template>
<router-view />
</template>
<route>
{
meta: {
layout: "module1layout"
}
}
</route>
10 changes: 10 additions & 0 deletions examples/spa/src/pages/module2.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<template>
<router-view />
</template>
<route>
{
meta: {
layout: "module2layout"
}
}
</route>
3 changes: 2 additions & 1 deletion examples/spa/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ const config = defineConfig({
syncIndex: false,
}),
Layouts({
defaultLayout: 'default'
defaultLayout: 'default',
layoutsDirs: ['src/layouts', 'src/**/layouts'],
}),
Markdown(),
],
Expand Down
14 changes: 13 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { resolve } from 'path'

import fg from 'fast-glob'
import type { ModuleNode, Plugin, ResolvedConfig } from 'vite'
import { createVirtualModuleCode } from './clientSide'
import { getFilesFromPath } from './files'
Expand Down Expand Up @@ -94,8 +94,20 @@ export default function Layout(userOptions: UserOptions = {}): Plugin {
? options.layoutsDirs
: [options.layoutsDirs]
const container: FileContainer[] = []
const layoutDirsResolved: string[] = []

for (const dir of layoutDirs) {
if (dir.includes('**')) {
const matches = await fg(dir, { onlyDirectories: true })
for (const match of matches)
layoutDirsResolved.push(normalizePath(resolve(config.root, match)))
}
else {
layoutDirsResolved.push(dir)
}
}

for (const dir of layoutDirsResolved) {
const layoutsDirPath = dir.substr(0, 1) === '/'
? normalizePath(dir)
: normalizePath(resolve(config.root, dir))
Expand Down

0 comments on commit 1cead57

Please sign in to comment.