{"_id":"@dream-uikit/vite-plugin-vue-layouts","name":"@dream-uikit/vite-plugin-vue-layouts","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"@dream-uikit/vite-plugin-vue-layouts","type":"module","version":"1.0.0","description":"Router-based layout plugin for Vite and Vue, supports the latest versions.","author":{"name":"ellise@qq.com"},"license":"MIT","keywords":["typescript","vite-plugin","vite6","vue-router","vue"],"exports":{"./client":{"types":"./client.d.ts"},"./*":"./*",".":{"types":"./dist/index.d.ts","import":"./dist/index.js","require":"./dist/index.cjs"}},"main":"dist/index.cjs","module":"dist/index.js","types":"dist/index.d.ts","scripts":{"dev":"npm run build -- --watch","build":"tsup src/index.ts --dts --format cjs,esm","prepublishOnly":"npm run build","release":"npx bumpp --commit --tag --push && npm publish","example:dev":"npm -C examples/spa run dev","example:build":"npm -C examples/spa run build","example:serve":"npm -C examples/spa run serve","example:dev-ssg":"npm -C examples/ssg run dev","example:build-ssg":"npm -C examples/ssg run build","example:serve-ssg":"npm -C examples/ssg run serve","example:dev-client":"npm -C examples/client-side run dev","example:build-client":"npm -C examples/client-side run build","example:serve-client":"npm -C examples/client-side run serve","example:dev-unplugin":"npm -C examples/unplugin-vue-router run dev","example:build-unplugin":"npm -C examples/unplugin-vue-router run build","example:serve-unplugin":"npm -C examples/unplugin-vue-router run serve","example:dev-vitesse":"npm -C examples/vitesse run dev","example:build-vitesse":"npm -C examples/vitesse run build","example:serve-vitesse":"npm -C examples/vitesse run preview","lint":"eslint","lint:fix":"eslint --fix"},"peerDependencies":{"vite":"^4.0.0 || ^5.0.0 || ^6.0.0","vue":"^3.2.4","vue-router":"^4.0.11"},"dependencies":{"debug":"^4.4.1","fast-glob":"^3.3.3"},"devDependencies":{"@antfu/eslint-config":"^4.13.2","@types/debug":"^4.1.12","@types/node":"^22.15.26","cross-env":"^7.0.3","eslint":"^9.27.0","eslint-plugin-format":"^1.0.1","rollup":"^4.41.1","tsup":"^8.5.0","typescript":"^5.8.3","vite":"^6.3.5","vue":"^3.5.16","vue-router":"^4.5.1"},"_id":"@dream-uikit/vite-plugin-vue-layouts@1.0.0","gitHead":"1a8add0b285782d1934d4615e027277bc3df0de2","_nodeVersion":"24.0.2","_npmVersion":"11.3.0","dist":{"integrity":"sha512-2h4MTGruR8dFRcBgaLL8oIYGQ8i+vVH54A5PdDD67vgIlNOdy/vDNxTn5/yldkb/XkKuqFM3m4udplx6okGdKg==","shasum":"5182640c71973c6af346dce2e3a55efafb57f98b","tarball":"https://registry.npmjs.org/@dream-uikit/vite-plugin-vue-layouts/-/vite-plugin-vue-layouts-1.0.0.tgz","fileCount":8,"unpackedSize":37422,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIGDCrioLyx1xR0FsWcwOtqg4CAmf6wfrcFkwlveBf247AiEAi3G9gJe6gSfYGd1OuBA8hJDgdmVynz0qUQ91aSRyjhk="}]},"_npmUser":{"name":"ellise","email":"ellise@qq.com"},"directories":{},"maintainers":[{"name":"ellise","email":"ellise@qq.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/vite-plugin-vue-layouts_1.0.0_1754405312232_0.426909983387568"},"_hasShrinkwrap":false}},"time":{"created":"2025-08-05T14:48:32.146Z","1.0.0":"2025-08-05T14:48:32.426Z","modified":"2025-08-05T14:48:32.729Z"},"maintainers":[{"name":"ellise","email":"ellise@qq.com"}],"description":"Router-based layout plugin for Vite and Vue, supports the latest versions.","keywords":["typescript","vite-plugin","vite6","vue-router","vue"],"author":{"name":"ellise@qq.com"},"license":"MIT","readme":"# vite-plugin-vue-layouts-next\n\n[![npm version](https://img.shields.io/npm/v/vite-plugin-vue-layouts-next)](https://www.npmjs.com/package/vite-plugin-vue-layouts-next)\n\n> Router based layout for Vue 3 applications using [Vite](https://github.com/vitejs/vite)\n\n## Overview\n\nA fork of [vite-plugin-vue-layouts](https://github.com/JohnCampionJr/vite-plugin-vue-layouts) with some improvements and fixes, supports the latest versions of Vite and Vue.\n\nThis works best along with the [vite-plugin-pages](https://github.com/hannoeru/vite-plugin-pages).\n\nLayouts are stored in the `/src/layouts` folder by default and are standard Vue components with a `<router-view></router-view>` in the template.\n\nPages without a layout specified use `default.vue` for their layout.\n\nYou can use route blocks to allow each page to determine its layout.  The block below in a page will look for `/src/layouts/users.vue` for its layout.\n\nSee the [Vitesse starter template](https://github.com/antfu/vitesse) for a working example with [vite-plugin-vue-layouts](https://github.com/JohnCampionJr/vite-plugin-vue-layouts) and it also works similarly with this.\n\n```html\n<route lang=\"yaml\">\nmeta:\n  layout: users\n</route>\n```\n\n## Getting Started\n\nInstall Layouts:\n\n```bash\nnpm install -D vite-plugin-vue-layouts-next\n```\n\nAdd to your `vite.config.js`:\n\n```js\nimport Vue from '@vitejs/plugin-vue'\nimport Pages from 'vite-plugin-pages'\nimport Layouts from 'vite-plugin-vue-layouts-next'\n\nexport default {\n  plugins: [Vue(), Pages(), Layouts()],\n}\n```\n\nIn main.ts, you need to add a few lines to import the generated code and setup the layouts.\n\n## vue-router\n\n```js\nimport { setupLayouts } from 'virtual:generated-layouts'\nimport { createRouter } from 'vue-router'\nimport generatedRoutes from '~pages'\n\nconst routes = setupLayouts(generatedRoutes)\n\nconst router = createRouter({\n  // ...\n  routes,\n})\n```\n\n## [unplugin-vue-router](https://github.com/posva/unplugin-vue-router)\n\n```js\nimport { setupLayouts } from 'virtual:generated-layouts'\nimport { createRouter } from 'vue-router'\nimport { routes } from 'vue-router/auto-routes'\n\nconst router = createRouter({\n  // ...\n  routes: setupLayouts(routes),\n})\n```\n\n## Client Types\n\nIf you want type definition of `virtual:generated-layouts`, add `vite-plugin-vue-layouts-next/client` to `compilerOptions.types` of your `tsconfig`:\n\n```json\n{\n  \"compilerOptions\": {\n    \"types\": [\"vite-plugin-vue-layouts-next/client\"]\n  }\n}\n```\n\n## Configuration\n\n```ts\ninterface UserOptions {\n  layoutsDirs?: string | string[]\n  pagesDirs?: string | string[] | null\n  extensions?: string[]\n  exclude?: string[]\n  defaultLayout?: string\n  importMode?: (name: string) => 'sync' | 'async'\n}\n```\n\n### Using configuration\n\nTo use custom configuration, pass your options to Layouts when instantiating the plugin:\n\n```js\n// vite.config.js\nimport Layouts from 'vite-plugin-vue-layouts-next'\n\nexport default {\n  plugins: [\n    Layouts({\n      layoutsDirs: 'src/mylayouts',\n      pagesDirs: 'src/pages',\n      defaultLayout: 'myDefault'\n    }),\n  ],\n}\n```\n\n### layoutsDirs\n\nRelative path to the layouts directory. Supports globs.\nAll .vue files in this folder are imported async into the generated code.\n\nCan also be an array of layout dirs\n\nCan use `**` to support scenarios like `module1/layouts` and `modules2/layouts` with a setting of `src/**/layouts`\n\nAny files named `__*__.vue` will be excluded, and you can specify any additional exclusions with the `exclude` option\n\n**Default:** `'src/layouts'`\n\n### pagesDirs\n\nDefines the pages dir to avoid HMR reloading for all added or deleted files anywhere in the project.\n\nRelative path to the pages directory.  If you want it to watch for all files, like in v0.8.0 or earlier, set to null.\n\nCan also be an array of layout dirs or use `**` glob patterns\n\n**Default:** `'src/pages'`\n\n### extensions\n\nValid file extensions for page components.\n\n**Default:** `['vue']`\n\n### exclude\n\nList of path globs to exclude when resolving pages.\n\n### defaultLayout\n\nFilename of default layout (\".vue\" is not needed).\n\n**Default:** `'default'`\n\n### importMode\n\nMode for importing layouts.\n\n**Default:** ssg is `'sync'`，other is `'async'`\n\n## How it works\n\n`setupLayouts` transforms the original `router` by\n\n1. Replacing every page with its specified layout\n2. Appending the original page in the `children` property.\n\nSimply put, layouts are [nested routes](https://next.router.vuejs.org/guide/essentials/nested-routes.html#nested-routes) with the same path.\n\nBefore:\n\n```text\nrouter: [ page1, page2, page3 ]\n```\n\nAfter `setupLayouts()`:\n\n```text\nrouter: [\n  layoutA: page1,\n  layoutB: page2,\n  layoutA: page3,\n]\n```\n\nThat means you have the full flexibility of the [vue-router API](https://next.router.vuejs.org/api/) at your disposal.\n\n## Common patterns\n\n### Transitions\n\nLayouts and Transitions work as expected and explained in the [vue-router docs](https://next.router.vuejs.org/guide/advanced/transitions.html) only as long as `Component` changes on each route. So if you want a transition between pages with the same layout *and* a different layout, you have to mutate `:key` on `<component>` (for a detailed example, see the vue docs about [transitions between elements](https://v3.vuejs.org/guide/transitions-enterleave.html#transitioning-between-elements)).\n\n`App.vue`\n\n```html\n<template>\n  <router-view v-slot=\"{ Component, route }\">\n    <transition name=\"slide\">\n      <component :is=\"Component\" :key=\"route\" />\n    </transition>\n  </router-view>\n</template>\n```\n\nNow Vue will always trigger a transition if you change the route.\n\n### Data from layout to page\n\nIf you want to send data *down* from the layout to the page, use props\n\n```html\n<router-view foo=\"bar\" />\n```\n\n### Set static data at the page\n\nIf you want to set state in your page and do something with it in your layout, add additional properties to a route's `meta` property. Doing so only works if you know the state at build-time.\n\nYou can use the `<route>` block if you work with [vite-plugin-pages](https://github.com/hannoeru/vite-plugin-pages).\n\nIn `page.vue`:\n\n```html\n<template><div>Content</div></template>\n<route lang=\"yaml\">\nmeta:\n  layout: default\n  bgColor: yellow\n</route>\n```\n\nNow you can read `bgColor` in `layout.vue`:\n\n```html\n<script setup>\nimport { useRouter } from 'vue-router'\n</script>\n<template>\n  <div :style=\"`background: ${useRouter().currentRoute.value.meta.bgColor};`\">\n    <router-view />\n  </div>\n</template>\n```\n\n### Data dynamically from page to layout\n\nIf you need to set `bgColor` dynamically at run-time, you can use [custom events](https://v3.vuejs.org/guide/component-custom-events.html#custom-events).\n\nEmit the event in `page.vue`:\n\n```html\n<script setup>\nimport { defineEmit } from 'vue'\nconst emit = defineEmit(['setColor'])\n\nif (2 + 2 === 4)\n  emit('setColor', 'green')\nelse\n  emit('setColor', 'red')\n</script>\n```\n\nListen for `setColor` custom-event in `layout.vue`:\n\n```html\n<script setup>\nimport { ref } from 'vue'\n\nconst bgColor = ref('yellow')\nconst setBg = (color) => {\n  bgColor.value = color\n}\n</script>\n\n<template>\n  <main :style=\"`background: ${bgColor};`\">\n    <router-view @set-color=\"setBg\" />\n  </main>\n</template>\n```\n\n## ClientSideLayout\n\nThe clientSideLayout uses a simpler [virtual file](https://vitejs.dev/guide/api-plugin.html#importing-a-virtual-file) + [glob import](https://vitejs.dev/guide/features.html#glob-import) scheme, This means that its hmr is faster and more accurate, but also more limited\n\n### Usage\n\n```js\n// vite.config.js\nimport { ClientSideLayout } from 'vite-plugin-vue-layouts-next'\n\nexport default {\n  plugins: [\n    ClientSideLayout({\n      layoutsDir: 'src/mylayouts', // default to 'src/layouts'\n      defaultLayout: 'myDefault', // default to 'default', no need '.vue'\n      importMode: 'sync' // The default will automatically detect -> ssg is sync，other is async\n    }),\n  ],\n}\n```\n","readmeFilename":"README.md","_rev":"1-80b4afec750c704cb3fb106bc54bffd7"}