vite-plugin-generoutes 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +2 -1
- package/dist/index.js +15 -14
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -12,12 +12,13 @@ function toPascalCase(str) {
|
|
|
12
12
|
});
|
|
13
13
|
}
|
|
14
14
|
var defaultOptions = {
|
|
15
|
-
routesFolder: "src/router/generoutes/"
|
|
15
|
+
routesFolder: "src/router/generoutes/",
|
|
16
|
+
pagesFolder: "src/pages"
|
|
16
17
|
};
|
|
17
18
|
function VitePluginGeneroutes(options = defaultOptions) {
|
|
18
19
|
const defineOptionsCache = /* @__PURE__ */ new Map();
|
|
19
20
|
let rootDir;
|
|
20
|
-
let routesFolder = options.routesFolder
|
|
21
|
+
let routesFolder = options.routesFolder;
|
|
21
22
|
async function writerRoutesFile() {
|
|
22
23
|
const { routes } = generateMenusAndRoutes();
|
|
23
24
|
let routesStr = `
|
|
@@ -31,7 +32,7 @@ function VitePluginGeneroutes(options = defaultOptions) {
|
|
|
31
32
|
}
|
|
32
33
|
const debounceWriter = debounce(500, writerRoutesFile);
|
|
33
34
|
function generateMenusAndRoutes() {
|
|
34
|
-
const pages = globSync(
|
|
35
|
+
const pages = globSync(`${options.pagesFolder}/**/index.vue`).concat(globSync(`src/pages/**/\\[...all\\].vue`));
|
|
35
36
|
const routes = pages.map((filePath) => {
|
|
36
37
|
filePath = slash(filePath);
|
|
37
38
|
const defineOptions = parseDefineOptions(filePath);
|
|
@@ -40,20 +41,20 @@ function VitePluginGeneroutes(options = defaultOptions) {
|
|
|
40
41
|
if (meta.enabled === false)
|
|
41
42
|
return null;
|
|
42
43
|
const fileName = path.basename(filePath);
|
|
43
|
-
const
|
|
44
|
-
const
|
|
44
|
+
const pageName = path.parse(fileName).name;
|
|
45
|
+
const pathSegments = filePath.replace(`${options.pagesFolder}/`, "").replace(fileName, "").split("/").filter((item) => !!item && !/^\(.*\)$/.test(item));
|
|
46
|
+
let name = defineOptions?.name || pathSegments.map((item) => toPascalCase(item)).join("_") || "Index";
|
|
45
47
|
const component = `##/${filePath}##`;
|
|
46
|
-
let routePath = `/${
|
|
47
|
-
if (
|
|
48
|
-
|
|
48
|
+
let routePath = `/${pathSegments.map((item) => item.replace(/\[(.*?)\]/g, (_, p1) => p1.split(",").map((i) => `:${i}`).join("/"))).join("/")}`;
|
|
49
|
+
if (pageName === "[...all]") {
|
|
50
|
+
name = `${name}_[...all]`;
|
|
51
|
+
routePath = routePath === "/" ? "/:pathMatch(.*)*" : `${routePath}/:pathMatch(.*)*`;
|
|
49
52
|
}
|
|
50
|
-
meta.isBase = !meta.code;
|
|
51
53
|
if (!("title" in meta))
|
|
52
54
|
meta.title = name;
|
|
53
55
|
if (!("show" in meta))
|
|
54
56
|
meta.show = true;
|
|
55
|
-
|
|
56
|
-
meta.keepAlive = true;
|
|
57
|
+
meta.keepAlive = !!defineOptions?.name && meta.keepAlive !== false;
|
|
57
58
|
return {
|
|
58
59
|
name,
|
|
59
60
|
path: routePath,
|
|
@@ -66,9 +67,9 @@ function VitePluginGeneroutes(options = defaultOptions) {
|
|
|
66
67
|
};
|
|
67
68
|
}
|
|
68
69
|
function createWatcher() {
|
|
69
|
-
const watcher = chokidar.watch("src/pages/**/
|
|
70
|
+
const watcher = chokidar.watch(["src/pages/**/*.vue", `src/pages/**/[...all].vue`], { ignoreInitial: true });
|
|
70
71
|
return watcher.on("all", async (event, path2) => {
|
|
71
|
-
if (path2.endsWith("index.vue") && (event === "add" || event === "unlink")) {
|
|
72
|
+
if ((path2.endsWith("index.vue") || path2.endsWith("[...all].vue")) && (event === "add" || event === "unlink")) {
|
|
72
73
|
debounceWriter();
|
|
73
74
|
await watcher.close();
|
|
74
75
|
createWatcher();
|
|
@@ -85,7 +86,7 @@ function VitePluginGeneroutes(options = defaultOptions) {
|
|
|
85
86
|
config.command !== "build" && createWatcher();
|
|
86
87
|
},
|
|
87
88
|
async handleHotUpdate({ file, read }) {
|
|
88
|
-
if (file.includes("src/pages") && file.endsWith("index.vue")) {
|
|
89
|
+
if (file.includes("src/pages") && (file.endsWith("index.vue") || file.endsWith("[...all].vue"))) {
|
|
89
90
|
const prevDefineOptions = defineOptionsCache.get(slash(path.relative(rootDir, file)));
|
|
90
91
|
if (!prevDefineOptions) {
|
|
91
92
|
debounceWriter();
|