vite-plugin-generoutes 0.2.1 → 0.2.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 +6 -0
- package/dist/index.js +40 -18
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -35,10 +35,33 @@ function convertToTree(routes) {
|
|
|
35
35
|
});
|
|
36
36
|
return result;
|
|
37
37
|
}
|
|
38
|
+
function findDuplicateRoutes(routes) {
|
|
39
|
+
const nameSet = /* @__PURE__ */ new Set();
|
|
40
|
+
const pathSet = /* @__PURE__ */ new Set();
|
|
41
|
+
const duplicateNames = [];
|
|
42
|
+
const duplicatePaths = [];
|
|
43
|
+
for (const route of routes) {
|
|
44
|
+
if (nameSet.has(route.name)) {
|
|
45
|
+
duplicateNames.push(route.name);
|
|
46
|
+
} else {
|
|
47
|
+
nameSet.add(route.name);
|
|
48
|
+
}
|
|
49
|
+
if (pathSet.has(route.path)) {
|
|
50
|
+
duplicatePaths.push(route.path);
|
|
51
|
+
} else {
|
|
52
|
+
pathSet.add(route.path);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return {
|
|
56
|
+
duplicateNames,
|
|
57
|
+
duplicatePaths
|
|
58
|
+
};
|
|
59
|
+
}
|
|
38
60
|
|
|
39
61
|
// src/index.ts
|
|
40
62
|
var defaultOptions = {
|
|
41
63
|
pagesFolder: "src/pages",
|
|
64
|
+
ignoreFolders: ["components"],
|
|
42
65
|
nested: false
|
|
43
66
|
};
|
|
44
67
|
function VitePluginGeneroutes(options = {}) {
|
|
@@ -48,9 +71,10 @@ function VitePluginGeneroutes(options = {}) {
|
|
|
48
71
|
const pagesFolder = options.pagesFolder || defaultOptions.pagesFolder;
|
|
49
72
|
const routesPath = options.routesPath || path.join(pagesFolder, "generoutes.js");
|
|
50
73
|
const nested = options.nested || defaultOptions.nested;
|
|
74
|
+
const ignoreFolders = options.ignoreFolders || defaultOptions.ignoreFolders;
|
|
51
75
|
const defineOptionsCache = /* @__PURE__ */ new Map();
|
|
52
76
|
function generateMenusAndRoutes() {
|
|
53
|
-
const pages = globSync(`${pagesFolder}
|
|
77
|
+
const pages = globSync(`${pagesFolder}/**/*.vue`, { ignore: ignoreFolders.map((folder) => `${pagesFolder}/**/${folder}/**`) });
|
|
54
78
|
const routes = pages.map((filePath) => {
|
|
55
79
|
filePath = slash(filePath);
|
|
56
80
|
const defineOptions = parseDefineOptions(filePath) || {};
|
|
@@ -58,15 +82,10 @@ function VitePluginGeneroutes(options = {}) {
|
|
|
58
82
|
const meta = defineOptions?.meta || {};
|
|
59
83
|
if (meta.enabled === false)
|
|
60
84
|
return null;
|
|
61
|
-
const
|
|
62
|
-
const
|
|
63
|
-
let name = defineOptions?.name || pathSegments.map((item) => toPascalCase(item)).join("_") || "Index";
|
|
85
|
+
const pathSegments = filePath.replace(`${pagesFolder}`, "").replace(".vue", "").replace("index", "").split("/").filter((item) => !!item && !/^\(.*\)$/.test(item));
|
|
86
|
+
const name = defineOptions?.name || pathSegments.map((item) => toPascalCase(item)).join("_") || "Index";
|
|
64
87
|
const component = `##/${filePath}##`;
|
|
65
|
-
|
|
66
|
-
if (fileName === "[...all].vue") {
|
|
67
|
-
name = `${name}_[...all]`;
|
|
68
|
-
routePath = routePath === "/" ? "/:pathMatch(.*)*" : `${routePath}/:pathMatch(.*)*`;
|
|
69
|
-
}
|
|
88
|
+
const routePath = `/${pathSegments.map((item) => item.replace(/\[(.*?)\]/g, (_, p1) => p1 === "...all" ? ":pathMatch(.*)*" : p1.split(",").map((i) => `:${i}`).join("/"))).join("/")}`;
|
|
70
89
|
if (!("title" in meta))
|
|
71
90
|
meta.title = name;
|
|
72
91
|
if (!("show" in meta))
|
|
@@ -80,6 +99,11 @@ function VitePluginGeneroutes(options = {}) {
|
|
|
80
99
|
parent: defineOptions?.parent
|
|
81
100
|
};
|
|
82
101
|
}).filter(Boolean);
|
|
102
|
+
const { duplicateNames, duplicatePaths } = findDuplicateRoutes(routes);
|
|
103
|
+
if (duplicateNames.length)
|
|
104
|
+
console.warn(`Warning: Duplicate names found in routes: ${duplicateNames.join(", ")}`);
|
|
105
|
+
if (duplicatePaths.length)
|
|
106
|
+
console.warn(`Warning: Duplicate paths found in routes: ${duplicatePaths.join(", ")}`);
|
|
83
107
|
return {
|
|
84
108
|
routes: nested ? convertToTree(routes) : routes
|
|
85
109
|
};
|
|
@@ -100,9 +124,11 @@ function VitePluginGeneroutes(options = {}) {
|
|
|
100
124
|
}
|
|
101
125
|
const debounceWriter = debounce(500, writerRoutesFile);
|
|
102
126
|
function createWatcher() {
|
|
103
|
-
const watcher = chokidar.watch(
|
|
127
|
+
const watcher = chokidar.watch(`${pagesFolder}/**/*.vue`, { ignoreInitial: true });
|
|
104
128
|
return watcher.on("all", async (event, path2) => {
|
|
105
|
-
if (
|
|
129
|
+
if (ignoreFolders.some((folder) => slash(path2).includes(`/${folder}/`)))
|
|
130
|
+
return;
|
|
131
|
+
if (path2.endsWith(".vue") && (event === "add" || event === "unlink")) {
|
|
106
132
|
debounceWriter();
|
|
107
133
|
await watcher.close();
|
|
108
134
|
createWatcher();
|
|
@@ -117,15 +143,11 @@ function VitePluginGeneroutes(options = {}) {
|
|
|
117
143
|
config.command !== "build" && createWatcher();
|
|
118
144
|
},
|
|
119
145
|
async handleHotUpdate({ file, read }) {
|
|
120
|
-
if (file.includes(pagesFolder) && (file.
|
|
146
|
+
if (file.includes(pagesFolder) && !ignoreFolders.some((folder) => file.includes(`/${folder}/`)) && file.endsWith(".vue")) {
|
|
121
147
|
const prevDefineOptions = defineOptionsCache.get(slash(path.relative(rootDir, file)));
|
|
122
|
-
|
|
148
|
+
const defineOptions = JSON.stringify(parseDefineOptions(file, await read()));
|
|
149
|
+
if (prevDefineOptions !== defineOptions) {
|
|
123
150
|
debounceWriter();
|
|
124
|
-
} else {
|
|
125
|
-
const defineOptions = JSON.stringify(parseDefineOptions(file, await read()));
|
|
126
|
-
if (prevDefineOptions !== defineOptions) {
|
|
127
|
-
debounceWriter();
|
|
128
|
-
}
|
|
129
151
|
}
|
|
130
152
|
}
|
|
131
153
|
}
|