Skip to content

Commit

Permalink
refactor!: override identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
markthree committed Aug 30, 2023
1 parent 7c51344 commit 421e8d4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 30 deletions.
16 changes: 6 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import type { Plugin } from "vite";
import {
createVirtualModuleCode,
createVirtualModuleID,
} from "./shared/create";
import { createVirtualModuleCode, createVirtualModuleID } from "./virtual";

interface Options {
export interface Options {
/**
* layouts dir
* @default "src/layouts"
Expand All @@ -21,16 +18,17 @@ interface Options {
importMode: "sync" | "async";
}

const usePlugin = (options?: Partial<Options>): Plugin => {
export default function MetaLayouts(options: Partial<Options> = {}): Plugin {
const {
target = "src/layouts",
defaultLayout = "default",
importMode = process.env.VITE_SSG ? "sync" : "async",
} = options || {};
} = options;

const { virtualModuleId, resolvedVirtualModuleId } = createVirtualModuleID(
"meta-layouts",
);

return {
name: "vite-plugin-vue-meta-layouts",
resolveId(id) {
Expand All @@ -48,6 +46,4 @@ const usePlugin = (options?: Partial<Options>): Plugin => {
}
},
};
};

export default usePlugin;
}
8 changes: 4 additions & 4 deletions src/shared/base.ts → src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { posix } from "path";
import { getPackageInfo } from "local-pkg";

export const normalizePath = (path: string): string => {
export function normalizePath(path: string) {
path = path.startsWith("/") ? path : "/" + path;
return posix.normalize(path);
};
}

export const isVite2 = async () => {
export async function isVite2() {
const info = await getPackageInfo("vite");
if (info) {
return /.?2/.test(info.version);
}
return false;
};
}
32 changes: 16 additions & 16 deletions src/shared/create.ts → src/virtual.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
import { isVite2, normalizePath } from "./base";
import { isVite2, normalizePath } from "./utils";

export const createVirtualModuleID = (name: string) => {
export function createVirtualModuleID(name: string) {
const virtualModuleId = `virtual:${name}`;
const resolvedVirtualModuleId = "\0" + virtualModuleId;
return {
virtualModuleId,
resolvedVirtualModuleId,
};
};

interface VirtualModuleCodeOptions {
target: string;
defaultLayout: string;
importMode: "sync" | "async";
}

const createVirtualGlob = async (
export async function createVirtualGlob(
target: string,
isSync: boolean,
) => {
) {
const g = `"${target}/**/*.vue"`;
if (await isVite2()) {
return isSync ? `import.meta.globEager(${g})` : `import.meta.glob(${g})`;
}
return `import.meta.glob(${g}, { eager: ${isSync} })`;
};
}

export const createVirtualModuleCode = async (
interface VirtualModuleCodeOptions {
target: string;
defaultLayout: string;
importMode: "sync" | "async";
}

export async function createVirtualModuleCode(
options: VirtualModuleCodeOptions,
) => {
) {
const { target, defaultLayout, importMode } = options;

const normalizedTarget = normalizePath(target);

const isSync = importMode === "sync";

return `
export const createGetRoutes = (router, withLayout = false) => {
export function createGetRoutes(router, withLayout = false) {
const routes = router.getRoutes()
if (withLayout) {
return routes
}
return () => routes.filter(route => !route.meta.isLayout)
}
export const setupLayouts = routes => {
export function setupLayouts(routes) {
const layouts = {}
const modules = ${await createVirtualGlob(
Expand Down Expand Up @@ -91,4 +91,4 @@ export const setupLayouts = routes => {
return deepSetupLayout(routes)
}`;
};
}

0 comments on commit 421e8d4

Please sign in to comment.