|
- 'use strict';
-
- const path = require('path');
- const localPkg = require('local-pkg');
-
- const normalizePath = (path$1) => {
- path$1 = path$1.startsWith("/") ? path$1 : "/" + path$1;
- return path.posix.normalize(path$1);
- };
- const isVite2 = async () => {
- const info = await localPkg.getPackageInfo("vite");
- if (info) {
- return /.?2/.test(info.version);
- }
- return false;
- };
-
- const createPluginName = (reusable = false) => {
- let i = 0;
- return (name) => {
- const base = `vite-plugin-${name}`;
- return reusable ? `${base}:${i++}` : base;
- };
- };
- const createVirtualModuleID = (name) => {
- const virtualModuleId = `virtual:${name}`;
- const resolvedVirtualModuleId = "\0" + virtualModuleId;
- return {
- virtualModuleId,
- resolvedVirtualModuleId
- };
- };
- const createVirtualGlob = async (target, isSync) => {
- const g = `"${target}/**/*.vue"`;
- if (await isVite2()) {
- return isSync ? `import.meta.globEager(${g})` : `import.meta.glob(${g})`;
- }
- return `import.meta.glob(${g}, { eager: ${isSync} })`;
- };
- const createVirtualModuleCode = async (options) => {
- const { target, defaultLayout, importMode } = options;
- const normalizedTarget = normalizePath(target);
- const isSync = importMode === "sync";
- return `
- export const createGetRoutes = (router, withLayout = false) => {
- const routes = router.getRoutes()
- if (withLayout) {
- return routes
- }
- return () => routes.filter(route => !route.meta.isLayout)
- }
-
- export const setupLayouts = routes => {
- const layouts = {}
-
- const modules = ${await createVirtualGlob(
- normalizedTarget,
- isSync
- )}
-
- Object.entries(modules).forEach(([name, module]) => {
- let key = name.replace("${normalizedTarget}/", '').replace('.vue', '')
- layouts[key] = ${isSync ? "module.default" : "module"}
- })
-
- function deepSetupLayout(routes, top = true) {
- return routes.map(route => {
- if (route.children?.length > 0) {
- route.children = deepSetupLayout(route.children, false)
- }
-
- if (top) {
- return {
- path: route.path,
- component: layouts[route.meta?.layout || '${defaultLayout}'],
- children: [ {...route, path: ''} ],
- meta: {
- isLayout: true
- }
- }
- }
-
- if (route.meta?.layout) {
- return {
- path: route.path,
- component: layouts[route.meta?.layout],
- children: [ {...route, path: ''} ],
- meta: {
- isLayout: true
- }
- }
- }
-
- return route
- })
- }
-
- return deepSetupLayout(routes)
- }`;
- };
-
- const useName = createPluginName(false);
- const usePlugin = (options) => {
- const {
- target = "src/layouts",
- defaultLayout = "default",
- importMode = process.env.VITE_SSG ? "sync" : "async"
- } = options || {};
- const { virtualModuleId, resolvedVirtualModuleId } = createVirtualModuleID("meta-layouts");
- return {
- name: useName("vue-meta-layouts"),
- resolveId(id) {
- if (id === virtualModuleId) {
- return resolvedVirtualModuleId;
- }
- },
- load(id) {
- if (id === resolvedVirtualModuleId) {
- return createVirtualModuleCode({
- target,
- importMode,
- defaultLayout
- });
- }
- }
- };
- };
-
- module.exports = usePlugin;
|